comparison orpg/mapper/background.py @ 76:37a11fea3304 ornery-dev

More clean up. Images now posts a Chat Info post if image doesn't load.
author sirebral
date Sat, 22 Aug 2009 04:02:14 -0500
parents 072ffc1d466f
children 763a04270cf6
comparison
equal deleted inserted replaced
75:f518bc709fd1 76:37a11fea3304
32 import thread 32 import thread
33 import urllib 33 import urllib
34 import os.path 34 import os.path
35 import time 35 import time
36 36
37 from orpg.orpgCore import component
38 from orpg.tools.orpg_log import logger
39 from orpg.tools.decorators import debugging
40 from orpg.tools.orpg_settings import settings
41
37 ##----------------------------- 42 ##-----------------------------
38 ## background layer 43 ## background layer
39 ##----------------------------- 44 ##-----------------------------
40 45
41 BG_NONE = 0 46 BG_NONE = 0
42 BG_TEXTURE = 1 47 BG_TEXTURE = 1
43 BG_IMAGE = 2 48 BG_IMAGE = 2
44 BG_COLOR = 3 49 BG_COLOR = 3
45 50
46 class layer_back_ground(layer_base): 51 class layer_back_ground(layer_base):
52 @debugging
47 def __init__(self, canvas): 53 def __init__(self, canvas):
48 self.canvas = canvas 54 self.canvas = canvas
49 self.log = self.canvas.log 55 self.log = component.get('log')
50 self.log.log("Enter layer_back_ground", ORPG_DEBUG)
51 self.settings = self.canvas.settings
52 layer_base.__init__(self) 56 layer_base.__init__(self)
53 self.canvas = canvas 57 self.canvas = canvas
54 self.r_h = RGBHex() 58 self.r_h = RGBHex()
55 self.clear() 59 self.clear()
56 self.log.log("Exit layer_back_ground", ORPG_DEBUG) 60
57 61 @debugging
58 def error_loading_image(self, image): 62 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` 63 msg = "Unable to load image:" + `image`
61 dlg = wx.MessageDialog(self.canvas,msg,'File not Found',wx.ICON_EXCLAMATION) 64 dlg = wx.MessageDialog(self.canvas,msg,'File not Found',wx.ICON_EXCLAMATION)
62 dlg.ShowModal() 65 dlg.ShowModal()
63 dlg.Destroy() 66 dlg.Destroy()
64 self.log.log("Exit layer_back_ground->error_loading_image(self, image)", ORPG_DEBUG) 67
65 68 @debugging
66 def clear(self): 69 def clear(self):
67 self.log.log("Enter layer_back_ground->clear(self)", ORPG_DEBUG)
68 self.type = BG_NONE 70 self.type = BG_NONE
69 self.bg_bmp = None 71 self.bg_bmp = None
70 self.bg_color = None 72 self.bg_color = None
71 self.img_path = None 73 self.img_path = None
72 self.local = False 74 self.local = False
73 self.localPath = '' 75 self.localPath = ''
74 self.localTime = -1 76 self.localTime = -1
75 self.isUpdated = True 77 self.isUpdated = True
76 self.log.log("Exit layer_back_ground->clear(self)", ORPG_DEBUG) 78
77 79 @debugging
78 def get_type(self): 80 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 81 return self.type
82 82
83 @debugging
83 def get_img_path(self): 84 def get_img_path(self):
84 self.log.log("Enter layer_back_ground->get_type(self)", ORPG_DEBUG) 85 if self.img_path: return self.img_path
85 if self.img_path: 86 else: return ""
86 self.log.log("Exit layer_back_ground->get_type(self) return " + self.img_path, ORPG_DEBUG) 87
87 return self.img_path 88 @debugging
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): 89 def get_color(self):
93 self.log.log("Enter layer_back_ground->get_color(self)", ORPG_DEBUG)
94 hexcolor = "#FFFFFF" 90 hexcolor = "#FFFFFF"
95 if self.bg_color: 91 if self.bg_color:
96 (red,green,blue) = self.bg_color.Get() 92 (red,green,blue) = self.bg_color.Get()
97 hexcolor = self.r_h.hexstring(red, green, blue) 93 hexcolor = self.r_h.hexstring(red, green, blue)
98 self.log.log("Exit layer_back_ground->get_color(self)", ORPG_DEBUG)
99 return hexcolor 94 return hexcolor
100 95
96 @debugging
101 def set_texture(self, path): 97 def set_texture(self, path):
102 self.log.log("Enter layer_back_ground->set_texture(self, path)", ORPG_DEBUG) 98 self.isUpdated = True
103 self.isUpdated = True
104
105 self.type = BG_TEXTURE 99 self.type = BG_TEXTURE
106 if self.img_path != path: 100 if self.img_path != path:
107 try: 101 try:
108 self.bg_bmp = ImageHandler.load(path, "texture", 0) 102 self.bg_bmp = ImageHandler.load(path, "texture", 0)
109 if self.bg_bmp == None: 103 if self.bg_bmp == None:
110 self.log.log("Invalid image type!", ORPG_GENERAL) 104 logger.general("Invalid image type!")
111 raise Exception, "Invalid image type!" 105 raise Exception, "Invalid image type!"
112 except: self.error_loading_image(path) 106 except: self.error_loading_image(path)
113 self.img_path = path 107 self.img_path = path
114 self.log.log("Enter layer_back_ground->set_texture(self, path)", ORPG_DEBUG) 108
115 109 @debugging
116 def set_image(self, path, scale): 110 def set_image(self, path, scale):
117 self.log.log("Enter layer_back_ground->set_image(self, path, scale)", ORPG_DEBUG) 111 self.isUpdated = True
118 self.isUpdated = True
119
120 self.type = BG_IMAGE 112 self.type = BG_IMAGE
121 if self.img_path != path: 113 if self.img_path != path:
122 self.bg_bmp = ImageHandler.load(path, "background", 0) 114 self.bg_bmp = ImageHandler.load(path, "background", 0)
123 try: 115 try:
124 if self.bg_bmp == None: 116 if self.bg_bmp == None:
125 self.log.log("Invalid image type!", ORPG_GENERAL) 117 logger.general("Invalid image type!")
126 raise Exception, "Invalid image type!" 118 raise Exception, "Invalid image type!"
127 except: self.error_loading_image(path) 119 except: self.error_loading_image(path)
128 self.img_path = path 120 self.img_path = path
129 self.log.log("Exit layer_back_ground->set_image(self, path, scale)", ORPG_DEBUG)
130 return (self.bg_bmp.GetWidth(),self.bg_bmp.GetHeight()) 121 return (self.bg_bmp.GetWidth(),self.bg_bmp.GetHeight())
131 122
123 @debugging
132 def set_color(self, color): 124 def set_color(self, color):
133 self.log.log("Enter layer_back_ground->set_color(self, color)", ORPG_DEBUG)
134 self.isUpdated = True 125 self.isUpdated = True
135 self.type = BG_COLOR 126 self.type = BG_COLOR
136 (r,g,b) = color.Get() 127 (r,g,b) = color.Get()
137 self.bg_color = cmpColour(r,g,b) 128 self.bg_color = cmpColour(r,g,b)
138 self.canvas.SetBackgroundColour(self.bg_color) 129 self.canvas.SetBackgroundColour(self.bg_color)
139 self.log.log("Exit layer_back_ground->set_color(self, color)", ORPG_DEBUG) 130
140 131 @debugging
141 def layerDraw(self, dc, scale, topleft, size): 132 def layerDraw(self, dc, scale, topleft, size):
142 self.log.log("Enter layer_back_ground->layerDraw(self, dc, scale, topleft, size)", ORPG_DEBUG)
143 if self.bg_bmp == None or not self.bg_bmp.Ok() or ((self.type != BG_TEXTURE) and (self.type != BG_IMAGE)): 133 if self.bg_bmp == None or not self.bg_bmp.Ok() or ((self.type != BG_TEXTURE) and (self.type != BG_IMAGE)):
144 self.log.log("Exit layer_back_ground->layerDraw(self, dc, scale, topleft, size) return False", ORPG_DEBUG)
145 return False 134 return False
146 dc2 = wx.MemoryDC() 135 dc2 = wx.MemoryDC()
147 dc2.SelectObject(self.bg_bmp) 136 dc2.SelectObject(self.bg_bmp)
148 topLeft = [int(topleft[0]/scale), int(topleft[1]/scale)] 137 topLeft = [int(topleft[0]/scale), int(topleft[1]/scale)]
149 topRight = [int((topleft[0]+size[0]+1)/scale)+1, int((topleft[1]+size[1]+1)/scale)+1] 138 topRight = [int((topleft[0]+size[0]+1)/scale)+1, int((topleft[1]+size[1]+1)/scale)+1]
210 if newH < 0: newH = 0 199 if newH < 0: newH = 0
211 dc.DrawBitmap(self.bg_bmp, posx, posy) 200 dc.DrawBitmap(self.bg_bmp, posx, posy)
212 dc.Blit(posx, posy, newW, newH, dc2, cl, ct) 201 dc.Blit(posx, posy, newW, newH, dc2, cl, ct)
213 dc2.SelectObject(wx.NullBitmap) 202 dc2.SelectObject(wx.NullBitmap)
214 del dc2 203 del dc2
215 self.log.log("Exit layer_back_ground->layerDraw(self, dc, scale, topleft, size)", ORPG_DEBUG)
216 return True 204 return True
217 205
206 @debugging
218 def layerToXML(self, action="update"): 207 def layerToXML(self, action="update"):
219 self.log.log("Enter layer_back_ground->layerToXML(self, " + action + ")", ORPG_DEBUG)
220 xml_str = "<bg" 208 xml_str = "<bg"
221 if self.bg_color != None: 209 if self.bg_color != None:
222 (red,green,blue) = self.bg_color.Get() 210 (red,green,blue) = self.bg_color.Get()
223 hexcolor = self.r_h.hexstring(red, green, blue) 211 hexcolor = self.r_h.hexstring(red, green, blue)
224 xml_str += ' color="' + hexcolor + '"' 212 xml_str += ' color="' + hexcolor + '"'
227 if self.local and self.img_path != None: 215 if self.local and self.img_path != None:
228 xml_str += ' local="True"' 216 xml_str += ' local="True"'
229 xml_str += ' localPath="' + urllib.quote(self.localPath).replace('%3A', ':') + '"' 217 xml_str += ' localPath="' + urllib.quote(self.localPath).replace('%3A', ':') + '"'
230 xml_str += ' localTime="' + str(self.localTime) + '"' 218 xml_str += ' localTime="' + str(self.localTime) + '"'
231 xml_str += "/>" 219 xml_str += "/>"
232 self.log.log(xml_str, ORPG_DEBUG) 220 logger.debug(xml_str)
233 self.log.log("Exit layer_back_ground->layerToXML(self, " + action + ")", ORPG_DEBUG)
234 if (action == "update" and self.isUpdated) or action == "new": 221 if (action == "update" and self.isUpdated) or action == "new":
235 self.isUpdated = False 222 self.isUpdated = False
236 return xml_str 223 return xml_str
237 else: return '' 224 else: return ''
238 225
226 @debugging
239 def layerTakeDOM(self, xml_dom): 227 def layerTakeDOM(self, xml_dom):
240 self.log.log("Enter layer_back_ground->layerTakeDOM(self, xml_dom)", ORPG_DEBUG)
241 type = BG_COLOR 228 type = BG_COLOR
242 color = xml_dom.getAttribute("color") 229 color = xml_dom.getAttribute("color")
243 self.log.log("color=" + color, ORPG_DEBUG) 230 logger.debug("color=" + color)
244 path = urllib.unquote(xml_dom.getAttribute("path")) 231 path = urllib.unquote(xml_dom.getAttribute("path"))
245 self.log.log("path=" + path, ORPG_DEBUG) 232 logger.debug("path=" + path)
246
247 # Begin ted's map changes 233 # Begin ted's map changes
248 if xml_dom.hasAttribute("color"): 234 if xml_dom.hasAttribute("color"):
249 r,g,b = self.r_h.rgb_tuple(xml_dom.getAttribute("color")) 235 r,g,b = self.r_h.rgb_tuple(xml_dom.getAttribute("color"))
250 self.set_color(cmpColour(r,g,b)) 236 self.set_color(cmpColour(r,g,b))
251 # End ted's map changes 237 # End ted's map changes
252 if xml_dom.hasAttribute("type"): 238 if xml_dom.hasAttribute("type"):
253 type = int(xml_dom.getAttribute("type")) 239 type = int(xml_dom.getAttribute("type"))
254 self.log.log("type=" + str(type), ORPG_DEBUG) 240 logger.debug("type=" + str(type))
255
256 if type == BG_TEXTURE: 241 if type == BG_TEXTURE:
257 if path != "": self.set_texture(path) 242 if path != "": self.set_texture(path)
258
259 elif type == BG_IMAGE: 243 elif type == BG_IMAGE:
260 if path != "": self.set_image(path, 1) 244 if path != "": self.set_image(path, 1)
261
262 elif type == BG_NONE: self.clear() 245 elif type == BG_NONE: self.clear()
263
264 if xml_dom.hasAttribute('local') and xml_dom.getAttribute('local') == 'True' and os.path.exists(urllib.unquote(xml_dom.getAttribute('localPath'))): 246 if xml_dom.hasAttribute('local') and xml_dom.getAttribute('local') == 'True' and os.path.exists(urllib.unquote(xml_dom.getAttribute('localPath'))):
265 self.localPath = urllib.unquote(xml_dom.getAttribute('localPath')) 247 self.localPath = urllib.unquote(xml_dom.getAttribute('localPath'))
266 self.local = True 248 self.local = True
267 self.localTime = int(xml_dom.getAttribute('localTime')) 249 self.localTime = int(xml_dom.getAttribute('localTime'))
268 if self.localTime-time.time() <= 144000: 250 if self.localTime-time.time() <= 144000:
271 file.close() 253 file.close()
272 filename = os.path.split(self.localPath) 254 filename = os.path.split(self.localPath)
273 (imgtype,j) = mimetypes.guess_type(filename[1]) 255 (imgtype,j) = mimetypes.guess_type(filename[1])
274 postdata = urllib.urlencode({'filename':filename[1], 'imgdata':imgdata, 'imgtype':imgtype}) 256 postdata = urllib.urlencode({'filename':filename[1], 'imgdata':imgdata, 'imgtype':imgtype})
275 thread.start_new_thread(self.upload, (postdata, self.localPath, type)) 257 thread.start_new_thread(self.upload, (postdata, self.localPath, type))
276 self.log.log("Exit layer_back_ground->layerTakeDOM(self, xml_dom)", ORPG_DEBUG) 258
277 259 @debugging
278 def upload(self, postdata, filename, type): 260 def upload(self, postdata, filename, type):
279 self.lock.acquire() 261 self.lock.acquire()
280 if type == 'Image' or type == 'Texture': 262 if type == 'Image' or type == 'Texture':
281 url = self.settings.get_setting('ImageServerBaseURL') 263 url = component.get('settings').get_setting('ImageServerBaseURL')
282 file = urllib.urlopen(url, postdata) 264 file = urllib.urlopen(url, postdata)
283 recvdata = file.read() 265 recvdata = file.read()
284 file.close() 266 file.close()
285 try: 267 try:
286 xml_dom = minidom.parseString(recvdata)._get_documentElement() 268 xml_dom = minidom.parseString(recvdata)._get_documentElement()