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
|
|
56 self.tree = open_rpg.get_component('tree')
|
|
57 self.frame = open_rpg.get_component('frame')
|
|
58 self.chat = open_rpg.get_component('chat')
|
|
59 self.drag = True
|
|
60 self.myeditor = None # designing
|
|
61 self.myviewer = None # prett print
|
|
62 self.mywindow = None # using
|
|
63 # call version hook
|
|
64 self.on_version(self.xml.get("version"))
|
|
65 # set to current version
|
|
66 self.xml.set("version",NODEHANDLER_VERSION)
|
|
67 # null events
|
|
68 self.frame_size = None
|
|
69 self.frame_pos = None
|
|
70 try:
|
|
71 frame = self.xml.get("frame")
|
|
72 if len(frame):
|
|
73 (sx,sy,px,py) = [int(value) for value in frame.split(',')]
|
|
74 self.frame_size = (sx, sy)
|
|
75 (maxx, maxy) = DisplaySize()
|
|
76 if px < maxx-80 and py < maxy-50: self.frame_pos = (px, py) #if it's off screen ignore the saved pos
|
|
77 except: pass
|
|
78
|
|
79 def on_version(self,old_version):
|
|
80 ## added version control code here or implement a new on_version in your derived class.
|
|
81 ## always call the base class on_version !
|
|
82 pass
|
|
83
|
|
84 def on_rclick(self,evt):
|
|
85 self.tree.do_std_menu(evt,self)
|
|
86
|
|
87 def on_ldclick(self,evt):
|
|
88 return 0
|
|
89
|
|
90 def usefulness(self,text):
|
|
91 if text=="useful": self.xml.set('status',"useful")
|
|
92 elif text=="useless": self.xml.set('status',"useless")
|
|
93 elif text=="indifferent": self.xml.set('status',"indifferent")
|
|
94
|
|
95 def on_design(self,evt):
|
|
96 try:
|
|
97 self.myeditor.Show()
|
|
98 self.myeditor.Raise()
|
|
99 except:
|
|
100 del self.myeditor
|
|
101 if self.create_designframe():
|
|
102 self.myeditor.Show()
|
|
103 self.myeditor.Raise()
|
|
104 else: return
|
|
105 wx.CallAfter(self.myeditor.Layout)
|
|
106
|
|
107 def create_designframe(self):
|
|
108 title = self.xml.get('name') + " Editor"
|
|
109 self.myeditor = wx.Frame(None, -1, title)
|
|
110 self.myeditor.Freeze()
|
|
111 if wx.Platform == '__WXMSW__':
|
|
112 icon = wx.Icon(orpg.dirpath.dir_struct["icon"] + 'grid.ico', wx.BITMAP_TYPE_ICO)
|
|
113 self.myeditor.SetIcon(icon)
|
|
114 del icon
|
|
115
|
|
116 self.myeditor.panel = self.get_design_panel(self.myeditor)
|
|
117 if self.myeditor.panel == None:
|
|
118 self.myeditor.Destroy()
|
|
119 return False
|
|
120 sizer = wx.BoxSizer(wx.VERTICAL)
|
|
121 sizer.Add(self.myeditor.panel, 1, wx.EXPAND)
|
|
122
|
|
123 self.myeditor.SetSizer(sizer)
|
|
124 self.myeditor.SetAutoLayout(True)
|
|
125
|
|
126 (x, y) = self.myeditor.GetSize()
|
|
127 if x < 400: x = 400
|
|
128 if y < 400: y = 400
|
|
129
|
|
130 self.myeditor.SetSize((x, y))
|
|
131 self.myeditor.Layout()
|
|
132 self.myeditor.Thaw()
|
|
133 return True
|
|
134
|
|
135 def on_use(self,evt):
|
|
136 try:
|
|
137 self.mywindow.Show()
|
|
138 self.mywindow.Raise()
|
|
139 except:
|
|
140 del self.mywindow
|
|
141 if self.create_useframe():
|
|
142 self.mywindow.SetSize(self.frame_size)
|
|
143 if self.frame_pos: self.mywindow.SetPosition(self.frame_pos)
|
|
144 self.mywindow.Show()
|
|
145 self.mywindow.Raise()
|
|
146 else: return
|
|
147 wx.CallAfter(self.mywindow.Layout)
|
|
148
|
|
149 def create_useframe(self):
|
|
150 caption = self.xml.get('name', '')
|
|
151 self.mywindow = wx.Frame(None, -1, caption)
|
|
152 self.mywindow.Freeze()
|
|
153 if wx.Platform == '__WXMSW__':
|
|
154 icon = wx.Icon(orpg.dirpath.dir_struct["icon"] + 'note.ico', wx.BITMAP_TYPE_ICO)
|
|
155 self.mywindow.SetIcon(icon)
|
|
156 del icon
|
|
157 self.mywindow.panel = self.get_use_panel(self.mywindow)
|
|
158 if self.mywindow.panel == None:
|
|
159 self.mywindow.Destroy()
|
|
160 return False
|
|
161 sizer = wx.BoxSizer(wx.VERTICAL)
|
|
162 sizer.Add(self.mywindow.panel, 2, wx.EXPAND)
|
|
163
|
|
164 self.mywindow.SetSizer(sizer)
|
|
165 self.mywindow.SetAutoLayout(True)
|
|
166
|
|
167 if self.frame_size is None:
|
|
168 self.frame_size = self.mywindow.GetSize()
|
|
169 if self.frame_size.x < 400: self.frame_size.x = 400
|
|
170 if self.frame_size.y < 400: self.frame_size.y = 400
|
|
171
|
|
172 self.mywindow.Layout()
|
|
173 self.mywindow.Thaw()
|
|
174 self.mywindow.Bind(wx.EVT_CLOSE, self.close_useframe)
|
|
175 return True
|
|
176
|
|
177 def close_useframe(self, evt):
|
|
178 self.frame_size = self.mywindow.GetSize()
|
|
179 self.frame_pos = self.mywindow.GetPosition()
|
|
180 frame_values = str(self.frame_size.x)+','+str(self.frame_size.y)+','+str(self.frame_pos.x)+','+str(self.frame_pos.y)
|
|
181 self.xml.set("frame", frame_values)
|
|
182 self.mywindow.Destroy()
|
|
183
|
|
184
|
|
185 def on_html_view(self,evt):
|
|
186 try: self.myviewer.Raise()
|
|
187 except:
|
|
188 caption = self.xml.get('name')
|
|
189 self.myviewer = wx.Frame(None, -1, caption)
|
|
190 if wx.Platform == '__WXMSW__':
|
|
191 icon = wx.Icon(orpg.dirpath.dir_struct["icon"] + 'grid.ico', wx.BITMAP_TYPE_ICO)
|
|
192 self.myviewer.SetIcon(icon)
|
|
193 del icon
|
|
194 self.myviewer.panel = self.get_html_panel(self.myviewer)
|
|
195 self.myviewer.Show()
|
|
196
|
|
197 def map_aware(self):
|
|
198 return 0
|
|
199
|
|
200 def can_clone(self):
|
|
201 return 1;
|
|
202
|
|
203 def on_del(self,evt):
|
|
204 print "on del"
|
|
205
|
|
206 def on_new_data(self,xml):
|
|
207 pass
|
|
208
|
|
209 def get_scaled_bitmap(self,x,y):
|
|
210 return None
|
|
211
|
|
212 def on_send_to_map(self,evt):
|
|
213 pass
|
|
214
|
|
215 def on_send_to_chat(self,evt):
|
|
216 self.chat.ParsePost(self.tohtml(),True,True)
|
|
217
|
|
218 def on_drop(self, evt):
|
135
|
219 drag_obj = self.tree.drag_obj
|
156
|
220 if drag_obj == self or self.tree.is_parent_node(self.mytree_node, drag_obj.mytree_node):
|
|
221 return
|
|
222 drop_xml = self.tree.drag_obj.delete()
|
|
223 parent_node = self.tree.GetItemParent(self.mytree_node)
|
|
224 prev_sib = self.tree.GetPrevSibling(self.mytree_node)
|
|
225 if parent_node == self.tree.root: parent_xml = self.tree.GetPyData(parent_node)
|
|
226 else: parent_xml = self.tree.GetPyData(parent_node).xml
|
|
227 for i in range(len(parent_xml)):
|
|
228 if parent_xml[i] is self.xml:
|
|
229 parent_xml.insert(i, drop_xml)
|
|
230 break
|
|
231 if not prev_sib.IsOk():
|
|
232 prev_sib = parent_node
|
135
|
233 self.tree.load_xml(drop_xml, parent_node, prev_sib)
|
|
234
|
|
235 def get_tree(self):
|
|
236 family = []
|
|
237 test = treenode
|
|
238 while test != self.tree.root:
|
|
239 test = self.tree.GetItemParent(test)
|
|
240 parent = self.tree.GetItemText(test)
|
|
241 family.append(parent)
|
156
|
242 return family
|
|
243
|
|
244 def toxml(self,pretty=0):
|
|
245 return tostring(self.xml) #toxml(self.master_dom,pretty)
|
|
246
|
|
247 def tohtml(self):
|
|
248 return self.xml.get("name")
|
|
249
|
|
250 def delete(self):
|
|
251 """ removes the tree_node and xml_node, and returns the removed xml_node """
|
|
252 parent_node = self.tree.GetItemParent(self.mytree_node)
|
|
253 if parent_node == self.tree.root: parent_xml = self.tree.GetPyData(parent_node)
|
|
254 else: parent_xml = self.tree.GetPyData(parent_node).xml
|
|
255 parent_xml.remove(self.xml)
|
|
256 self.tree.Delete(self.mytree_node)
|
|
257 return self.xml
|
|
258
|
|
259 def rename(self,name):
|
|
260 if len(name):
|
|
261 self.tree.SetItemText(self.mytree_node,name)
|
|
262 self.xml.set('name', name)
|
|
263
|
|
264 def change_icon(self,icon):
|
|
265 self.xml.set("icon",icon)
|
|
266 self.tree.SetItemImage(self.mytree_node, self.tree.icons[icon])
|
|
267 self.tree.SetItemImage(self.mytree_node, self.tree.icons[icon], wx.TreeItemIcon_Selected)
|
|
268 self.tree.Refresh()
|
|
269
|
|
270 def on_save(self,evt):
|
|
271 f = wx.FileDialog(self.tree,"Select a file", orpg.dirpath.dir_struct["user"],"","XML files (*.xml)|*.xml",wx.SAVE)
|
|
272 if f.ShowModal() == wx.ID_OK: ElementTree(self.xml).write(f.GetPath())
|
|
273 f.Destroy()
|
|
274
|
|
275 def get_design_panel(self,parent):
|
|
276 return None
|
|
277
|
|
278 def get_use_panel(self,parent):
|
|
279 return None
|
|
280
|
|
281 def get_html_panel(self,parent):
|
|
282 html_str = "<html><body bgcolor=\"#FFFFFF\" >"+self.tohtml()+"</body></html>"
|
|
283 wnd = wx.html.HtmlWindow(parent,-1)
|
|
284 html_str = self.chat.ParseDice(html_str)
|
|
285 wnd.SetPage(html_str)
|
|
286 return wnd
|
|
287
|
|
288 def get_size_constraint(self):
|
|
289 return 0
|
|
290
|
|
291 def about(self):
|
|
292 """html_str = "<b>"+ self.xml.get('class')
|
|
293 html_str += " Applet</b><br />by Chris Davis<br />chris@rpgarchive.com"
|
152
|
294 return html_str"""
|
|
295 text = self.xml.get('class') + ' Applet\n'
|
|
296 text += 'by Chris Davis chris@rpgarchive.com'
|
|
297 return text
|
135
|
298
|
|
299 def get_value(self):
|
156
|
300 return None
|
|
301
|
|
302 P_TITLE = 10
|
152
|
303 P_BODY = 20
|
156
|
304
|
|
305 class text_edit_panel(wx.Panel):
|
|
306 def __init__(self, parent, handler):
|
|
307 wx.Panel.__init__(self, parent, -1)
|
|
308 self.handler = handler
|
|
309 sizer = wx.BoxSizer(wx.VERTICAL)
|
|
310 self.text = { P_TITLE : wx.TextCtrl(self, P_TITLE, handler.xml.get('name')),
|
|
311 P_BODY : html_text_edit(self,P_BODY,handler.text,self.on_text)
|
|
312 }
|
|
313 sizer.Add(wx.StaticText(self, -1, "Title:"), 0, wx.EXPAND)
|
|
314 sizer.Add(self.text[P_TITLE], 0, wx.EXPAND)
|
|
315 sizer.Add(wx.StaticText(self, -1, "Text Body:"), 0, wx.EXPAND)
|
|
316 sizer.Add(self.text[P_BODY], 1, wx.EXPAND)
|
|
317 self.sizer = sizer
|
|
318 self.outline = wx.StaticBox(self,-1,"Text Block")
|
|
319 self.Bind(wx.EVT_TEXT, self.on_text, id=P_TITLE)
|
|
320
|
|
321 def on_text(self,evt):
|
|
322 id = evt.GetId()
|
|
323 if id == P_TITLE:
|
|
324 txt = self.text[id].GetValue()
|
|
325 # The following block strips out 8-bit characters
|
|
326 u_txt = ""
|
|
327 bad_txt_found = 0
|
|
328 for c in txt:
|
|
329 if ord(c) < 128: u_txt += c
|
|
330 else: bad_txt_found = 1
|
|
331 if bad_txt_found:
|
|
332 wx.MessageBox("Some non 7-bit ASCII characters found and stripped","Warning!")
|
|
333 txt = u_txt
|
|
334 if txt != "":
|
|
335 self.handler.xml.set('name',txt)
|
|
336 self.handler.rename(txt)
|
|
337 elif id == P_BODY:
|
|
338 txt = self.text[id].get_text()
|
|
339 u_txt = ""
|
|
340 bad_txt_found = 0
|
|
341 for c in txt:
|
|
342 if ord(c) < 128: u_txt += c
|
|
343 else: bad_txt_found = 1
|
|
344 if bad_txt_found:
|
|
345 wx.MessageBox("Some non 7-bit ASCII characters found and stripped","Warning!")
|
|
346 txt = u_txt
|
|
347 self.handler.text._set_nodeValue(txt)
|
|
348
|
|
349
|
|
350
|
|
351 ##########################
|
|
352 ## node loader
|
|
353 ##########################
|
|
354 class node_loader(node_handler):
|
|
355 """ clones childe node and insert it at top of tree
|
|
356 <nodehandler name='?' module='core' class='node_loader' />
|
|
357 """
|
|
358 def __init__(self,xml,tree_node):
|
|
359 node_handler.__init__(self,xml,tree_node)
|
|
360
|
|
361 def on_rclick(self,evt):
|
|
362 pass
|
|
363
|
|
364 def on_ldclick(self,evt):
|
|
365 title = self.xml.get('name')
|
|
366 new_xml = XML(tostring(self.xml[0]))
|
|
367 self.tree.root_xml.insert(0, new_xml)
|
|
368 tree_node = self.tree.load_xml(new_xml,self.tree.root,self.tree.root)
|
|
369 return 1
|
|
370
|
|
371 ##########################
|
|
372 ## file loader
|
|
373 ##########################
|
|
374
|
|
375 class file_loader(node_handler):
|
|
376 """ loads file and insert into game tree
|
|
377 <nodehandler name='?' module='core' class='file_loader' >
|
|
378 <file name="file_name.xml" />
|
|
379 </nodehandler>
|
|
380 """
|
|
381 def __init__(self,xml,tree_node):
|
|
382 node_handler.__init__(self,xml,tree_node)
|
|
383 self.file_node = self.xml[0]
|
|
384 self.frame = open_rpg.get_component('frame')
|
|
385
|
|
386 def on_ldclick(self,evt):
|
|
387 file_name = self.file_node.get("name")
|
|
388 self.tree.insert_xml(open(orpg.dirpath.dir_struct["nodes"] + file_name,"r").read())
|
|
389 return 1
|
|
390
|
|
391 def on_design(self,evt):
|
|
392 tlist = ['Title','File Name']
|
|
393 vlist = [self.xml.get("name"),
|
|
394 self.file_node.get("name")]
|
|
395 dlg = orpgMultiTextEntry(self.tree.GetParent(),tlist,vlist,"File Loader Edit")
|
|
396 if dlg.ShowModal() == wx.ID_OK:
|
|
397 vlist = dlg.get_values()
|
|
398 self.file_node.set('name', vlist[1])
|
|
399 self.xml.set('name', vlist[0])
|
|
400 self.tree.SetItemText(self.mytree_node,vlist[0])
|
|
401 dlg.Destroy()
|
|
402
|
|
403 ##########################
|
|
404 ## URL loader
|
|
405 ##########################
|
|
406
|
|
407 class url_loader(node_handler):
|
|
408 """ loads file from url and insert into game tree
|
|
409 <nodehandler name='?' module='core' class='url_loader' >
|
|
410 <file name="http://file_name.xml" />
|
|
411 </nodehandler>
|
|
412 """
|
|
413 def __init__(self,xml,tree_node):
|
|
414 node_handler.__init__(self,xml,tree_node)
|
|
415 self.file_node = self.xml[0]
|
|
416 self.frame = open_rpg.get_component('frame')
|
|
417
|
|
418 def on_ldclick(self,evt):
|
|
419 file_name = self.file_node.get("url")
|
|
420 file = urllib.urlopen(file_name)
|
|
421 self.tree.insert_xml(file.read())
|
|
422 return 1
|
|
423
|
|
424 def on_design(self,evt):
|
|
425 tlist = ['Title','URL']
|
|
426 print "design filename",self.xml.get('name')
|
|
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)
|
|
447 self.map = open_rpg.get_component('map')
|
|
448 self.mapdata = self.xml[0]
|
|
449
|
|
450 def on_ldclick(self,evt):
|
|
451 self.map.new_data(tostring(self.mapdata))
|
|
452 return 1
|