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