Mercurial > traipse
comparison orpg/mapper/background.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 | 211ac836b6a0 |
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: background.py | |
21 # Author: Chris Davis | |
22 # Maintainer: | |
23 # Version: | |
24 # $Id: background.py,v 1.29 2007/03/09 14:11:55 digitalxero Exp $ | |
25 # | |
26 # Description: This file contains some of the basic definitions for the chat | |
27 # utilities in the orpg project. | |
28 # | |
29 __version__ = "$Id: background.py,v 1.29 2007/03/09 14:11:55 digitalxero Exp $" | |
30 | |
31 from base import * | |
32 import thread | |
33 import urllib | |
34 import os.path | |
35 import time | |
36 | |
37 ##----------------------------- | |
38 ## background layer | |
39 ##----------------------------- | |
40 | |
41 BG_NONE = 0 | |
42 BG_TEXTURE = 1 | |
43 BG_IMAGE = 2 | |
44 BG_COLOR = 3 | |
45 | |
46 class layer_back_ground(layer_base): | |
47 def __init__(self, canvas): | |
48 self.canvas = canvas | |
49 self.log = self.canvas.log | |
50 self.log.log("Enter layer_back_ground", ORPG_DEBUG) | |
51 self.settings = self.canvas.settings | |
52 layer_base.__init__(self) | |
53 self.canvas = canvas | |
54 self.r_h = RGBHex() | |
55 self.clear() | |
56 self.log.log("Exit layer_back_ground", ORPG_DEBUG) | |
57 | |
58 def error_loading_image(self, image): | |
59 self.log.log("Enter layer_back_ground->error_loading_image(self, image)", ORPG_DEBUG) | |
60 msg = "Unable to load image:" + `image` | |
61 dlg = wx.MessageDialog(self.canvas,msg,'File not Found',wx.ICON_EXCLAMATION) | |
62 dlg.ShowModal() | |
63 dlg.Destroy() | |
64 self.log.log("Exit layer_back_ground->error_loading_image(self, image)", ORPG_DEBUG) | |
65 | |
66 def clear(self): | |
67 self.log.log("Enter layer_back_ground->clear(self)", ORPG_DEBUG) | |
68 self.type = BG_NONE | |
69 self.bg_bmp = None | |
70 self.bg_color = None | |
71 self.img_path = None | |
72 self.local = False | |
73 self.localPath = '' | |
74 self.localTime = -1 | |
75 self.isUpdated = True | |
76 self.log.log("Exit layer_back_ground->clear(self)", ORPG_DEBUG) | |
77 | |
78 def get_type(self): | |
79 self.log.log("Enter layer_back_ground->get_type(self)", ORPG_DEBUG) | |
80 self.log.log("Exit layer_back_ground->get_type(self)", ORPG_DEBUG) | |
81 return self.type | |
82 | |
83 def get_img_path(self): | |
84 self.log.log("Enter layer_back_ground->get_type(self)", ORPG_DEBUG) | |
85 if self.img_path: | |
86 self.log.log("Exit layer_back_ground->get_type(self) return " + self.img_path, ORPG_DEBUG) | |
87 return self.img_path | |
88 else: | |
89 self.log.log("Exit layer_back_ground->get_type(self) return None", ORPG_DEBUG) | |
90 return "" | |
91 | |
92 def get_color(self): | |
93 self.log.log("Enter layer_back_ground->get_color(self)", ORPG_DEBUG) | |
94 hexcolor = "#FFFFFF" | |
95 if self.bg_color: | |
96 (red,green,blue) = self.bg_color.Get() | |
97 hexcolor = self.r_h.hexstring(red, green, blue) | |
98 self.log.log("Exit layer_back_ground->get_color(self)", ORPG_DEBUG) | |
99 return hexcolor | |
100 | |
101 def set_texture(self, path): | |
102 self.log.log("Enter layer_back_ground->set_texture(self, path)", ORPG_DEBUG) | |
103 self.isUpdated = True | |
104 | |
105 self.type = BG_TEXTURE | |
106 if self.img_path != path: | |
107 try: | |
108 self.bg_bmp = ImageHandler.load(path, "texture", 0) | |
109 if self.bg_bmp == None: | |
110 self.log.log("Invalid image type!", ORPG_GENERAL) | |
111 raise Exception, "Invalid image type!" | |
112 except: | |
113 self.error_loading_image(path) | |
114 self.img_path = path | |
115 self.log.log("Enter layer_back_ground->set_texture(self, path)", ORPG_DEBUG) | |
116 | |
117 def set_image(self, path, scale): | |
118 self.log.log("Enter layer_back_ground->set_image(self, path, scale)", ORPG_DEBUG) | |
119 self.isUpdated = True | |
120 | |
121 self.type = BG_IMAGE | |
122 if self.img_path != path: | |
123 self.bg_bmp = ImageHandler.load(path, "background", 0) | |
124 try: | |
125 if self.bg_bmp == None: | |
126 self.log.log("Invalid image type!", ORPG_GENERAL) | |
127 raise Exception, "Invalid image type!" | |
128 except: | |
129 self.error_loading_image(path) | |
130 self.img_path = path | |
131 self.log.log("Exit layer_back_ground->set_image(self, path, scale)", ORPG_DEBUG) | |
132 return (self.bg_bmp.GetWidth(),self.bg_bmp.GetHeight()) | |
133 | |
134 def set_color(self, color): | |
135 self.log.log("Enter layer_back_ground->set_color(self, color)", ORPG_DEBUG) | |
136 self.isUpdated = True | |
137 self.type = BG_COLOR | |
138 (r,g,b) = color.Get() | |
139 self.bg_color = cmpColour(r,g,b) | |
140 self.canvas.SetBackgroundColour(self.bg_color) | |
141 self.log.log("Exit layer_back_ground->set_color(self, color)", ORPG_DEBUG) | |
142 | |
143 def layerDraw(self, dc, scale, topleft, size): | |
144 self.log.log("Enter layer_back_ground->layerDraw(self, dc, scale, topleft, size)", ORPG_DEBUG) | |
145 if self.bg_bmp == None or not self.bg_bmp.Ok() or ((self.type != BG_TEXTURE) and (self.type != BG_IMAGE)): | |
146 self.log.log("Exit layer_back_ground->layerDraw(self, dc, scale, topleft, size) return False", ORPG_DEBUG) | |
147 return False | |
148 dc2 = wx.MemoryDC() | |
149 dc2.SelectObject(self.bg_bmp) | |
150 topLeft = [int(topleft[0]/scale), int(topleft[1]/scale)] | |
151 topRight = [int((topleft[0]+size[0]+1)/scale)+1, int((topleft[1]+size[1]+1)/scale)+1] | |
152 if (topRight[0] > self.canvas.size[0]): | |
153 topRight[0] = self.canvas.size[0] | |
154 if (topRight[1] > self.canvas.size[1]): | |
155 topRight[1] = self.canvas.size[1] | |
156 bmpW = self.bg_bmp.GetWidth() | |
157 bmpH = self.bg_bmp.GetHeight() | |
158 if self.type == BG_TEXTURE: | |
159 x = (topLeft[0]/bmpW)*bmpW | |
160 y1 = int(topLeft[1]/bmpH)*bmpH | |
161 if x < topLeft[0]: | |
162 posx = topLeft[0] | |
163 cl = topLeft[0]-x | |
164 else: | |
165 cl = 0 | |
166 posx = x | |
167 while x < topRight[0]: | |
168 if x+bmpW > topRight[0]: | |
169 cr = x+bmpW-topRight[0] | |
170 else: | |
171 cr = 0 | |
172 y = int(topLeft[1]/bmpH)*bmpH | |
173 if y < topLeft[1]: | |
174 posy = topLeft[1] | |
175 ct = topLeft[1]-y | |
176 else: | |
177 ct = 0 | |
178 posy = y | |
179 while y < topRight[1]: | |
180 if y+bmpH > topRight[1]: | |
181 cb = y+bmpH-topRight[1] | |
182 else: | |
183 cb = 0 | |
184 newW = bmpW-cr-cl | |
185 newH = bmpH-cb-ct | |
186 if newW < 0: | |
187 newW = 0 | |
188 if newH < 0: | |
189 newH = 0 | |
190 dc.DrawBitmap(self.bg_bmp, posx, posy) | |
191 dc.Blit(posx, posy, newW, newH, dc2, cl, ct) | |
192 ct = 0 | |
193 y = y+bmpH | |
194 posy = y | |
195 cl = 0 | |
196 x = x+bmpW | |
197 posx = x | |
198 elif self.type == BG_IMAGE: | |
199 x = 0 | |
200 y = 0 | |
201 if x < topLeft[0]: | |
202 posx = topLeft[0] | |
203 cl = topLeft[0]-x | |
204 else: | |
205 cl = 0 | |
206 posx = x | |
207 | |
208 if y < topLeft[1]: | |
209 posy = topLeft[1] | |
210 ct = topLeft[1]-y | |
211 else: | |
212 ct = 0 | |
213 posy = y | |
214 if x+bmpW > topRight[0]: | |
215 cr = x+bmpW-topRight[0] | |
216 else: | |
217 cr = 0 | |
218 if y+bmpH > topRight[1]: | |
219 cb = y+bmpH-topRight[1] | |
220 else: | |
221 cb = 0 | |
222 newW = bmpW-cr-cl | |
223 newH = bmpH-cb-ct | |
224 if newW < 0: | |
225 newW = 0 | |
226 if newH < 0: | |
227 newH = 0 | |
228 dc.DrawBitmap(self.bg_bmp, posx, posy) | |
229 dc.Blit(posx, posy, newW, newH, dc2, cl, ct) | |
230 dc2.SelectObject(wx.NullBitmap) | |
231 del dc2 | |
232 self.log.log("Exit layer_back_ground->layerDraw(self, dc, scale, topleft, size)", ORPG_DEBUG) | |
233 return True | |
234 | |
235 def layerToXML(self, action="update"): | |
236 self.log.log("Enter layer_back_ground->layerToXML(self, " + action + ")", ORPG_DEBUG) | |
237 xml_str = "<bg" | |
238 if self.bg_color != None: | |
239 (red,green,blue) = self.bg_color.Get() | |
240 hexcolor = self.r_h.hexstring(red, green, blue) | |
241 xml_str += ' color="' + hexcolor + '"' | |
242 if self.img_path != None: | |
243 xml_str += ' path="' + urllib.quote(self.img_path).replace('%3A', ':') + '"' | |
244 if self.type != None: | |
245 xml_str += ' type="' + str(self.type) + '"' | |
246 if self.local and self.img_path != None: | |
247 xml_str += ' local="True"' | |
248 xml_str += ' localPath="' + urllib.quote(self.localPath).replace('%3A', ':') + '"' | |
249 xml_str += ' localTime="' + str(self.localTime) + '"' | |
250 xml_str += "/>" | |
251 self.log.log(xml_str, ORPG_DEBUG) | |
252 self.log.log("Exit layer_back_ground->layerToXML(self, " + action + ")", ORPG_DEBUG) | |
253 if (action == "update" and self.isUpdated) or action == "new": | |
254 self.isUpdated = False | |
255 return xml_str | |
256 else: | |
257 return '' | |
258 | |
259 def layerTakeDOM(self, xml_dom): | |
260 self.log.log("Enter layer_back_ground->layerTakeDOM(self, xml_dom)", ORPG_DEBUG) | |
261 type = BG_COLOR | |
262 color = xml_dom.getAttribute("color") | |
263 self.log.log("color=" + color, ORPG_DEBUG) | |
264 path = urllib.unquote(xml_dom.getAttribute("path")) | |
265 self.log.log("path=" + path, ORPG_DEBUG) | |
266 | |
267 # Begin ted's map changes | |
268 if xml_dom.hasAttribute("color"): | |
269 r,g,b = self.r_h.rgb_tuple(xml_dom.getAttribute("color")) | |
270 self.set_color(cmpColour(r,g,b)) | |
271 # End ted's map changes | |
272 if xml_dom.hasAttribute("type"): | |
273 type = int(xml_dom.getAttribute("type")) | |
274 self.log.log("type=" + str(type), ORPG_DEBUG) | |
275 | |
276 if type == BG_TEXTURE: | |
277 if path != "": | |
278 self.set_texture(path) | |
279 | |
280 elif type == BG_IMAGE: | |
281 if path != "": | |
282 self.set_image(path, 1) | |
283 | |
284 elif type == BG_NONE: | |
285 self.clear() | |
286 | |
287 if xml_dom.hasAttribute('local') and xml_dom.getAttribute('local') == 'True' and os.path.exists(urllib.unquote(xml_dom.getAttribute('localPath'))): | |
288 self.localPath = urllib.unquote(xml_dom.getAttribute('localPath')) | |
289 self.local = True | |
290 self.localTime = int(xml_dom.getAttribute('localTime')) | |
291 if self.localTime-time.time() <= 144000: | |
292 file = open(self.localPath, "rb") | |
293 imgdata = file.read() | |
294 file.close() | |
295 filename = os.path.split(self.localPath) | |
296 (imgtype,j) = mimetypes.guess_type(filename[1]) | |
297 postdata = urllib.urlencode({'filename':filename[1], 'imgdata':imgdata, 'imgtype':imgtype}) | |
298 thread.start_new_thread(self.upload, (postdata, self.localPath, type)) | |
299 self.log.log("Exit layer_back_ground->layerTakeDOM(self, xml_dom)", ORPG_DEBUG) | |
300 | |
301 def upload(self, postdata, filename, type): | |
302 self.lock.acquire() | |
303 if type == 'Image' or type == 'Texture': | |
304 url = self.settings.get_setting('ImageServerBaseURL') | |
305 file = urllib.urlopen(url, postdata) | |
306 recvdata = file.read() | |
307 file.close() | |
308 try: | |
309 xml_dom = minidom.parseString(recvdata)._get_documentElement() | |
310 if xml_dom.nodeName == 'path': | |
311 path = xml_dom.getAttribute('url') | |
312 path = urllib.unquote(path) | |
313 | |
314 if type == 'Image': | |
315 self.set_image(path, 1) | |
316 else: | |
317 self.set_texture(path) | |
318 | |
319 self.localPath = filename | |
320 self.local = True | |
321 self.localTime = time.time() | |
322 else: | |
323 print xml_dom.getAttribute('msg') | |
324 except Exception, e: | |
325 print e | |
326 print recvdata | |
327 self.lock.release() |