Mercurial > pyntnclick
comparison pyntnclick/widgets/text.py @ 803:bcc9277a23e6 pyntnclick
Refactor widget positioning API. Remove unused widgets
author | Stefano Rivera <stefano@rivera.za.net> |
---|---|
date | Sun, 27 Jan 2013 14:52:16 +0200 |
parents | bdaffaa8b6bf |
children | 3a875256f795 |
comparison
equal
deleted
inserted
replaced
802:5ec7905b2365 | 803:bcc9277a23e6 |
---|---|
5 | 5 |
6 from pyntnclick.widgets.base import Widget, Button, convert_color | 6 from pyntnclick.widgets.base import Widget, Button, convert_color |
7 | 7 |
8 | 8 |
9 class TextWidget(Widget): | 9 class TextWidget(Widget): |
10 def __init__(self, rect, gd, text, fontname=None, fontsize=None, | 10 def __init__(self, pos, gd, text, size=None, fontname=None, fontsize=None, |
11 color=None): | 11 color=None): |
12 super(TextWidget, self).__init__(rect, gd) | 12 super(TextWidget, self).__init__(pos, gd, size) |
13 self.text = text | 13 self.text = text |
14 constants = self.gd.constants | 14 constants = self.gd.constants |
15 self.fontname = fontname or constants.font | 15 self.fontname = fontname or constants.font |
16 self.fontsize = fontsize or constants.font_size | 16 self.fontsize = fontsize or constants.font_size |
17 self.color = color or constants.text_color | 17 self.color = color or constants.text_color |
20 def prepare(self): | 20 def prepare(self): |
21 self.font = self.resource.get_font(self.fontname, self.fontsize) | 21 self.font = self.resource.get_font(self.fontname, self.fontsize) |
22 self.color = convert_color(self.color) | 22 self.color = convert_color(self.color) |
23 self.surface = self.font.render(self.text, True, self.color) | 23 self.surface = self.font.render(self.text, True, self.color) |
24 self.text_rect = self.surface.get_rect() | 24 self.text_rect = self.surface.get_rect() |
25 width, height = self.surface.get_rect().size | 25 if not self.size: |
26 self.rect.width = max(self.rect.width, width) | 26 self.rect.size = self.text_rect.size |
27 self.rect.height = max(self.rect.height, height) | |
28 | 27 |
29 def draw(self, surface): | 28 def draw(self, surface): |
30 if self.visible: | 29 if self.visible: |
31 self.do_prepare() | 30 self.do_prepare() |
32 surface.blit(self.surface, self.rect) | 31 surface.blit(self.surface, self.rect) |
33 | 32 |
34 | 33 |
35 class LabelWidget(TextWidget): | 34 class LabelWidget(TextWidget): |
36 def __init__(self, rect, gd, *args, **kwargs): | 35 def __init__(self, pos, gd, *args, **kwargs): |
37 constants = gd.constants | 36 constants = gd.constants |
38 self.padding = kwargs.pop('padding', constants.label_padding) | 37 self.padding = kwargs.pop('padding', constants.label_padding) |
39 self.border = kwargs.pop('border', constants.label_border) | 38 self.border = kwargs.pop('border', constants.label_border) |
40 self.bg_color = convert_color( | 39 self.bg_color = convert_color( |
41 kwargs.pop('bg_color', constants.label_bg_color)) | 40 kwargs.pop('bg_color', constants.label_bg_color)) |
42 self.border_color = convert_color( | 41 self.border_color = convert_color( |
43 kwargs.pop('border_color', constants.label_border_color)) | 42 kwargs.pop('border_color', constants.label_border_color)) |
44 super(LabelWidget, self).__init__(rect, gd, *args, **kwargs) | 43 super(LabelWidget, self).__init__(pos, gd, *args, **kwargs) |
45 | 44 |
46 def prepare(self): | 45 def prepare(self): |
47 super(LabelWidget, self).prepare() | 46 super(LabelWidget, self).prepare() |
48 self.rect.width += 2 * self.padding | 47 if not self.size: |
49 self.rect.height += 2 * self.padding | 48 self.rect.width += 2 * self.padding |
49 self.rect.height += 2 * self.padding | |
50 new_surface = pygame.Surface(self.rect.size) | 50 new_surface = pygame.Surface(self.rect.size) |
51 new_surface = new_surface.convert_alpha() | 51 new_surface = new_surface.convert_alpha() |
52 new_surface.fill(self.bg_color) | 52 new_surface.fill(self.bg_color) |
53 new_surface.blit(self.surface, self.surface.get_rect().move( | 53 new_surface.blit(self.surface, self.surface.get_rect().move( |
54 (self.padding, self.padding))) | 54 (self.padding, self.padding))) |
62 self.do_prepare() | 62 self.do_prepare() |
63 surface.blit(self.surface, self.rect) | 63 surface.blit(self.surface, self.rect) |
64 | 64 |
65 | 65 |
66 class TextButton(Button, TextWidget): | 66 class TextButton(Button, TextWidget): |
67 def __init__(self, rect, gd, *args, **kwargs): | 67 def __init__(self, pos, gd, *args, **kwargs): |
68 constants = gd.constants | 68 constants = gd.constants |
69 self.padding = kwargs.pop('padding', constants.label_padding) | 69 self.padding = kwargs.pop('padding', constants.label_padding) |
70 self.border = kwargs.pop('border', constants.label_border) | 70 self.border = kwargs.pop('border', constants.label_border) |
71 | 71 |
72 kwargs['color'] = convert_color( | 72 kwargs['color'] = convert_color( |
74 self.disabled_color = convert_color( | 74 self.disabled_color = convert_color( |
75 kwargs.pop('disabled_color', constants.button_disabled_color)) | 75 kwargs.pop('disabled_color', constants.button_disabled_color)) |
76 self.bg_color = convert_color( | 76 self.bg_color = convert_color( |
77 kwargs.pop('bg_color', constants.button_bg_color)) | 77 kwargs.pop('bg_color', constants.button_bg_color)) |
78 | 78 |
79 super(TextButton, self).__init__(rect, gd, *args, **kwargs) | 79 super(TextButton, self).__init__(pos, gd, *args, **kwargs) |
80 | 80 |
81 def prepare(self): | 81 def prepare(self): |
82 super(TextButton, self).prepare() | 82 super(TextButton, self).prepare() |
83 text = self.surface | 83 text = self.surface |
84 text_rect = self.text_rect | 84 text_rect = self.text_rect |
85 color = self.disabled_color if self.disabled else self.color | 85 color = self.disabled_color if self.disabled else self.color |
86 | 86 |
87 width = text_rect.width + self.padding * 2 | 87 width = text_rect.width + self.padding * 2 |
88 height = text_rect.height + self.padding * 2 | 88 height = text_rect.height + self.padding * 2 |
89 self.rect.width = max(self.rect.width, width) | 89 if not self.size: |
90 self.rect.height = max(self.rect.height, height) | 90 self.rect.width = max(self.rect.width, width) |
91 self.rect.height = max(self.rect.height, height) | |
91 self.surface = pygame.Surface(self.rect.size, SRCALPHA) | 92 self.surface = pygame.Surface(self.rect.size, SRCALPHA) |
92 self.surface.fill(self.bg_color) | 93 self.surface.fill(self.bg_color) |
93 offset = ( | 94 offset = ( |
94 (self.rect.width - width) / 2 + self.padding, | 95 (self.rect.width - width) / 2 + self.padding, |
95 (self.rect.height - height) / 2 + self.padding) | 96 (self.rect.height - height) / 2 + self.padding) |
104 | 105 |
105 | 106 |
106 class WrappedTextLabel(LabelWidget): | 107 class WrappedTextLabel(LabelWidget): |
107 """A Label Widget that wraps the text to a given maximum width""" | 108 """A Label Widget that wraps the text to a given maximum width""" |
108 | 109 |
109 def __init__(self, rect, gd, *args, **kwargs): | 110 def __init__(self, pos, gd, *args, **kwargs): |
110 self.max_width = kwargs.pop('max_width', gd.constants.screen[0] - 50) | 111 self.max_width = kwargs.pop('max_width', gd.constants.screen[0] - 50) |
111 self._wrap_width = None | 112 self._wrap_width = None |
112 self._text_lines = None | 113 self._text_lines = None |
113 super(WrappedTextLabel, self).__init__(rect, gd, *args, **kwargs) | 114 super(WrappedTextLabel, self).__init__(pos, gd, *args, **kwargs) |
114 | 115 |
115 def prepare(self): | 116 def prepare(self): |
116 if self._wrap_width is None: | 117 if self._wrap_width is None: |
117 # Start without wrapping | 118 # Start without wrapping |
118 self._wrap_width = len(self.text) + 1 | 119 self._wrap_width = len(self.text) + 1 |
127 # Very simplistic approach | 128 # Very simplistic approach |
128 self._wrap_width = self._wrap_width / 2 | 129 self._wrap_width = self._wrap_width / 2 |
129 self._text_lines = wrap(self.text, self._wrap_width) | 130 self._text_lines = wrap(self.text, self._wrap_width) |
130 self._render() | 131 self._render() |
131 width, height = self.surface.get_rect().size | 132 width, height = self.surface.get_rect().size |
132 self.rect.width = max(self.rect.width, width) | 133 if not self.size: |
133 self.rect.height = max(self.rect.height, height) | 134 self.rect.width = max(self.rect.width, width) |
135 self.rect.height = max(self.rect.height, height) | |
134 | 136 |
135 if self.border: | 137 if self.border: |
136 pygame.draw.rect(self.surface, self.border_color, | 138 pygame.draw.rect(self.surface, self.border_color, |
137 self.surface.get_rect(), | 139 self.surface.get_rect(), |
138 self.border) | 140 self.border) |