comparison pyntnclick/widgets/text.py @ 670:fe7023750b20 pyntnclick

Add convert_color function
author Stefano Rivera <stefano@rivera.za.net>
date Sun, 12 Feb 2012 18:00:18 +0200
parents de3f6ad0da9d
children fa168b5e2624
comparison
equal deleted inserted replaced
669:562f8133bb07 670:fe7023750b20
1 import pygame 1 import pygame
2 from pygame.constants import SRCALPHA 2 from pygame.constants import SRCALPHA
3 3
4 from pyntnclick.widgets.base import Widget, Button 4 from pyntnclick.widgets.base import Widget, Button, convert_color
5 5
6 6
7 class TextWidget(Widget): 7 class TextWidget(Widget):
8 def __init__(self, rect, gd, text, fontname=None, fontsize=None, 8 def __init__(self, rect, gd, text, fontname=None, fontsize=None,
9 color=None): 9 color=None):
10 super(TextWidget, self).__init__(rect, gd) 10 super(TextWidget, self).__init__(rect, gd)
11 self.text = text 11 self.text = text
12 constants = self.gd.constants 12 constants = self.gd.constants
13 if fontname is None: 13 self.fontname = fontname or constants.font
14 self.fontname = constants.font 14 self.fontsize = fontsize or constants.font_size
15 else: 15 self.color = color or constants.text_color
16 self.fontname = fontname
17 if fontsize is None:
18 self.fontsize = constants.font_size
19 else:
20 self.fontsize = fontsize
21 if color is None:
22 self.color = constants.text_color
23 else:
24 self.color = color
25 self.prepare() 16 self.prepare()
26 17
27 def prepare(self): 18 def prepare(self):
28 self.font = self.resource.get_font(self.fontname, self.fontsize) 19 self.font = self.resource.get_font(self.fontname, self.fontsize)
29 if not isinstance(self.color, pygame.Color): 20 self.color = convert_color(self.color)
30 self.color = pygame.Color(self.color)
31 self.surface = self.font.render(self.text, True, self.color) 21 self.surface = self.font.render(self.text, True, self.color)
32 self.text_rect = self.surface.get_rect() 22 self.text_rect = self.surface.get_rect()
33 width, height = self.surface.get_rect().size 23 width, height = self.surface.get_rect().size
34 self.rect.width = max(self.rect.width, width) 24 self.rect.width = max(self.rect.width, width)
35 self.rect.height = max(self.rect.height, height) 25 self.rect.height = max(self.rect.height, height)
41 class LabelWidget(TextWidget): 31 class LabelWidget(TextWidget):
42 def __init__(self, rect, gd, *args, **kwargs): 32 def __init__(self, rect, gd, *args, **kwargs):
43 constants = gd.constants 33 constants = gd.constants
44 self.padding = kwargs.pop('padding', constants.label_padding) 34 self.padding = kwargs.pop('padding', constants.label_padding)
45 self.border = kwargs.pop('border', constants.label_border) 35 self.border = kwargs.pop('border', constants.label_border)
46 self.bg_color = kwargs.pop('bg_color', constants.label_bg_color) 36 self.bg_color = convert_color(
47 if not isinstance(self.bg_color, pygame.Color): 37 kwargs.pop('bg_color', constants.label_bg_color))
48 self.bg_color = pygame.Color(*self.bg_color) 38 self.border_color = convert_color(
49 self.border_color = kwargs.pop('border_color', 39 kwargs.pop('border_color', constants.label_border_color))
50 constants.label_border_color)
51 if not isinstance(self.border_color, pygame.Color):
52 self.border_color = pygame.Color(*self.border_color)
53 super(LabelWidget, self).__init__(rect, gd, *args, **kwargs) 40 super(LabelWidget, self).__init__(rect, gd, *args, **kwargs)
54 41
55 def prepare(self): 42 def prepare(self):
56 super(LabelWidget, self).prepare() 43 super(LabelWidget, self).prepare()
57 self.rect.width += 2 * self.padding 44 self.rect.width += 2 * self.padding
74 class TextButton(Button, TextWidget): 61 class TextButton(Button, TextWidget):
75 def __init__(self, rect, gd, *args, **kwargs): 62 def __init__(self, rect, gd, *args, **kwargs):
76 constants = gd.constants 63 constants = gd.constants
77 self.padding = kwargs.pop('padding', constants.label_padding) 64 self.padding = kwargs.pop('padding', constants.label_padding)
78 self.border = kwargs.pop('border', constants.label_border) 65 self.border = kwargs.pop('border', constants.label_border)
79 self.disabled_color = kwargs.pop('disabled_color', 66
80 constants.disabled_color) 67 kwargs['color'] = convert_color(
81 if not isinstance(self.disabled_color, pygame.Color): 68 kwargs.pop('color', constants.button_color))
82 self.disabled_color = pygame.Color(*self.disabled_color) 69 self.disabled_color = convert_color(
70 kwargs.pop('disabled_color', constants.button_disabled_color))
71 self.bg_color = convert_color(
72 kwargs.pop('bg_color', constants.button_bg_color))
73
83 super(TextButton, self).__init__(rect, gd, *args, **kwargs) 74 super(TextButton, self).__init__(rect, gd, *args, **kwargs)
84 75
85 def prepare(self): 76 def prepare(self):
86 super(TextButton, self).prepare() 77 super(TextButton, self).prepare()
87 text = self.surface 78 text = self.surface
91 width = text_rect.width + self.padding * 2 82 width = text_rect.width + self.padding * 2
92 height = text_rect.height + self.padding * 2 83 height = text_rect.height + self.padding * 2
93 self.rect.width = max(self.rect.width, width) 84 self.rect.width = max(self.rect.width, width)
94 self.rect.height = max(self.rect.height, height) 85 self.rect.height = max(self.rect.height, height)
95 self.surface = pygame.Surface(self.rect.size, SRCALPHA) 86 self.surface = pygame.Surface(self.rect.size, SRCALPHA)
96 self.surface.fill(0) 87 self.surface.fill(self.bg_color)
97 offset = ( 88 offset = (
98 (self.rect.width - width) / 2 + self.padding, 89 (self.rect.width - width) / 2 + self.padding,
99 (self.rect.height - height) / 2 + self.padding) 90 (self.rect.height - height) / 2 + self.padding)
100 self.surface.blit(text, text.get_rect().move(offset)) 91 self.surface.blit(text, text.get_rect().move(offset))
92
101 if self.border: 93 if self.border:
102 pygame.draw.rect(self.surface, color, self.surface.get_rect(), 94 pygame.draw.rect(self.surface, color, self.surface.get_rect(),
103 self.border) 95 self.border)
104 96
105 def draw(self, surface): 97 def draw(self, surface):