5 CONFIG
= 'game_config.xml'
7 WINDOW_SIZE
= (640, 480)
8 WINDOW_CAPTION
= "Testing stuff"
11 BROADCAST_BIND
= 50031
20 SERVERREQUEST
= "i_want_server"
21 SERVEROFFER
= "Want_server?"
22 SERVERKILL
= "DIE_server!!"
23 YOUTHERE
= "you_there?"
24 IMHERE
= "yeah,i'm_here"
29 LIST
= "peoples_on_server"
30 SOMEONEJOINED
= "dude,someone_joined"
31 SOMEONELEFT
= "someone_left"
32 YOUROUT
= "get_lost_punk"
33 LETTER
= "listen_to_me"
44 from xml
.dom
.minidom
import parse
, parseString
45 from threading
import Lock
53 from pgu
import gui
as pgui
59 from OpenGL
.GL
import *
61 from OpenGL
.GLU
import *
65 the_event_receiver
= 0
72 def __init__(self
, params
):
79 self
.outgoing_lock
= Lock()
80 self
.incoming_lock
= Lock()
82 self
.broadcast
= socket(AF_INET
, SOCK_DGRAM
)
83 self
.broadcast
.setsockopt(SOL_SOCKET
, SO_BROADCAST
, 1)
85 self
.message
= socket(AF_INET
, SOCK_DGRAM
)
87 self
.message_lock
= Lock()
92 self
.current_server
= []
93 self
.requested_server
= ()
97 self
.commands
= CommandLine()
99 self
.messages_in_lock
= Lock()
101 if (params
.has_key('broadcast_bind')):
102 self
.broadcast
.bind(((''), params
['broadcast_bind']))
104 print "Client.__init__(): No broadcast port number chosen, using", BROADCAST_BIND
105 self
.broadcast
.bind(((''), BROADCAST_BIND
))
107 if (params
.has_key('message_port')):
108 self
.message
.bind(((''), params
['message_port']))
110 print "Client.__init__(): No broadcast port number chosen, using", MESSAGE
111 self
.message
.bind(((''), MESSAGE
))
115 print "Client.run(): Making threads..."
117 self
.send_thread
= threading
.Thread(target
=self
.send
)
118 self
.receive_thread
= threading
.Thread(target
=self
.receive
)
119 self
.process_in_thread
= threading
.Thread(target
=self
.process_in
)
120 self
.process_out_thread
= threading
.Thread(target
=self
.process_out
)
122 self
.send_thread
.start()
123 self
.receive_thread
.start()
124 self
.process_in_thread
.start()
125 self
.process_out_thread
.start()
127 self
.send_thread
.join()
128 self
.receive_thread
.join()
129 self
.process_in_thread
.join()
130 self
.process_out_thread
.join()
132 print "Client.run(): Closed threads"
138 print "Client.send() beginning..."
140 while (the_engine
.running
):
142 if (len(self
.outgoing
)):
144 self
.outgoing_lock
.acquire()
145 print "Client.send(): Acquired outgoing_lock"
147 data_out
= self
.outgoing
.pop(0)
149 self
.outgoing_lock
.release()
150 print "Client.send(): Released outgoing_lock"
152 if type(data_out
[1]) != type(()):
153 data_out
[1] = (data_out
[1][0], data_out
[1][1])
155 if data_out
[1][0] != '<broadcast>':
156 self
.message_lock
.acquire()
157 self
.message
.sendto(data_out
[0], data_out
[1])
158 self
.message_lock
.release()
160 self
.broadcast
.sendto(data_out
[0], data_out
[1])
161 print "Client.send(): Sent data", data_out
166 print "Client.receive() beginning..."
168 self
.message
.settimeout(TIMEOUT
)
170 while (the_engine
.running
):
175 self
.message_lock
.acquire()
176 data_in
= self
.message
.recvfrom(BUFSIZE
)
177 self
.message_lock
.release()
182 self
.incoming_lock
.acquire()
183 self
.incoming
.append(data_in
)
184 self
.incoming_lock
.release()
186 def process_in(self
):
188 print "Client.process_in() beginning..."
192 while (the_engine
.running
):
194 if (len(self
.incoming
)):
197 self
.incoming_lock
.acquire()
198 print "Client.process_in(): Acquired incoming_lock"
200 data_in
= self
.incoming
.pop(0)
202 self
.incoming_lock
.release()
203 print "Client.process_in(): Released incoming_lock"
205 if (data_in
[0][:len(YOUTHERE
)] == YOUTHERE
):
206 print "Client.process_in(): Received YOUTHERE, responding with IMHERE"
208 response
= [IMHERE
, data_in
[1]]
210 self
.outgoing_lock
.acquire()
211 print "Client.process_in(): Acquired outgoing_lock"
213 self
.outgoing
.append(response
)
215 self
.outgoing_lock
.release()
216 print "Client.process_in(): Released outgoing_lock"
218 if (data_in
[0][:len(SERVEROFFER
)] == SERVEROFFER
):
219 print "Client.process_in(): Received SERVEROFFER"
221 split_strings
= data_in
[0].split()
222 self
.servers
[split_strings
[2]] = [data_in
[0][0], int(split_strings
[1])]
224 event
= [SERVEROFFER
, {'server_name': split_strings
[2]}]
226 the_engine
.events
.addEvent(event
)
228 if (data_in
[0][:len(YOUREIN
)] == YOUREIN
):
229 if (data_in
[1] == self
.requested_server
):
230 self
.current_server
= data_in
[1]
231 self
.requested_server
= ()
234 data
= {'server': data_in
[1]}
237 the_engine
.events
.addEvent(event
)
239 print "Client.process_in(): Received YOUREIN, joined server"
241 if (data_in
[0][:len(LIST
)] == LIST
):
242 print "Client.process_in(): Received LIST"
244 if (data_in
[1] == self
.current_server
):
245 split_strings
= data_in
[0].split()
249 for string
in split_strings
:
251 self
.members
.append(string
)
254 data
= {'names': self
.members
}
257 the_engine
.events
.addEvent(event
)
259 if (data_in
[0][:len(SOMEONEJOINED
)] == SOMEONEJOINED
):
260 print "Client.process_in(): Received SOMEONEJOINED"
262 if (data_in
[1] == self
.current_server
):
265 for member
in self
.members
:
266 if member
== data_in
[0][len(SOMEONEJOINED
) + 1:]:
269 if left_member
!= '':
270 self
.members
.remove(left_member
)
272 if (data_in
[0][:len(YOUROUT
)] == YOUROUT
):
273 print "Client.process_in(): Received YOUROUT"
275 if (data_in
[1] == self
.current_server
):
276 self
.current_server
= []
282 the_engine
.events
.addEvent(event
)
284 if (data_in
[0][:len(LETTER
)] == LETTER
):
285 print "Client.process_in(): Received LETTER"
287 if (data_in
[1] == self
.current_server
):
288 split_strings
= data_in
[0].split(':')
290 the_message_data
= data_in
[0][this_string
.find(":", len(LETTER
) + 1) + 1:]
291 the_message_origin
= split_strings
[1]
293 self
.messages_in_lock
.acquire()
294 the_message
= (the_message_data
, the_message_origin
)
295 self
.messages_in
.append(the_message
)
296 self
.messages_in_lock
.release()
299 data
= {'message': the_message_data
}
302 the_engine
.events
.addEvent(event
)
304 def process_out(self
):
306 print "Client.process_out(): beginning..."
308 while (the_engine
.running
):
310 if (self
.commands
.size()):
312 # {'type': ..., 'data': ...}
314 command
= self
.commands
.getCommand()
316 if command
['type'] == SERVERKILL
:
318 if self
.current_server
!= ():
319 out_message
= [''.join([SERVERKILL
, ' ']), self
.current_server
]
321 if command
.has_key('password'):
322 out_message
[0] += command
['password']
324 self
.outgoing_lock
.acquire()
325 self
.outgoing
.append(out_message
)
326 self
.outgoing_lock
.release()
328 if command
['type'] == SERVERREQUEST
:
332 out_message
.append(''.join([SERVERREQUEST
, " ", str(self
.params
['message_port'])]))
333 out_message
.append(['<broadcast>', self
.params
['broadcast_port']])
335 self
.outgoing_lock
.acquire()
336 self
.outgoing
.append(out_message
)
337 self
.outgoing_lock
.release()
339 if command
['type'] == GETLIST
:
341 if self
.current_server
!= ():
343 out_message
= [GETLIST
, self
.current_server
]
345 self
.outgoing_lock
.acquire()
346 self
.outgoing
.append(out_message
)
347 self
.outgoing_lock
.release()
349 if command
['type'] == IMOUT
:
351 if self
.current_sever
!= ():
353 out_message
= [IMOUT
, self
.current_server
]
355 self
.outgoing_lock
.acquire()
356 self
.outgoing
.append(out_message
)
357 self
.outgoing_lock
.release()
359 if command
['type'] == LETTER
:
361 if self
.current_server
!= ():
363 out_message
= [LETTER
+ ':', self
.current_server
]
365 for dest
in command
['destinations']:
366 if dest
in self
.members
:
367 out_message
[0] += " " + dest
369 out_message
[0] += ":" + command
['message']
371 self
.outgoing_lock
.acquire()
372 self
.outgoing
.append(out_message
)
373 self
.outgoing_lock
.release()
381 self
.items_lock
= Lock()
385 return len(self
.items
)
389 self
.items_lock
.acquire()
390 the_item
= self
.items
.pop(0)
391 self
.items_lock
.release()
395 def addItem(self
, new_item
):
397 self
.items_lock
.acquire()
398 self
.items
.append(new_item
)
399 self
.items_lock
.release()
403 class EventReceiver(QueueLock
):
406 return self
.getItem()
407 def addEvent(self
, event
):
411 class CommandLine(QueueLock
):
413 def getCommand(self
):
414 return self
.getItem()
415 def addCommand(self
, command
):
416 self
.addItem(command
)
421 def __init__(self
, config
):
429 video_flags
= OPENGL|DOUBLEBUF
432 self
.display
= pygame
.display
.set_mode(config
['window_size'], video_flags
)
434 self
.font
= pygame
.font
.SysFont("default", 18)
435 self
.fontBig
= pygame
.font
.SysFont("default", 24)
436 self
.fontSub
= pygame
.font
.SysFont("default", 20)
437 self
.theme
= pgui
.Theme(['test_theme', 'gray', 'default'])
438 # self.theme = pgui.Theme('gray')
439 # self.theme = pgui.Theme('default')
441 self
.resize((config
['window_size']))
443 glShadeModel(GL_SMOOTH
)
444 glClearColor(self
.config
['background_color'][0],
445 self
.config
['background_color'][1],
446 self
.config
['background_color'][2],
447 self
.config
['background_color'][3])
449 glEnable(GL_DEPTH_TEST
)
450 glDepthFunc(GL_LEQUAL
)
451 glHint(GL_PERSPECTIVE_CORRECTION_HINT
, GL_NICEST
)
453 pygame
.display
.set_caption(config
['window_title'])
455 self
.gui_screen
= lamina
.LaminaScreenSurface()
457 self
.app
= pgui
.App(theme
=self
.theme
)
458 self
.app
._screen
= self
.gui_screen
.surf
459 self
.main_container
= pgui
.Container(width
=config
['window_size'][0])
463 self
.events
= EventReceiver()
465 # self.files is a map of filename to file
468 # {parent, [children], {object_defs},
469 # {menu_defs}, {object_instances}, {menu_instances}}
473 for file in listdir(self
.config
['pack_dir']):
474 if (is_zipfile(os
.path
.join(self
.config
['pack_dir'], file))):
475 self
.zipfiles
.append(ZipFile(os
.path
.join(self
.config
['pack_dir'], file)))
477 self
.running
= self
.addFile(self
.config
['init'], '')
478 self
.app
.init(self
.main_container
)
480 self
.ticks
= pygame
.time
.get_ticks()
483 if self
.running
== 0:
484 print "Engine.__init__(): Failed adding initial file"
490 self
.process_events()
496 pygame
.display
.flip()
502 self
.oldticks
= self
.ticks
503 self
.ticks
= pygame
.time
.get_ticks()
507 self
.fps
= (self
.frames
* 1000) / (self
.ticks
- self
.oldticks
)
509 pygame
.display
.set_caption(''.join([self
.config
['window_title'], ' ', str(self
.fps
)]))
513 def process_events(self
):
517 for e
in pygame
.event
.get():
520 print "Should be quitting..."
523 change
= self
.app
.update(self
.gui_screen
.surf
)
526 self
.gui_screen
.refresh(change
)
528 new_event
= self
.convertEvent(e
)
529 self
.events
.addEvent(new_event
)
534 if self
.events
.size():
535 new_event
= self
.events
.getEvent()
543 for file in self
.files
.keys():
545 for name
in self
.files
[file]['object_instances'].keys():
547 self
.files
[file]['object_instances'][name
].event(new_event
[0], new_event
[1])
551 def convertEvent(self
, e
):
558 if e
.type == ACTIVEEVENT
:
559 data
['gain'] = e
.gain
560 data
['state'] = e
.state
562 if e
.type == KEYDOWN
:
563 data
['unicode'] = e
.unicode
571 if e
.type == MOUSEMOTION
:
574 data
['buttons'] = e
.buttons
576 if e
.type == MOUSEBUTTONUP
:
578 data
['button'] = e
.button
580 if e
.type == MOUSEBUTTONDOWN
:
582 data
['button'] = e
.button
584 if e
.type == JOYAXISMOTION
:
586 data
['axis'] = e
.axis
587 data
['value'] = e
.value
589 if e
.type == JOYBALLMOTION
:
591 data
['ball'] = e
.ball
594 if e
.type == JOYHATMOTION
:
597 data
['value'] = e
.value
599 if e
.type == JOYBUTTONUP
:
601 data
['button'] = e
.button
603 if e
.type == JOYBUTTONDOWN
:
605 data
['button'] = e
.button
607 if e
.type == VIDEORESIZE
:
608 data
['size'] = e
.size
612 if e
.type == VIDEOEXPOSE
:
615 if e
.type == USEREVENT
:
616 data
['code'] = e
.code
620 if e
.type == QUIT
: type = "QUIT"
621 if e
.type == ACTIVEEVENT
: type = "ACTIVEEVENT"
622 if e
.type == KEYDOWN
: type = "KEYDOWN"
623 if e
.type == KEYUP
: type = "KEYUP"
624 if e
.type == MOUSEMOTION
: type = "MOUSEMOTION"
625 if e
.type == MOUSEBUTTONUP
: type = "MOUSEBUTTONUP"
626 if e
.type == MOUSEBUTTONDOWN
: type = "MOUSEBUTTONDOWN"
627 if e
.type == JOYAXISMOTION
: type = "JOYAXISMOTION"
628 if e
.type == JOYBALLMOTION
: type = "JOYBALLMOTION"
629 if e
.type == JOYHATMOTION
: type = "JOYHATMOTION"
630 if e
.type == JOYBUTTONUP
: type = "JOYBUTTONUP"
631 if e
.type == JOYBUTTONDOWN
: type = "JOYBUTTONDOWN"
632 if e
.type == VIDEORESIZE
: type = "VIDEORESIZE"
633 if e
.type == VIDEOEXPOSE
: type = "VIDEOEXPOSE"
634 if e
.type == USEREVENT
: type = "USEREVENT"
640 glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT
)
646 self
.gui_screen
.display()
648 def resize(self
, (width
, height
)):
651 glViewport(0, 0, width
, height
)
652 glMatrixMode(GL_PROJECTION
)
654 gluPerspective(45, 1.0*width
/height
, 0.1, 100.0)
655 glMatrixMode(GL_MODELVIEW
)
660 def addFile(self
, filename
, parent
):
662 # Because I have alot of nesting in this function,
663 # I am using 2 space formatting
665 for zipfile
in self
.zipfiles
:
667 for file in zipfile
.namelist():
669 if (file == filename
):
670 dom
= parseString(zipfile
.read(file))
672 object_instances_d
= []
675 if dom
.childNodes
[0].nodeName
== 'game':
676 self
.files
[filename
] = {
680 'object_instances': {},
681 'menu_instances': {},
684 for node
in dom
.childNodes
[0].childNodes
:
686 if (node
.nodeName
== 'def'):
688 for sub_node
in node
.childNodes
:
690 if (sub_node
.nodeName
== 'object'):
694 for suber_node
in sub_node
.childNodes
:
696 if (suber_node
.nodeName
== 'name'):
697 if len(suber_node
.childNodes
):
698 temp_object_def
['name'] = suber_node
.childNodes
[0].nodeValue
700 if (suber_node
.nodeName
== 'script'):
701 if len(suber_node
.childNodes
):
702 temp_object_def
['script'] = suber_node
.childNodes
[0].nodeValue
704 if (suber_node
.nodeName
== 'tangible'):
705 if len(suber_node
.childNodes
):
706 temp_object_def
['tangible'] = suber_node
.childNodes
[0].nodeValue
708 if (suber_node
.nodeName
== 'type'):
709 if len(suber_node
.childNodes
):
710 temp_object_def
['type'] = suber_node
.childNodes
[0].nodeValue
712 self
.files
[filename
]['object_defs'][temp_object_def
['name']] = temp_object_def
715 if (sub_node
.nodeName
== 'menu'):
718 temp_menu_def
['elements'] = {}
720 temp_element_set
= {}
722 for suber_node
in sub_node
.childNodes
:
724 if (suber_node
.nodeName
== 'name'):
725 temp_menu_def
['name'] = suber_node
.childNodes
[0].nodeValue
727 if (suber_node
.nodeName
== 'elements'):
729 for suberer_node
in suber_node
.childNodes
:
731 if (suberer_node
.nodeType
!= 3 and suberer_node
.nodeType
!= 8):
732 if (suberer_node
.hasAttribute('name')):
733 temp_element_set
['name'] = suberer_node
.getAttribute('name')
735 if (suberer_node
.hasAttribute('x')):
736 temp_element_set
['x'] = int(suberer_node
.getAttribute('x'))
738 if (suberer_node
.hasAttribute('y')):
739 temp_element_set
['y'] = int(suberer_node
.getAttribute('y'))
741 if (suberer_node
.hasAttribute('width')):
742 temp_element_set
['width'] = int(suberer_node
.getAttribute('width'))
744 if (suberer_node
.hasAttribute('height')):
745 temp_element_set
['height'] = int(suberer_node
.getAttribute('height'))
747 if (suberer_node
.hasAttribute('parent')):
748 temp_element_set
['parent'] = suberer_node
.getAttribute('parent')
750 if (suberer_node
.hasAttribute('target')):
751 temp_element_set
['target'] = suberer_node
.getAttribute('target')
753 if (suberer_node
.hasAttribute('text')):
754 temp_element_set
['text'] = suberer_node
.getAttribute('text')
756 temp_element_set
['type'] = suberer_node
.nodeName
757 temp_menu_def
['elements'][temp_element_set
['name']] = temp_element_set
758 temp_element_set
= {}
760 self
.files
[filename
]['menu_defs'][temp_menu_def
['name']] = temp_menu_def
763 temp_element_set
= {}
765 if (node
.nodeName
== 'instance'):
767 for sub_node
in node
.childNodes
:
769 if (sub_node
.nodeName
== 'object'):
770 object_instances_d
.append(sub_node
.childNodes
[0].nodeValue
)
772 if (sub_node
.nodeName
== 'menu'):
773 menu_instances
.append(sub_node
.childNodes
[0].nodeValue
)
776 self
.files
[parent
]['children'].append(filename
)
777 self
.files
[filename
]['parent'] = parent
781 for menuname
, menu_data
in self
.files
[filename
]['menu_defs'].iteritems():
782 self
.files
[filename
]['menu_instances'][menuname
] = Menu(menu_def
=menu_data
)
784 for menuname
in menu_instances
:
785 if (self
.files
[filename
]['menu_instances'].has_key(menuname
)):
786 self
.files
[filename
]['menu_instances'][menuname
].show()
788 for file in self
.files
.keys():
789 if self
.files
[file]['menu_instances'].has_key(menuname
):
790 self
.files
[file]['menu_instances'][menuname
].show()
792 for objectname
in object_instances_d
:
793 self
.addObject(objectname
)
797 # ending 2-space formatting
799 def removeFile(self
, filename
):
801 if (self
.files
.has_key(filename
)):
803 for child
in self
.files
[filename
]['children']:
804 self
.removeFile(child
)
806 del self
.files
[filename
]['children']
808 parent
= self
.files
[filename
]['parent']
809 self
.files
[parent
].pop(self
.files
[parent
]['children'].index(filename
))
811 del self
.files
[filename
]
815 def hideAllMenus(self
):
817 for file, contents
in self
.files
.iteritems():
818 for name
, menu
in contents
['menu_instances'].iteritems():
823 def showMenu(self
, menu_name
):
825 for file, contents
in self
.files
.iteritems():
826 for name
, menu
in contents
['menu_instances'].iteritems():
827 if contents
['menu_instances'].has_key(menu_name
):
828 contents
['menu_instances'][menu_name
].show()
832 def hideMenu(self
, menu_name
):
834 for file, contents
in self
.files
:
835 for name
, menu
in contents
['menu_instances'].iteritems():
836 if contents
['menu_instances'].has_key(menu_name
):
837 contents
['menu_instances'][menu_name
].hide()
841 def setMenu(self
, menu_name
):
844 self
.showMenu(menu_name
)
848 def addObject(self
, objectname
):
850 for file in self
.files
.keys():
851 if self
.files
[file]['object_defs'].has_key(objectname
):
852 self
.files
[file]['object_instances'][objectname
] = GameObject(
853 self
.files
[file]['object_defs'][objectname
])
857 def removeObject(self
, objectname
):
859 for file in self
.files
.keys():
860 if self
.files
[file]['object_defs'].has_key(objectname
):
861 del self
.files
[file]['object_instances'][objectname
]
865 def getImage(self
, filename
):
867 the_buf
= pygame
.image
.load(self
.getStringIOFile(filename
))
871 def getStringIOFile(self
, filename
):
873 for zip_file
in self
.zipfiles
:
874 for file in zip_file
.namelist():
876 if (filename
== file):
878 the_string_io
= StringIO
.StringIO()
879 print >>the_string_io
, zip_file
.read(file)
880 the_string_io
.seek(0)
885 def getStringData(self
, filename
):
886 for zip_file
in self
.zipfiles
:
887 for file in zip_file
.namelist():
889 if (filename
== file):
890 return zip_file
.read(file)
895 def __init__(self
, menu_def
):
897 self
.menu_def
= menu_def
900 for name
, element
in menu_def
['elements'].iteritems():
902 multiplier_x
= float(the_engine
.config
['window_size'][0]) / 100.00
903 multiplier_y
= float(the_engine
.config
['window_size'][1]) / 100.00
905 dx
= float(element
['x']) * multiplier_x
906 dy
= float(element
['y']) * multiplier_y
908 dwidth
= float(element
['width']) * multiplier_x
909 dheight
= float(element
['height']) * multiplier_y
911 if (element
['type'] == 'button'):
912 self
.widgets
[name
] = pgui
.Button(element
['text'].encode(), width
=dwidth
, height
=dheight
)
913 self
.widgets
[name
].connect(pgui
.CLICK
, self
.clicked
, name
)
915 if (element
['type'] == 'image'):
916 self
.widgets
[name
] = pgui
.Image(the_engine
.getImage(element
['text']))
918 if (element
['type'] == 'label'):
919 self
.widgets
[name
] = pgui
.Label(element
['text'])
921 if (element
['type'] == 'listbox'):
922 self
.widgets
[name
] = pgui
.List(width
=dwidth
, height
=dheight
)
924 if (self
.widgets
.has_key(name
)):
925 self
.widgets
[name
].resize(width
=dwidth
, height
=dheight
)
927 print "Menu.__init__(): Widget type", element
['type'], " not implemented yet, skipping."
935 multiplier_x
= float(the_engine
.config
['window_size'][0]) / 100.00
936 multiplier_y
= float(the_engine
.config
['window_size'][1]) / 100.00
938 if (self
.hidden
== 1):
939 for name
, element
in self
.menu_def
['elements'].iteritems():
940 dx
= float(element
['x']) * multiplier_x
941 dy
= float(element
['y']) * multiplier_y
943 if self
.widgets
.has_key(name
):
944 the_engine
.main_container
.add(self
.widgets
[name
], dx
, dy
)
954 if (self
.hidden
!= 1):
955 for name
, element
in self
.menu_def
['elements'].iteritems():
957 if (self
.widgets
.has_key(name
)):
958 the_engine
.main_container
.remove(self
.widgets
[name
])
964 def clicked(self
, button_name
):
966 if self
.menu_def
['elements'].has_key(button_name
):
968 if self
.menu_def
['elements'][button_name
]['target'] != '':
969 exec(self
.menu_def
['elements'][button_name
]['target'])
971 the_engine
.events
.addEvent(['BUTTON_CLICKED',
972 {'name': button_name
, 'menu_name': self
.menu_def
['name']}])
978 def __init__(self
, dmold
):
984 if self
.mold
['script'] != '':
988 the_file
= the_engine
.getStringData(self
.mold
['script'])
993 self
.my_functions
= the_functions
997 def event(self
, event
, data
):
999 if self
.my_functions
.has_key(event
):
1001 self
.my_functions
[event
](data
)
1006 # parse command line stuff
1008 if (len(sys
.argv
) > 1):
1009 config
= sys
.argv
[1]
1011 print "main(): No config specified, using", CONFIG
1014 configuration
= parse_config(config
)
1016 if (configuration
.has_key('error') and configuration
['error']):
1017 print "main(): Error in parsing config."
1019 the_event_receiver
= EventReceiver()
1021 client_params
= {'broadcast_port':configuration
['broadcast_port'],
1022 'broadcast_bind':configuration
['broadcast_bind'],
1023 'message_port':configuration
['message_port'],
1024 'name':configuration
['name']}
1029 the_client
= Client(client_params
)
1030 the_engine
= Engine(configuration
)
1032 the_client_thread
= threading
.Thread(target
=the_client
.run
)
1033 # the_engine_thread = threading.Thread(target=the_engine.run)
1035 the_client_thread
.start()
1036 # the_engine_thread.start()
1040 the_client_thread
.join()
1041 # the_engine_thread.join()
1045 def parse_config(filename
):
1046 results
= {'error': 1}
1048 if (exists(filename
)):
1049 dom
= parse(filename
)
1051 print "parse_config():", filename
, "doesn't exist."
1054 if (dom
.childNodes
[0].nodeName
== 'config'):
1055 for node
in dom
.childNodes
[0].childNodes
:
1057 if (node
.nodeName
== 'window_title'):
1058 results
['window_title'] = node
.childNodes
[0].nodeValue
1060 if (node
.nodeName
== 'log'):
1061 results
['log'] = node
.childNodes
[0].nodeValue
1063 if (node
.nodeName
== 'name'):
1064 results
['name'] = node
.childNodes
[0].nodeValue
1066 if (node
.nodeName
== 'font'):
1067 results
['font'] = node
.childNodes
[0].nodeValue
1069 if (node
.nodeName
== 'init'):
1070 results
['init'] = node
.childNodes
[0].nodeValue
1072 if (node
.nodeName
== 'message_port'):
1073 results
['message_port'] = int(node
.childNodes
[0].nodeValue
)
1075 if (node
.nodeName
== 'broadcast_port'):
1076 results
['broadcast_port'] = int(node
.childNodes
[0].nodeValue
)
1078 if (node
.nodeName
== 'broadcast_bind'):
1079 results
['broadcast_bind'] = int(node
.childNodes
[0].nodeValue
)
1081 if (node
.nodeName
== 'fullscreen'):
1082 results
['fullscreen'] = int(node
.childNodes
[0].nodeValue
)
1084 if (node
.nodeName
== 'background_color'):
1086 string_parts
= node
.childNodes
[0].nodeValue
.split()
1087 results
['background_color'] = [float(string_parts
[0]), float(string_parts
[1])]
1088 results
['background_color'].append(float(string_parts
[2]))
1089 results
['background_color'].append(float(string_parts
[3]))
1091 if (node
.nodeName
== 'window_size'):
1093 string_parts
= node
.childNodes
[0].nodeValue
.split()
1094 results
['window_size'] = ((int(string_parts
[0]), int(string_parts
[1])))
1096 if (node
.nodeName
== 'gravity'):
1098 string_parts
= node
.childNodes
[0].nodeValue
.split()
1099 results
['gravity'] = [float(string_parts
[0]), float(string_parts
[1])]
1100 results
['gravity'].append(float(string_parts
[2]))
1102 if (node
.nodeName
== 'step_size'):
1103 results
['step_size'] = float(node
.childNodes
[0].nodeValue
)
1105 if (node
.nodeName
== 'pack_dir'):
1106 results
['pack_dir'] = node
.childNodes
[0].nodeValue
1108 results
['error'] = 0
1112 if __name__
== '__main__':