156
|
1 # Copyright (C) 2000-2001 The OpenRPG Project
|
|
2 #
|
|
3 # openrpg-dev@lists.sourceforge.net
|
|
4 #
|
|
5 # This program is free software; you can redistribute it and/or modify
|
|
6 # it under the terms of the GNU General Public License as published by
|
|
7 # the Free Software Foundation; either version 2 of the License, or
|
|
8 # (at your option) any later version.
|
|
9 #
|
|
10 # This program is distributed in the hope that it will be useful,
|
|
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
13 # GNU General Public License for more details.
|
|
14 #
|
|
15 # You should have received a copy of the GNU General Public License
|
|
16 # along with this program; if not, write to the Free Software
|
|
17 # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
|
18 # --
|
|
19 #
|
|
20 # File: core.py
|
|
21 # Author: Chris Davis
|
|
22 # Maintainer:
|
|
23 # Version:
|
|
24 # $Id: core.py,v 1.49 2007/12/07 20:39:48 digitalxero Exp $
|
|
25 #
|
|
26 # Description: The file contains code for the core nodehanlers
|
|
27 #
|
|
28
|
|
29 __version__ = "$Id: core.py,v 1.49 2007/12/07 20:39:48 digitalxero Exp $"
|
|
30
|
|
31 from nodehandler_version import NODEHANDLER_VERSION
|
|
32
|
|
33 try:
|
|
34 from orpg.orpg_windows import *
|
|
35 from orpg.dirpath import dir_struct
|
|
36 from orpg.orpgCore import component
|
|
37 import webbrowser, os
|
|
38 from orpg.mapper import map
|
|
39 from wx import DisplaySize
|
|
40 except:
|
|
41 import wx
|
|
42
|
135
|
43 from xml.etree.ElementTree import ElementTree, Element, tostring, XML
|
156
|
44 from orpg.tools.orpg_log import debug
|
|
45
|
|
46 #html defaults
|
|
47 TH_BG = "#E9E9E9"
|
|
48 ##########################
|
|
49 ## base node handler
|
|
50 ##########################
|
|
51 class node_handler:
|
|
52 """ Base nodehandler with virtual functions and standard implmentations """
|
|
53 def __init__(self,xml,tree_node):
|
|
54 self.xml = xml
|
|
55 self.mytree_node = tree_node
|
160
|
56 self.tree = component.get('tree')
|
183
|
57 #self.tree = component.get('tree_fs')
|
160
|
58 self.frame = component.get('frame')
|
|
59 self.chat = component.get('chat')
|
156
|
60 self.drag = True
|
|
61 self.myeditor = None # designing
|
|
62 self.myviewer = None # prett print
|
|
63 self.mywindow = None # using
|
|
64 # call version hook
|
|
65 self.on_version(self.xml.get("version"))
|
|
66 # set to current version
|
|
67 self.xml.set("version",NODEHANDLER_VERSION)
|
|
68 # null events
|
|
69 self.frame_size = None
|
|
70 self.frame_pos = None
|
|
71 try:
|
|
72 frame = self.xml.get("frame")
|
|
73 if len(frame):
|
|
74 (sx,sy,px,py) = [int(value) for value in frame.split(',')]
|
|
75 self.frame_size = (sx, sy)
|
|
76 (maxx, maxy) = DisplaySize()
|
|
77 if px < maxx-80 and py < maxy-50: self.frame_pos = (px, py) #if it's off screen ignore the saved pos
|
|
78 except: pass
|
|
79
|
|
80 def on_version(self,old_version):
|
|
81 ## added version control code here or implement a new on_version in your derived class.
|
|
82 ## always call the base class on_version !
|
|
83 pass
|
|
84
|
|
85 def on_rclick(self,evt):
|
|
86 self.tree.do_std_menu(evt,self)
|
|
87
|
|
88 def on_ldclick(self,evt):
|
|
89 return 0
|
|
90
|
|
91 def usefulness(self,text):
|
|
92 if text=="useful": self.xml.set('status',"useful")
|
|
93 elif text=="useless": self.xml.set('status',"useless")
|
|
94 elif text=="indifferent": self.xml.set('status',"indifferent")
|
|
95
|
|
96 def on_design(self,evt):
|
|
97 try:
|
|
98 self.myeditor.Show()
|
|
99 self.myeditor.Raise()
|
|
100 except:
|
|
101 del self.myeditor
|
|
102 if self.create_designframe():
|
|
103 self.myeditor.Show()
|
|
104 self.myeditor.Raise()
|
|
105 else: return
|
|
106 wx.CallAfter(self.myeditor.Layout)
|
|
107
|
|
108 def create_designframe(self):
|
|
109 title = self.xml.get('name') + " Editor"
|
|
110 self.myeditor = wx.Frame(None, -1, title)
|
|
111 self.myeditor.Freeze()
|
|
112 if wx.Platform == '__WXMSW__':
|
|
113 icon = wx.Icon(orpg.dirpath.dir_struct["icon"] + 'grid.ico', wx.BITMAP_TYPE_ICO)
|
|
114 self.myeditor.SetIcon(icon)
|
|
115 del icon
|
|
116
|
|
117 self.myeditor.panel = self.get_design_panel(self.myeditor)
|
|
118 if self.myeditor.panel == None:
|
|
119 self.myeditor.Destroy()
|
|
120 return False
|
|
121 sizer = wx.BoxSizer(wx.VERTICAL)
|
|
122 sizer.Add(self.myeditor.panel, 1, wx.EXPAND)
|
|
123
|
|
124 self.myeditor.SetSizer(sizer)
|
|
125 self.myeditor.SetAutoLayout(True)
|
|
126
|
|
127 (x, y) = self.myeditor.GetSize()
|
|
128 if x < 400: x = 400
|
|
129 if y < 400: y = 400
|
|
130
|
|
131 self.myeditor.SetSize((x, y))
|
|
132 self.myeditor.Layout()
|
|
133 self.myeditor.Thaw()
|
|
134 return True
|
|
135
|
183
|
136 def on_use(self, evt):
|
156
|
137 try:
|
|
138 self.mywindow.Show()
|
|
139 self.mywindow.Raise()
|
|
140 except:
|
|
141 del self.mywindow
|
|
142 if self.create_useframe():
|
|
143 self.mywindow.SetSize(self.frame_size)
|
|
144 if self.frame_pos: self.mywindow.SetPosition(self.frame_pos)
|
|
145 self.mywindow.Show()
|
|
146 self.mywindow.Raise()
|
|
147 else: return
|
|
148 wx.CallAfter(self.mywindow.Layout)
|
|
149
|
|
150 def create_useframe(self):
|
|
151 caption = self.xml.get('name', '')
|
|
152 self.mywindow = wx.Frame(None, -1, caption)
|
|
153 self.mywindow.Freeze()
|
|
154 if wx.Platform == '__WXMSW__':
|
|
155 icon = wx.Icon(orpg.dirpath.dir_struct["icon"] + 'note.ico', wx.BITMAP_TYPE_ICO)
|
|
156 self.mywindow.SetIcon(icon)
|
|
157 del icon
|
|
158 self.mywindow.panel = self.get_use_panel(self.mywindow)
|
|
159 if self.mywindow.panel == None:
|
|
160 self.mywindow.Destroy()
|
|
161 return False
|
|
162 sizer = wx.BoxSizer(wx.VERTICAL)
|
|
163 sizer.Add(self.mywindow.panel, 2, wx.EXPAND)
|
|
164
|
|
165 self.mywindow.SetSizer(sizer)
|
|
166 self.mywindow.SetAutoLayout(True)
|
|
167
|
|
168 if self.frame_size is None:
|
|
169 self.frame_size = self.mywindow.GetSize()
|
|
170 if self.frame_size.x < 400: self.frame_size.x = 400
|
|
171 if self.frame_size.y < 400: self.frame_size.y = 400
|
|
172
|
|
173 self.mywindow.Layout()
|
|
174 self.mywindow.Thaw()
|
|
175 self.mywindow.Bind(wx.EVT_CLOSE, self.close_useframe)
|
|
176 return True
|
|
177
|
|
178 def close_useframe(self, evt):
|
|
179 self.frame_size = self.mywindow.GetSize()
|
|
180 self.frame_pos = self.mywindow.GetPosition()
|
|
181 frame_values = str(self.frame_size.x)+','+str(self.frame_size.y)+','+str(self.frame_pos.x)+','+str(self.frame_pos.y)
|
|
182 self.xml.set("frame", frame_values)
|
|
183 self.mywindow.Destroy()
|
|
184
|
|
185
|
|
186 def on_html_view(self,evt):
|
|
187 try: self.myviewer.Raise()
|
|
188 except:
|
|
189 caption = self.xml.get('name')
|
|
190 self.myviewer = wx.Frame(None, -1, caption)
|
|
191 if wx.Platform == '__WXMSW__':
|
|
192 icon = wx.Icon(orpg.dirpath.dir_struct["icon"] + 'grid.ico', wx.BITMAP_TYPE_ICO)
|
|
193 self.myviewer.SetIcon(icon)
|
|
194 del icon
|
|
195 self.myviewer.panel = self.get_html_panel(self.myviewer)
|
|
196 self.myviewer.Show()
|
|
197
|
|
198 def map_aware(self):
|
|
199 return 0
|
|
200
|
|
201 def can_clone(self):
|
|
202 return 1;
|
|
203
|
|
204 def on_del(self,evt):
|
|
205 print "on del"
|
|
206
|
|
207 def on_new_data(self,xml):
|
|
208 pass
|
|
209
|
|
210 def get_scaled_bitmap(self,x,y):
|
|
211 return None
|
|
212
|
|
213 def on_send_to_map(self,evt):
|
|
214 pass
|
|
215
|
|
216 def on_send_to_chat(self,evt):
|
|
217 self.chat.ParsePost(self.tohtml(),True,True)
|
|
218
|
|
219 def on_drop(self, evt):
|
135
|
220 drag_obj = self.tree.drag_obj
|
156
|
221 if drag_obj == self or self.tree.is_parent_node(self.mytree_node, drag_obj.mytree_node):
|
|
222 return
|
|
223 drop_xml = self.tree.drag_obj.delete()
|
|
224 parent_node = self.tree.GetItemParent(self.mytree_node)
|
|
225 prev_sib = self.tree.GetPrevSibling(self.mytree_node)
|
|
226 if parent_node == self.tree.root: parent_xml = self.tree.GetPyData(parent_node)
|
|
227 else: parent_xml = self.tree.GetPyData(parent_node).xml
|
|
228 for i in range(len(parent_xml)):
|
|
229 if parent_xml[i] is self.xml:
|
|
230 parent_xml.insert(i, drop_xml)
|
|
231 break
|
|
232 if not prev_sib.IsOk():
|
|
233 prev_sib = parent_node
|
135
|
234 self.tree.load_xml(drop_xml, parent_node, prev_sib)
|
|
235
|
|
236 def get_tree(self):
|
|
237 family = []
|
|
238 test = treenode
|
|
239 while test != self.tree.root:
|
|
240 test = self.tree.GetItemParent(test)
|
|
241 parent = self.tree.GetItemText(test)
|
|
242 family.append(parent)
|
156
|
243 return family
|
|
244
|
|
245 def toxml(self,pretty=0):
|
|
246 return tostring(self.xml) #toxml(self.master_dom,pretty)
|
|
247
|
|
248 def tohtml(self):
|
|
249 return self.xml.get("name")
|
|
250
|
|
251 def delete(self):
|
|
252 """ removes the tree_node and xml_node, and returns the removed xml_node """
|
|
253 parent_node = self.tree.GetItemParent(self.mytree_node)
|
|
254 if parent_node == self.tree.root: parent_xml = self.tree.GetPyData(parent_node)
|
|
255 else: parent_xml = self.tree.GetPyData(parent_node).xml
|
|
256 parent_xml.remove(self.xml)
|
|
257 self.tree.Delete(self.mytree_node)
|
|
258 return self.xml
|
|
259
|
|
260 def rename(self,name):
|
|
261 if len(name):
|
|
262 self.tree.SetItemText(self.mytree_node,name)
|
|
263 self.xml.set('name', name)
|
|
264
|
|
265 def change_icon(self,icon):
|
|
266 self.xml.set("icon",icon)
|
|
267 self.tree.SetItemImage(self.mytree_node, self.tree.icons[icon])
|
|
268 self.tree.SetItemImage(self.mytree_node, self.tree.icons[icon], wx.TreeItemIcon_Selected)
|
|
269 self.tree.Refresh()
|
|
270
|
|
271 def on_save(self,evt):
|
|
272 f = wx.FileDialog(self.tree,"Select a file", orpg.dirpath.dir_struct["user"],"","XML files (*.xml)|*.xml",wx.SAVE)
|
|
273 if f.ShowModal() == wx.ID_OK: ElementTree(self.xml).write(f.GetPath())
|
|
274 f.Destroy()
|
|
275
|
|
276 def get_design_panel(self,parent):
|
|
277 return None
|
|
278
|
|
279 def get_use_panel(self,parent):
|
|
280 return None
|
|
281
|
|
282 def get_html_panel(self,parent):
|
|
283 html_str = "<html><body bgcolor=\"#FFFFFF\" >"+self.tohtml()+"</body></html>"
|
|
284 wnd = wx.html.HtmlWindow(parent,-1)
|
|
285 html_str = self.chat.ParseDice(html_str)
|
|
286 wnd.SetPage(html_str)
|
|
287 return wnd
|
|
288
|
|
289 def get_size_constraint(self):
|
|
290 return 0
|
|
291
|
|
292 def about(self):
|
|
293 """html_str = "<b>"+ self.xml.get('class')
|
|
294 html_str += " Applet</b><br />by Chris Davis<br />chris@rpgarchive.com"
|
152
|
295 return html_str"""
|
|
296 text = self.xml.get('class') + ' Applet\n'
|
|
297 text += 'by Chris Davis chris@rpgarchive.com'
|
|
298 return text
|
135
|
299
|
|
300 def get_value(self):
|
156
|
301 return None
|
|
302
|
|
303 P_TITLE = 10
|
152
|
304 P_BODY = 20
|
156
|
305
|
|
306 class text_edit_panel(wx.Panel):
|
|
307 def __init__(self, parent, handler):
|
|
308 wx.Panel.__init__(self, parent, -1)
|
|
309 self.handler = handler
|
|
310 sizer = wx.BoxSizer(wx.VERTICAL)
|
|
311 self.text = { P_TITLE : wx.TextCtrl(self, P_TITLE, handler.xml.get('name')),
|
|
312 P_BODY : html_text_edit(self,P_BODY,handler.text,self.on_text)
|
|
313 }
|
|
314 sizer.Add(wx.StaticText(self, -1, "Title:"), 0, wx.EXPAND)
|
|
315 sizer.Add(self.text[P_TITLE], 0, wx.EXPAND)
|
|
316 sizer.Add(wx.StaticText(self, -1, "Text Body:"), 0, wx.EXPAND)
|
|
317 sizer.Add(self.text[P_BODY], 1, wx.EXPAND)
|
|
318 self.sizer = sizer
|
|
319 self.outline = wx.StaticBox(self,-1,"Text Block")
|
|
320 self.Bind(wx.EVT_TEXT, self.on_text, id=P_TITLE)
|
|
321
|
|
322 def on_text(self,evt):
|
|
323 id = evt.GetId()
|
|
324 if id == P_TITLE:
|
|
325 txt = self.text[id].GetValue()
|
|
326 # The following block strips out 8-bit characters
|
|
327 u_txt = ""
|
|
328 bad_txt_found = 0
|
|
329 for c in txt:
|
|
330 if ord(c) < 128: u_txt += c
|
|
331 else: bad_txt_found = 1
|
|
332 if bad_txt_found:
|
|
333 wx.MessageBox("Some non 7-bit ASCII characters found and stripped","Warning!")
|
|
334 txt = u_txt
|
|
335 if txt != "":
|
|
336 self.handler.xml.set('name',txt)
|
|
337 self.handler.rename(txt)
|
|
338 elif id == P_BODY:
|
|
339 txt = self.text[id].get_text()
|
|
340 u_txt = ""
|
|
341 bad_txt_found = 0
|
|
342 for c in txt:
|
|
343 if ord(c) < 128: u_txt += c
|
|
344 else: bad_txt_found = 1
|
|
345 if bad_txt_found:
|
|
346 wx.MessageBox("Some non 7-bit ASCII characters found and stripped","Warning!")
|
|
347 txt = u_txt
|
|
348 self.handler.text._set_nodeValue(txt)
|
|
349
|
|
350
|
|
351
|
|
352 ##########################
|
|
353 ## node loader
|
|
354 ##########################
|
|
355 class node_loader(node_handler):
|
|
356 """ clones childe node and insert it at top of tree
|
|
357 <nodehandler name='?' module='core' class='node_loader' />
|
|
358 """
|
|
359 def __init__(self,xml,tree_node):
|
|
360 node_handler.__init__(self,xml,tree_node)
|
|
361
|
|
362 def on_rclick(self,evt):
|
|
363 pass
|
|
364
|
|
365 def on_ldclick(self,evt):
|
|
366 title = self.xml.get('name')
|
|
367 new_xml = XML(tostring(self.xml[0]))
|
|
368 self.tree.root_xml.insert(0, new_xml)
|
|
369 tree_node = self.tree.load_xml(new_xml,self.tree.root,self.tree.root)
|
|
370 return 1
|
|
371
|
|
372 ##########################
|
|
373 ## file loader
|
|
374 ##########################
|
|
375
|
|
376 class file_loader(node_handler):
|
|
377 """ loads file and insert into game tree
|
|
378 <nodehandler name='?' module='core' class='file_loader' >
|
|
379 <file name="file_name.xml" />
|
|
380 </nodehandler>
|
|
381 """
|
|
382 def __init__(self,xml,tree_node):
|
|
383 node_handler.__init__(self,xml,tree_node)
|
|
384 self.file_node = self.xml[0]
|
160
|
385 self.frame = component.get('frame')
|
156
|
386
|
|
387 def on_ldclick(self,evt):
|
|
388 file_name = self.file_node.get("name")
|
|
389 self.tree.insert_xml(open(orpg.dirpath.dir_struct["nodes"] + file_name,"r").read())
|
|
390 return 1
|
|
391
|
|
392 def on_design(self,evt):
|
|
393 tlist = ['Title','File Name']
|
|
394 vlist = [self.xml.get("name"),
|
|
395 self.file_node.get("name")]
|
|
396 dlg = orpgMultiTextEntry(self.tree.GetParent(),tlist,vlist,"File Loader Edit")
|
|
397 if dlg.ShowModal() == wx.ID_OK:
|
|
398 vlist = dlg.get_values()
|
|
399 self.file_node.set('name', vlist[1])
|
|
400 self.xml.set('name', vlist[0])
|
|
401 self.tree.SetItemText(self.mytree_node,vlist[0])
|
|
402 dlg.Destroy()
|
|
403
|
|
404 ##########################
|
|
405 ## URL loader
|
|
406 ##########################
|
|
407
|
|
408 class url_loader(node_handler):
|
|
409 """ loads file from url and insert into game tree
|
|
410 <nodehandler name='?' module='core' class='url_loader' >
|
|
411 <file name="http://file_name.xml" />
|
|
412 </nodehandler>
|
|
413 """
|
|
414 def __init__(self,xml,tree_node):
|
|
415 node_handler.__init__(self,xml,tree_node)
|
|
416 self.file_node = self.xml[0]
|
160
|
417 self.frame = component.get('frame')
|
156
|
418
|
|
419 def on_ldclick(self,evt):
|
|
420 file_name = self.file_node.get("url")
|
|
421 file = urllib.urlopen(file_name)
|
|
422 self.tree.insert_xml(file.read())
|
|
423 return 1
|
|
424
|
|
425 def on_design(self,evt):
|
|
426 tlist = ['Title','URL']
|
|
427 vlist = [self.xml.get("name"),
|
|
428 self.file_node.get("url")]
|
|
429 dlg = orpgMultiTextEntry(self.tree.GetParent(),tlist,vlist,"File Loader Edit")
|
|
430 if dlg.ShowModal() == wx.ID_OK:
|
|
431 vlist = dlg.get_values()
|
|
432 self.file_node.set('url', vlist[1])
|
|
433 self.xml.set('name', vlist[0])
|
|
434 self.tree.SetItemText(self.mytree_node,vlist[0])
|
|
435 dlg.Destroy()
|
|
436
|
|
437
|
|
438 ##########################
|
|
439 ## minature map loader
|
|
440 ##########################
|
|
441 class min_map(node_handler):
|
|
442 """ clones childe node and insert it at top of tree
|
|
443 <nodehandler name='?' module='core' class='min_map' />
|
|
444 """
|
|
445 def __init__(self,xml,tree_node):
|
|
446 node_handler.__init__(self,xml,tree_node)
|
160
|
447 self.map = component.get('map')
|
|
448 self.mapdata = self.xml.find('map')
|
156
|
449
|
|
450 def on_ldclick(self,evt):
|
160
|
451 if (component.get('session').my_role() != component.get('session').ROLE_GM):
|
|
452 component.get('chat').InfoPost("You must be a GM to use this feature")
|
|
453 return
|
156
|
454 self.map.new_data(tostring(self.mapdata))
|
160
|
455 component.get('session').send(tostring(self.mapdata))
|
|
456 self.map.canvas.send_map_data()
|
156
|
457 return 1
|