comparison orpg/mapper/background.py @ 20:072ffc1d466f traipse_dev

2nd attempt. Still untested.
author sirebral
date Sat, 25 Jul 2009 19:23:25 -0500
parents 4385a7d0efd1
children 37a11fea3304
comparison
equal deleted inserted replaced
19:78407d627cba 20:072ffc1d466f
107 try: 107 try:
108 self.bg_bmp = ImageHandler.load(path, "texture", 0) 108 self.bg_bmp = ImageHandler.load(path, "texture", 0)
109 if self.bg_bmp == None: 109 if self.bg_bmp == None:
110 self.log.log("Invalid image type!", ORPG_GENERAL) 110 self.log.log("Invalid image type!", ORPG_GENERAL)
111 raise Exception, "Invalid image type!" 111 raise Exception, "Invalid image type!"
112 except: 112 except: self.error_loading_image(path)
113 self.error_loading_image(path)
114 self.img_path = path 113 self.img_path = path
115 self.log.log("Enter layer_back_ground->set_texture(self, path)", ORPG_DEBUG) 114 self.log.log("Enter layer_back_ground->set_texture(self, path)", ORPG_DEBUG)
116 115
117 def set_image(self, path, scale): 116 def set_image(self, path, scale):
118 self.log.log("Enter layer_back_ground->set_image(self, path, scale)", ORPG_DEBUG) 117 self.log.log("Enter layer_back_ground->set_image(self, path, scale)", ORPG_DEBUG)
123 self.bg_bmp = ImageHandler.load(path, "background", 0) 122 self.bg_bmp = ImageHandler.load(path, "background", 0)
124 try: 123 try:
125 if self.bg_bmp == None: 124 if self.bg_bmp == None:
126 self.log.log("Invalid image type!", ORPG_GENERAL) 125 self.log.log("Invalid image type!", ORPG_GENERAL)
127 raise Exception, "Invalid image type!" 126 raise Exception, "Invalid image type!"
128 except: 127 except: self.error_loading_image(path)
129 self.error_loading_image(path)
130 self.img_path = path 128 self.img_path = path
131 self.log.log("Exit layer_back_ground->set_image(self, path, scale)", ORPG_DEBUG) 129 self.log.log("Exit layer_back_ground->set_image(self, path, scale)", ORPG_DEBUG)
132 return (self.bg_bmp.GetWidth(),self.bg_bmp.GetHeight()) 130 return (self.bg_bmp.GetWidth(),self.bg_bmp.GetHeight())
133 131
134 def set_color(self, color): 132 def set_color(self, color):
147 return False 145 return False
148 dc2 = wx.MemoryDC() 146 dc2 = wx.MemoryDC()
149 dc2.SelectObject(self.bg_bmp) 147 dc2.SelectObject(self.bg_bmp)
150 topLeft = [int(topleft[0]/scale), int(topleft[1]/scale)] 148 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] 149 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]): 150 if (topRight[0] > self.canvas.size[0]): topRight[0] = self.canvas.size[0]
153 topRight[0] = self.canvas.size[0] 151 if (topRight[1] > self.canvas.size[1]): topRight[1] = self.canvas.size[1]
154 if (topRight[1] > self.canvas.size[1]):
155 topRight[1] = self.canvas.size[1]
156 bmpW = self.bg_bmp.GetWidth() 152 bmpW = self.bg_bmp.GetWidth()
157 bmpH = self.bg_bmp.GetHeight() 153 bmpH = self.bg_bmp.GetHeight()
158 if self.type == BG_TEXTURE: 154 if self.type == BG_TEXTURE:
159 x = (topLeft[0]/bmpW)*bmpW 155 x = (topLeft[0]/bmpW)*bmpW
160 y1 = int(topLeft[1]/bmpH)*bmpH 156 y1 = int(topLeft[1]/bmpH)*bmpH
163 cl = topLeft[0]-x 159 cl = topLeft[0]-x
164 else: 160 else:
165 cl = 0 161 cl = 0
166 posx = x 162 posx = x
167 while x < topRight[0]: 163 while x < topRight[0]:
168 if x+bmpW > topRight[0]: 164 if x+bmpW > topRight[0]: cr = x+bmpW-topRight[0]
169 cr = x+bmpW-topRight[0] 165 else: cr = 0
170 else:
171 cr = 0
172 y = int(topLeft[1]/bmpH)*bmpH 166 y = int(topLeft[1]/bmpH)*bmpH
173 if y < topLeft[1]: 167 if y < topLeft[1]:
174 posy = topLeft[1] 168 posy = topLeft[1]
175 ct = topLeft[1]-y 169 ct = topLeft[1]-y
176 else: 170 else:
177 ct = 0 171 ct = 0
178 posy = y 172 posy = y
179 while y < topRight[1]: 173 while y < topRight[1]:
180 if y+bmpH > topRight[1]: 174 if y+bmpH > topRight[1]: cb = y+bmpH-topRight[1]
181 cb = y+bmpH-topRight[1] 175 else: cb = 0
182 else:
183 cb = 0
184 newW = bmpW-cr-cl 176 newW = bmpW-cr-cl
185 newH = bmpH-cb-ct 177 newH = bmpH-cb-ct
186 if newW < 0: 178 if newW < 0: newW = 0
187 newW = 0 179 if newH < 0: newH = 0
188 if newH < 0:
189 newH = 0
190 dc.DrawBitmap(self.bg_bmp, posx, posy) 180 dc.DrawBitmap(self.bg_bmp, posx, posy)
191 dc.Blit(posx, posy, newW, newH, dc2, cl, ct) 181 dc.Blit(posx, posy, newW, newH, dc2, cl, ct)
192 ct = 0 182 ct = 0
193 y = y+bmpH 183 y = y+bmpH
194 posy = y 184 posy = y
202 posx = topLeft[0] 192 posx = topLeft[0]
203 cl = topLeft[0]-x 193 cl = topLeft[0]-x
204 else: 194 else:
205 cl = 0 195 cl = 0
206 posx = x 196 posx = x
207
208 if y < topLeft[1]: 197 if y < topLeft[1]:
209 posy = topLeft[1] 198 posy = topLeft[1]
210 ct = topLeft[1]-y 199 ct = topLeft[1]-y
211 else: 200 else:
212 ct = 0 201 ct = 0
213 posy = y 202 posy = y
214 if x+bmpW > topRight[0]: 203 if x+bmpW > topRight[0]: cr = x+bmpW-topRight[0]
215 cr = x+bmpW-topRight[0] 204 else: cr = 0
216 else: 205 if y+bmpH > topRight[1]: cb = y+bmpH-topRight[1]
217 cr = 0 206 else: cb = 0
218 if y+bmpH > topRight[1]:
219 cb = y+bmpH-topRight[1]
220 else:
221 cb = 0
222 newW = bmpW-cr-cl 207 newW = bmpW-cr-cl
223 newH = bmpH-cb-ct 208 newH = bmpH-cb-ct
224 if newW < 0: 209 if newW < 0: newW = 0
225 newW = 0 210 if newH < 0: newH = 0
226 if newH < 0:
227 newH = 0
228 dc.DrawBitmap(self.bg_bmp, posx, posy) 211 dc.DrawBitmap(self.bg_bmp, posx, posy)
229 dc.Blit(posx, posy, newW, newH, dc2, cl, ct) 212 dc.Blit(posx, posy, newW, newH, dc2, cl, ct)
230 dc2.SelectObject(wx.NullBitmap) 213 dc2.SelectObject(wx.NullBitmap)
231 del dc2 214 del dc2
232 self.log.log("Exit layer_back_ground->layerDraw(self, dc, scale, topleft, size)", ORPG_DEBUG) 215 self.log.log("Exit layer_back_ground->layerDraw(self, dc, scale, topleft, size)", ORPG_DEBUG)
237 xml_str = "<bg" 220 xml_str = "<bg"
238 if self.bg_color != None: 221 if self.bg_color != None:
239 (red,green,blue) = self.bg_color.Get() 222 (red,green,blue) = self.bg_color.Get()
240 hexcolor = self.r_h.hexstring(red, green, blue) 223 hexcolor = self.r_h.hexstring(red, green, blue)
241 xml_str += ' color="' + hexcolor + '"' 224 xml_str += ' color="' + hexcolor + '"'
242 if self.img_path != None: 225 if self.img_path != None: xml_str += ' path="' + urllib.quote(self.img_path).replace('%3A', ':') + '"'
243 xml_str += ' path="' + urllib.quote(self.img_path).replace('%3A', ':') + '"' 226 if self.type != None: xml_str += ' type="' + str(self.type) + '"'
244 if self.type != None:
245 xml_str += ' type="' + str(self.type) + '"'
246 if self.local and self.img_path != None: 227 if self.local and self.img_path != None:
247 xml_str += ' local="True"' 228 xml_str += ' local="True"'
248 xml_str += ' localPath="' + urllib.quote(self.localPath).replace('%3A', ':') + '"' 229 xml_str += ' localPath="' + urllib.quote(self.localPath).replace('%3A', ':') + '"'
249 xml_str += ' localTime="' + str(self.localTime) + '"' 230 xml_str += ' localTime="' + str(self.localTime) + '"'
250 xml_str += "/>" 231 xml_str += "/>"
251 self.log.log(xml_str, ORPG_DEBUG) 232 self.log.log(xml_str, ORPG_DEBUG)
252 self.log.log("Exit layer_back_ground->layerToXML(self, " + action + ")", ORPG_DEBUG) 233 self.log.log("Exit layer_back_ground->layerToXML(self, " + action + ")", ORPG_DEBUG)
253 if (action == "update" and self.isUpdated) or action == "new": 234 if (action == "update" and self.isUpdated) or action == "new":
254 self.isUpdated = False 235 self.isUpdated = False
255 return xml_str 236 return xml_str
256 else: 237 else: return ''
257 return ''
258 238
259 def layerTakeDOM(self, xml_dom): 239 def layerTakeDOM(self, xml_dom):
260 self.log.log("Enter layer_back_ground->layerTakeDOM(self, xml_dom)", ORPG_DEBUG) 240 self.log.log("Enter layer_back_ground->layerTakeDOM(self, xml_dom)", ORPG_DEBUG)
261 type = BG_COLOR 241 type = BG_COLOR
262 color = xml_dom.getAttribute("color") 242 color = xml_dom.getAttribute("color")
272 if xml_dom.hasAttribute("type"): 252 if xml_dom.hasAttribute("type"):
273 type = int(xml_dom.getAttribute("type")) 253 type = int(xml_dom.getAttribute("type"))
274 self.log.log("type=" + str(type), ORPG_DEBUG) 254 self.log.log("type=" + str(type), ORPG_DEBUG)
275 255
276 if type == BG_TEXTURE: 256 if type == BG_TEXTURE:
277 if path != "": 257 if path != "": self.set_texture(path)
278 self.set_texture(path)
279 258
280 elif type == BG_IMAGE: 259 elif type == BG_IMAGE:
281 if path != "": 260 if path != "": self.set_image(path, 1)
282 self.set_image(path, 1) 261
283 262 elif type == BG_NONE: self.clear()
284 elif type == BG_NONE:
285 self.clear()
286 263
287 if xml_dom.hasAttribute('local') and xml_dom.getAttribute('local') == 'True' and os.path.exists(urllib.unquote(xml_dom.getAttribute('localPath'))): 264 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')) 265 self.localPath = urllib.unquote(xml_dom.getAttribute('localPath'))
289 self.local = True 266 self.local = True
290 self.localTime = int(xml_dom.getAttribute('localTime')) 267 self.localTime = int(xml_dom.getAttribute('localTime'))
308 try: 285 try:
309 xml_dom = minidom.parseString(recvdata)._get_documentElement() 286 xml_dom = minidom.parseString(recvdata)._get_documentElement()
310 if xml_dom.nodeName == 'path': 287 if xml_dom.nodeName == 'path':
311 path = xml_dom.getAttribute('url') 288 path = xml_dom.getAttribute('url')
312 path = urllib.unquote(path) 289 path = urllib.unquote(path)
313 290 if type == 'Image': self.set_image(path, 1)
314 if type == 'Image': 291 else: self.set_texture(path)
315 self.set_image(path, 1)
316 else:
317 self.set_texture(path)
318
319 self.localPath = filename 292 self.localPath = filename
320 self.local = True 293 self.local = True
321 self.localTime = time.time() 294 self.localTime = time.time()
322 else: 295 else:
323 print xml_dom.getAttribute('msg') 296 print xml_dom.getAttribute('msg')