comparison orpg/gametree/nodehandlers/minilib.py @ 227:81d0bfd5e800 alpha

Traipse Alpha 'OpenRPG' {100612-00} Traipse is a distribution of OpenRPG that is designed to be easy to setup and go. Traipse also makes it easy for developers to work on code without fear of sacrifice. 'Ornery-Orc' continues the trend of 'Grumpy' and adds fixes to the code. 'Ornery-Orc's main goal is to offer more advanced features and enhance the productivity of the user. Update Summary (Preparing to close updates) New Features: New to Map, can re-order Grid, Miniatures, and Whiteboard layer draw order Fixes: Fix to InterParse that was causing an Infernal Loop with Namespace Internal Fix to XML data, removed old Minidom and switched to Element Tree Fix to Server that was causing eternal attempt to find a Server ID, in Register Rooms thread Fix to metaservers.xml file not being created
author sirebral
date Sat, 12 Jun 2010 03:50:37 -0500
parents bf799efe7a8a
children
comparison
equal deleted inserted replaced
182:4b2884f29a72 227:81d0bfd5e800
19 # 19 #
20 # File: minilib.py 20 # File: minilib.py
21 # Author: Ted Berg 21 # Author: Ted Berg
22 # Maintainer: 22 # Maintainer:
23 # Version: 23 # Version:
24 # $Id: minilib.py,v 1.28 2007/04/22 22:00:18 digitalxero Exp $ 24 # $Id: minilib.py,v Traipse 'Ornery-Orc' prof.ebral Exp $
25 # 25 #
26 # Description: nodehandler for a collection of miniatures. 26 # Description: nodehandler for a collection of miniatures.
27 # 27 #
28 28
29 __version__ = "$Id: minilib.py,v 1.28 2007/04/22 22:00:18 digitalxero Exp $" 29 __version__ = "$Id: minilib.py,v Traipse 'Ornery-Orc' prof.ebral Exp $"
30 30
31 """Nodehandler for collections of miniatures. User can add, delete, edit 31 """Nodehandler for collections of miniatures. User can add, delete, edit
32 miniatures as sending them to the map singly or in batches. 32 miniatures as sending them to the map singly or in batches.
33 """ 33 """
34 from core import * 34 from core import *
35 from orpg.dirpath import dir_struct 35 from orpg.dirpath import dir_struct
36 import string 36 import string
37 import map_miniature_nodehandler 37 import map_miniature_nodehandler
38 import orpg.mapper.map_msg 38 import orpg.mapper.map_msg
39 import orpg.minidom as minidom 39 import orpg.minidom as minidom
40 from orpg.tools.InterParse import Parse
40 # import scriptkit 41 # import scriptkit
41 42
42 # Constants 43 # Constants
43 TO_MINILIB_MAP = {'path':'url', 'label':'name', 'id':None, 'action':None} 44 TO_MINILIB_MAP = {'path':'url', 'label':'name', 'id':None, 'action':None}
44 FROM_MINILIB_MAP = {'url':'path', 'name':'label', 'unique':None} 45 FROM_MINILIB_MAP = {'url':'path', 'name':'label', 'unique':None}
45 CORE_ATTRIBUTES = ['name', 'url', 'unique', 'posy', 'posx', 'hide', 'face', 'heading', 'align', 'locked', 'width', 'height'] 46 CORE_ATTRIBUTES = ['name', 'url', 'unique', 'posy', 'posx', 'hide', 'face', 'heading', 'align', 'locked', 'width', 'height']
46 47
47 ATTRIBUTE_NAME = 'name'
48 ATTRIBUTE_URL = 'url'
49 ATTRIBUTE_UNIQUE = 'unique'
50 ATTRIBUTE_ID = 'id'
51 ATTRIBUTE_POSX = 'posx'
52 ATTRIBUTE_POSY = 'posy'
53
54 TAG_MINIATURE = 'miniature'
55
56 COMPONENT_MAP = 'map'
57 COMPONENT_SESSION = 'session'
58 # <nodehandler name='?' module='minilib' class='minilib_handler'> 48 # <nodehandler name='?' module='minilib' class='minilib_handler'>
59 # <miniature name='?' url='?' unique='?'></miniature> 49 # <miniature name='?' url='?' unique='?'></miniature>
60 # </nodehandler> 50 # </nodehandler>
61 51
62 class minilib_handler( node_handler ): 52 class minilib_handler( node_handler ):
98 widgets being used don't handle cells wider than the widgets are 88 widgets being used don't handle cells wider than the widgets are
99 expecting for a given column. 89 expecting for a given column.
100 """ 90 """
101 str = '<table border="2" >' 91 str = '<table border="2" >'
102 str += "<tr><th width='20%'>Label</th><th>Image</th><th width='65%'>URL</th><th>Unique</th></tr>" 92 str += "<tr><th width='20%'>Label</th><th>Image</th><th width='65%'>URL</th><th>Unique</th></tr>"
103 for mini in self.xml.findall(TAG_MINIATURE): 93 for mini in self.xml.findall('miniature'):
104 url = mini.get(ATTRIBUTE_URL) 94 url = mini.get('url')
105 label = mini.get(ATTRIBUTE_NAME) 95 label = mini.get('name')
106 flag = 0 96 flag = 0
107 try: flag = eval( mini.get(ATTRIBUTE_UNIQUE) ) 97 try: flag = eval( mini.get('unique') )
108 except: pass 98 except: pass
109 show = 'yes' 99 show = 'yes'
110 if flag: show = 'no' 100 if flag: show = 'no'
111 str += """<tr> 101 str += """<tr>
112 <td> %s </td> 102 <td> %s </td>
131 dict = {} 121 dict = {}
132 unique = '' 122 unique = ''
133 for attrib in obj.keys(): 123 for attrib in obj.keys():
134 key = TO_MINILIB_MAP.get( attrib, attrib ) 124 key = TO_MINILIB_MAP.get( attrib, attrib )
135 if key != None: dict[ key ] = obj.get( attrib ) 125 if key != None: dict[ key ] = obj.get( attrib )
136 dict[ ATTRIBUTE_UNIQUE ] = unique 126 dict[ 'unique' ] = unique
137 self.new_mini( dict ) 127 self.new_mini( dict )
138 else: node_handler.on_drop(self, evt) 128 else: node_handler.on_drop(self, evt)
139 129
140 130
141 def new_mini( self, data={}, add=1 ): 131 def new_mini( self, data={}, add=1 ):
142 mini = Element( TAG_MINIATURE ) 132 mini = Element( 'miniature' )
143 for key in data.keys(): mini.set( key, data[ key ] ) 133 for key in data.keys(): mini.set( key, data[ key ] )
144 for key in CORE_ATTRIBUTES: 134 for key in CORE_ATTRIBUTES:
145 if mini.get( key ) == '': mini.set( key, '0' ) 135 if mini.get( key ) == ('' or None): mini.set( key, '0' )
146 if add: 136 if add:
147 self.add_mini( mini ) 137 self.add_mini( mini )
148 self.add_leaf( mini ) 138 self.add_leaf( mini )
149 return mini 139 return mini
150 140
152 self.xml.append( mini ) 142 self.xml.append( mini )
153 143
154 def add_leaf( self, mini, icon='gear' ): 144 def add_leaf( self, mini, icon='gear' ):
155 tree = self.tree 145 tree = self.tree
156 icons = tree.icons 146 icons = tree.icons
157 key = mini.get( ATTRIBUTE_NAME ) 147 key = mini.get( 'name' )
158 self.mydata.append( mini ) 148 self.mydata.append( mini )
159 149
160 def update_leaves( self ): 150 def update_leaves( self ):
161 self.mydata = [] 151 self.mydata = []
162 for n in self.xml.findall(TAG_MINIATURE): self.add_leaf( n ) 152 for n in self.xml.findall('miniature'): self.add_leaf( n )
163 153
164 def on_drag( self, evt ): 154 def on_drag( self, evt ):
165 print 'drag event caught' 155 print 'drag event caught'
166 156
167 def send_mini_to_map( self, mini, count=1, addName=True ): 157 def send_mini_to_map( self, mini, count=1, addName=True ):
168 if mini == None: return 158 if mini == None: return
169 if mini.get( ATTRIBUTE_URL ) == '' or mini.get( ATTRIBUTE_URL ) == 'http://': 159 if mini.get( 'url' ) == '' or mini.get( 'url' ) == 'http://':
170 self.chat.ParsePost( self.chat.colorize(self.chat.syscolor, '"%s" is not a valid URL, the mini "%s" will not be added to the map' % ( mini.get( ATTRIBUTE_URL ), mini.get( ATTRIBUTE_NAME ) )) ) 160 Parse.Post( self.chat.colorize(self.chat.syscolor,
161 '"%s" is not a valid URL, the mini "%s" will not be added to the map' % (
162 mini.get( 'url' ), mini.get( 'name' ) )) )
171 return 163 return
172 session = component.get( COMPONENT_SESSION ) 164 session = component.get( 'session' )
173 if (session.my_role() != session.ROLE_GM) and (session.my_role() != session.ROLE_PLAYER): 165 if (session.my_role() != session.ROLE_GM) and (session.my_role() != session.ROLE_PLAYER):
174 component.get("chat").InfoPost("You must be either a player or GM to use the miniature Layer") 166 component.get("chat").InfoPost("You must be either a player or GM to use the miniature Layer")
175 return 167 return
176 map = component.get(COMPONENT_MAP) 168 canvas = component.get('map')
177 for loop in range( count ): 169 for loop in range( count ):
178 msg = self.get_miniature_XML( mini, addName) 170 msg = self.get_miniature_XML( mini, addName)
179 msg = str("<map action='update'><miniatures>" + msg + "</miniatures></map>") 171 msg = str("<map action='update'><miniatures>" + msg + "</miniatures></map>")
180 map.new_data( msg ) 172 canvas.new_data( msg )
181 session.send( msg ) 173 session.send( msg )
182 174
183 def get_miniature_XML( self, mini_xml, addName = True ): 175 def get_miniature_XML( self, mini_xml, addName = True ):
184 msg = orpg.mapper.map_msg.mini_msg() 176 msg = orpg.mapper.map_msg.mini_msg()
185 map = component.get( COMPONENT_MAP ) 177 canvas = component.get( 'map' )
186 session = component.get( COMPONENT_SESSION ) 178 session = component.get( 'session' )
187 msg.init_prop( ATTRIBUTE_ID, session.get_next_id() ) 179 msg.init_prop( 'id', session.get_next_id() )
188 msg.init_prop('selected', '1')# this will make the mini initially selected 180 msg.init_prop('selected', '1')# this will make the mini initially selected
189 for k in mini_xml.keys(): 181 for k in mini_xml.keys():
190 # translate our attributes to map attributes 182 # translate our attributes to map attributes
191 key = FROM_MINILIB_MAP.get( k, k ) 183 key = FROM_MINILIB_MAP.get( k, k )
192 if key != None: 184 if key != None:
193 if not addName and k == 'name': pass 185 if not addName and k == 'name': pass
194 else: msg.init_prop( key, mini_xml.get( k ) ) 186 else: msg.init_prop( key, mini_xml.get( k ) )
195 unique = self.is_unique( mini_xml ) 187 unique = self.is_unique( mini_xml )
196 if addName: label = mini_xml.get( ATTRIBUTE_NAME ) 188 if addName: label = mini_xml.get( 'name' )
197 else: label = '' 189 else: label = ''
198 return msg.get_all_xml() 190 return msg.get_all_xml()
199 191
200 def is_unique( self, mini ): 192 def is_unique( self, mini ):
201 unique = mini.get( ATTRIBUTE_UNIQUE ) 193 unique = mini.get( 'unique' )
202 val = 0 194 val = 0
203 try: val = eval( unique ) 195 try: val = eval( unique )
204 except: val = len( unique ) 196 except: val = len( unique )
205 return val 197 return val
206 198
207 def sanity_check_nodes( self ): 199 def sanity_check_nodes( self ):
208 for node in self.xml.findall(TAG_MINIATURE): 200 for node in self.xml.findall('miniature'):
209 if node.get( ATTRIBUTE_POSX ) == '': node.set( ATTRIBUTE_POSX, '0' ) 201 if node.get( 'posx' ) == '': node.set( 'posx', '0' )
210 if node.get( ATTRIBUTE_POSY ) == '': node.set( ATTRIBUTE_POSY, '0' ) 202 if node.get( 'posy' ) == '': node.set( 'posy', '0' )
211 203
212 def get_mini( self, index ): 204 def get_mini( self, index ):
213 try: return self.xml.findall(TAG_MINIATURE)[index] 205 try: return self.xml.findall('miniature')[index]
214 except: return None 206 except: return None
215 207
216 class mini_handler( node_handler ): 208 class mini_handler( node_handler ):
217 def __init__( self, xml, tree_node, handler ): 209 def __init__( self, xml, tree_node, handler ):
218 node_handler.__init__( self, xml, tree_node) 210 node_handler.__init__( self, xml, tree_node)
277 269
278 def buildList( self ): 270 def buildList( self ):
279 """Returns a dictionary of label => game tree miniature DOM node mappings. 271 """Returns a dictionary of label => game tree miniature DOM node mappings.
280 """ 272 """
281 self.list = [] 273 self.list = []
282 for mini in self.handler.xml.findall(TAG_MINIATURE): self.list.append( mini.get( ATTRIBUTE_NAME ) ) 274 for mini in self.handler.xml.findall('miniature'): self.list.append( mini.get( 'name' ) )
283 return self.list 275 return self.list
284 276
285 def on_close(self, evt): 277 def on_close(self, evt):
286 self.frame.Close() 278 self.frame.Close()
287 279
312 wx.Panel.__init__( self, frame, -1 ) 304 wx.Panel.__init__( self, frame, -1 )
313 self.handler = handler 305 self.handler = handler
314 self.frame = frame 306 self.frame = frame
315 307
316 self.sizer = wx.BoxSizer( wx.VERTICAL ) 308 self.sizer = wx.BoxSizer( wx.VERTICAL )
309
310 self.text = wx.TextCtrl(self, 3, handler.xml.get('name'))
311 self.sizer.Add(wx.StaticText(self, -1, "Title:"), 0, wx.EXPAND)
312 self.sizer.Add(self.text, 0, wx.EXPAND)
313
317 self.grid = minilib_grid( self, handler ) 314 self.grid = minilib_grid( self, handler )
318 315
319 bbox = wx.BoxSizer( wx.HORIZONTAL ) 316 bbox = wx.BoxSizer( wx.HORIZONTAL )
320 newMiniBtn = wx.Button( self, wx.ID_ANY, "New mini" ) 317 newMiniBtn = wx.Button( self, wx.ID_ANY, "New mini" )
321 delMiniBtn = wx.Button( self, wx.ID_ANY, "Del mini" ) 318 delMiniBtn = wx.Button( self, wx.ID_ANY, "Del mini" )
331 self.sizer.Add( bbox, 0) 328 self.sizer.Add( bbox, 0)
332 self.SetSizer(self.sizer) 329 self.SetSizer(self.sizer)
333 self.SetAutoLayout(True) 330 self.SetAutoLayout(True)
334 self.Fit() 331 self.Fit()
335 332
333 self.Bind(wx.EVT_TEXT, self.on_text, id=3)
336 self.Bind(wx.EVT_BUTTON, self.add_mini, newMiniBtn) 334 self.Bind(wx.EVT_BUTTON, self.add_mini, newMiniBtn)
337 self.Bind(wx.EVT_BUTTON, self.del_mini, delMiniBtn) 335 self.Bind(wx.EVT_BUTTON, self.del_mini, delMiniBtn)
338 self.Bind(wx.EVT_BUTTON, self.send_to_map, addMiniBtn) 336 self.Bind(wx.EVT_BUTTON, self.send_to_map, addMiniBtn)
339 self.Bind(wx.EVT_BUTTON, self.send_group_to_map, addBatchBtn) 337 self.Bind(wx.EVT_BUTTON, self.send_group_to_map, addBatchBtn)
340 338
352 350
353 def send_to_map( self, evt=None ): 351 def send_to_map( self, evt=None ):
354 """Event handler for the 'Add 1' button. Sends the 352 """Event handler for the 'Add 1' button. Sends the
355 miniature defined by the currently selected row to the map, once. 353 miniature defined by the currently selected row to the map, once.
356 """ 354 """
357 index = self.grid.GetGridCursorRow() 355 index = self.grid.GetSelectedRows()[0] if len(self.grid.GetSelectedRows()) > 0 else 0
358 self.handler.send_mini_to_map( self.handler.get_mini( index ) ) 356 self.handler.send_mini_to_map( self.handler.get_mini( index ) )
359 357
360 def send_group_to_map( self, evt=None ): 358 def send_group_to_map( self, evt=None ):
361 """Event handler for the 'Add batch' button. Querys the user 359 """Event handler for the 'Add batch' button. Querys the user
362 for a mini count and sends the miniature defined by the currently 360 for a mini count and sends the miniature defined by the currently
368 ( self.grid.getSelectedLabel() ), 'Batch mini add', '2' ) 366 ( self.grid.getSelectedLabel() ), 'Batch mini add', '2' )
369 if dlg.ShowModal() == wx.ID_OK: 367 if dlg.ShowModal() == wx.ID_OK:
370 try: value = eval( dlg.GetValue() ) 368 try: value = eval( dlg.GetValue() )
371 except: value = 0 369 except: value = 0
372 print 'getting selected index for batch send' 370 print 'getting selected index for batch send'
373 index = self.grid.GetGridCursorRow() 371 index = self.grid.GetSelectedRows()[0] if len(self.grid.GetSelectedRows()) > 0 else 0
374 print 'sending batch to map' 372 print 'sending batch to map'
375 self.handler.send_mini_to_map( self.handler.get_mini( index ), value ) 373 self.handler.send_mini_to_map( self.handler.get_mini( index ), value )
374
375 def on_text(self, evt):
376 txt = self.text.GetValue()
377 if txt != "":
378 self.handler.xml.set('name',txt)
379 self.handler.rename(txt)
376 380
377 class minilib_grid(wx.grid.Grid): 381 class minilib_grid(wx.grid.Grid):
378 """A wxGrid subclass designed for editing game tree miniature library 382 """A wxGrid subclass designed for editing game tree miniature library
379 nodes. 383 nodes.
380 """ 384 """
390 self.update_all() 394 self.update_all()
391 self.selectedRow = 0 395 self.selectedRow = 0
392 self.AutoSizeColumns() 396 self.AutoSizeColumns()
393 self.Bind(wx.grid.EVT_GRID_CELL_CHANGE, self.on_cell_change) 397 self.Bind(wx.grid.EVT_GRID_CELL_CHANGE, self.on_cell_change)
394 self.Bind(wx.grid.EVT_GRID_SELECT_CELL, self.select_cell) 398 self.Bind(wx.grid.EVT_GRID_SELECT_CELL, self.select_cell)
399 self.Bind(wx.grid.EVT_GRID_LABEL_LEFT_CLICK, self.select_cell)
395 400
396 def update_cols( self ): 401 def update_cols( self ):
397 for n in self.handler.xml.findall(TAG_MINIATURE): 402 for n in self.handler.xml.findall('miniature'):
398 for k in n.keys(): 403 for k in n.keys():
399 if k not in self.keys: self.keys.append( k ) 404 if k not in self.keys: self.keys.append( k )
400 405
401 def select_cell( self, evt ): 406 def select_cell( self, evt ):
402 """Event handler for grid cell selection changes. It stores the 407 """Event handler for grid cell selection changes. It stores the
411 416
412 def getList( self ): 417 def getList( self ):
413 """Returns the list of 'miniature' DOM elements associated with this 418 """Returns the list of 'miniature' DOM elements associated with this
414 miniature library. 419 miniature library.
415 """ 420 """
416 return self.handler.xml.findall( TAG_MINIATURE ) 421 return self.handler.xml.findall( 'miniature' )
417 422
418 def add_row( self, count = 1 ): 423 def add_row( self, count = 1 ):
419 """creates a new miniature node, and then adds it to the current 424 """creates a new miniature node, and then adds it to the current
420 miniature library, and to the grid. 425 miniature library, and to the grid.
421 """ 426 """
422 self.AppendRows( count ) 427 self.AppendRows( count )
423 node = self.handler.new_mini( { 428 node = self.handler.new_mini( {
424 ATTRIBUTE_NAME :' ', 429 'name' :' ',
425 ATTRIBUTE_URL :'http://'} )# minidom.Element( TAG_MINIATURE ) 430 'url' :'http://'} )# minidom.Element( 'miniature' )
426 self.update_all() 431 self.update_all()
427 #self.handler.xml.append( node ) 432 #self.handler.xml.append( node )
428 433
429 def del_row( self ): 434 def del_row( self ):
430 """deletes the miniature associated with the currently selected 435 """deletes the miniature associated with the currently selected
431 row. BUG BUG BUG this method should drop a child from the DOM but 436 row. BUG BUG BUG this method should drop a child from the DOM but
432 does not. 437 does not.
433 """ 438 """
434 if self.selectedRow > -1: 439 if self.selectedRow > -1:
435 pos = self.selectedRow 440 pos = self.selectedRow
436 list = self.handler.xml.findall(TAG_MINIATURE) 441 list = self.handler.xml.findall('miniature')
437 self.handler.xml.remove( list[pos] ) 442 self.handler.xml.remove( list[pos] )
438 self.DeleteRows( pos, 1 ) 443 self.DeleteRows( pos, 1 )
439 list = self.getList()
440 del list[ pos ]
441 444
442 def on_cell_change( self, evt ): 445 def on_cell_change( self, evt ):
443 """Event handler for cell selection changes. selected row is used 446 """Event handler for cell selection changes. selected row is used
444 to update data for that row. 447 to update data for that row.
445 """ 448 """
479 """Returns the URL for the selected row 482 """Returns the URL for the selected row
480 """ 483 """
481 return self.GetTable().GetValue( self.selectedRow, 1 ) 484 return self.GetTable().GetValue( self.selectedRow, 1 )
482 485
483 def getSelectedSerial( self ): 486 def getSelectedSerial( self ):
484 """Returns the ATTRIBUTE_UNIQUE value for the selected row 487 """Returns the 'unique' value for the selected row
485 """ 488 """
486 return self.GetTable().GetValue( self.selectedRow, 2 ) 489 return self.GetTable().GetValue( self.selectedRow, 2 )
487 490
488 def update_grid_row( self, row ): 491 def update_grid_row( self, row ):
489 """Updates the specified grid row with data from the DOM node 492 """Updates the specified grid row with data from the DOM node