Mercurial > boomslang
comparison pyntnclick/widgets.py @ 548:ded4324b236e pyntnclick
Moved stuff around, broke everything.
author | Jeremy Thurgood <firxen@gmail.com> |
---|---|
date | Sat, 11 Feb 2012 13:10:18 +0200 |
parents | gamelib/widgets.py@2d87cb7eee50 |
children | 38fb04728ac5 |
comparison
equal
deleted
inserted
replaced
547:33ce7ff757c3 | 548:ded4324b236e |
---|---|
1 # widgets.py | |
2 # Copyright Boomslang team, 2010 (see COPYING File) | |
3 | |
4 """Custom Albow widgets""" | |
5 | |
6 import textwrap | |
7 | |
8 import albow.controls | |
9 import albow.menu | |
10 from albow.resource import get_font, get_image | |
11 from pygame.color import Color | |
12 from pygame.rect import Rect | |
13 from pygame.draw import lines as draw_lines | |
14 from pygame import mouse | |
15 | |
16 from constants import BUTTON_SIZE | |
17 from cursor import CursorWidget | |
18 | |
19 | |
20 class BoomLabel(albow.controls.Label): | |
21 | |
22 trim_line_top = 0 | |
23 | |
24 def __init__(self, text, width=None, **kwds): | |
25 albow.controls.Label.__init__(self, text, width, **kwds) | |
26 w, h = self.size | |
27 h -= self.trim_line_top * len(self.text.split('\n')) | |
28 self.size = (w, h) | |
29 | |
30 def set_margin(self, margin): | |
31 """Add a set_margin method that recalculates the label size""" | |
32 old_margin = self.margin | |
33 w, h = self.size | |
34 d = margin - old_margin | |
35 self.margin = margin | |
36 self.size = (w + 2 * d, h + 2 * d) | |
37 | |
38 def draw_all(self, surface): | |
39 bg_color = self.bg_color | |
40 self.bg_color = None | |
41 if bg_color is not None: | |
42 new_surface = surface.convert_alpha() | |
43 new_surface.fill(bg_color) | |
44 surface.blit(new_surface, surface.get_rect()) | |
45 albow.controls.Label.draw_all(self, surface) | |
46 self._draw_all_no_bg(surface) | |
47 self.bg_color = bg_color | |
48 | |
49 def _draw_all_no_bg(self, surface): | |
50 pass | |
51 | |
52 def draw_with(self, surface, fg, _bg=None): | |
53 m = self.margin | |
54 align = self.align | |
55 width = surface.get_width() | |
56 y = m | |
57 lines = self.text.split("\n") | |
58 font = self.font | |
59 dy = font.get_linesize() - self.trim_line_top | |
60 for line in lines: | |
61 image = font.render(line, True, fg) | |
62 r = image.get_rect() | |
63 image = image.subsurface(r.clip(r.move(0, self.trim_line_top))) | |
64 r.top = y | |
65 if align == 'l': | |
66 r.left = m | |
67 elif align == 'r': | |
68 r.right = width - m | |
69 else: | |
70 r.centerx = width // 2 | |
71 surface.blit(image, r) | |
72 y += dy | |
73 | |
74 | |
75 class BoomButton(BoomLabel): | |
76 | |
77 def __init__(self, text, action, screen): | |
78 super(BoomButton, self).__init__(text, font=get_font(20, 'Vera.ttf'), | |
79 margin=4) | |
80 self.bg_color = (0, 0, 0) | |
81 self._frame_color = Color(50, 50, 50) | |
82 self.action = action | |
83 self.screen = screen | |
84 | |
85 def mouse_down(self, event): | |
86 self.action() | |
87 self.screen.state_widget.mouse_move(event) | |
88 | |
89 def mouse_move(self, event): | |
90 self.screen.state.highlight_override = True | |
91 | |
92 def draw(self, surface): | |
93 super(BoomButton, self).draw(surface) | |
94 r = surface.get_rect() | |
95 w = 2 | |
96 top, bottom, left, right = r.top, r.bottom, r.left, r.right | |
97 draw_lines(surface, self._frame_color, False, [ | |
98 (left, bottom), (left, top), (right - w, top), (right - w, bottom) | |
99 ], w) | |
100 | |
101 | |
102 class MessageDialog(BoomLabel, CursorWidget): | |
103 | |
104 def __init__(self, screen, text, wrap_width, style=None, **kwds): | |
105 CursorWidget.__init__(self, screen) | |
106 self.set_style(style) | |
107 paras = text.split("\n\n") | |
108 text = "\n".join([textwrap.fill(para, wrap_width) for para in paras]) | |
109 BoomLabel.__init__(self, text, **kwds) | |
110 | |
111 def set_style(self, style): | |
112 self.set_margin(5) | |
113 self.border_width = 1 | |
114 self.border_color = (0, 0, 0) | |
115 self.bg_color = (127, 127, 127) | |
116 self.fg_color = (0, 0, 0) | |
117 if style == "JIM": | |
118 self.set(font=get_font(20, "Monospace.ttf")) | |
119 self.trim_line_top = 10 | |
120 self.bg_color = Color(255, 175, 127, 191) | |
121 self.fg_color = (0, 0, 0) | |
122 self.border_color = (127, 15, 0) | |
123 | |
124 def draw_all(self, surface): | |
125 root_surface = self.get_root().surface | |
126 overlay = root_surface.convert_alpha() | |
127 overlay.fill(Color(0, 0, 0, 191)) | |
128 root_surface.blit(overlay, (0, 0)) | |
129 BoomLabel.draw_all(self, surface) | |
130 | |
131 def _draw_all_no_bg(self, surface): | |
132 CursorWidget.draw_all(self, surface) | |
133 | |
134 def mouse_down(self, event): | |
135 self.dismiss() | |
136 self.screen.state_widget._mouse_move(mouse.get_pos()) | |
137 for widget in self.screen.state_widget.subwidgets: | |
138 widget._mouse_move(mouse.get_pos()) | |
139 | |
140 def cursor_highlight(self): | |
141 return False | |
142 | |
143 | |
144 class HandButton(albow.controls.Image): | |
145 """The fancy hand button for the widget""" | |
146 | |
147 def __init__(self, action): | |
148 # FIXME: Yes, please. | |
149 this_image = get_image('items', 'hand.png') | |
150 albow.controls.Image.__init__(self, image=this_image) | |
151 self.action = action | |
152 self.set_rect(Rect(0, 0, BUTTON_SIZE, BUTTON_SIZE)) | |
153 | |
154 def mouse_down(self, event): | |
155 self.action() | |
156 | |
157 | |
158 class PopupMenuButton(albow.controls.Button): | |
159 | |
160 def __init__(self, text, action): | |
161 albow.controls.Button.__init__(self, text, action) | |
162 | |
163 self.font = get_font(16, 'Vera.ttf') | |
164 self.set_rect(Rect(0, 0, BUTTON_SIZE, BUTTON_SIZE)) | |
165 self.margin = (BUTTON_SIZE - self.font.get_linesize()) / 2 | |
166 | |
167 | |
168 class PopupMenu(albow.menu.Menu, CursorWidget): | |
169 | |
170 def __init__(self, screen): | |
171 CursorWidget.__init__(self, screen) | |
172 self.screen = screen | |
173 self.shell = screen.shell | |
174 items = [ | |
175 ('Quit Game', 'quit'), | |
176 ('Exit to Main Menu', 'main_menu'), | |
177 ] | |
178 # albow.menu.Menu ignores title string | |
179 albow.menu.Menu.__init__(self, None, items) | |
180 self.font = get_font(16, 'Vera.ttf') | |
181 | |
182 def show_menu(self): | |
183 """Call present, with the correct position""" | |
184 item_height = self.font.get_linesize() | |
185 menu_top = 600 - (len(self.items) * item_height + BUTTON_SIZE) | |
186 item = self.present(self.shell, (0, menu_top)) | |
187 if item > -1: | |
188 # A menu item needs to be invoked | |
189 self.invoke_item(item) | |
190 | |
191 | |
192 class BoomImageButton(albow.controls.Image): | |
193 """The fancy image button for the screens""" | |
194 | |
195 FOLDER = None | |
196 | |
197 def __init__(self, filename, x, y, action, enable=None): | |
198 this_image = get_image(self.FOLDER, filename) | |
199 albow.controls.Image.__init__(self, image=this_image) | |
200 self.action = action | |
201 self.set_rect(Rect((x, y), this_image.get_size())) | |
202 self.enable = enable | |
203 | |
204 def draw(self, surface): | |
205 if self.is_enabled(): | |
206 surface.blit(self.get_image(), self.get_rect()) | |
207 | |
208 def mouse_down(self, event): | |
209 if self.is_enabled(): | |
210 self.action() | |
211 | |
212 def is_enabled(self): | |
213 if self.enable: | |
214 return self.enable() | |
215 return True |