comparison orpg/mapper/whiteboard.py @ 13:211ac836b6a0 grumpy-goblin

{090731-00} Fixes problem with Name & Settings window, cleans code.
author sirebral
date Fri, 31 Jul 2009 15:22:11 -0500
parents 4385a7d0efd1
children 97265586402b
comparison
equal deleted inserted replaced
12:d492f137cd3c 13:211ac836b6a0
32 from orpg.mapper.map_utils import * 32 from orpg.mapper.map_utils import *
33 33
34 def cmp_zorder(first,second): 34 def cmp_zorder(first,second):
35 f = first.zorder 35 f = first.zorder
36 s = second.zorder 36 s = second.zorder
37 if f == None: 37 if f == None: f = 0
38 f = 0 38 if s == None: s = 0
39 if s == None: 39 if f == s: value = 0
40 s = 0 40 elif f < s: value = -1
41 if f == s: 41 else: value = 1
42 value = 0
43 elif f < s:
44 value = -1
45 else:
46 value = 1
47 return value 42 return value
48 43
49 class WhiteboardText: 44 class WhiteboardText:
50 def __init__(self, id, text_string, pos, style, pointsize, weight, color="#000000", log=None): 45 def __init__(self, id, text_string, pos, style, pointsize, weight, color="#000000", log=None):
51 self.log = log 46 self.log = log
101 return wx.Rect(self.posx,self.posy,w,(x+y+z)) 96 return wx.Rect(self.posx,self.posy,w,(x+y+z))
102 97
103 def draw(self, parent, dc, op=wx.COPY): 98 def draw(self, parent, dc, op=wx.COPY):
104 self.log.log("Enter WhiteboardText->draw(self, parent, dc, op)", ORPG_DEBUG) 99 self.log.log("Enter WhiteboardText->draw(self, parent, dc, op)", ORPG_DEBUG)
105 self.scale = parent.canvas.layers['grid'].mapscale 100 self.scale = parent.canvas.layers['grid'].mapscale
106 if self.highlighted: 101 if self.highlighted: textcolor = self.highlight_color
107 textcolor = self.highlight_color 102 else: textcolor = self.textcolor
108 else: 103 try: dc.SetTextForeground(textcolor)
109 textcolor = self.textcolor 104 except Exception,e: dc.SetTextForeground('#000000')
110 try:
111 dc.SetTextForeground(textcolor)
112 except Exception,e:
113 dc.SetTextForeground('#000000')
114 dc.SetUserScale(self.scale, self.scale) 105 dc.SetUserScale(self.scale, self.scale)
115 106
116 # Draw text 107 # Draw text
117 (w,x,y,z) = self.get_rect(dc) 108 (w,x,y,z) = self.get_rect(dc)
118 dc.SetFont(self.font) 109 dc.SetFont(self.font)
128 self.log.log("Exit WhiteboardText->toxml(self, " + action + ")", ORPG_DEBUG) 119 self.log.log("Exit WhiteboardText->toxml(self, " + action + ")", ORPG_DEBUG)
129 return xml_str 120 return xml_str
130 xml_str = "<text" 121 xml_str = "<text"
131 xml_str += " action='" + action + "'" 122 xml_str += " action='" + action + "'"
132 xml_str += " id='" + str(self.id) + "'" 123 xml_str += " id='" + str(self.id) + "'"
133 if self.pointsize != None: 124 if self.pointsize != None: xml_str += " pointsize='" + str(self.pointsize) + "'"
134 xml_str += " pointsize='" + str(self.pointsize) + "'" 125 if self.style != None: xml_str += " style='" + str(self.style) + "'"
135 if self.style != None: 126 if self.weight != None: xml_str += " weight='" + str(self.weight) + "'"
136 xml_str += " style='" + str(self.style) + "'" 127 if self.posx != None: xml_str+= " posx='" + str(self.posx) + "'"
137 if self.weight != None: 128 if not (self.posy is None): xml_str += " posy='" + str(self.posy) + "'"
138 xml_str += " weight='" + str(self.weight) + "'" 129 if self.text_string != None: xml_str+= " text_string='" + self.text_string + "'"
139 if self.posx != None: 130 if self.textcolor != None: xml_str += " color='" + self.textcolor + "'"
140 xml_str+= " posx='" + str(self.posx) + "'"
141 if not (self.posy is None):
142 xml_str += " posy='" + str(self.posy) + "'"
143 if self.text_string != None:
144 xml_str+= " text_string='" + self.text_string + "'"
145 if self.textcolor != None:
146 xml_str += " color='" + self.textcolor + "'"
147 xml_str += "/>" 131 xml_str += "/>"
148 self.log.log(xml_str, ORPG_DEBUG) 132 self.log.log(xml_str, ORPG_DEBUG)
149 self.log.log("Exit WhiteboardText->toxml(self, " + action + ")", ORPG_DEBUG) 133 self.log.log("Exit WhiteboardText->toxml(self, " + action + ")", ORPG_DEBUG)
150 if (action == "update" and self.isUpdated) or action == "new": 134 if (action == "update" and self.isUpdated) or action == "new":
151 self.isUpdated = False 135 self.isUpdated = False
152 return xml_str 136 return xml_str
153 else: 137 else: return ''
154 return ''
155 138
156 def takedom(self, xml_dom): 139 def takedom(self, xml_dom):
157 self.log.log("Enter WhiteboardText->takedom(self, xml_dom)", ORPG_DEBUG) 140 self.log.log("Enter WhiteboardText->takedom(self, xml_dom)", ORPG_DEBUG)
158 self.text_string = xml_dom.getAttribute("text_string") 141 self.text_string = xml_dom.getAttribute("text_string")
159 self.log.log("self.text_string=" + self.text_string, ORPG_DEBUG) 142 self.log.log("self.text_string=" + self.text_string, ORPG_DEBUG)
177 self.pointsize = int(xml_dom.getAttribute("pointsize")) 160 self.pointsize = int(xml_dom.getAttribute("pointsize"))
178 self.font.SetPointSize(self.pointsize) 161 self.font.SetPointSize(self.pointsize)
179 self.log.log("self.pointsize=" + str(self.pointsize), ORPG_DEBUG) 162 self.log.log("self.pointsize=" + str(self.pointsize), ORPG_DEBUG)
180 if xml_dom.hasAttribute("color") and xml_dom.getAttribute("color") != '': 163 if xml_dom.hasAttribute("color") and xml_dom.getAttribute("color") != '':
181 self.textcolor = xml_dom.getAttribute("color") 164 self.textcolor = xml_dom.getAttribute("color")
182 if self.textcolor == '#0000000': 165 if self.textcolor == '#0000000': self.textcolor = '#000000'
183 self.textcolor = '#000000'
184 self.log.log("self.textcolor=" + self.textcolor, ORPG_DEBUG) 166 self.log.log("self.textcolor=" + self.textcolor, ORPG_DEBUG)
185 self.log.log("Exit WhiteboardText->takedom(self, xml_dom)", ORPG_DEBUG) 167 self.log.log("Exit WhiteboardText->takedom(self, xml_dom)", ORPG_DEBUG)
186 168
187 class WhiteboardLine: 169 class WhiteboardLine:
188 def __init__(self, id, line_string, upperleft, lowerright, color="#000000", width=1, log=None): 170 def __init__(self, id, line_string, upperleft, lowerright, color="#000000", width=1, log=None):
189 self.log = log 171 self.log = log
190 self.log.log("Enter WhiteboardLine", ORPG_DEBUG) 172 self.log.log("Enter WhiteboardLine", ORPG_DEBUG)
191 self.scale = 1 173 self.scale = 1
192 self.r_h = RGBHex() 174 self.r_h = RGBHex()
193 if color == '': 175 if color == '': color = "#000000"
194 color = "#000000"
195 self.linecolor = color 176 self.linecolor = color
196 self.linewidth = width 177 self.linewidth = width
197 self.lowerright = lowerright 178 self.lowerright = lowerright
198 self.upperleft = upperleft 179 self.upperleft = upperleft
199 self.selected = False 180 self.selected = False
207 def highlight(self, highlight=True): 188 def highlight(self, highlight=True):
208 self.log.log("Enter WhiteboardLine->highlight(self, highlight)", ORPG_DEBUG) 189 self.log.log("Enter WhiteboardLine->highlight(self, highlight)", ORPG_DEBUG)
209 self.highlighted = highlight 190 self.highlighted = highlight
210 self.log.log("Enter WhiteboardLine->highlight(self, highlight)", ORPG_DEBUG) 191 self.log.log("Enter WhiteboardLine->highlight(self, highlight)", ORPG_DEBUG)
211 192
212 def set_line_props(self, line_string="", upperleftx=0, upperlefty=0, lowerrightx=0, lowerrighty=0, color="#000000", width=1): 193 def set_line_props(self, line_string="", upperleftx=0, upperlefty=0,
194 lowerrightx=0, lowerrighty=0, color="#000000", width=1):
213 self.log.log("Enter WhiteboardLine->set_line_props(self, line_string, upperleftx, upperlefty, lowerrightx, lowerrighty, color, width)", ORPG_DEBUG) 195 self.log.log("Enter WhiteboardLine->set_line_props(self, line_string, upperleftx, upperlefty, lowerrightx, lowerrighty, color, width)", ORPG_DEBUG)
214 self.line_string = line_string 196 self.line_string = line_string
215 self.upperleft.x = upperleftx 197 self.upperleft.x = upperleftx
216 self.upperleft.y = upperlefty 198 self.upperleft.y = upperlefty
217 self.lowerright.x = lowerrightx 199 self.lowerright.x = lowerrightx
239 return False 221 return False
240 222
241 def draw(self, parent, dc, op=wx.COPY): 223 def draw(self, parent, dc, op=wx.COPY):
242 self.log.log("Enter WhiteboardLine->draw(self, parent, dc, op=wx.COPY)", ORPG_DEBUG) 224 self.log.log("Enter WhiteboardLine->draw(self, parent, dc, op=wx.COPY)", ORPG_DEBUG)
243 self.scale = parent.canvas.layers['grid'].mapscale 225 self.scale = parent.canvas.layers['grid'].mapscale
244 if self.highlighted: 226 if self.highlighted: linecolor = self.highlight_color
245 linecolor = self.highlight_color 227 else: linecolor = self.linecolor
246 else:
247 linecolor = self.linecolor
248 pen = wx.BLACK_PEN 228 pen = wx.BLACK_PEN
249 try: 229 try: pen.SetColour(linecolor)
250 pen.SetColour(linecolor) 230 except Exception,e: pen.SetColour('#000000')
251 except Exception,e:
252 pen.SetColour('#000000')
253 pen.SetWidth( self.linewidth ) 231 pen.SetWidth( self.linewidth )
254 dc.SetPen( pen ) 232 dc.SetPen( pen )
255 dc.SetBrush(wx.BLACK_BRUSH) 233 dc.SetBrush(wx.BLACK_BRUSH)
256 # draw lines 234 # draw lines
257 dc.SetUserScale(self.scale,self.scale) 235 dc.SetUserScale(self.scale,self.scale)
260 for m in range(len(pointArray)-1): 238 for m in range(len(pointArray)-1):
261 x = pointArray[m] 239 x = pointArray[m]
262 points = x.split(",") 240 points = x.split(",")
263 x1 = int(points[0]) 241 x1 = int(points[0])
264 y1 = int(points[1]) 242 y1 = int(points[1])
265 if x2 != -999: 243 if x2 != -999: dc.DrawLine(x2,y2,x1,y1)
266 dc.DrawLine(x2,y2,x1,y1)
267 x2 = x1 244 x2 = x1
268 y2 = y1 245 y2 = y1
269 pen.SetColour(wx.Colour(0,0,0)) 246 pen.SetColour(wx.Colour(0,0,0))
270 dc.SetPen(pen) 247 dc.SetPen(pen)
271 dc.SetPen(wx.NullPen) 248 dc.SetPen(wx.NullPen)
296 if self.linewidth != None: 273 if self.linewidth != None:
297 xml_str += " width='" + str(self.linewidth) + "'" 274 xml_str += " width='" + str(self.linewidth) + "'"
298 xml_str += "/>" 275 xml_str += "/>"
299 self.log.log(xml_str, ORPG_DEBUG) 276 self.log.log(xml_str, ORPG_DEBUG)
300 self.log.log("Exit WhiteboardLine->toxml(self, " + action + ")", ORPG_DEBUG) 277 self.log.log("Exit WhiteboardLine->toxml(self, " + action + ")", ORPG_DEBUG)
301 if action == "new": 278 if action == "new": return xml_str
302 return xml_str
303 return '' 279 return ''
304 280
305 def takedom(self, xml_dom): 281 def takedom(self, xml_dom):
306 self.log.log("Enter WhiteboardLine->takedom(self, xml_dom)", ORPG_DEBUG) 282 self.log.log("Enter WhiteboardLine->takedom(self, xml_dom)", ORPG_DEBUG)
307 self.line_string = xml_dom.getAttribute("line_string") 283 self.line_string = xml_dom.getAttribute("line_string")
441 self.canvas.Refresh(True) 417 self.canvas.Refresh(True)
442 self.log.log("Exit whiteboard_layer->del_text(self, text)", ORPG_DEBUG) 418 self.log.log("Exit whiteboard_layer->del_text(self, text)", ORPG_DEBUG)
443 419
444 def layerDraw(self, dc): 420 def layerDraw(self, dc):
445 self.log.log("Enter whiteboard_layer->layerDraw(self, dc)", ORPG_DEBUG) 421 self.log.log("Enter whiteboard_layer->layerDraw(self, dc)", ORPG_DEBUG)
446 for m in self.lines: 422 for m in self.lines: m.draw(self, dc)
447 m.draw(self, dc) 423 for m in self.texts: m.draw(self,dc)
448 for m in self.texts:
449 m.draw(self,dc)
450 self.log.log("Exit whiteboard_layer->layerDraw(self, dc)", ORPG_DEBUG) 424 self.log.log("Exit whiteboard_layer->layerDraw(self, dc)", ORPG_DEBUG)
451 425
452 def hit_test_text(self, pos, dc): 426 def hit_test_text(self, pos, dc):
453 self.log.log("Enter whiteboard_layer->hit_test_text(self, pos, dc)", ORPG_DEBUG) 427 self.log.log("Enter whiteboard_layer->hit_test_text(self, pos, dc)", ORPG_DEBUG)
454 list_of_texts_matching = [] 428 list_of_texts_matching = []
455 if self.canvas.layers['fog'].use_fog == 1: 429 if self.canvas.layers['fog'].use_fog == 1:
456 if self.canvas.frame.session.role != "GM": 430 if self.canvas.frame.session.role != "GM":
457 self.log.log("Exit whiteboard_layer->hit_test_text(self, pos, dc)", ORPG_DEBUG) 431 self.log.log("Exit whiteboard_layer->hit_test_text(self, pos, dc)", ORPG_DEBUG)
458 return list_of_texts_matching 432 return list_of_texts_matching
459 for m in self.texts: 433 for m in self.texts:
460 if m.hit_test(pos,dc): 434 if m.hit_test(pos,dc): list_of_texts_matching.append(m)
461 list_of_texts_matching.append(m)
462 self.log.log("Exit whiteboard_layer->hit_test_text(self, pos, dc)", ORPG_DEBUG) 435 self.log.log("Exit whiteboard_layer->hit_test_text(self, pos, dc)", ORPG_DEBUG)
463 return list_of_texts_matching 436 return list_of_texts_matching
464 437
465 def hit_test_lines(self, pos, dc): 438 def hit_test_lines(self, pos, dc):
466 self.log.log("Enter whiteboard_layer->hit_test_lines(self, pos, dc)", ORPG_DEBUG) 439 self.log.log("Enter whiteboard_layer->hit_test_lines(self, pos, dc)", ORPG_DEBUG)
468 if self.canvas.layers['fog'].use_fog == 1: 441 if self.canvas.layers['fog'].use_fog == 1:
469 if self.canvas.frame.session.role != "GM": 442 if self.canvas.frame.session.role != "GM":
470 self.log.log("Exit whiteboard_layer->hit_test_lines(self, pos, dc)", ORPG_DEBUG) 443 self.log.log("Exit whiteboard_layer->hit_test_lines(self, pos, dc)", ORPG_DEBUG)
471 return list_of_lines_matching 444 return list_of_lines_matching
472 for m in self.lines: 445 for m in self.lines:
473 if m.hit_test(pos): 446 if m.hit_test(pos): list_of_lines_matching.append(m)
474 list_of_lines_matching.append(m)
475 self.log.log("Exit whiteboard_layer->hit_test_lines(self, pos, dc)", ORPG_DEBUG) 447 self.log.log("Exit whiteboard_layer->hit_test_lines(self, pos, dc)", ORPG_DEBUG)
476 return list_of_lines_matching 448 return list_of_lines_matching
477 449
478 def find_line(self, pt): 450 def find_line(self, pt):
479 self.log.log("Enter whiteboard_layer->find_line(self, pt)", ORPG_DEBUG) 451 self.log.log("Enter whiteboard_layer->find_line(self, pt)", ORPG_DEBUG)
550 def layerToXML(self, action="update"): 522 def layerToXML(self, action="update"):
551 """ format """ 523 """ format """
552 self.log.log("Enter whiteboard_layer->layerToXML(self, " + action + ")", ORPG_DEBUG) 524 self.log.log("Enter whiteboard_layer->layerToXML(self, " + action + ")", ORPG_DEBUG)
553 white_string = "" 525 white_string = ""
554 if self.lines: 526 if self.lines:
555 for l in self.lines: 527 for l in self.lines: white_string += l.toxml(action)
556 white_string += l.toxml(action)
557 if self.texts: 528 if self.texts:
558 for l in self.texts: 529 for l in self.texts: white_string += l.toxml(action)
559 white_string += l.toxml(action)
560 if len(white_string): 530 if len(white_string):
561 s = "<whiteboard" 531 s = "<whiteboard"
562 s += " serial='" + str(self.serial_number) + "'" 532 s += " serial='" + str(self.serial_number) + "'"
563 s += ">" 533 s += ">"
564 s += white_string 534 s += white_string
571 return "" 541 return ""
572 542
573 def layerTakeDOM(self, xml_dom): 543 def layerTakeDOM(self, xml_dom):
574 self.log.log("Enter whiteboard_layer->layerTakeDOM(self, xml_dom)", ORPG_DEBUG) 544 self.log.log("Enter whiteboard_layer->layerTakeDOM(self, xml_dom)", ORPG_DEBUG)
575 serial_number = xml_dom.getAttribute('serial') 545 serial_number = xml_dom.getAttribute('serial')
576 if serial_number != "": 546 if serial_number != "": self.serial_number = int(serial_number)
577 self.serial_number = int(serial_number)
578 children = xml_dom._get_childNodes() 547 children = xml_dom._get_childNodes()
579 for l in children: 548 for l in children:
580 nodename = l._get_nodeName() 549 nodename = l._get_nodeName()
581 action = l.getAttribute("action") 550 action = l.getAttribute("action")
582 id = l.getAttribute('id') 551 id = l.getAttribute('id')
583 if action == "del": 552 if action == "del":
584 if nodename == 'line': 553 if nodename == 'line':
585 line = self.get_line_by_id(id) 554 line = self.get_line_by_id(id)
586 if line != None: 555 if line != None: self.lines.remove(line)
587 self.lines.remove(line) 556 else: self.log.log("Whiteboard error: Deletion of unknown line object attempted.", ORPG_GENERAL)
588 else:
589 self.log.log("Whiteboard error: Deletion of unknown line object attempted.", ORPG_GENERAL)
590 elif nodename == 'text': 557 elif nodename == 'text':
591 text = self.get_text_by_id(id) 558 text = self.get_text_by_id(id)
592 if text != None: 559 if text != None: self.texts.remove(text)
593 self.texts.remove(text) 560 else: self.log.log("Whiteboard error: Deletion of unknown text object attempted.", ORPG_GENERAL)
594 else: 561 else: self.log.log("Whiteboard error: Deletion of unknown whiteboard object attempted.", ORPG_GENERAL)
595 self.log.log("Whiteboard error: Deletion of unknown text object attempted.", ORPG_GENERAL)
596 else:
597 self.log.log("Whiteboard error: Deletion of unknown whiteboard object attempted.", ORPG_GENERAL)
598 elif action == "new": 562 elif action == "new":
599 if nodename == "line": 563 if nodename == "line":
600 try: 564 try:
601 line_string = l.getAttribute('line_string') 565 line_string = l.getAttribute('line_string')
602 upperleftx = l.getAttribute('upperleftx') 566 upperleftx = l.getAttribute('upperleftx')
604 lowerrightx = l.getAttribute('lowerrightx') 568 lowerrightx = l.getAttribute('lowerrightx')
605 lowerrighty = l.getAttribute('lowerrighty') 569 lowerrighty = l.getAttribute('lowerrighty')
606 upperleft = wx.Point(int(upperleftx),int(upperlefty)) 570 upperleft = wx.Point(int(upperleftx),int(upperlefty))
607 lowerright = wx.Point(int(lowerrightx),int(lowerrighty)) 571 lowerright = wx.Point(int(lowerrightx),int(lowerrighty))
608 color = l.getAttribute('color') 572 color = l.getAttribute('color')
609 if color == '#0000000': 573 if color == '#0000000': color = '#000000'
610 color = '#000000'
611 id = l.getAttribute('id') 574 id = l.getAttribute('id')
612 width = int(l.getAttribute('width')) 575 width = int(l.getAttribute('width'))
613 except: 576 except:
614 line_string = upperleftx = upperlefty = lowerrightx = lowerrighty = color = 0 577 line_string = upperleftx = upperlefty = lowerrightx = lowerrighty = color = 0
615 self.log.log(traceback.format_exc(), ORPG_GENERAL) 578 self.log.log(traceback.format_exc(), ORPG_GENERAL)
622 text_string = l.getAttribute('text_string') 585 text_string = l.getAttribute('text_string')
623 style = l.getAttribute('style') 586 style = l.getAttribute('style')
624 pointsize = l.getAttribute('pointsize') 587 pointsize = l.getAttribute('pointsize')
625 weight = l.getAttribute('weight') 588 weight = l.getAttribute('weight')
626 color = l.getAttribute('color') 589 color = l.getAttribute('color')
627 if color == '#0000000': 590 if color == '#0000000': color = '#000000'
628 color = '#000000'
629 id = l.getAttribute('id') 591 id = l.getAttribute('id')
630 posx = l.getAttribute('posx') 592 posx = l.getAttribute('posx')
631 posy = l.getAttribute('posy') 593 posy = l.getAttribute('posy')
632 pos = wx.Point(0,0) 594 pos = wx.Point(0,0)
633 pos.x = int(posx) 595 pos.x = int(posx)
639 text = WhiteboardText(id, text_string, pos, style, pointsize, weight, color, self.log) 601 text = WhiteboardText(id, text_string, pos, style, pointsize, weight, color, self.log)
640 self.texts.append(text) 602 self.texts.append(text)
641 else: 603 else:
642 if nodename == "line": 604 if nodename == "line":
643 line = self.get_line_by_id(id) 605 line = self.get_line_by_id(id)
644 if line: 606 if line: line.takedom(l)
645 line.takedom(l) 607 else: self.log.log("Whiteboard error: Update of unknown line attempted.", ORPG_GENERAL)
646 else:
647 self.log.log("Whiteboard error: Update of unknown line attempted.", ORPG_GENERAL)
648 if nodename == "text": 608 if nodename == "text":
649 text = self.get_text_by_id(id) 609 text = self.get_text_by_id(id)
650 if text: 610 if text: text.takedom(l)
651 text.takedom(l) 611 else: self.log.log("Whiteboard error: Update of unknown text attempted.", ORPG_GENERAL)
652 else:
653 self.log.log("Whiteboard error: Update of unknown text attempted.", ORPG_GENERAL)
654 self.log.log("Enter whiteboard_layer->layerTakeDOM(self, xml_dom)", ORPG_DEBUG) 612 self.log.log("Enter whiteboard_layer->layerTakeDOM(self, xml_dom)", ORPG_DEBUG)
655 #self.canvas.send_map_data() 613 #self.canvas.send_map_data()
656 614
657 def add_temp_line(self, line_string): 615 def add_temp_line(self, line_string):
658 line = WhiteboardLine(0, line_string, wx.Point(0,0), wx.Point(0,0), color=self.color, width=self.width, log=self.log) 616 line = WhiteboardLine(0, line_string, wx.Point(0,0), wx.Point(0,0),
617 color=self.color, width=self.width, log=self.log)
659 self.lines.append(line) 618 self.lines.append(line)
660 return line 619 return line
661 620
662 def del_temp_line(self, line): 621 def del_temp_line(self, line):
663 if line: 622 if line: self.lines.remove(line)
664 self.lines.remove(line)