155
|
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:
|
227
|
24 # $Id: core.py,v Traipse 'Ornery-Orc' prof.ebral Exp $
|
155
|
25 #
|
|
26 # Description: The file contains code for the core nodehanlers
|
|
27 #
|
|
28
|
227
|
29 __version__ = "$Id: core.py,v Traipse 'Ornery-Orc' prof.ebral Exp $"
|
155
|
30
|
|
31 from nodehandler_version import NODEHANDLER_VERSION
|
227
|
32 from orpg.tools.InterParse import Parse
|
155
|
33
|
|
34 try:
|
|
35 from orpg.orpg_windows import *
|
|
36 from orpg.dirpath import dir_struct
|
|
37 from orpg.orpgCore import component
|
|
38 import webbrowser, os
|
|
39 from orpg.mapper import map
|
|
40 from wx import DisplaySize
|
|
41 except:
|
|
42 import wx
|
|
43
|
132
|
44 from xml.etree.ElementTree import ElementTree, Element, tostring, XML
|
155
|
45 from orpg.tools.orpg_log import debug
|
|
46
|
|
47 #html defaults
|
|
48 TH_BG = "#E9E9E9"
|
|
49 ##########################
|
|
50 ## base node handler
|
|
51 ##########################
|
|
52 class node_handler:
|
|
53 """ Base nodehandler with virtual functions and standard implmentations """
|
|
54 def __init__(self,xml,tree_node):
|
|
55 self.xml = xml
|
|
56 self.mytree_node = tree_node
|
159
|
57 self.tree = component.get('tree')
|
|
58 self.frame = component.get('frame')
|
|
59 self.chat = component.get('chat')
|
155
|
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):
|
180
|
86 self.tree.do_std_menu(evt,self)
|
155
|
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
|
173
|
136 def on_use(self, evt):
|
155
|
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):
|
180
|
220 drag_obj = self.tree.drag_obj
|
|
221 if drag_obj == self or self.tree.is_parent_node(self.mytree_node, drag_obj.mytree_node):
|
155
|
222 return
|
180
|
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
|
155
|
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
|
180
|
234 self.tree.load_xml(drop_xml, parent_node, prev_sib)
|
132
|
235
|
|
236 def get_tree(self):
|
|
237 family = []
|
|
238 test = treenode
|
180
|
239 while test != self.tree.root:
|
|
240 test = self.tree.GetItemParent(test)
|
|
241 parent = self.tree.GetItemText(test)
|
132
|
242 family.append(parent)
|
155
|
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 """
|
180
|
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
|
155
|
256 parent_xml.remove(self.xml)
|
180
|
257 self.tree.Delete(self.mytree_node)
|
155
|
258 return self.xml
|
|
259
|
|
260 def rename(self,name):
|
|
261 if len(name):
|
180
|
262 self.tree.SetItemText(self.mytree_node,name)
|
155
|
263 self.xml.set('name', name)
|
|
264
|
|
265 def change_icon(self,icon):
|
|
266 self.xml.set("icon",icon)
|
180
|
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()
|
155
|
270
|
|
271 def on_save(self,evt):
|
180
|
272 f = wx.FileDialog(self.tree,"Select a file", orpg.dirpath.dir_struct["user"],"","XML files (*.xml)|*.xml",wx.SAVE)
|
155
|
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)
|
227
|
285 html_str = Parse.Dice(html_str)
|
155
|
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"
|
151
|
295 return html_str"""
|
|
296 text = self.xml.get('class') + ' Applet\n'
|
|
297 text += 'by Chris Davis chris@rpgarchive.com'
|
|
298 return text
|
132
|
299
|
|
300 def get_value(self):
|
155
|
301 return None
|
|
302
|
|
303 P_TITLE = 10
|
151
|
304 P_BODY = 20
|
155
|
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
|
227
|
348 print txt, self.handler, self.handler.xml
|
155
|
349 self.handler.text._set_nodeValue(txt)
|
|
350
|
|
351
|
|
352
|
|
353 ##########################
|
|
354 ## node loader
|
|
355 ##########################
|
|
356 class node_loader(node_handler):
|
|
357 """ clones childe node and insert it at top of tree
|
|
358 <nodehandler name='?' module='core' class='node_loader' />
|
|
359 """
|
|
360 def __init__(self,xml,tree_node):
|
|
361 node_handler.__init__(self,xml,tree_node)
|
|
362
|
|
363 def on_rclick(self,evt):
|
|
364 pass
|
|
365
|
|
366 def on_ldclick(self,evt):
|
|
367 title = self.xml.get('name')
|
|
368 new_xml = XML(tostring(self.xml[0]))
|
180
|
369 self.tree.root_xml.insert(0, new_xml)
|
|
370 tree_node = self.tree.load_xml(new_xml,self.tree.root,self.tree.root)
|
155
|
371 return 1
|
|
372
|
|
373 ##########################
|
|
374 ## file loader
|
|
375 ##########################
|
|
376
|
|
377 class file_loader(node_handler):
|
|
378 """ loads file and insert into game tree
|
|
379 <nodehandler name='?' module='core' class='file_loader' >
|
|
380 <file name="file_name.xml" />
|
|
381 </nodehandler>
|
|
382 """
|
|
383 def __init__(self,xml,tree_node):
|
|
384 node_handler.__init__(self,xml,tree_node)
|
|
385 self.file_node = self.xml[0]
|
159
|
386 self.frame = component.get('frame')
|
155
|
387
|
|
388 def on_ldclick(self,evt):
|
|
389 file_name = self.file_node.get("name")
|
227
|
390 try: self.tree.insert_xml(open(orpg.dirpath.dir_struct["nodes"] + file_name,"r").read())
|
|
391 except: wx.MessageBox('Invalid File', 'Error')
|
155
|
392 return 1
|
|
393
|
|
394 def on_design(self,evt):
|
|
395 tlist = ['Title','File Name']
|
|
396 vlist = [self.xml.get("name"),
|
|
397 self.file_node.get("name")]
|
180
|
398 dlg = orpgMultiTextEntry(self.tree.GetParent(),tlist,vlist,"File Loader Edit")
|
155
|
399 if dlg.ShowModal() == wx.ID_OK:
|
|
400 vlist = dlg.get_values()
|
|
401 self.file_node.set('name', vlist[1])
|
|
402 self.xml.set('name', vlist[0])
|
180
|
403 self.tree.SetItemText(self.mytree_node,vlist[0])
|
155
|
404 dlg.Destroy()
|
|
405
|
|
406 ##########################
|
|
407 ## URL loader
|
|
408 ##########################
|
|
409
|
|
410 class url_loader(node_handler):
|
|
411 """ loads file from url and insert into game tree
|
|
412 <nodehandler name='?' module='core' class='url_loader' >
|
|
413 <file name="http://file_name.xml" />
|
|
414 </nodehandler>
|
|
415 """
|
|
416 def __init__(self,xml,tree_node):
|
|
417 node_handler.__init__(self,xml,tree_node)
|
|
418 self.file_node = self.xml[0]
|
159
|
419 self.frame = component.get('frame')
|
155
|
420
|
|
421 def on_ldclick(self,evt):
|
|
422 file_name = self.file_node.get("url")
|
|
423 file = urllib.urlopen(file_name)
|
180
|
424 self.tree.insert_xml(file.read())
|
155
|
425 return 1
|
|
426
|
|
427 def on_design(self,evt):
|
|
428 tlist = ['Title','URL']
|
|
429 vlist = [self.xml.get("name"),
|
|
430 self.file_node.get("url")]
|
180
|
431 dlg = orpgMultiTextEntry(self.tree.GetParent(),tlist,vlist,"File Loader Edit")
|
155
|
432 if dlg.ShowModal() == wx.ID_OK:
|
|
433 vlist = dlg.get_values()
|
|
434 self.file_node.set('url', vlist[1])
|
|
435 self.xml.set('name', vlist[0])
|
180
|
436 self.tree.SetItemText(self.mytree_node,vlist[0])
|
155
|
437 dlg.Destroy()
|
|
438
|
|
439
|
|
440 ##########################
|
|
441 ## minature map loader
|
|
442 ##########################
|
|
443 class min_map(node_handler):
|
|
444 """ clones childe node and insert it at top of tree
|
|
445 <nodehandler name='?' module='core' class='min_map' />
|
|
446 """
|
|
447 def __init__(self,xml,tree_node):
|
|
448 node_handler.__init__(self,xml,tree_node)
|
159
|
449 self.map = component.get('map')
|
|
450 self.mapdata = self.xml.find('map')
|
155
|
451
|
|
452 def on_ldclick(self,evt):
|
159
|
453 if (component.get('session').my_role() != component.get('session').ROLE_GM):
|
|
454 component.get('chat').InfoPost("You must be a GM to use this feature")
|
|
455 return
|
155
|
456 self.map.new_data(tostring(self.mapdata))
|
159
|
457 component.get('session').send(tostring(self.mapdata))
|
|
458 self.map.canvas.send_map_data()
|
155
|
459 return 1
|