Mercurial > pyntnclick
annotate pyntnclick/widgets/text.py @ 679:fa168b5e2624 pyntnclick
The return of the message dialog (and there was much rejoicing)
author | Neil Muller <neil@dip.sun.ac.za> |
---|---|
date | Sun, 12 Feb 2012 23:06:44 +0200 |
parents | fe7023750b20 |
children | 6892d36dad75 |
rev | line source |
---|---|
679
fa168b5e2624
The return of the message dialog (and there was much rejoicing)
Neil Muller <neil@dip.sun.ac.za>
parents:
670
diff
changeset
|
1 from textwrap import wrap |
fa168b5e2624
The return of the message dialog (and there was much rejoicing)
Neil Muller <neil@dip.sun.ac.za>
parents:
670
diff
changeset
|
2 |
555
c0474fe18b96
Copy in widgets from mamba (currently unused)
Stefano Rivera <stefano@rivera.za.net>
parents:
diff
changeset
|
3 import pygame |
559
b7d8b89de71a
Rip out keyboard handling
Stefano Rivera <stefano@rivera.za.net>
parents:
558
diff
changeset
|
4 from pygame.constants import SRCALPHA |
679
fa168b5e2624
The return of the message dialog (and there was much rejoicing)
Neil Muller <neil@dip.sun.ac.za>
parents:
670
diff
changeset
|
5 from pygame.locals import MOUSEBUTTONDOWN |
555
c0474fe18b96
Copy in widgets from mamba (currently unused)
Stefano Rivera <stefano@rivera.za.net>
parents:
diff
changeset
|
6 |
670
fe7023750b20
Add convert_color function
Stefano Rivera <stefano@rivera.za.net>
parents:
666
diff
changeset
|
7 from pyntnclick.widgets.base import Widget, Button, convert_color |
638
d1ec9e739e23
Make TextWidget work (probably)
Stefano Rivera <stefano@rivera.za.net>
parents:
602
diff
changeset
|
8 |
555
c0474fe18b96
Copy in widgets from mamba (currently unused)
Stefano Rivera <stefano@rivera.za.net>
parents:
diff
changeset
|
9 |
c0474fe18b96
Copy in widgets from mamba (currently unused)
Stefano Rivera <stefano@rivera.za.net>
parents:
diff
changeset
|
10 class TextWidget(Widget): |
644 | 11 def __init__(self, rect, gd, text, fontname=None, fontsize=None, |
12 color=None): | |
639
fdc63049b08c
Add game_description to the widgets,, since it looks a useful thing to do
Neil Muller <neil@dip.sun.ac.za>
parents:
638
diff
changeset
|
13 super(TextWidget, self).__init__(rect, gd) |
555
c0474fe18b96
Copy in widgets from mamba (currently unused)
Stefano Rivera <stefano@rivera.za.net>
parents:
diff
changeset
|
14 self.text = text |
644 | 15 constants = self.gd.constants |
670
fe7023750b20
Add convert_color function
Stefano Rivera <stefano@rivera.za.net>
parents:
666
diff
changeset
|
16 self.fontname = fontname or constants.font |
fe7023750b20
Add convert_color function
Stefano Rivera <stefano@rivera.za.net>
parents:
666
diff
changeset
|
17 self.fontsize = fontsize or constants.font_size |
fe7023750b20
Add convert_color function
Stefano Rivera <stefano@rivera.za.net>
parents:
666
diff
changeset
|
18 self.color = color or constants.text_color |
555
c0474fe18b96
Copy in widgets from mamba (currently unused)
Stefano Rivera <stefano@rivera.za.net>
parents:
diff
changeset
|
19 self.prepare() |
c0474fe18b96
Copy in widgets from mamba (currently unused)
Stefano Rivera <stefano@rivera.za.net>
parents:
diff
changeset
|
20 |
c0474fe18b96
Copy in widgets from mamba (currently unused)
Stefano Rivera <stefano@rivera.za.net>
parents:
diff
changeset
|
21 def prepare(self): |
641
dae56fc7b902
Remove a hacked resorce from TextWidget
Stefano Rivera <stefano@rivera.za.net>
parents:
639
diff
changeset
|
22 self.font = self.resource.get_font(self.fontname, self.fontsize) |
670
fe7023750b20
Add convert_color function
Stefano Rivera <stefano@rivera.za.net>
parents:
666
diff
changeset
|
23 self.color = convert_color(self.color) |
555
c0474fe18b96
Copy in widgets from mamba (currently unused)
Stefano Rivera <stefano@rivera.za.net>
parents:
diff
changeset
|
24 self.surface = self.font.render(self.text, True, self.color) |
c0474fe18b96
Copy in widgets from mamba (currently unused)
Stefano Rivera <stefano@rivera.za.net>
parents:
diff
changeset
|
25 self.text_rect = self.surface.get_rect() |
c0474fe18b96
Copy in widgets from mamba (currently unused)
Stefano Rivera <stefano@rivera.za.net>
parents:
diff
changeset
|
26 width, height = self.surface.get_rect().size |
c0474fe18b96
Copy in widgets from mamba (currently unused)
Stefano Rivera <stefano@rivera.za.net>
parents:
diff
changeset
|
27 self.rect.width = max(self.rect.width, width) |
c0474fe18b96
Copy in widgets from mamba (currently unused)
Stefano Rivera <stefano@rivera.za.net>
parents:
diff
changeset
|
28 self.rect.height = max(self.rect.height, height) |
c0474fe18b96
Copy in widgets from mamba (currently unused)
Stefano Rivera <stefano@rivera.za.net>
parents:
diff
changeset
|
29 |
c0474fe18b96
Copy in widgets from mamba (currently unused)
Stefano Rivera <stefano@rivera.za.net>
parents:
diff
changeset
|
30 def draw(self, surface): |
c0474fe18b96
Copy in widgets from mamba (currently unused)
Stefano Rivera <stefano@rivera.za.net>
parents:
diff
changeset
|
31 surface.blit(self.surface, self.rect) |
c0474fe18b96
Copy in widgets from mamba (currently unused)
Stefano Rivera <stefano@rivera.za.net>
parents:
diff
changeset
|
32 |
c0474fe18b96
Copy in widgets from mamba (currently unused)
Stefano Rivera <stefano@rivera.za.net>
parents:
diff
changeset
|
33 |
644 | 34 class LabelWidget(TextWidget): |
666
de3f6ad0da9d
Better looking labels, and more use of constants
Stefano Rivera <stefano@rivera.za.net>
parents:
665
diff
changeset
|
35 def __init__(self, rect, gd, *args, **kwargs): |
644 | 36 constants = gd.constants |
666
de3f6ad0da9d
Better looking labels, and more use of constants
Stefano Rivera <stefano@rivera.za.net>
parents:
665
diff
changeset
|
37 self.padding = kwargs.pop('padding', constants.label_padding) |
de3f6ad0da9d
Better looking labels, and more use of constants
Stefano Rivera <stefano@rivera.za.net>
parents:
665
diff
changeset
|
38 self.border = kwargs.pop('border', constants.label_border) |
670
fe7023750b20
Add convert_color function
Stefano Rivera <stefano@rivera.za.net>
parents:
666
diff
changeset
|
39 self.bg_color = convert_color( |
fe7023750b20
Add convert_color function
Stefano Rivera <stefano@rivera.za.net>
parents:
666
diff
changeset
|
40 kwargs.pop('bg_color', constants.label_bg_color)) |
fe7023750b20
Add convert_color function
Stefano Rivera <stefano@rivera.za.net>
parents:
666
diff
changeset
|
41 self.border_color = convert_color( |
fe7023750b20
Add convert_color function
Stefano Rivera <stefano@rivera.za.net>
parents:
666
diff
changeset
|
42 kwargs.pop('border_color', constants.label_border_color)) |
666
de3f6ad0da9d
Better looking labels, and more use of constants
Stefano Rivera <stefano@rivera.za.net>
parents:
665
diff
changeset
|
43 super(LabelWidget, self).__init__(rect, gd, *args, **kwargs) |
644 | 44 |
45 def prepare(self): | |
46 super(LabelWidget, self).prepare() | |
666
de3f6ad0da9d
Better looking labels, and more use of constants
Stefano Rivera <stefano@rivera.za.net>
parents:
665
diff
changeset
|
47 self.rect.width += 2 * self.padding |
de3f6ad0da9d
Better looking labels, and more use of constants
Stefano Rivera <stefano@rivera.za.net>
parents:
665
diff
changeset
|
48 self.rect.height += 2 * self.padding |
644 | 49 new_surface = pygame.Surface(self.rect.size) |
50 new_surface = new_surface.convert_alpha() | |
51 new_surface.fill(self.bg_color) | |
666
de3f6ad0da9d
Better looking labels, and more use of constants
Stefano Rivera <stefano@rivera.za.net>
parents:
665
diff
changeset
|
52 new_surface.blit(self.surface, self.surface.get_rect().move( |
de3f6ad0da9d
Better looking labels, and more use of constants
Stefano Rivera <stefano@rivera.za.net>
parents:
665
diff
changeset
|
53 (self.padding, self.padding))) |
de3f6ad0da9d
Better looking labels, and more use of constants
Stefano Rivera <stefano@rivera.za.net>
parents:
665
diff
changeset
|
54 if self.border: |
de3f6ad0da9d
Better looking labels, and more use of constants
Stefano Rivera <stefano@rivera.za.net>
parents:
665
diff
changeset
|
55 pygame.draw.rect(new_surface, self.border_color, |
de3f6ad0da9d
Better looking labels, and more use of constants
Stefano Rivera <stefano@rivera.za.net>
parents:
665
diff
changeset
|
56 new_surface.get_rect(), |
de3f6ad0da9d
Better looking labels, and more use of constants
Stefano Rivera <stefano@rivera.za.net>
parents:
665
diff
changeset
|
57 self.border) |
644 | 58 self.surface = new_surface |
59 | |
60 def draw(self, surface): | |
61 surface.blit(self.surface, self.rect) | |
62 | |
63 | |
555
c0474fe18b96
Copy in widgets from mamba (currently unused)
Stefano Rivera <stefano@rivera.za.net>
parents:
diff
changeset
|
64 class TextButton(Button, TextWidget): |
666
de3f6ad0da9d
Better looking labels, and more use of constants
Stefano Rivera <stefano@rivera.za.net>
parents:
665
diff
changeset
|
65 def __init__(self, rect, gd, *args, **kwargs): |
de3f6ad0da9d
Better looking labels, and more use of constants
Stefano Rivera <stefano@rivera.za.net>
parents:
665
diff
changeset
|
66 constants = gd.constants |
de3f6ad0da9d
Better looking labels, and more use of constants
Stefano Rivera <stefano@rivera.za.net>
parents:
665
diff
changeset
|
67 self.padding = kwargs.pop('padding', constants.label_padding) |
de3f6ad0da9d
Better looking labels, and more use of constants
Stefano Rivera <stefano@rivera.za.net>
parents:
665
diff
changeset
|
68 self.border = kwargs.pop('border', constants.label_border) |
670
fe7023750b20
Add convert_color function
Stefano Rivera <stefano@rivera.za.net>
parents:
666
diff
changeset
|
69 |
fe7023750b20
Add convert_color function
Stefano Rivera <stefano@rivera.za.net>
parents:
666
diff
changeset
|
70 kwargs['color'] = convert_color( |
fe7023750b20
Add convert_color function
Stefano Rivera <stefano@rivera.za.net>
parents:
666
diff
changeset
|
71 kwargs.pop('color', constants.button_color)) |
fe7023750b20
Add convert_color function
Stefano Rivera <stefano@rivera.za.net>
parents:
666
diff
changeset
|
72 self.disabled_color = convert_color( |
fe7023750b20
Add convert_color function
Stefano Rivera <stefano@rivera.za.net>
parents:
666
diff
changeset
|
73 kwargs.pop('disabled_color', constants.button_disabled_color)) |
fe7023750b20
Add convert_color function
Stefano Rivera <stefano@rivera.za.net>
parents:
666
diff
changeset
|
74 self.bg_color = convert_color( |
fe7023750b20
Add convert_color function
Stefano Rivera <stefano@rivera.za.net>
parents:
666
diff
changeset
|
75 kwargs.pop('bg_color', constants.button_bg_color)) |
fe7023750b20
Add convert_color function
Stefano Rivera <stefano@rivera.za.net>
parents:
666
diff
changeset
|
76 |
666
de3f6ad0da9d
Better looking labels, and more use of constants
Stefano Rivera <stefano@rivera.za.net>
parents:
665
diff
changeset
|
77 super(TextButton, self).__init__(rect, gd, *args, **kwargs) |
555
c0474fe18b96
Copy in widgets from mamba (currently unused)
Stefano Rivera <stefano@rivera.za.net>
parents:
diff
changeset
|
78 |
c0474fe18b96
Copy in widgets from mamba (currently unused)
Stefano Rivera <stefano@rivera.za.net>
parents:
diff
changeset
|
79 def prepare(self): |
c0474fe18b96
Copy in widgets from mamba (currently unused)
Stefano Rivera <stefano@rivera.za.net>
parents:
diff
changeset
|
80 super(TextButton, self).prepare() |
c0474fe18b96
Copy in widgets from mamba (currently unused)
Stefano Rivera <stefano@rivera.za.net>
parents:
diff
changeset
|
81 text = self.surface |
c0474fe18b96
Copy in widgets from mamba (currently unused)
Stefano Rivera <stefano@rivera.za.net>
parents:
diff
changeset
|
82 text_rect = self.text_rect |
666
de3f6ad0da9d
Better looking labels, and more use of constants
Stefano Rivera <stefano@rivera.za.net>
parents:
665
diff
changeset
|
83 color = self.disabled_color if self.disabled else self.color |
555
c0474fe18b96
Copy in widgets from mamba (currently unused)
Stefano Rivera <stefano@rivera.za.net>
parents:
diff
changeset
|
84 |
c0474fe18b96
Copy in widgets from mamba (currently unused)
Stefano Rivera <stefano@rivera.za.net>
parents:
diff
changeset
|
85 width = text_rect.width + self.padding * 2 |
c0474fe18b96
Copy in widgets from mamba (currently unused)
Stefano Rivera <stefano@rivera.za.net>
parents:
diff
changeset
|
86 height = text_rect.height + self.padding * 2 |
c0474fe18b96
Copy in widgets from mamba (currently unused)
Stefano Rivera <stefano@rivera.za.net>
parents:
diff
changeset
|
87 self.rect.width = max(self.rect.width, width) |
c0474fe18b96
Copy in widgets from mamba (currently unused)
Stefano Rivera <stefano@rivera.za.net>
parents:
diff
changeset
|
88 self.rect.height = max(self.rect.height, height) |
c0474fe18b96
Copy in widgets from mamba (currently unused)
Stefano Rivera <stefano@rivera.za.net>
parents:
diff
changeset
|
89 self.surface = pygame.Surface(self.rect.size, SRCALPHA) |
670
fe7023750b20
Add convert_color function
Stefano Rivera <stefano@rivera.za.net>
parents:
666
diff
changeset
|
90 self.surface.fill(self.bg_color) |
665 | 91 offset = ( |
92 (self.rect.width - width) / 2 + self.padding, | |
93 (self.rect.height - height) / 2 + self.padding) | |
94 self.surface.blit(text, text.get_rect().move(offset)) | |
670
fe7023750b20
Add convert_color function
Stefano Rivera <stefano@rivera.za.net>
parents:
666
diff
changeset
|
95 |
665 | 96 if self.border: |
97 pygame.draw.rect(self.surface, color, self.surface.get_rect(), | |
98 self.border) | |
555
c0474fe18b96
Copy in widgets from mamba (currently unused)
Stefano Rivera <stefano@rivera.za.net>
parents:
diff
changeset
|
99 |
c0474fe18b96
Copy in widgets from mamba (currently unused)
Stefano Rivera <stefano@rivera.za.net>
parents:
diff
changeset
|
100 def draw(self, surface): |
c0474fe18b96
Copy in widgets from mamba (currently unused)
Stefano Rivera <stefano@rivera.za.net>
parents:
diff
changeset
|
101 super(TextButton, self).draw(surface) |
679
fa168b5e2624
The return of the message dialog (and there was much rejoicing)
Neil Muller <neil@dip.sun.ac.za>
parents:
670
diff
changeset
|
102 |
fa168b5e2624
The return of the message dialog (and there was much rejoicing)
Neil Muller <neil@dip.sun.ac.za>
parents:
670
diff
changeset
|
103 |
fa168b5e2624
The return of the message dialog (and there was much rejoicing)
Neil Muller <neil@dip.sun.ac.za>
parents:
670
diff
changeset
|
104 class WrappedTextLabel(LabelWidget): |
fa168b5e2624
The return of the message dialog (and there was much rejoicing)
Neil Muller <neil@dip.sun.ac.za>
parents:
670
diff
changeset
|
105 """A Label Widget that wraps the text to a given maximum width""" |
fa168b5e2624
The return of the message dialog (and there was much rejoicing)
Neil Muller <neil@dip.sun.ac.za>
parents:
670
diff
changeset
|
106 |
fa168b5e2624
The return of the message dialog (and there was much rejoicing)
Neil Muller <neil@dip.sun.ac.za>
parents:
670
diff
changeset
|
107 def __init__(self, rect, gd, *args, **kwargs): |
fa168b5e2624
The return of the message dialog (and there was much rejoicing)
Neil Muller <neil@dip.sun.ac.za>
parents:
670
diff
changeset
|
108 self.max_width = kwargs.pop('max_width', gd.constants.screen[0] - 50) |
fa168b5e2624
The return of the message dialog (and there was much rejoicing)
Neil Muller <neil@dip.sun.ac.za>
parents:
670
diff
changeset
|
109 self._wrap_width = None |
fa168b5e2624
The return of the message dialog (and there was much rejoicing)
Neil Muller <neil@dip.sun.ac.za>
parents:
670
diff
changeset
|
110 self._text_lines = None |
fa168b5e2624
The return of the message dialog (and there was much rejoicing)
Neil Muller <neil@dip.sun.ac.za>
parents:
670
diff
changeset
|
111 super(WrappedTextLabel, self).__init__(rect, gd, *args, **kwargs) |
fa168b5e2624
The return of the message dialog (and there was much rejoicing)
Neil Muller <neil@dip.sun.ac.za>
parents:
670
diff
changeset
|
112 self.prepare() |
fa168b5e2624
The return of the message dialog (and there was much rejoicing)
Neil Muller <neil@dip.sun.ac.za>
parents:
670
diff
changeset
|
113 |
fa168b5e2624
The return of the message dialog (and there was much rejoicing)
Neil Muller <neil@dip.sun.ac.za>
parents:
670
diff
changeset
|
114 def prepare(self): |
fa168b5e2624
The return of the message dialog (and there was much rejoicing)
Neil Muller <neil@dip.sun.ac.za>
parents:
670
diff
changeset
|
115 if self._wrap_width is None: |
fa168b5e2624
The return of the message dialog (and there was much rejoicing)
Neil Muller <neil@dip.sun.ac.za>
parents:
670
diff
changeset
|
116 # Start without wrapping |
fa168b5e2624
The return of the message dialog (and there was much rejoicing)
Neil Muller <neil@dip.sun.ac.za>
parents:
670
diff
changeset
|
117 self._wrap_width = len(self.text) + 1 |
fa168b5e2624
The return of the message dialog (and there was much rejoicing)
Neil Muller <neil@dip.sun.ac.za>
parents:
670
diff
changeset
|
118 self._text_lines = [self.text] |
fa168b5e2624
The return of the message dialog (and there was much rejoicing)
Neil Muller <neil@dip.sun.ac.za>
parents:
670
diff
changeset
|
119 |
fa168b5e2624
The return of the message dialog (and there was much rejoicing)
Neil Muller <neil@dip.sun.ac.za>
parents:
670
diff
changeset
|
120 self.font = self.resource.get_font(self.fontname, self.fontsize) |
fa168b5e2624
The return of the message dialog (and there was much rejoicing)
Neil Muller <neil@dip.sun.ac.za>
parents:
670
diff
changeset
|
121 self.color = convert_color(self.color) |
fa168b5e2624
The return of the message dialog (and there was much rejoicing)
Neil Muller <neil@dip.sun.ac.za>
parents:
670
diff
changeset
|
122 self._render() |
fa168b5e2624
The return of the message dialog (and there was much rejoicing)
Neil Muller <neil@dip.sun.ac.za>
parents:
670
diff
changeset
|
123 self.text_rect = self.surface.get_rect() |
fa168b5e2624
The return of the message dialog (and there was much rejoicing)
Neil Muller <neil@dip.sun.ac.za>
parents:
670
diff
changeset
|
124 width, height = self.surface.get_rect().size |
fa168b5e2624
The return of the message dialog (and there was much rejoicing)
Neil Muller <neil@dip.sun.ac.za>
parents:
670
diff
changeset
|
125 while width > self.max_width: |
fa168b5e2624
The return of the message dialog (and there was much rejoicing)
Neil Muller <neil@dip.sun.ac.za>
parents:
670
diff
changeset
|
126 # Very simplistic approach |
fa168b5e2624
The return of the message dialog (and there was much rejoicing)
Neil Muller <neil@dip.sun.ac.za>
parents:
670
diff
changeset
|
127 self._wrap_width = self._wrap_width / 2 |
fa168b5e2624
The return of the message dialog (and there was much rejoicing)
Neil Muller <neil@dip.sun.ac.za>
parents:
670
diff
changeset
|
128 self._text_lines = wrap(self.text, self._wrap_width) |
fa168b5e2624
The return of the message dialog (and there was much rejoicing)
Neil Muller <neil@dip.sun.ac.za>
parents:
670
diff
changeset
|
129 self._render() |
fa168b5e2624
The return of the message dialog (and there was much rejoicing)
Neil Muller <neil@dip.sun.ac.za>
parents:
670
diff
changeset
|
130 width, height = self.surface.get_rect().size |
fa168b5e2624
The return of the message dialog (and there was much rejoicing)
Neil Muller <neil@dip.sun.ac.za>
parents:
670
diff
changeset
|
131 self.rect.width = max(self.rect.width, width) |
fa168b5e2624
The return of the message dialog (and there was much rejoicing)
Neil Muller <neil@dip.sun.ac.za>
parents:
670
diff
changeset
|
132 self.rect.height = max(self.rect.height, height) |
fa168b5e2624
The return of the message dialog (and there was much rejoicing)
Neil Muller <neil@dip.sun.ac.za>
parents:
670
diff
changeset
|
133 |
fa168b5e2624
The return of the message dialog (and there was much rejoicing)
Neil Muller <neil@dip.sun.ac.za>
parents:
670
diff
changeset
|
134 self.rect.width += 2 * self.padding |
fa168b5e2624
The return of the message dialog (and there was much rejoicing)
Neil Muller <neil@dip.sun.ac.za>
parents:
670
diff
changeset
|
135 self.rect.height += 2 * self.padding |
fa168b5e2624
The return of the message dialog (and there was much rejoicing)
Neil Muller <neil@dip.sun.ac.za>
parents:
670
diff
changeset
|
136 new_surface = pygame.Surface(self.rect.size) |
fa168b5e2624
The return of the message dialog (and there was much rejoicing)
Neil Muller <neil@dip.sun.ac.za>
parents:
670
diff
changeset
|
137 new_surface = new_surface.convert_alpha() |
fa168b5e2624
The return of the message dialog (and there was much rejoicing)
Neil Muller <neil@dip.sun.ac.za>
parents:
670
diff
changeset
|
138 new_surface.fill(self.bg_color) |
fa168b5e2624
The return of the message dialog (and there was much rejoicing)
Neil Muller <neil@dip.sun.ac.za>
parents:
670
diff
changeset
|
139 new_surface.blit(self.surface, self.surface.get_rect().move( |
fa168b5e2624
The return of the message dialog (and there was much rejoicing)
Neil Muller <neil@dip.sun.ac.za>
parents:
670
diff
changeset
|
140 (self.padding, self.padding))) |
fa168b5e2624
The return of the message dialog (and there was much rejoicing)
Neil Muller <neil@dip.sun.ac.za>
parents:
670
diff
changeset
|
141 if self.border: |
fa168b5e2624
The return of the message dialog (and there was much rejoicing)
Neil Muller <neil@dip.sun.ac.za>
parents:
670
diff
changeset
|
142 pygame.draw.rect(new_surface, self.border_color, |
fa168b5e2624
The return of the message dialog (and there was much rejoicing)
Neil Muller <neil@dip.sun.ac.za>
parents:
670
diff
changeset
|
143 new_surface.get_rect(), |
fa168b5e2624
The return of the message dialog (and there was much rejoicing)
Neil Muller <neil@dip.sun.ac.za>
parents:
670
diff
changeset
|
144 self.border) |
fa168b5e2624
The return of the message dialog (and there was much rejoicing)
Neil Muller <neil@dip.sun.ac.za>
parents:
670
diff
changeset
|
145 self.surface = new_surface |
fa168b5e2624
The return of the message dialog (and there was much rejoicing)
Neil Muller <neil@dip.sun.ac.za>
parents:
670
diff
changeset
|
146 |
fa168b5e2624
The return of the message dialog (and there was much rejoicing)
Neil Muller <neil@dip.sun.ac.za>
parents:
670
diff
changeset
|
147 def _render(self): |
fa168b5e2624
The return of the message dialog (and there was much rejoicing)
Neil Muller <neil@dip.sun.ac.za>
parents:
670
diff
changeset
|
148 surfaces = [] |
fa168b5e2624
The return of the message dialog (and there was much rejoicing)
Neil Muller <neil@dip.sun.ac.za>
parents:
670
diff
changeset
|
149 width = 0 |
fa168b5e2624
The return of the message dialog (and there was much rejoicing)
Neil Muller <neil@dip.sun.ac.za>
parents:
670
diff
changeset
|
150 height = 0 |
fa168b5e2624
The return of the message dialog (and there was much rejoicing)
Neil Muller <neil@dip.sun.ac.za>
parents:
670
diff
changeset
|
151 for line in self._text_lines: |
fa168b5e2624
The return of the message dialog (and there was much rejoicing)
Neil Muller <neil@dip.sun.ac.za>
parents:
670
diff
changeset
|
152 line_surf = self.font.render(line, True, self.color, |
fa168b5e2624
The return of the message dialog (and there was much rejoicing)
Neil Muller <neil@dip.sun.ac.za>
parents:
670
diff
changeset
|
153 self.bg_color) |
fa168b5e2624
The return of the message dialog (and there was much rejoicing)
Neil Muller <neil@dip.sun.ac.za>
parents:
670
diff
changeset
|
154 surfaces.append(line_surf) |
fa168b5e2624
The return of the message dialog (and there was much rejoicing)
Neil Muller <neil@dip.sun.ac.za>
parents:
670
diff
changeset
|
155 width = max(line_surf.get_rect().width, width) |
fa168b5e2624
The return of the message dialog (and there was much rejoicing)
Neil Muller <neil@dip.sun.ac.za>
parents:
670
diff
changeset
|
156 height += line_surf.get_rect().height |
fa168b5e2624
The return of the message dialog (and there was much rejoicing)
Neil Muller <neil@dip.sun.ac.za>
parents:
670
diff
changeset
|
157 self.surface = pygame.Surface((width, height)) |
fa168b5e2624
The return of the message dialog (and there was much rejoicing)
Neil Muller <neil@dip.sun.ac.za>
parents:
670
diff
changeset
|
158 self.surface.fill(self.bg_color) |
fa168b5e2624
The return of the message dialog (and there was much rejoicing)
Neil Muller <neil@dip.sun.ac.za>
parents:
670
diff
changeset
|
159 height = 0 |
fa168b5e2624
The return of the message dialog (and there was much rejoicing)
Neil Muller <neil@dip.sun.ac.za>
parents:
670
diff
changeset
|
160 for line_surf in surfaces: |
fa168b5e2624
The return of the message dialog (and there was much rejoicing)
Neil Muller <neil@dip.sun.ac.za>
parents:
670
diff
changeset
|
161 rect = pygame.Rect((0, height), (line_surf.get_rect().size)) |
fa168b5e2624
The return of the message dialog (and there was much rejoicing)
Neil Muller <neil@dip.sun.ac.za>
parents:
670
diff
changeset
|
162 self.surface.blit(line_surf, rect) |
fa168b5e2624
The return of the message dialog (and there was much rejoicing)
Neil Muller <neil@dip.sun.ac.za>
parents:
670
diff
changeset
|
163 height += line_surf.get_rect().height |
fa168b5e2624
The return of the message dialog (and there was much rejoicing)
Neil Muller <neil@dip.sun.ac.za>
parents:
670
diff
changeset
|
164 |
fa168b5e2624
The return of the message dialog (and there was much rejoicing)
Neil Muller <neil@dip.sun.ac.za>
parents:
670
diff
changeset
|
165 |
fa168b5e2624
The return of the message dialog (and there was much rejoicing)
Neil Muller <neil@dip.sun.ac.za>
parents:
670
diff
changeset
|
166 class ModalWrappedTextLabel(WrappedTextLabel): |
fa168b5e2624
The return of the message dialog (and there was much rejoicing)
Neil Muller <neil@dip.sun.ac.za>
parents:
670
diff
changeset
|
167 """A WrappedTextLabel that removes itself when a mouse |
fa168b5e2624
The return of the message dialog (and there was much rejoicing)
Neil Muller <neil@dip.sun.ac.za>
parents:
670
diff
changeset
|
168 click occurs""" |
fa168b5e2624
The return of the message dialog (and there was much rejoicing)
Neil Muller <neil@dip.sun.ac.za>
parents:
670
diff
changeset
|
169 def __init__(self, rect, gd, *args, **kwargs): |
fa168b5e2624
The return of the message dialog (and there was much rejoicing)
Neil Muller <neil@dip.sun.ac.za>
parents:
670
diff
changeset
|
170 super(ModalWrappedTextLabel, self).__init__(rect, gd, *args, **kwargs) |
fa168b5e2624
The return of the message dialog (and there was much rejoicing)
Neil Muller <neil@dip.sun.ac.za>
parents:
670
diff
changeset
|
171 self.add_callback(MOUSEBUTTONDOWN, self.close) |
fa168b5e2624
The return of the message dialog (and there was much rejoicing)
Neil Muller <neil@dip.sun.ac.za>
parents:
670
diff
changeset
|
172 |
fa168b5e2624
The return of the message dialog (and there was much rejoicing)
Neil Muller <neil@dip.sun.ac.za>
parents:
670
diff
changeset
|
173 def close(self, ev, widget): |
fa168b5e2624
The return of the message dialog (and there was much rejoicing)
Neil Muller <neil@dip.sun.ac.za>
parents:
670
diff
changeset
|
174 if self.parent: |
fa168b5e2624
The return of the message dialog (and there was much rejoicing)
Neil Muller <neil@dip.sun.ac.za>
parents:
670
diff
changeset
|
175 self.parent.remove(self) |
fa168b5e2624
The return of the message dialog (and there was much rejoicing)
Neil Muller <neil@dip.sun.ac.za>
parents:
670
diff
changeset
|
176 return True |