comparison orpg/gametree/nodehandlers/core.py @ 0:4385a7d0efd1 grumpy-goblin

Deleted and repushed it with the 'grumpy-goblin' branch. I forgot a y
author sirebral
date Tue, 14 Jul 2009 16:41:58 -0500
parents
children e8029caed892
comparison
equal deleted inserted replaced
-1:000000000000 0:4385a7d0efd1
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 try:
33 from orpg.orpg_windows import *
34 import orpg.dirpath
35 from orpg.orpg_xml import *
36 from orpg.orpgCore import open_rpg
37 import webbrowser
38 from orpg.mapper import map
39 import os
40 except:
41 import wx
42
43
44
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_dom,tree_node):
54 self.master_dom = xml_dom
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.xml = open_rpg.get_component('xml')
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.master_dom.getAttribute("version"))
66 # set to current version
67 self.master_dom.setAttribute("version",NODEHANDLER_VERSION)
68 # null events
69
70 def on_version(self,old_version):
71 ## added version control code here or implement a new on_version in your derived class.
72 ## always call the base class on_version !
73 pass
74
75 def on_rclick(self,evt):
76 self.tree.do_std_menu(evt,self)
77
78 def on_ldclick(self,evt):
79 return 0
80
81 def traverse(self, traverseroot, function, cookie=0, event=None, recursive=True):
82 """ walk tree control """
83 if traverseroot.IsOk():
84 # step in subtree if there are items or ...
85 if self.tree.ItemHasChildren(traverseroot) and recursive:
86 firstchild, cookie = self.tree.GetFirstChild(traverseroot)
87 obj = self.tree.GetPyData(firstchild)
88 function(obj, event)
89 self.traverse(firstchild, function, cookie, event, recursive)
90
91 # ... loop siblings
92 obj = self.tree.GetPyData(traverseroot)
93 function(obj, event)
94
95 child = self.tree.GetNextSibling(traverseroot)
96 if child.IsOk():
97 self.traverse(child, function, cookie, event, recursive)
98
99
100 def usefulness(self,text):
101 if text=="useful":
102 self.master_dom.setAttribute('status',"useful")
103 elif text=="useless":
104 self.master_dom.setAttribute('status',"useless")
105 elif text=="indifferent":
106 self.master_dom.setAttribute('status',"indifferent")
107
108 def on_design(self,evt):
109 try:
110 self.myeditor.Show()
111 self.myeditor.Raise()
112 except:
113 del self.myeditor
114 if self.create_designframe():
115 self.myeditor.Show()
116 self.myeditor.Raise()
117 else:
118 return
119 wx.CallAfter(self.myeditor.Layout)
120
121
122 def create_designframe(self):
123 title = self.master_dom.getAttribute('name') + " Editor"
124 self.myeditor = wx.Frame(None, -1, title)
125 self.myeditor.Freeze()
126 if wx.Platform == '__WXMSW__':
127 icon = wx.Icon(orpg.dirpath.dir_struct["icon"] + 'grid.ico', wx.BITMAP_TYPE_ICO)
128 self.myeditor.SetIcon(icon)
129 del icon
130
131 self.myeditor.panel = self.get_design_panel(self.myeditor)
132 if self.myeditor.panel == None:
133 self.myeditor.Destroy()
134 return False
135 sizer = wx.BoxSizer(wx.VERTICAL)
136 sizer.Add(self.myeditor.panel, 1, wx.EXPAND)
137
138 self.myeditor.SetSizer(sizer)
139 self.myeditor.SetAutoLayout(True)
140
141 (x, y) = self.myeditor.GetSize()
142 if x < 400:
143 x = 400
144 if y < 400:
145 y = 400
146
147 self.myeditor.SetSize((x, y))
148 self.myeditor.Layout()
149 self.myeditor.Thaw()
150
151 return True
152
153 def on_use(self,evt):
154 try:
155 self.mywindow.Show()
156 self.mywindow.Raise()
157 except:
158 del self.mywindow
159 if self.create_useframe():
160 self.mywindow.Show()
161 self.mywindow.Raise()
162 else:
163 return
164 wx.CallAfter(self.mywindow.Layout)
165
166
167 def create_useframe(self):
168 caption = self.master_dom.getAttribute('name')
169 self.mywindow = wx.Frame(None, -1, caption)
170 self.mywindow.Freeze()
171
172 if wx.Platform == '__WXMSW__':
173 icon = wx.Icon(orpg.dirpath.dir_struct["icon"] + 'note.ico', wx.BITMAP_TYPE_ICO)
174 self.mywindow.SetIcon(icon)
175 del icon
176 self.mywindow.panel = self.get_use_panel(self.mywindow)
177 if self.mywindow.panel == None:
178 self.mywindow.Destroy()
179 return False
180 sizer = wx.BoxSizer(wx.VERTICAL)
181 sizer.Add(self.mywindow.panel, 2, wx.EXPAND)
182
183 self.mywindow.SetSizer(sizer)
184 self.mywindow.SetAutoLayout(True)
185
186 (x, y) = self.mywindow.GetSize()
187 if x < 400:
188 x = 400
189 if y < 400:
190 y = 400
191
192 self.mywindow.SetSize((x, y))
193 self.mywindow.Layout()
194 self.mywindow.Thaw()
195
196 return True
197
198
199 def on_html_view(self,evt):
200 try:
201 self.myviewer.Raise()
202 except:
203 caption = self.master_dom.getAttribute('name')
204 self.myviewer = wx.Frame(None, -1, caption)
205 if wx.Platform == '__WXMSW__':
206 icon = wx.Icon(orpg.dirpath.dir_struct["icon"] + 'grid.ico', wx.BITMAP_TYPE_ICO)
207 self.myviewer.SetIcon(icon)
208 del icon
209 self.myviewer.panel = self.get_html_panel(self.myviewer)
210 self.myviewer.Show()
211
212 def map_aware(self):
213 return 0
214
215 def can_clone(self):
216 return 1;
217
218 def on_del(self,evt):
219 print "on del"
220
221 def on_new_data(self,xml_dom):
222 pass
223
224 def get_scaled_bitmap(self,x,y):
225 return None
226
227 def on_send_to_map(self,evt):
228 pass
229
230 def on_send_to_chat(self,evt):
231 self.chat.ParsePost(self.tohtml(),True,True)
232
233 def on_drop(self,evt):
234 drag_obj = self.tree.drag_obj
235 if drag_obj == self or self.tree.is_parent_node(self.mytree_node,drag_obj.mytree_node):
236 return
237 #if self.is_my_child(self.mytree_node,drag_obj.mytree_node):
238 # return
239 xml_dom = self.tree.drag_obj.delete()
240 parent = self.master_dom._get_parentNode()
241 xml_dom = parent.insertBefore(xml_dom,self.master_dom)
242 parent_node = self.tree.GetItemParent(self.mytree_node)
243 prev_sib = self.tree.GetPrevSibling(self.mytree_node)
244 if not prev_sib.IsOk():
245 prev_sib = parent_node
246 self.tree.load_xml(xml_dom, parent_node, prev_sib)
247
248 def toxml(self,pretty=0):
249 return toxml(self.master_dom,pretty)
250
251 def tohtml(self):
252 return self.master_dom.getAttribute("name")
253
254 def delete(self):
255 """ removes the tree_node and xml_node, and returns the removed xml_node """
256
257 self.tree.Delete(self.mytree_node)
258 parent = self.master_dom._get_parentNode()
259 return parent.removeChild(self.master_dom)
260
261 def rename(self,name):
262 if len(name):
263 self.tree.SetItemText(self.mytree_node,name)
264 self.master_dom.setAttribute('name', name)
265
266 def change_icon(self,icon):
267 self.master_dom.setAttribute("icon",icon)
268 self.tree.SetItemImage(self.mytree_node, self.tree.icons[icon])
269 self.tree.SetItemImage(self.mytree_node, self.tree.icons[icon], wx.TreeItemIcon_Selected)
270 self.tree.Refresh()
271
272 def on_save(self,evt):
273 f = wx.FileDialog(self.tree,"Select a file", orpg.dirpath.dir_struct["user"],"","XML files (*.xml)|*.xml",wx.SAVE)
274 if f.ShowModal() == wx.ID_OK:
275 type = f.GetFilterIndex()
276 file = open(f.GetPath(),"w")
277 file.write(self.toxml(1))
278 file.close()
279 f.Destroy()
280
281 def get_design_panel(self,parent):
282 return None
283
284 def get_use_panel(self,parent):
285 return None
286
287 def get_html_panel(self,parent):
288 html_str = "<html><body bgcolor=\"#FFFFFF\" >"+self.tohtml()+"</body></html>"
289 wnd = wx.html.HtmlWindow(parent,-1)
290 html_str = self.chat.ParseDice(html_str)
291 wnd.SetPage(html_str)
292 return wnd
293
294 def get_size_constraint(self):
295 return 0
296
297 def about(self):
298 html_str = "<b>"+ self.master_dom.getAttribute('class')
299 html_str += " Applet</b><br />by Chris Davis<br />chris@rpgarchive.com"
300 return html_str
301
302 P_TITLE = 10
303 P_BODY = 20
304 class text_edit_panel(wx.Panel):
305 def __init__(self, parent, handler):
306 wx.Panel.__init__(self, parent, -1)
307 self.handler = handler
308 sizer = wx.BoxSizer(wx.VERTICAL)
309 self.text = { P_TITLE : wx.TextCtrl(self, P_TITLE, handler.master_dom.getAttribute('name')),
310 P_BODY : html_text_edit(self,P_BODY,handler.text._get_nodeValue(),self.on_text)
311 }
312 #P_BODY : wx.TextCtrl(self, P_BODY,handler.text._get_nodeValue(), style=wx.TE_MULTILINE)
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:
331 u_txt += c
332 else:
333 bad_txt_found = 1
334 if bad_txt_found:
335 wx.MessageBox("Some non 7-bit ASCII characters found and stripped","Warning!")
336 txt = u_txt
337 if txt != "":
338 self.handler.master_dom.setAttribute('name',txt)
339 self.handler.rename(txt)
340 elif id == P_BODY:
341 txt = self.text[id].get_text()
342 u_txt = ""
343 bad_txt_found = 0
344 for c in txt:
345 if ord(c) < 128:
346 u_txt += c
347 else:
348 bad_txt_found = 1
349
350 if bad_txt_found:
351 wx.MessageBox("Some non 7-bit ASCII characters found and stripped","Warning!")
352 txt = u_txt
353 self.handler.text._set_nodeValue(txt)
354
355
356
357 ##########################
358 ## node loader
359 ##########################
360 class node_loader(node_handler):
361 """ clones childe node and insert it at top of tree
362 <nodehandler name='?' module='core' class='node_loader' />
363 """
364 def __init__(self,xml_dom,tree_node):
365 node_handler.__init__(self,xml_dom,tree_node)
366
367 def on_rclick(self,evt):
368 pass
369
370 def on_ldclick(self,evt):
371 title = self.master_dom.getAttribute('name')
372 new_node = self.master_dom._get_firstChild()
373 new_node = new_node.cloneNode(True)
374 child = self.tree.master_dom._get_firstChild()
375 new_node = self.tree.master_dom.insertBefore(new_node,child)
376 tree_node = self.tree.load_xml(new_node,self.tree.root,self.tree.root)
377 obj = self.tree.GetPyData(tree_node)
378 return 1
379 #obj.on_design(None)
380
381 ##########################
382 ## file loader
383 ##########################
384
385 class file_loader(node_handler):
386 """ loads file and insert into game tree
387 <nodehandler name='?' module='core' class='file_loader' >
388 <file name="file_name.xml" />
389 </nodehandler>
390 """
391 def __init__(self,xml_dom,tree_node):
392 node_handler.__init__(self,xml_dom,tree_node)
393 self.file_node = self.master_dom._get_firstChild()
394 self.frame = open_rpg.get_component('frame')
395
396 def on_ldclick(self,evt):
397 file_name = self.file_node.getAttribute("name")
398 self.tree.insert_xml(open(orpg.dirpath.dir_struct["nodes"] + file_name,"r").read())
399 return 1
400
401 def on_design(self,evt):
402 tlist = ['Title','File Name']
403 vlist = [self.master_dom.getAttribute("name"),
404 self.file_node.getAttribute("name")]
405 dlg = orpgMultiTextEntry(self.tree.GetParent(),tlist,vlist,"File Loader Edit")
406 if dlg.ShowModal() == wx.ID_OK:
407 vlist = dlg.get_values()
408 self.file_node.setAttribute('name', vlist[1])
409 self.master_dom.setAttribute('name', vlist[0])
410 self.tree.SetItemText(self.mytree_node,vlist[0])
411 dlg.Destroy()
412
413 ##########################
414 ## URL loader
415 ##########################
416
417 class url_loader(node_handler):
418 """ loads file from url and insert into game tree
419 <nodehandler name='?' module='core' class='url_loader' >
420 <file name="http://file_name.xml" />
421 </nodehandler>
422 """
423 def __init__(self,xml_dom,tree_node):
424 node_handler.__init__(self,xml_dom,tree_node)
425 self.file_node = self.master_dom._get_firstChild()
426 self.frame = open_rpg.get_component('frame')
427
428 def on_ldclick(self,evt):
429 file_name = self.file_node.getAttribute("url")
430 file = urllib.urlopen(file_name)
431 self.tree.insert_xml(file.read())
432 return 1
433
434 def on_design(self,evt):
435 tlist = ['Title','URL']
436 print "design filename",self.master_dom.getAttribute('name')
437 vlist = [self.master_dom.getAttribute("name"),
438 self.file_node.getAttribute("url")]
439 dlg = orpgMultiTextEntry(self.tree.GetParent(),tlist,vlist,"File Loader Edit")
440 if dlg.ShowModal() == wx.ID_OK:
441 vlist = dlg.get_values()
442 self.file_node.setAttribute('url', vlist[1])
443 self.master_dom.setAttribute('name', vlist[0])
444 self.tree.SetItemText(self.mytree_node,vlist[0])
445 dlg.Destroy()
446
447
448 ##########################
449 ## minature map loader
450 ##########################
451 class min_map(node_handler):
452 """ clones childe node and insert it at top of tree
453 <nodehandler name='?' module='core' class='min_map' />
454 """
455 def __init__(self,xml_dom,tree_node):
456 node_handler.__init__(self,xml_dom,tree_node)
457 self.map = open_rpg.get_component('map')
458 self.mapdata = self.master_dom._get_firstChild()
459
460 def on_ldclick(self,evt):
461 self.map.new_data(toxml(self.mapdata))
462 return 1