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