1 | #!/usr/bin/env python |
---|
2 | # -*- coding: utf-8 -*- |
---|
3 | # |
---|
4 | # Puzzlebox - Brainstorms - Client Interface |
---|
5 | # |
---|
6 | # Copyright Puzzlebox Productions, LLC (2010) |
---|
7 | # |
---|
8 | # Portions of this code have been previously |
---|
9 | # released under the GNU Pulic License (GPL) version 2 |
---|
10 | # and is Copyright Steven M. Castellotti (2010) |
---|
11 | # For more information please refer to http://www.gnu.org/copyleft/gpl.htm |
---|
12 | # |
---|
13 | # Last Update: 2010.02.02 |
---|
14 | # |
---|
15 | ##################################################################### |
---|
16 | |
---|
17 | import os, sys |
---|
18 | |
---|
19 | import pygame |
---|
20 | #import pygame.font |
---|
21 | import pygame.image |
---|
22 | import pygame.locals |
---|
23 | |
---|
24 | from twisted.internet import reactor, protocol, defer |
---|
25 | |
---|
26 | import puzzlebox_brainstorms_configuration as configuration |
---|
27 | import puzzlebox_brainstorms_client |
---|
28 | import puzzlebox_brainstorms_client_interface_component as component |
---|
29 | import puzzlebox_brainstorms_client_interface_button as button |
---|
30 | #import puzzlebox_logger |
---|
31 | |
---|
32 | ##################################################################### |
---|
33 | # Globals |
---|
34 | ##################################################################### |
---|
35 | |
---|
36 | DEBUG = 1 |
---|
37 | |
---|
38 | FLIP = 1 |
---|
39 | |
---|
40 | SERVER_HOST = configuration.SERVER_HOST |
---|
41 | SERVER_PORT = configuration.SERVER_PORT |
---|
42 | |
---|
43 | DISPLAY_WINDOW_X_COORDINATE = configuration.DISPLAY_WINDOW_X_COORDINATE |
---|
44 | DISPLAY_WINDOW_Y_COORDINATE = configuration.DISPLAY_WINDOW_Y_COORDINATE |
---|
45 | DISPLAY_WINDOW_X_DIMENSION = configuration.DISPLAY_WINDOW_X_DIMENSION |
---|
46 | DISPLAY_WINDOW_Y_DIMENSION = configuration.DISPLAY_WINDOW_Y_DIMENSION |
---|
47 | |
---|
48 | WINDOW_BACKGROUND_COLOR = configuration.WINDOW_BACKGROUND_COLOR |
---|
49 | |
---|
50 | IMAGE_DIRECTORY = configuration.IMAGE_DIRECTORY |
---|
51 | |
---|
52 | BUTTON_LAYOUT = configuration.BUTTON_LAYOUT |
---|
53 | |
---|
54 | ##################################################################### |
---|
55 | # Classes |
---|
56 | ##################################################################### |
---|
57 | |
---|
58 | class puzzlebox_brainstorms_client_interface( \ |
---|
59 | component.puzzlebox_brainstorms_client_interface_component, \ |
---|
60 | protocol.ClientFactory): |
---|
61 | |
---|
62 | def __init__(self, log, \ |
---|
63 | display_window_x_coordinate, \ |
---|
64 | display_window_y_coordinate, \ |
---|
65 | display_window_x_dimension, \ |
---|
66 | display_window_y_dimension, \ |
---|
67 | window_background_color, \ |
---|
68 | DEBUG=DEBUG): |
---|
69 | |
---|
70 | self.log = log |
---|
71 | self.DEBUG = DEBUG |
---|
72 | |
---|
73 | self.display_window_x_coordinate = display_window_x_coordinate |
---|
74 | self.display_window_y_coordinate = display_window_y_coordinate |
---|
75 | self.display_window_x_dimension = display_window_x_dimension |
---|
76 | self.display_window_y_dimension = display_window_y_dimension |
---|
77 | self.window_background_color = window_background_color |
---|
78 | |
---|
79 | if (os.name == "nt") or (os.name == "dos"): |
---|
80 | self.operating_system = "windows" |
---|
81 | else: |
---|
82 | self.operating_system = os.name |
---|
83 | |
---|
84 | self.pygame = pygame |
---|
85 | self.screen = None |
---|
86 | self.FLIP = FLIP |
---|
87 | self.image_directory = IMAGE_DIRECTORY |
---|
88 | |
---|
89 | self.initialize_pygame() |
---|
90 | |
---|
91 | self.pygame.FLIP = self.FLIP |
---|
92 | |
---|
93 | self.buttons = [] |
---|
94 | |
---|
95 | self.initialize_buttons(BUTTON_LAYOUT) |
---|
96 | |
---|
97 | self.update_display() |
---|
98 | |
---|
99 | #self.client = \ |
---|
100 | #puzzlebox_brainstorms_client.puzzlebox_brainstorms_client( |
---|
101 | #self.log, \ |
---|
102 | #configuration.SERVER_HOST, \ |
---|
103 | #configuration.SERVER_PORT, \ |
---|
104 | #configuration.MAX_CONNECTION_ATTEMPTS) |
---|
105 | |
---|
106 | |
---|
107 | ##################################################################### |
---|
108 | |
---|
109 | def initialize_pygame(self): |
---|
110 | |
---|
111 | if (self.operating_system != "windows"): |
---|
112 | os.environ['SDL_VIDEO_WINDOW_POS'] = \ |
---|
113 | "%i,%i" % (self.display_window_x_coordinate, \ |
---|
114 | self.display_window_y_coordinate) |
---|
115 | |
---|
116 | self.pygame.init() |
---|
117 | |
---|
118 | if (self.FLIP): |
---|
119 | |
---|
120 | self.screen = self.pygame.display.set_mode((self.display_window_x_dimension, \ |
---|
121 | self.display_window_y_dimension), \ |
---|
122 | pygame.HWSURFACE|pygame.DOUBLEBUF) |
---|
123 | |
---|
124 | else: |
---|
125 | |
---|
126 | self.screen = self.pygame.display.set_mode((self.display_window_x_dimension, \ |
---|
127 | self.display_window_y_dimension), \ |
---|
128 | pygame.HWSURFACE) |
---|
129 | |
---|
130 | |
---|
131 | if (self.DEBUG >= 2): |
---|
132 | print "Display Driver:", |
---|
133 | print pygame.display.get_driver() |
---|
134 | print "Display Info:", |
---|
135 | print pygame.display.Info() |
---|
136 | #print "Get Flags:", |
---|
137 | #print pygame.display.screen.get_flags() |
---|
138 | |
---|
139 | |
---|
140 | #self.pygame.mouse.set_visible(0) |
---|
141 | self.pygame.mouse.set_visible(1) |
---|
142 | |
---|
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 | |
---|
167 | for each in button_layout['buttons']: |
---|
168 | button_image = each[0] |
---|
169 | activated_image = each[1] |
---|
170 | button_x = each[2] |
---|
171 | button_y = each[3] |
---|
172 | command = each[4] |
---|
173 | |
---|
174 | if ((button_image != None) and \ |
---|
175 | (button_image != '.') and \ |
---|
176 | (button_image != '..') and \ |
---|
177 | (os.path.exists(os.path.join(self.image_directory, button_image)))): |
---|
178 | |
---|
179 | button_image = os.path.join(self.image_directory, button_image) |
---|
180 | |
---|
181 | else: |
---|
182 | if self.DEBUG: |
---|
183 | print "Error: Button image does not exist:" |
---|
184 | print " %s" % os.path.join(self.image_directory, button_image) |
---|
185 | continue |
---|
186 | |
---|
187 | |
---|
188 | if ((activated_image != None) and \ |
---|
189 | (activated_image != '.') and \ |
---|
190 | (activated_image != '..') and \ |
---|
191 | (os.path.exists(os.path.join(self.image_directory, activated_image)))): |
---|
192 | |
---|
193 | activated_image = os.path.join(self.image_directory, 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, activated_image) |
---|
199 | continue |
---|
200 | |
---|
201 | |
---|
202 | interface_button = \ |
---|
203 | button.puzzlebox_brainstorms_client_interface_button( \ |
---|
204 | self.pygame, \ |
---|
205 | self.screen, \ |
---|
206 | button_image, \ |
---|
207 | activated_image, \ |
---|
208 | button_x, \ |
---|
209 | button_y, \ |
---|
210 | command, \ |
---|
211 | self.DEBUG) |
---|
212 | |
---|
213 | |
---|
214 | self.buttons.append(interface_button) |
---|
215 | |
---|
216 | |
---|
217 | ################################################################## |
---|
218 | |
---|
219 | def update_button(self, command): |
---|
220 | |
---|
221 | for each in self.buttons: |
---|
222 | if each.command == command: |
---|
223 | each.activated = True |
---|
224 | each.display() |
---|
225 | |
---|
226 | |
---|
227 | ################################################################## |
---|
228 | |
---|
229 | def send_commands_to_server(self, commands): |
---|
230 | |
---|
231 | for command in commands: |
---|
232 | |
---|
233 | #Command Line Client version |
---|
234 | command_line = 'python puzzlebox_brainstorms_client.py %s' % command |
---|
235 | |
---|
236 | if self.DEBUG: |
---|
237 | print "executing: %s" % command_line |
---|
238 | |
---|
239 | os.system(command_line) |
---|
240 | |
---|
241 | |
---|
242 | ################################################################## |
---|
243 | |
---|
244 | def start(self): |
---|
245 | |
---|
246 | update_screen_on_next_pass = False |
---|
247 | clock = self.pygame.time.Clock() |
---|
248 | |
---|
249 | while 1: |
---|
250 | |
---|
251 | if (update_screen_on_next_pass): |
---|
252 | # Redraw Background |
---|
253 | self.draw_background() |
---|
254 | |
---|
255 | for each in self.buttons: |
---|
256 | each.activated = False |
---|
257 | each.display() |
---|
258 | |
---|
259 | |
---|
260 | self.update_display() |
---|
261 | |
---|
262 | update_screen_on_next_pass = False |
---|
263 | |
---|
264 | |
---|
265 | commands = [] |
---|
266 | |
---|
267 | for event in self.pygame.event.get(): |
---|
268 | |
---|
269 | if (event.type is self.pygame.locals.QUIT): |
---|
270 | #reactor.stop() |
---|
271 | sys.exit() |
---|
272 | |
---|
273 | |
---|
274 | elif (event.type == self.pygame.locals.KEYDOWN): |
---|
275 | |
---|
276 | if 'key' in event.dict.keys(): |
---|
277 | |
---|
278 | if (event.dict['key'] == self.pygame.K_ESCAPE): |
---|
279 | sys.exit() |
---|
280 | |
---|
281 | elif ((event.dict['key'] == self.pygame.K_UP) or \ |
---|
282 | (event.dict['key'] == self.pygame.K_w)): |
---|
283 | command = 'drive_forward' |
---|
284 | self.update_button(command) |
---|
285 | commands.append(command) |
---|
286 | |
---|
287 | elif ((event.dict['key'] == self.pygame.K_DOWN) or \ |
---|
288 | (event.dict['key'] == self.pygame.K_s) or \ |
---|
289 | (event.dict['key'] == self.pygame.K_x)): |
---|
290 | command = 'drive_reverse' |
---|
291 | self.update_button(command) |
---|
292 | commands.append(command) |
---|
293 | |
---|
294 | elif ((event.dict['key'] == self.pygame.K_LEFT) or \ |
---|
295 | (event.dict['key'] == self.pygame.K_a) or \ |
---|
296 | (event.dict['key'] == self.pygame.K_q)): |
---|
297 | command = 'turn_left' |
---|
298 | self.update_button(command) |
---|
299 | commands.append(command) |
---|
300 | |
---|
301 | elif ((event.dict['key'] == self.pygame.K_RIGHT) or \ |
---|
302 | (event.dict['key'] == self.pygame.K_d) or \ |
---|
303 | (event.dict['key'] == self.pygame.K_e)): |
---|
304 | command = 'turn_right' |
---|
305 | self.update_button(command) |
---|
306 | commands.append(command) |
---|
307 | |
---|
308 | elif (event.dict['key'] == self.pygame.K_z): |
---|
309 | command = 'turn_left_in_reverse' |
---|
310 | self.update_button(command) |
---|
311 | commands.append(command) |
---|
312 | |
---|
313 | elif ((event.dict['key'] == self.pygame.K_PAGEDOWN) or \ |
---|
314 | (event.dict['key'] == self.pygame.K_c)): |
---|
315 | command = 'turn_right_in_reverse' |
---|
316 | self.update_button(command) |
---|
317 | commands.append(command) |
---|
318 | |
---|
319 | |
---|
320 | if commands != []: |
---|
321 | self.update_display() |
---|
322 | self.send_commands_to_server(commands) |
---|
323 | update_screen_on_next_pass = True |
---|
324 | |
---|
325 | |
---|
326 | clock.tick(60) |
---|
327 | |
---|
328 | |
---|
329 | ##################################################################### |
---|
330 | # Functions |
---|
331 | ##################################################################### |
---|
332 | |
---|
333 | ##################################################################### |
---|
334 | # Main |
---|
335 | ##################################################################### |
---|
336 | |
---|
337 | if __name__ == '__main__': |
---|
338 | |
---|
339 | #log = puzzlebox_logger.puzzlebox_logger(logfile='mcc') |
---|
340 | log = None |
---|
341 | |
---|
342 | |
---|
343 | # Collect default settings and command line parameters |
---|
344 | server_host = SERVER_HOST |
---|
345 | server_port = SERVER_PORT |
---|
346 | display_window_x_coordinate = DISPLAY_WINDOW_X_COORDINATE |
---|
347 | display_window_y_coordinate = DISPLAY_WINDOW_Y_COORDINATE |
---|
348 | display_window_x_dimension = DISPLAY_WINDOW_X_DIMENSION |
---|
349 | display_window_y_dimension = DISPLAY_WINDOW_Y_DIMENSION |
---|
350 | window_background_color = WINDOW_BACKGROUND_COLOR |
---|
351 | |
---|
352 | |
---|
353 | for each in sys.argv: |
---|
354 | |
---|
355 | if each.startswith("--host="): |
---|
356 | server_host = each[ len("--host="): ] |
---|
357 | if each.startswith("--port="): |
---|
358 | server_port = each[ len("--port="): ] |
---|
359 | |
---|
360 | |
---|
361 | # Window Defaults |
---|
362 | try: |
---|
363 | display_window_x_coordinate = string.atoi(sys.argv[-4]) |
---|
364 | display_window_y_coordinate = string.atoi(sys.argv[-3]) |
---|
365 | display_window_x_dimension = string.atoi(sys.argv[-2]) |
---|
366 | display_window_y_dimension = string.atoi(sys.argv[-1]) |
---|
367 | except: |
---|
368 | if DEBUG: |
---|
369 | print "Using default display coordinates" |
---|
370 | |
---|
371 | |
---|
372 | interface = puzzlebox_brainstorms_client_interface( \ |
---|
373 | log, \ |
---|
374 | display_window_x_coordinate, \ |
---|
375 | display_window_y_coordinate, \ |
---|
376 | display_window_x_dimension, \ |
---|
377 | display_window_y_dimension, \ |
---|
378 | window_background_color, \ |
---|
379 | DEBUG=DEBUG) |
---|
380 | |
---|
381 | interface.start() |
---|
382 | |
---|
383 | reactor.callWhenRunning(interface.start) |
---|
384 | #print "bing" |
---|
385 | reactor.run() |
---|
386 | |
---|