comparison orpg/mapper/miniatures.py @ 19:78407d627cba traipse_dev

Beginning of Code Clean-up. Some code untested, may break!
author sirebral
date Sat, 25 Jul 2009 17:24:40 -0500
parents 2b9e766f9dee
children 072ffc1d466f
comparison
equal deleted inserted replaced
18:67b900deb35c 19:78407d627cba
67 else: 67 else:
68 value = 1 68 value = 1
69 return value 69 return value
70 70
71 class BmpMiniature: 71 class BmpMiniature:
72 def __init__(self, id,path, bmp, pos=cmpPoint(0,0), heading=FACE_NONE, face=FACE_NONE, label="", locked=False, hide=False, snap_to_align=SNAPTO_ALIGN_CENTER, zorder=0, width=0, height=0, log=None, local=False, localPath='', localTime=-1): 72 def __init__(self, id,path, bmp, pos=cmpPoint(0,0), heading=FACE_NONE, face=FACE_NONE, label="",
73 locked=False, hide=False, snap_to_align=SNAPTO_ALIGN_CENTER, zorder=0, width=0, height=0,
74 log=None, local=False, localPath='', localTime=-1):
73 self.log = log 75 self.log = log
74 self.log.log("Enter BmpMiniature", ORPG_DEBUG) 76 self.log.log("Enter BmpMiniature", ORPG_DEBUG)
75 self.heading = heading 77 self.heading = heading
76 self.face = face 78 self.face = face
77 self.label = label 79 self.label = label
113 self.log.log("Enter BmpMiniature->set_bmp(self, bmp)", ORPG_DEBUG) 115 self.log.log("Enter BmpMiniature->set_bmp(self, bmp)", ORPG_DEBUG)
114 self.bmp = bmp 116 self.bmp = bmp
115 self.log.log("Exit BmpMiniature->set_bmp(self, bmp)", ORPG_DEBUG) 117 self.log.log("Exit BmpMiniature->set_bmp(self, bmp)", ORPG_DEBUG)
116 118
117 def set_min_props(self, heading=FACE_NONE, face=FACE_NONE, label="", locked=False, hide=False, width=0, height=0): 119 def set_min_props(self, heading=FACE_NONE, face=FACE_NONE, label="", locked=False, hide=False, width=0, height=0):
118 self.log.log("Enter BmpMiniature->set_min_props(self, heading, face, label, locked, hide, width, height)", ORPG_DEBUG) 120 self.log.log("Enter BmpMiniature->set_min_props(self, heading, face, label, locked, hide, width, height)",
121 ORPG_DEBUG)
119 self.heading = heading 122 self.heading = heading
120 self.face = face 123 self.face = face
121 self.label = label 124 self.label = label
122 if locked: 125 if locked:
123 self.locked = True 126 self.locked = True
128 else: 131 else:
129 self.hide = False 132 self.hide = False
130 self.width = int(width) 133 self.width = int(width)
131 self.height = int(height) 134 self.height = int(height)
132 self.isUpdated = True 135 self.isUpdated = True
133 self.log.log("Exit BmpMiniature->set_min_props(self, heading, face, label, locked, hide, width, height)", ORPG_DEBUG) 136 self.log.log("Exit BmpMiniature->set_min_props(self, heading, face, label, locked, hide, width, height)",
137 ORPG_DEBUG)
134 138
135 def hit_test(self, pt): 139 def hit_test(self, pt):
136 self.log.log("Enter BmpMiniature->hit_test(self, pt)", ORPG_DEBUG) 140 self.log.log("Enter BmpMiniature->hit_test(self, pt)", ORPG_DEBUG)
137 rect = self.get_rect() 141 rect = self.get_rect()
138 result = None 142 result = None
160 if self.width and self.height: 164 if self.width and self.height:
161 tmp_image = self.bmp.ConvertToImage() 165 tmp_image = self.bmp.ConvertToImage()
162 tmp_image.Rescale(int(self.width), int(self.height)) 166 tmp_image.Rescale(int(self.width), int(self.height))
163 tmp_image.ConvertAlphaToMask() 167 tmp_image.ConvertAlphaToMask()
164 self.bmp = tmp_image.ConvertToBitmap() 168 self.bmp = tmp_image.ConvertToBitmap()
165 mask = wx.Mask(self.bmp, wx.Colour(tmp_image.GetMaskRed(), tmp_image.GetMaskGreen(), tmp_image.GetMaskBlue())) 169 mask = wx.Mask(self.bmp, wx.Colour(tmp_image.GetMaskRed(),
170 tmp_image.GetMaskGreen(), tmp_image.GetMaskBlue()))
166 self.bmp.SetMask(mask) 171 self.bmp.SetMask(mask)
167 del tmp_image 172 del tmp_image
168 del mask 173 del mask
169 self.left = 0 174 self.left = 0
170 self.right = self.bmp.GetWidth() 175 self.right = self.bmp.GetWidth()
181 xoffset = 5 186 xoffset = 5
182 if self.bmp.GetHeight() <= 20: 187 if self.bmp.GetHeight() <= 20:
183 yoffset = 1 188 yoffset = 1
184 else: 189 else:
185 yoffset = 5 190 yoffset = 5
186 dc.DrawRectangle(self.pos.x + xoffset, self.pos.y + yoffset, self.bmp.GetWidth() - (xoffset * 2), self.bmp.GetHeight() - (yoffset * 2)) 191 dc.DrawRectangle(self.pos.x + xoffset,
192 self.pos.y + yoffset, self.bmp.GetWidth() - (xoffset * 2),
193 self.bmp.GetHeight() - (yoffset * 2))
187 dc.SetBrush(wx.NullBrush) 194 dc.SetBrush(wx.NullBrush)
188 dc.SetPen(wx.NullPen) 195 dc.SetPen(wx.NullPen)
189 ## draw label in the center of the mini 196 ## draw label in the center of the mini
190 label = mini_layer.get_mini_label(self) 197 label = mini_layer.get_mini_label(self)
191 if len(label): 198 if len(label):
224 if self.width and self.height: 231 if self.width and self.height:
225 tmp_image = self.bmp.ConvertToImage() 232 tmp_image = self.bmp.ConvertToImage()
226 tmp_image.Rescale(int(self.width), int(self.height)) 233 tmp_image.Rescale(int(self.width), int(self.height))
227 tmp_image.ConvertAlphaToMask() 234 tmp_image.ConvertAlphaToMask()
228 self.bmp = tmp_image.ConvertToBitmap() 235 self.bmp = tmp_image.ConvertToBitmap()
229 mask = wx.Mask(self.bmp, wx.Colour(tmp_image.GetMaskRed(), tmp_image.GetMaskGreen(), tmp_image.GetMaskBlue())) 236 mask = wx.Mask(self.bmp, wx.Colour(tmp_image.GetMaskRed(),
237 tmp_image.GetMaskGreen(), tmp_image.GetMaskBlue()))
230 self.bmp.SetMask(mask) 238 self.bmp.SetMask(mask)
231 if self.gray: 239 if self.gray:
232 tmp_image = tmp_image.ConvertToGreyscale() 240 tmp_image = tmp_image.ConvertToGreyscale()
233 bmp = tmp_image.ConvertToBitmap() 241 bmp = tmp_image.ConvertToBitmap()
234 else: 242 else:
552 def rollback_serial(self): 560 def rollback_serial(self):
553 self.log.log("Enter miniature_layer->rollback_serial(self)", ORPG_DEBUG) 561 self.log.log("Enter miniature_layer->rollback_serial(self)", ORPG_DEBUG)
554 self.serial_number -= 1 562 self.serial_number -= 1
555 self.log.log("Exit miniature_layer->rollback_serial(self)", ORPG_DEBUG) 563 self.log.log("Exit miniature_layer->rollback_serial(self)", ORPG_DEBUG)
556 564
557 def add_miniature(self, id, path, pos=cmpPoint(0,0), label="", heading=FACE_NONE, face=FACE_NONE, width=0, height=0, local=False, localPath='', localTime=-1): 565 def add_miniature(self, id, path, pos=cmpPoint(0,0), label="", heading=FACE_NONE,
558 self.log.log("Enter miniature_layer->add_miniature(self, id, path, pos, label, heading, face, width, height)", ORPG_DEBUG) 566 face=FACE_NONE, width=0, height=0, local=False, localPath='', localTime=-1):
567 self.log.log("Enter miniature_layer->add_miniature(self, id, path, pos, label, heading, face, width, height)",
568 ORPG_DEBUG)
559 self.log.log("Before mini creation: " + str(self.get_next_highest_z()), ORPG_DEBUG) 569 self.log.log("Before mini creation: " + str(self.get_next_highest_z()), ORPG_DEBUG)
560 bmp = ImageHandler.load(path, 'miniature', id) 570 bmp = ImageHandler.load(path, 'miniature', id)
561 if bmp: 571 if bmp:
562 mini = BmpMiniature(id, path, bmp, pos, heading, face, label, zorder=self. get_next_highest_z(), width=width, height=height, log=self.log, local=local, localPath=localPath, localTime=localTime) 572 mini = BmpMiniature(id, path, bmp, pos, heading, face, label,
573 zorder=self. get_next_highest_z(), width=width,
574 height=height, log=self.log, local=local, localPath=localPath, localTime=localTime)
563 self.log.log("After mini creation:" + str(self.get_next_highest_z()), ORPG_DEBUG) 575 self.log.log("After mini creation:" + str(self.get_next_highest_z()), ORPG_DEBUG)
564 self.miniatures.append(mini) 576 self.miniatures.append(mini)
565 self.log.log("After mini addition:" + str(self.get_next_highest_z()), ORPG_DEBUG) 577 self.log.log("After mini addition:" + str(self.get_next_highest_z()), ORPG_DEBUG)
566 xml_str = "<map><miniatures>" 578 xml_str = "<map><miniatures>"
567 xml_str += mini.toxml("new") 579 xml_str += mini.toxml("new")
568 xml_str += "</miniatures></map>" 580 xml_str += "</miniatures></map>"
569 self.canvas.frame.session.send(xml_str) 581 self.canvas.frame.session.send(xml_str)
570 else: 582 else:
571 self.log.log("Invalid image " + path + " has been ignored!", ORPG_DEBUG) 583 self.log.log("Invalid image " + path + " has been ignored!", ORPG_DEBUG)
572 self.log.log("Exit miniature_layer->add_miniature(self, id, path, pos, label, heading, face, width, height)", ORPG_DEBUG) 584 self.log.log("Exit miniature_layer->add_miniature(self, id, path, pos, label, heading, face, width, height)",
585 ORPG_DEBUG)
573 586
574 def get_miniature_by_id(self, id): 587 def get_miniature_by_id(self, id):
575 self.log.log("Enter miniature_layer->get_miniature_by_id(self, id)", ORPG_DEBUG) 588 self.log.log("Enter miniature_layer->get_miniature_by_id(self, id)", ORPG_DEBUG)
576 for mini in self.miniatures: 589 for mini in self.miniatures:
577 if str(mini.id) == str(id): 590 if str(mini.id) == str(id):
599 self.collapse_zorder() 612 self.collapse_zorder()
600 self.log.log("Exit miniature_layer->del_all_miniatures(self)", ORPG_DEBUG) 613 self.log.log("Exit miniature_layer->del_all_miniatures(self)", ORPG_DEBUG)
601 614
602 def layerDraw(self, dc, topleft, size): 615 def layerDraw(self, dc, topleft, size):
603 self.log.log("Enter miniature_layer->layerDraw(self, dc, topleft, size)", ORPG_DEBUG) 616 self.log.log("Enter miniature_layer->layerDraw(self, dc, topleft, size)", ORPG_DEBUG)
604
605 dc.SetFont(self.label_font) 617 dc.SetFont(self.label_font)
606
607 sorted_miniatures = self.miniatures[:] 618 sorted_miniatures = self.miniatures[:]
608 sorted_miniatures.sort(cmp_zorder) 619 sorted_miniatures.sort(cmp_zorder)
609 for m in sorted_miniatures: 620 for m in sorted_miniatures:
610 if (m.pos.x>topleft[0]-m.right and 621 if (m.pos.x>topleft[0]-m.right and
611 m.pos.y>topleft[1]-m.bottom and 622 m.pos.y>topleft[1]-m.bottom and
689 face = int(c.getAttribute('face')) 700 face = int(c.getAttribute('face'))
690 if c.hasAttribute('align'): 701 if c.hasAttribute('align'):
691 snap_to_align = int(c.getAttribute('align')) 702 snap_to_align = int(c.getAttribute('align'))
692 if c.getAttribute('zorder'): 703 if c.getAttribute('zorder'):
693 zorder = int(c.getAttribute('zorder')) 704 zorder = int(c.getAttribute('zorder'))
694 min = BmpMiniature(id, path, ImageHandler.load(path, 'miniature', id), pos, heading, face, label, locked, hide, snap_to_align, zorder, width, height, self.log) 705 min = BmpMiniature(id, path, ImageHandler.load(path, 'miniature', id), pos, heading,
706 face, label, locked, hide, snap_to_align, zorder, width, height, self.log)
695 self.miniatures.append(min) 707 self.miniatures.append(min)
696 if c.hasAttribute('local') and c.getAttribute('local') == 'True' and os.path.exists(urllib.unquote(c.getAttribute('localPath'))): 708 if c.hasAttribute('local') and c.getAttribute('local') == 'True' and os.path.exists(urllib.unquote(c.getAttribute('localPath'))):
697 localPath = urllib.unquote(c.getAttribute('localPath')) 709 localPath = urllib.unquote(c.getAttribute('localPath'))
698 local = True 710 local = True
699 localTime = float(c.getAttribute('localTime')) 711 localTime = float(c.getAttribute('localTime'))
733 if self.canvas.parent.layer_handlers[2].auto_label: 745 if self.canvas.parent.layer_handlers[2].auto_label:
734 min_label = path[start:len(path)-4] 746 min_label = path[start:len(path)-4]
735 else: 747 else:
736 min_label = "" 748 min_label = ""
737 id = 'mini-' + self.canvas.frame.session.get_next_id() 749 id = 'mini-' + self.canvas.frame.session.get_next_id()
738 self.add_miniature(id, path, pos=pos, label=min_label, local=True, localPath=filename, localTime=time.time()) 750 self.add_miniature(id, path, pos=pos, label=min_label, local=True,
751 localPath=filename, localTime=time.time())
739 else: 752 else:
740 self.miniatures[len(self.miniatures)-1].local = True 753 self.miniatures[len(self.miniatures)-1].local = True
741 self.miniatures[len(self.miniatures)-1].localPath = filename 754 self.miniatures[len(self.miniatures)-1].localPath = filename
742 self.miniatures[len(self.miniatures)-1].localTime = time.time() 755 self.miniatures[len(self.miniatures)-1].localTime = time.time()
743 self.miniatures[len(self.miniatures)-1].path = path 756 self.miniatures[len(self.miniatures)-1].path = path