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: map_miniature_nodehandler.py
|
|
21 # Author: Andrew Bennett
|
|
22 # Maintainer:
|
|
23 # Version:
|
184
|
24 # $Id: map_miniature_nodehandler.py,v Traipse 'Ornery-Orc' prof.ebral Exp $
|
156
|
25 #
|
|
26 # Description: nodehandler for miniature images
|
|
27 #
|
|
28
|
|
29 #from nodehandlers.core import *
|
|
30 from core import *
|
|
31 from orpg.gametree import *
|
|
32 from orpg.mapper.miniatures_msg import mini_msg
|
|
33 from orpg.mapper.images import ImageHandler
|
|
34 import urllib
|
|
35
|
|
36 class map_miniature_handler(node_handler):
|
|
37
|
|
38 """ A node handler for miniatures
|
|
39 <nodehandler name='Elf-1' module='map_miniature_nodehandler' class='map_miniature_handler' >
|
|
40 <miniature id='' label='Elf-1' posx='' posy='' path='' ... />
|
|
41 </nodehandler >
|
|
42 """
|
|
43
|
|
44 def __init__(self,xml,tree_node):
|
|
45 node_handler.__init__(self,xml,tree_node)
|
|
46 self.mapper = component.get("map")
|
|
47 self.session = component.get("session")
|
|
48 self.miniature_xml = self.xml.find("miniature")
|
|
49
|
|
50 def get_scaled_bitmap(self,x,y):
|
|
51 my_mini_msg = mini_msg()
|
|
52 my_mini_msg.init_from_dom(self.miniature_xml)
|
|
53 bmp = None
|
|
54 path = my_mini_msg.get_prop("path")
|
|
55
|
|
56 if path:
|
|
57 path = urllib.unquote(path)
|
|
58 if ImageHandler.Cache.has_key(path): bmp = ImageHandler.Cache[path]
|
|
59 else: bmp = ImageHandler.directLoad(path)# Old Code TaS.
|
|
60
|
|
61 if bmp:
|
|
62 img = wx.ImageFromMime(ImageHandler.Cache[path][1], ImageHandler.Cache[path][2])
|
|
63 #img = wx.ImageFromBitmap(bmp)
|
|
64 scaled_img = img.Scale(x,y)
|
|
65 scaled_bmp = scaled_img.ConvertToBitmap()
|
|
66 scratch = scaled_img.ConvertToBitmap()
|
|
67 memDC = wx.MemoryDC()
|
|
68 memDC.BeginDrawing()
|
|
69 memDC.SelectObject(scaled_bmp)
|
|
70 memDC.SetBrush(wx.WHITE_BRUSH)
|
|
71 memDC.SetPen(wx.WHITE_PEN)
|
|
72 memDC.DrawRectangle(0,0,x,y)
|
|
73 memDC.SetPen(wx.NullPen)
|
|
74 memDC.SetBrush(wx.NullBrush)
|
|
75 memDC.DrawBitmap(scratch,0,0,1)
|
|
76 memDC.SelectObject(wx.NullBitmap)
|
|
77 memDC.EndDrawing()
|
|
78 del memDC
|
|
79 return scaled_bmp
|
|
80
|
|
81 def map_aware(self):
|
|
82 return 1
|
|
83
|
|
84 def get_miniature_XML(self):
|
|
85 my_mini_msg = mini_msg()
|
|
86 my_mini_msg.init_from_dom(self.miniature_xml)
|
|
87 my_mini_msg.init_prop("id",self.session.get_next_id())
|
|
88 label = self.xml.get("name")
|
|
89 my_mini_msg.init_prop("label",label)
|
|
90 new_xml = my_mini_msg.get_all_xml()
|
|
91 return new_xml
|
|
92
|
|
93 def get_to_map_XML(self):
|
|
94 new_xml = self.get_miniature_XML()
|
|
95 new_xml = str("<map action='update'><miniatures>" + new_xml + "</miniatures></map>")
|
|
96 return new_xml
|
|
97
|
|
98 def on_send_to_map(self,evt):
|
|
99 if isinstance(evt, wx.MouseEvent) and evt.LeftUp():# as opposed to a menu event
|
|
100 dc = wx.ClientDC(self.mapper.canvas)
|
|
101 self.mapper.canvas.PrepareDC(dc)
|
|
102 grid = self.mapper.canvas.layers['grid']
|
|
103 dc.SetUserScale(grid.mapscale, grid.mapscale)
|
|
104 pos = evt.GetLogicalPosition(dc)
|
|
105 try:
|
|
106 align = int(self.xml.get("align"))
|
|
107 width = int(self.xml.get("width"))
|
|
108 height = int(self.xml.get("height"))
|
|
109 pos = grid.get_snapped_to_pos(pos, align, width, height)
|
|
110 except: pass
|
|
111 self.miniature_xml.set("posx", str(pos.x))
|
|
112 self.miniature_xml.set("posy", str(pos.y))
|
|
113 new_xml = self.get_to_map_XML()
|
|
114 if (self.session.my_role() != self.session.ROLE_GM) and (self.session.my_role() != self.session.ROLE_PLAYER):
|
|
115 component.get("chat").InfoPost("You must be either a player or GM to use the miniature Layer")
|
|
116 return
|
|
117 if new_xml:
|
|
118 self.mapper.new_data(new_xml)
|
|
119 self.session.send(new_xml)
|
|
120 else: print "problem converting old mini xml to new mini xml"
|
|
121
|
|
122 def about(self):
|
|
123 return "Miniature node by Andrew Bennett"
|
|
124
|
|
125 def tohtml(self):
|
|
126 html_str = "<table><tr><td>"
|
|
127 html_str += "<center><img src='" + self.xml.get("path") + "'>"
|
|
128 html_str += "</center></td></tr>\n"
|
|
129 html_str += "<tr><td><center>" + self.xml.get("name") + "</center></td></tr></table>"
|
152
|
130 return html_str
|