1 | #!/usr/bin/env python |
---|
2 | # -*- coding: utf-8 -*- |
---|
3 | # |
---|
4 | # Puzzlebox - Brainstorms - Client Interface - Pygame |
---|
5 | # |
---|
6 | # Copyright Puzzlebox Productions, LLC (2010) |
---|
7 | # |
---|
8 | # This code is released under the GNU Pulic License (GPL) version 2 |
---|
9 | # For more information please refer to http://www.gnu.org/copyleft/gpl.html |
---|
10 | # |
---|
11 | # Last Update: 2010.06.28 |
---|
12 | # |
---|
13 | ##################################################################### |
---|
14 | |
---|
15 | import os, sys |
---|
16 | |
---|
17 | import pygame |
---|
18 | #import pygame.font |
---|
19 | import pygame.image |
---|
20 | import pygame.locals |
---|
21 | |
---|
22 | from twisted.internet import reactor, protocol, defer |
---|
23 | |
---|
24 | import puzzlebox_brainstorms_configuration as configuration |
---|
25 | import puzzlebox_brainstorms_network_client_twisted as client |
---|
26 | import puzzlebox_brainstorms_client_interface_pygame_component as component |
---|
27 | import puzzlebox_brainstorms_client_interface_pygame_button as interface_button |
---|
28 | #import puzzlebox_logger |
---|
29 | |
---|
30 | ##################################################################### |
---|
31 | # Globals |
---|
32 | ##################################################################### |
---|
33 | |
---|
34 | DEBUG = 1 |
---|
35 | |
---|
36 | FLIP = 1 |
---|
37 | |
---|
38 | DISCRETE_CONTROL_COMMANDS = configuration.BRAINSTORMS_DISCRETE_CONTROL_COMMANDS |
---|
39 | |
---|
40 | SERVER_HOST = configuration.BRAINSTORMS_SERVER_HOST |
---|
41 | SERVER_PORT = configuration.BRAINSTORMS_SERVER_PORT |
---|
42 | |
---|
43 | MAX_CONNECTION_ATTEMPTS = configuration.TWISTED_CLIENT_MAX_CONNECTION_ATTEMPTS |
---|
44 | |
---|
45 | DISPLAY_WINDOW_X_COORDINATE = configuration.CLIENT_PYGAME_DISPLAY_WINDOW_X_COORDINATE |
---|
46 | DISPLAY_WINDOW_Y_COORDINATE = configuration.CLIENT_PYGAME_DISPLAY_WINDOW_Y_COORDINATE |
---|
47 | DISPLAY_WINDOW_X_DIMENSION = configuration.CLIENT_PYGAME_DISPLAY_WINDOW_X_DIMENSION |
---|
48 | DISPLAY_WINDOW_Y_DIMENSION = configuration.CLIENT_PYGAME_DISPLAY_WINDOW_Y_DIMENSION |
---|
49 | |
---|
50 | WINDOW_BACKGROUND_COLOR = configuration.CLIENT_PYGAME_WINDOW_BACKGROUND_COLOR |
---|
51 | |
---|
52 | IMAGE_DIRECTORY = configuration.CLIENT_PYGAME_IMAGE_DIRECTORY |
---|
53 | |
---|
54 | BUTTON_LAYOUT = configuration.CLIENT_PYGAME_BUTTON_LAYOUT |
---|
55 | |
---|
56 | ##################################################################### |
---|
57 | # Classes |
---|
58 | ##################################################################### |
---|
59 | |
---|
60 | class puzzlebox_brainstorms_client_interface_pygame( \ |
---|
61 | component.puzzlebox_brainstorms_client_interface_pygame_component, \ |
---|
62 | client.puzzlebox_brainstorms_network_client_twisted): |
---|
63 | |
---|
64 | def __init__(self, log, \ |
---|
65 | display_window_x_coordinate, \ |
---|
66 | display_window_y_coordinate, \ |
---|
67 | display_window_x_dimension, \ |
---|
68 | display_window_y_dimension, \ |
---|
69 | window_background_color, \ |
---|
70 | DEBUG=DEBUG): |
---|
71 | |
---|
72 | self.log = log |
---|
73 | self.DEBUG = DEBUG |
---|
74 | |
---|
75 | self.display_window_x_coordinate = display_window_x_coordinate |
---|
76 | self.display_window_y_coordinate = display_window_y_coordinate |
---|
77 | self.display_window_x_dimension = display_window_x_dimension |
---|
78 | self.display_window_y_dimension = display_window_y_dimension |
---|
79 | self.window_background_color = window_background_color |
---|
80 | |
---|
81 | self.server_host = SERVER_HOST |
---|
82 | self.server_port = SERVER_PORT |
---|
83 | self.max_connection_attempts = MAX_CONNECTION_ATTEMPTS |
---|
84 | |
---|
85 | if (os.name == "nt") or (os.name == "dos"): |
---|
86 | self.operating_system = "windows" |
---|
87 | else: |
---|
88 | self.operating_system = os.name |
---|
89 | |
---|
90 | self.pygame = pygame |
---|
91 | self.screen = None |
---|
92 | self.FLIP = FLIP |
---|
93 | self.DISCRETE_CONTROL_COMMANDS = DISCRETE_CONTROL_COMMANDS |
---|
94 | self.image_directory = IMAGE_DIRECTORY |
---|
95 | |
---|
96 | self.initialize_pygame() |
---|
97 | |
---|
98 | self.pygame.FLIP = self.FLIP |
---|
99 | |
---|
100 | self.buttons = {} |
---|
101 | self.match_key_index = {} |
---|
102 | |
---|
103 | self.initialize_buttons(BUTTON_LAYOUT) |
---|
104 | |
---|
105 | self.update_display() |
---|
106 | |
---|
107 | |
---|
108 | ##################################################################### |
---|
109 | |
---|
110 | def initialize_pygame(self): |
---|
111 | |
---|
112 | if (self.operating_system != "windows"): |
---|
113 | os.environ['SDL_VIDEO_WINDOW_POS'] = \ |
---|
114 | "%i,%i" % (self.display_window_x_coordinate, \ |
---|
115 | self.display_window_y_coordinate) |
---|
116 | |
---|
117 | self.pygame.init() |
---|
118 | |
---|
119 | if (self.FLIP): |
---|
120 | |
---|
121 | self.screen = self.pygame.display.set_mode((self.display_window_x_dimension, \ |
---|
122 | self.display_window_y_dimension), \ |
---|
123 | pygame.HWSURFACE|pygame.DOUBLEBUF) |
---|
124 | |
---|
125 | else: |
---|
126 | |
---|
127 | self.screen = self.pygame.display.set_mode((self.display_window_x_dimension, \ |
---|
128 | self.display_window_y_dimension), \ |
---|
129 | pygame.HWSURFACE) |
---|
130 | |
---|
131 | |
---|
132 | if (self.DEBUG >= 2): |
---|
133 | print "Display Driver:", |
---|
134 | print pygame.display.get_driver() |
---|
135 | print |
---|
136 | print "Display Info:" |
---|
137 | print pygame.display.Info() |
---|
138 | #print |
---|
139 | #print "Windows System Info:" |
---|
140 | #print pygame.display.get_wm_info() |
---|
141 | #print |
---|
142 | #print "Get Flags:", |
---|
143 | #print pygame.display.screen.get_flags() |
---|
144 | |
---|
145 | |
---|
146 | #self.pygame.mouse.set_visible(0) |
---|
147 | self.pygame.mouse.set_visible(1) |
---|
148 | |
---|
149 | self.pygame.display.set_caption('Puzzlebox Brainstorms - Client Interface') |
---|
150 | |
---|
151 | # Set Background |
---|
152 | self.background = pygame.Surface(self.screen.get_size()) |
---|
153 | #self.background.set_alpha(0) |
---|
154 | self.draw_background() |
---|
155 | |
---|
156 | |
---|
157 | ################################################################## |
---|
158 | |
---|
159 | def draw_background(self): |
---|
160 | |
---|
161 | self.background.fill(self.window_background_color) |
---|
162 | |
---|
163 | self.screen.blit(self.background, (0,0)) |
---|
164 | |
---|
165 | |
---|
166 | ##################################################################### |
---|
167 | |
---|
168 | def initialize_buttons(self, button_layout): |
---|
169 | |
---|
170 | self.buttons = {} |
---|
171 | self.match_key_index = {} |
---|
172 | |
---|
173 | for each in button_layout.keys(): |
---|
174 | |
---|
175 | button = button_layout[each] |
---|
176 | |
---|
177 | if ((button['button_image'] != None) and \ |
---|
178 | (button['button_image'] != '.') and \ |
---|
179 | (button['button_image'] != '..') and \ |
---|
180 | (os.path.exists(os.path.join(self.image_directory, button['button_image'])))): |
---|
181 | |
---|
182 | button_image_path = \ |
---|
183 | os.path.join(self.image_directory, button['button_image']) |
---|
184 | |
---|
185 | else: |
---|
186 | if self.DEBUG: |
---|
187 | print "Error: Button image does not exist:" |
---|
188 | print " %s" % os.path.join(self.image_directory, button['button_image']) |
---|
189 | continue |
---|
190 | |
---|
191 | |
---|
192 | if ((button['activated_image'] != None) and \ |
---|
193 | (button['activated_image'] != '.') and \ |
---|
194 | (button['activated_image'] != '..') and \ |
---|
195 | (os.path.exists(os.path.join(self.image_directory, button['activated_image'])))): |
---|
196 | |
---|
197 | activated_image_path = \ |
---|
198 | os.path.join(self.image_directory, button['activated_image']) |
---|
199 | |
---|
200 | else: |
---|
201 | if self.DEBUG: |
---|
202 | print "Error: Activated button image does not exist:" |
---|
203 | print " %s" % os.path.join(self.image_directory, button['activated_image']) |
---|
204 | continue |
---|
205 | |
---|
206 | |
---|
207 | for match in button['match_keys']: |
---|
208 | self.match_key_index[match] = button['command'] |
---|
209 | |
---|
210 | |
---|
211 | new_button = \ |
---|
212 | interface_button.puzzlebox_brainstorms_client_interface_pygame_button( \ |
---|
213 | self.pygame, \ |
---|
214 | self.screen, \ |
---|
215 | button_image_path, \ |
---|
216 | activated_image_path, \ |
---|
217 | button['image_x'], \ |
---|
218 | button['image_y'], \ |
---|
219 | button['command'], \ |
---|
220 | button['match_keys'], \ |
---|
221 | self.DEBUG) |
---|
222 | |
---|
223 | |
---|
224 | self.buttons[ button['command'] ] = new_button |
---|
225 | |
---|
226 | |
---|
227 | ################################################################## |
---|
228 | |
---|
229 | def update_button(self, command, activated): |
---|
230 | |
---|
231 | self.buttons[command].activated=activated |
---|
232 | self.buttons[command].display() |
---|
233 | self.update_display() |
---|
234 | |
---|
235 | |
---|
236 | ################################################################## |
---|
237 | |
---|
238 | def process_mouse_click(self, position, mouse_button): |
---|
239 | |
---|
240 | if (mouse_button == 1): |
---|
241 | # first mouse button clicked |
---|
242 | |
---|
243 | postion_rect = self.pygame.Rect(position, (1,1) ) |
---|
244 | |
---|
245 | for each in self.buttons.keys(): |
---|
246 | |
---|
247 | if self.buttons[each].check_collision(postion_rect): |
---|
248 | |
---|
249 | command = self.buttons[each].command |
---|
250 | self.update_button(command, activated=True) |
---|
251 | self.send_command(command) |
---|
252 | |
---|
253 | |
---|
254 | ################################################################## |
---|
255 | |
---|
256 | def process_mouse_release(self, position, mouse_button): |
---|
257 | |
---|
258 | if (mouse_button == 1): |
---|
259 | # first mouse button released |
---|
260 | |
---|
261 | postion_rect = self.pygame.Rect(position, (1,1) ) |
---|
262 | |
---|
263 | for each in self.buttons.keys(): |
---|
264 | |
---|
265 | if self.buttons[each].check_collision(postion_rect): |
---|
266 | |
---|
267 | command = self.buttons[each].command |
---|
268 | self.update_button(command, activated=False) |
---|
269 | |
---|
270 | |
---|
271 | ################################################################## |
---|
272 | |
---|
273 | def release_all_buttons(self): |
---|
274 | |
---|
275 | for each in self.buttons.keys(): |
---|
276 | |
---|
277 | command = self.buttons[each].command |
---|
278 | self.update_button(command, activated=False) |
---|
279 | |
---|
280 | |
---|
281 | ################################################################## |
---|
282 | |
---|
283 | def check_events(self): |
---|
284 | |
---|
285 | for event in self.pygame.event.get(): |
---|
286 | |
---|
287 | if (event.type is self.pygame.locals.QUIT): |
---|
288 | reactor.stop() |
---|
289 | |
---|
290 | |
---|
291 | elif (event.type == self.pygame.locals.KEYDOWN): |
---|
292 | |
---|
293 | if 'key' in event.dict.keys(): |
---|
294 | |
---|
295 | key_pressed = event.dict['key'] |
---|
296 | |
---|
297 | if (key_pressed == self.pygame.K_ESCAPE): |
---|
298 | reactor.stop() |
---|
299 | |
---|
300 | |
---|
301 | elif (key_pressed in self.match_key_index.keys()): |
---|
302 | |
---|
303 | # keypress matches recognized key |
---|
304 | command = self.match_key_index[key_pressed] |
---|
305 | self.update_button(command, activated=True) |
---|
306 | self.send_command(command) |
---|
307 | |
---|
308 | |
---|
309 | elif (event.type == self.pygame.locals.KEYUP): |
---|
310 | |
---|
311 | if 'key' in event.dict.keys(): |
---|
312 | |
---|
313 | key_pressed = event.dict['key'] |
---|
314 | |
---|
315 | if (key_pressed in self.match_key_index.keys()): |
---|
316 | |
---|
317 | # keypress matches recognized key |
---|
318 | command = self.match_key_index[key_pressed] |
---|
319 | self.update_button(command, activated=False) |
---|
320 | |
---|
321 | |
---|
322 | elif (event.type == self.pygame.locals.MOUSEBUTTONDOWN): |
---|
323 | |
---|
324 | position = event.pos |
---|
325 | mouse_button = event.button |
---|
326 | |
---|
327 | self.process_mouse_click(position, mouse_button) |
---|
328 | |
---|
329 | |
---|
330 | elif (event.type == self.pygame.locals.MOUSEBUTTONUP): |
---|
331 | |
---|
332 | # We release all activated buttons when a mouse button is |
---|
333 | # released because its possible the user may have pressed |
---|
334 | # the mouse button down while hovering over one button on |
---|
335 | # the screen, then dragged the pointer while still holding |
---|
336 | # down the mouse button, finally releasing it over another |
---|
337 | # button on the screen. |
---|
338 | |
---|
339 | mouse_button = event.button |
---|
340 | |
---|
341 | if (mouse_button == 1): |
---|
342 | # First mouse button released |
---|
343 | self.release_all_buttons() |
---|
344 | |
---|
345 | |
---|
346 | else: |
---|
347 | |
---|
348 | if self.DEBUG > 2: |
---|
349 | print "Unrecognized event:", |
---|
350 | print event |
---|
351 | |
---|
352 | |
---|
353 | # Sleep timer provides approximately 33.3 fps |
---|
354 | reactor.callLater(0.03, self.check_events) |
---|
355 | |
---|
356 | |
---|
357 | ##################################################################### |
---|
358 | # Functions |
---|
359 | ##################################################################### |
---|
360 | |
---|
361 | ##################################################################### |
---|
362 | # Main |
---|
363 | ##################################################################### |
---|
364 | |
---|
365 | if __name__ == '__main__': |
---|
366 | |
---|
367 | pass |
---|
368 | |
---|