comparison orpg/mapper/fog.py @ 71:449a8900f9ac ornery-dev

Code refining almost completed, for this round. Some included files are still in need of some clean up, but this is test worthy.
author sirebral
date Thu, 20 Aug 2009 03:00:39 -0500
parents 072ffc1d466f
children f38df4bf9715
comparison
equal deleted inserted replaced
70:52a5fa913008 71:449a8900f9ac
31 import traceback 31 import traceback
32 COURSE = 10 32 COURSE = 10
33 33
34 class FogArea: 34 class FogArea:
35 def __init__(self, outline, log): 35 def __init__(self, outline, log):
36 self.log = log
37 self.log.log("Enter FogArea", ORPG_DEBUG)
38 self.outline = outline 36 self.outline = outline
39 self.log.log("Exit FogArea", ORPG_DEBUG)
40 37
41 def set_fog_props(self, str): 38 def set_fog_props(self, str):
42 self.log.log("Enter FogArea->set_fog_props(self, str)", ORPG_DEBUG)
43 self.outline = str 39 self.outline = str
44 self.log.log("Exit FogArea->set_fog_props(self, str)", ORPG_DEBUG)
45 40
46 def points_to_elements(self, points=None): 41 def points_to_elements(self, points=None):
47 self.log.log("Enter FogArea->points_to_elements(self, points)", ORPG_DEBUG)
48 result = [] 42 result = []
49 if points == None: 43 if points == None:
50 self.log.log("Exit FogArea->points_to_elements(self, points)", ORPG_DEBUG)
51 return result 44 return result
45
52 for pairs in string.split( points, ';' ): 46 for pairs in string.split( points, ';' ):
53 pair = string.split( pairs, ',' ) 47 pair = string.split( pairs, ',' )
54 p = Element( "point" ) 48 p = Element( "point" )
55 p.setAttribute( "x", pair[0] ) 49 p.setAttribute( "x", pair[0] )
56 p.setAttribute( "y", pair[1] ) 50 p.setAttribute( "y", pair[1] )
57 result.append( p ) 51 result.append( p )
58 self.log.log("Exit FogArea->points_to_elements(self, points)", ORPG_DEBUG)
59 return result 52 return result
60 53
61 def toxml(self, action="update"): 54 def toxml(self, action="update"):
62 self.log.log("Enter FogArea->toxml(self, " + action + ")", ORPG_DEBUG)
63 xml_str = "" 55 xml_str = ""
64 localOutline = self.outline 56 localOutline = self.outline
65 if localOutline != None and localOutline != "all" and localOutline != "none": 57 if localOutline != None and localOutline != "all" and localOutline != "none":
66 localOutline = "points" 58 localOutline = "points"
67 elem = Element( "poly" ) 59 elem = Element( "poly" )
71 if localOutline == 'points': 63 if localOutline == 'points':
72 list = self.points_to_elements( self.outline ) 64 list = self.points_to_elements( self.outline )
73 for p in list: elem.appendChild( p ) 65 for p in list: elem.appendChild( p )
74 str = elem.toxml() 66 str = elem.toxml()
75 elem.unlink() 67 elem.unlink()
76 self.log.log(str, ORPG_DEBUG)
77 self.log.log("Exit FogArea->toxml(self, " + action + ")", ORPG_DEBUG)
78 return str 68 return str
79 elem.setAttribute( "action", action ) 69 elem.setAttribute( "action", action )
80 if localOutline != None: 70 if localOutline != None:
81 elem.setAttribute( "outline", localOutline ) 71 elem.setAttribute( "outline", localOutline )
82 if localOutline == 'points': 72 if localOutline == 'points':
83 list = self.points_to_elements( self.outline ) 73 list = self.points_to_elements( self.outline )
84 for p in list: elem.appendChild( p ) 74 for p in list: elem.appendChild( p )
85 xml_str = elem.toxml() 75 xml_str = elem.toxml()
86 elem.unlink() 76 elem.unlink()
87 self.log.log(xml_str, ORPG_DEBUG)
88 self.log.log("Exit FogArea->toxml(self, " + action + ")", ORPG_DEBUG)
89 return xml_str 77 return xml_str
90 78
91 class fog_layer(layer_base): 79 class fog_layer(layer_base):
92 def __init__(self, canvas): 80 def __init__(self, canvas):
93 self.canvas = canvas 81 self.canvas = canvas
94 self.log = self.canvas.log
95 self.log.log("Enter fog_layer", ORPG_DEBUG)
96 layer_base.__init__(self) 82 layer_base.__init__(self)
97 self.color = wx.Color(128,128,128) 83 self.color = wx.Color(128,128,128)
98 if "__WXGTK__" not in wx.PlatformInfo: self.color = wx.Color(128,128,128, 128) 84 if "__WXGTK__" not in wx.PlatformInfo: self.color = wx.Color(128,128,128, 128)
99 self.fogregion = wx.Region() 85 self.fogregion = wx.Region()
100 self.fogregion.Clear() 86 self.fogregion.Clear()
101 self.fog_bmp = None 87 self.fog_bmp = None
102 self.width = 0 88 self.width = 0
103 self.height = 0 89 self.height = 0
104 self.use_fog = False 90 self.use_fog = False
105 self.last_role = "" 91 self.last_role = ""
106 self.log.log("Exit fog_layer", ORPG_DEBUG)
107 92
108 def clear(self): 93 def clear(self):
109 self.log.log("Enter fog_layer->clear(self)", ORPG_DEBUG)
110 self.fogregion.Clear() 94 self.fogregion.Clear()
111 self.use_fog = True 95 self.use_fog = True
112 self.del_area("all") 96 self.del_area("all")
113 self.recompute_fog() 97 self.recompute_fog()
114 self.log.log("Exit fog_layer->clear(self)", ORPG_DEBUG)
115 98
116 def remove_fog(self): 99 def remove_fog(self):
117 self.log.log("Enter fog_layer->remove_fog(self)", ORPG_DEBUG)
118 self.fogregion.Clear() 100 self.fogregion.Clear()
119 self.use_fog = False 101 self.use_fog = False
120 self.del_area("all") 102 self.del_area("all")
121 self.add_area("none") 103 self.add_area("none")
122 self.fog_bmp = None 104 self.fog_bmp = None
123 self.log.log("Exit fog_layer->remove_fog(self)", ORPG_DEBUG)
124 105
125 def resize(self, size): 106 def resize(self, size):
126 self.log.log("Enter fog_layer->resize(self, size)", ORPG_DEBUG)
127 try: 107 try:
128 if self.width == size[0] and self.height == size[1]: 108 if self.width == size[0] and self.height == size[1]:
129 self.log.log("Exit fog_layer->resize(self, size)", ORPG_DEBUG)
130 return 109 return
131 self.recompute_fog() 110 self.recompute_fog()
132 except: pass 111 except: pass
133 self.log.log("Exit fog_layer->resize(self, size)", ORPG_DEBUG)
134 112
135 def recompute_fog(self): 113 def recompute_fog(self):
136 self.log.log("Enter fog_layer->recompute_fog(self)", ORPG_DEBUG)
137 if not self.use_fog: 114 if not self.use_fog:
138 self.log.log("Exit fog_layer->recompute_fog(self)", ORPG_DEBUG)
139 return 115 return
140 size = self.canvas.size 116 size = self.canvas.size
141 self.width = size[0]/COURSE+1 117 self.width = size[0]/COURSE+1
142 self.height = size[1]/COURSE+1 118 self.height = size[1]/COURSE+1
143 self.fog_bmp = wx.EmptyBitmap(self.width+2,self.height+2) 119 self.fog_bmp = wx.EmptyBitmap(self.width+2,self.height+2)
144 self.fill_fog() 120 self.fill_fog()
145 self.log.log("Exit fog_layer->recompute_fog(self)", ORPG_DEBUG)
146 121
147 def fill_fog(self): 122 def fill_fog(self):
148 self.log.log("Enter fog_layer->fill_fog(self)", ORPG_DEBUG)
149 if not self.use_fog: 123 if not self.use_fog:
150 self.log.log("Exit fog_layer->fill_fog(self)", ORPG_DEBUG)
151 return 124 return
152 if "__WXGTK__" in wx.PlatformInfo: 125 if "__WXGTK__" in wx.PlatformInfo:
153 mdc = wx.MemoryDC() 126 mdc = wx.MemoryDC()
154 mdc.SelectObject(self.fog_bmp) 127 mdc.SelectObject(self.fog_bmp)
155 mdc.SetPen(wx.TRANSPARENT_PEN) 128 mdc.SetPen(wx.TRANSPARENT_PEN)
163 if self.fogregion.GetBox().GetWidth()>0: 136 if self.fogregion.GetBox().GetWidth()>0:
164 mdc.SetClippingRegionAsRegion(self.fogregion) 137 mdc.SetClippingRegionAsRegion(self.fogregion)
165 mdc.DrawRectangle(0, 0, self.width+2, self.height+2) 138 mdc.DrawRectangle(0, 0, self.width+2, self.height+2)
166 mdc.SelectObject(wx.NullBitmap) 139 mdc.SelectObject(wx.NullBitmap)
167 del mdc 140 del mdc
168 self.log.log("Exit fog_layer->fill_fog(self)", ORPG_DEBUG)
169 141
170 def layerDraw(self, dc, topleft, size): 142 def layerDraw(self, dc, topleft, size):
171 self.log.log("Enter fog_layer->layerDraw(self, dc, topleft, size)", ORPG_DEBUG)
172 if self.fog_bmp == None or not self.fog_bmp.Ok() or not self.use_fog: 143 if self.fog_bmp == None or not self.fog_bmp.Ok() or not self.use_fog:
173 self.log.log("Exit fog_layer->layerDraw(self, dc, topleft, size)", ORPG_DEBUG)
174 return 144 return
175 if self.last_role != self.canvas.frame.session.role: self.fill_fog() 145 if self.last_role != self.canvas.frame.session.role: self.fill_fog()
176 if "__WXGTK__" not in wx.PlatformInfo: 146 if "__WXGTK__" not in wx.PlatformInfo:
177 gc = wx.GraphicsContext.Create(dc) 147 gc = wx.GraphicsContext.Create(dc)
178 gc.SetBrush(wx.Brush(wx.BLACK)) 148 gc.SetBrush(wx.Brush(wx.BLACK))
207 dc.SetUserScale(1,1) 177 dc.SetUserScale(1,1)
208 dc.Blit(topleft[0], topleft[1], size[0], size[1], mdc,0,0,wx.AND) 178 dc.Blit(topleft[0], topleft[1], size[0], size[1], mdc,0,0,wx.AND)
209 dc.SetUserScale(sc[0],sc[1]) 179 dc.SetUserScale(sc[0],sc[1])
210 mdc.SelectObject(wx.NullBitmap) 180 mdc.SelectObject(wx.NullBitmap)
211 del mdc 181 del mdc
212 self.log.log("Exit fog_layer->layerDraw(self, dc, topleft, size)", ORPG_DEBUG)
213 182
214 def createregn2(self, polyline, mode, show): 183 def createregn2(self, polyline, mode, show):
215 self.log.log("Enter fog_layer->createregn2(self, polyline, mode, show)", ORPG_DEBUG)
216 regn = self.scanConvert(polyline) 184 regn = self.scanConvert(polyline)
217 area = "" 185 area = ""
218 for i in polyline: 186 for i in polyline:
219 if (area != ""): 187 if (area != ""):
220 area += ";" 188 area += ";"
229 self.fogregion.SubtractRegion(regn) 197 self.fogregion.SubtractRegion(regn)
230 else: 198 else:
231 self.fogregion = wx.Region(0, 0, self.canvas.size[0]+2, self.canvas.size[1]+2) 199 self.fogregion = wx.Region(0, 0, self.canvas.size[0]+2, self.canvas.size[1]+2)
232 self.fogregion.SubtractRegion(regn) 200 self.fogregion.SubtractRegion(regn)
233 self.del_area(area, show) 201 self.del_area(area, show)
234 self.log.log("Exit fog_layer->createregn2(self, polyline, mode, show)", ORPG_DEBUG)
235 202
236 def createregn(self, polyline, mode, show="Yes"): 203 def createregn(self, polyline, mode, show="Yes"):
237 self.log.log("Enter fog_layer->createregn(self, polyline, mode, show)", ORPG_DEBUG)
238 if not self.use_fog and mode == 'del': 204 if not self.use_fog and mode == 'del':
239 self.clear() 205 self.clear()
240 self.canvas.Refresh(False) 206 self.canvas.Refresh(False)
241 if self.use_fog: 207 if self.use_fog:
242 self.createregn2(polyline, mode, show) 208 self.createregn2(polyline, mode, show)
243 self.fill_fog() 209 self.fill_fog()
244 self.log.log("Exit fog_layer->createregn(self, polyline, mode, show)", ORPG_DEBUG)
245 210
246 def scanConvert(self, polypt): 211 def scanConvert(self, polypt):
247 self.log.log("Enter fog_layer->scanConvert(self, polypt)", ORPG_DEBUG)
248 regn = wx.Region() 212 regn = wx.Region()
249 regn.Clear() 213 regn.Clear()
250 list = IRegion().scan_Convert(polypt) 214 list = IRegion().scan_Convert(polypt)
251 for i in list: 215 for i in list:
252 if regn.IsEmpty(): 216 if regn.IsEmpty():
253 if "__WXGTK__" not in wx.PlatformInfo: 217 if "__WXGTK__" not in wx.PlatformInfo: regn = wx.Region(i.left*COURSE, i.y*COURSE,
254 regn = wx.Region(i.left*COURSE, i.y*COURSE, i.right*COURSE+1-i.left*COURSE, 1*COURSE) 218 i.right*COURSE+1-i.left*COURSE, 1*COURSE)
255 else: regn = wx.Region(i.left, i.y, i.right+1-i.left, 1) 219 else: regn = wx.Region(i.left, i.y, i.right+1-i.left, 1)
256 else: 220 else:
257 if "__WXGTK__" not in wx.PlatformInfo: 221 if "__WXGTK__" not in wx.PlatformInfo: regn.Union(i.left*COURSE, i.y*COURSE,
258 regn.Union(i.left*COURSE, i.y*COURSE, i.right*COURSE+1-i.left*COURSE, 1*COURSE) 222 i.right*COURSE+1-i.left*COURSE, 1*COURSE)
259 else: regn.Union(i.left, i.y, i.right+1-i.left, 1) 223 else: regn.Union(i.left, i.y, i.right+1-i.left, 1)
260 self.log.log("Exit fog_layer->scanConvert(self, polypt)", ORPG_DEBUG)
261 return regn 224 return regn
262 225
263 def add_area(self, area="", show="Yes"): 226 def add_area(self, area="", show="Yes"):
264 self.log.log("Enter fog_layer->add_area(self, area, show)", ORPG_DEBUG)
265 poly = FogArea(area, self.log) 227 poly = FogArea(area, self.log)
266 xml_str = "<map><fog>" 228 xml_str = "<map><fog>"
267 xml_str += poly.toxml("new") 229 xml_str += poly.toxml("new")
268 xml_str += "</fog></map>" 230 xml_str += "</fog></map>"
269 if show == "Yes": self.canvas.frame.session.send(xml_str) 231 if show == "Yes": self.canvas.frame.session.send(xml_str)
270 self.log.log(xml_str, ORPG_DEBUG)
271 self.log.log("Exit fog_layer->add_area(self, area, show)", ORPG_DEBUG)
272 232
273 def del_area(self, area="", show="Yes"): 233 def del_area(self, area="", show="Yes"):
274 self.log.log("Enter fog_layer->del_area(self, area, show)", ORPG_DEBUG)
275 poly = FogArea(area, self.log) 234 poly = FogArea(area, self.log)
276 xml_str = "<map><fog>" 235 xml_str = "<map><fog>"
277 xml_str += poly.toxml("del") 236 xml_str += poly.toxml("del")
278 xml_str += "</fog></map>" 237 xml_str += "</fog></map>"
279 if show == "Yes": self.canvas.frame.session.send(xml_str) 238 if show == "Yes": self.canvas.frame.session.send(xml_str)
280 self.log.log(xml_str, ORPG_DEBUG)
281 self.log.log("Exit fog_layer->del_area(self, area, show)", ORPG_DEBUG)
282 239
283 def layerToXML(self, action="update"): 240 def layerToXML(self, action="update"):
284 self.log.log("Enter fog_layer->layerToXML(self, " + action + ")", ORPG_DEBUG)
285 if not self.use_fog: 241 if not self.use_fog:
286 self.log.log("Exit fog_layer->layerToXML(self, " + action + ") return None", ORPG_DEBUG)
287 return "" 242 return ""
288 fog_string = "" 243 fog_string = ""
289 ri = wx.RegionIterator(self.fogregion) 244 ri = wx.RegionIterator(self.fogregion)
290 if not (ri.HaveRects()): fog_string = FogArea("all", self.log).toxml("del") 245 if not (ri.HaveRects()): fog_string = FogArea("all", self.log).toxml("del")
291 while ri.HaveRects(): 246 while ri.HaveRects():
308 if fog_string: 263 if fog_string:
309 s = "<fog" 264 s = "<fog"
310 s += ">" 265 s += ">"
311 s += fog_string 266 s += fog_string
312 s += "</fog>" 267 s += "</fog>"
313 self.log.log(s, ORPG_DEBUG)
314 self.log.log("Exit fog_layer->layerToXML(self, " + action + ")", ORPG_DEBUG)
315 return s 268 return s
316 else: 269 else:
317 self.log.log("Exit fog_layer->layerToXML(self, " + action + ") return None", ORPG_DEBUG)
318 return "" 270 return ""
319 271
320 def layerTakeDOM(self, xml_dom): 272 def layerTakeDOM(self, xml_dom):
321 self.log.log("Enter fog_layer->layerTakeDOM(self, xml_dom)", ORPG_DEBUG)
322 try: 273 try:
323 if not self.use_fog: 274 if not self.use_fog:
324 self.use_fog = True 275 self.use_fog = True
325 self.recompute_fog() 276 self.recompute_fog()
326 if xml_dom.hasAttribute('serial'): self.serial_number = int(xml_dom.getAttribute('serial')) 277 if xml_dom.hasAttribute('serial'): self.serial_number = int(xml_dom.getAttribute('serial'))
348 polyline.append(IPoint().make(int(x), int(y))) 299 polyline.append(IPoint().make(int(x), int(y)))
349 lastx = x 300 lastx = x
350 lasty = y 301 lasty = y
351 if (len(polyline) > 1): self.createregn2(polyline, action, "No") 302 if (len(polyline) > 1): self.createregn2(polyline, action, "No")
352 self.fill_fog() 303 self.fill_fog()
353 except: self.log.log(traceback.format_exc(), ORPG_GENERAL) 304 except: pass
354 self.log.log("Exit fog_layer->layerTakeDOM(self, xml_dom)", ORPG_DEBUG)