Exemple #1
0
    def __init__(self, *args, **kwargs):
        # set a click handler, if we get one
        if 'click' in kwargs:
            self.click = kwargs['click']

        # text label
        # TODO: add more options?
        self.text = text.Text(kwargs.get('label',''),
                              color=kwargs.get('labelColor',Game.color('black')),
                              )

        # graphics
        self.graphic = kwargs.get('graphic',None)
        if isinstance(self.graphic, basestring):
            self.graphic = Image().load(self.graphic)

        # background
        self.bgColor = kwargs.get('bgColor', 'gray70')
        if isinstance(self.bgColor, basestring):
            self.bgColor = Game.color(self.bgColor)

        # border
        self.border = kwargs.get('border',8)

        # border color
        self.borderColor = kwargs.get('borderColor', None)

        # if we don't get a size, figure one out
        # TODO: if we get a graphic and text, make it the bigger of the two
        if 'size' not in kwargs:
            if self.graphic is not None:
                kwargs['size'] = self.graphic.size
            elif self.text is not None:
                kwargs['size'] = self.text.size
            else:
                kwargs['size'] = (0,0)

        kwargs['size'] += (self.border,self.border)

        super(Button, self).__init__(*args, **kwargs)

        self.pixels = Game.Surface((self.width,self.height),Game.Constants.SRCALPHA)
        self.pixels.fill(self.bgColor)
        self._drawBorder()
Exemple #2
0
    def _render_text(self):
        """This is a helper method to render text to a surface with wrapping."""
        if self._autowidth:
            # "autowidth" means one line, no wrapping, and the left edge
            # stays in the same place
            textlines = [self.text]
            textheight = self.font.get_linesize()
            oldleft = self.left
            self.width = self.font.size(self.text)[0]
            self.x = oldleft + self.width / 2.
        else:
            textlines = self._wrap(self.text)
            textheight = len(textlines) * self.font.get_linesize()

        textwidth = self.width
        self.height = textheight

        # fix in case we're drawing black text
        if self.background is None and self.color == Game.rgb(0, 0, 0):
            ALPHA = Game.Constants.SRCALPHA
        else:
            ALPHA = 0

        surf = Game.Surface((textwidth, textheight), ALPHA)

        for lineno, line in enumerate(textlines):
            if self.background is not None:
                r = self.font.render(line, self.antialias, self.color,
                                     self.background)
            else:
                r = self.font.render(line, self.antialias, self.color)
            surf.blit(r, (0, lineno * self.font.get_linesize()))

        self.pixels = surf

        if self.background is None:
            # per-pixel alphas override colorkeys, so this line won't do anything
            # if the text is black
            self.pixels.set_colorkey((0, 0, 0), Game.Constants.RLEACCEL)

        self.redraw()