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

2nd attempt. Still untested.
author sirebral
date Sat, 25 Jul 2009 19:23:25 -0500
parents 78407d627cba
children 449a8900f9ac
comparison
equal deleted inserted replaced
19:78407d627cba 20:072ffc1d466f
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
240 return False 221 return False
241 222
242 def draw(self, parent, dc, op=wx.COPY): 223 def draw(self, parent, dc, op=wx.COPY):
243 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)
244 self.scale = parent.canvas.layers['grid'].mapscale 225 self.scale = parent.canvas.layers['grid'].mapscale
245 if self.highlighted: 226 if self.highlighted: linecolor = self.highlight_color
246 linecolor = self.highlight_color 227 else: linecolor = self.linecolor
247 else:
248 linecolor = self.linecolor
249 pen = wx.BLACK_PEN 228 pen = wx.BLACK_PEN
250 try: 229 try: pen.SetColour(linecolor)
251 pen.SetColour(linecolor) 230 except Exception,e: pen.SetColour('#000000')
252 except Exception,e:
253 pen.SetColour('#000000')
254 pen.SetWidth( self.linewidth ) 231 pen.SetWidth( self.linewidth )
255 dc.SetPen( pen ) 232 dc.SetPen( pen )
256 dc.SetBrush(wx.BLACK_BRUSH) 233 dc.SetBrush(wx.BLACK_BRUSH)
257 # draw lines 234 # draw lines
258 dc.SetUserScale(self.scale,self.scale) 235 dc.SetUserScale(self.scale,self.scale)
261 for m in range(len(pointArray)-1): 238 for m in range(len(pointArray)-1):
262 x = pointArray[m] 239 x = pointArray[m]
263 points = x.split(",") 240 points = x.split(",")
264 x1 = int(points[0]) 241 x1 = int(points[0])
265 y1 = int(points[1]) 242 y1 = int(points[1])
266 if x2 != -999: 243 if x2 != -999: dc.DrawLine(x2,y2,x1,y1)
267 dc.DrawLine(x2,y2,x1,y1)
268 x2 = x1 244 x2 = x1
269 y2 = y1 245 y2 = y1
270 pen.SetColour(wx.Colour(0,0,0)) 246 pen.SetColour(wx.Colour(0,0,0))
271 dc.SetPen(pen) 247 dc.SetPen(pen)
272 dc.SetPen(wx.NullPen) 248 dc.SetPen(wx.NullPen)
297 if self.linewidth != None: 273 if self.linewidth != None:
298 xml_str += " width='" + str(self.linewidth) + "'" 274 xml_str += " width='" + str(self.linewidth) + "'"
299 xml_str += "/>" 275 xml_str += "/>"
300 self.log.log(xml_str, ORPG_DEBUG) 276 self.log.log(xml_str, ORPG_DEBUG)
301 self.log.log("Exit WhiteboardLine->toxml(self, " + action + ")", ORPG_DEBUG) 277 self.log.log("Exit WhiteboardLine->toxml(self, " + action + ")", ORPG_DEBUG)
302 if action == "new": 278 if action == "new": return xml_str
303 return xml_str
304 return '' 279 return ''
305 280
306 def takedom(self, xml_dom): 281 def takedom(self, xml_dom):
307 self.log.log("Enter WhiteboardLine->takedom(self, xml_dom)", ORPG_DEBUG) 282 self.log.log("Enter WhiteboardLine->takedom(self, xml_dom)", ORPG_DEBUG)
308 self.line_string = xml_dom.getAttribute("line_string") 283 self.line_string = xml_dom.getAttribute("line_string")
442 self.canvas.Refresh(True) 417 self.canvas.Refresh(True)
443 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)
444 419
445 def layerDraw(self, dc): 420 def layerDraw(self, dc):
446 self.log.log("Enter whiteboard_layer->layerDraw(self, dc)", ORPG_DEBUG) 421 self.log.log("Enter whiteboard_layer->layerDraw(self, dc)", ORPG_DEBUG)
447 for m in self.lines: 422 for m in self.lines: m.draw(self, dc)
448 m.draw(self, dc) 423 for m in self.texts: m.draw(self,dc)
449 for m in self.texts:
450 m.draw(self,dc)
451 self.log.log("Exit whiteboard_layer->layerDraw(self, dc)", ORPG_DEBUG) 424 self.log.log("Exit whiteboard_layer->layerDraw(self, dc)", ORPG_DEBUG)
452 425
453 def hit_test_text(self, pos, dc): 426 def hit_test_text(self, pos, dc):
454 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)
455 list_of_texts_matching = [] 428 list_of_texts_matching = []
456 if self.canvas.layers['fog'].use_fog == 1: 429 if self.canvas.layers['fog'].use_fog == 1:
457 if self.canvas.frame.session.role != "GM": 430 if self.canvas.frame.session.role != "GM":
458 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)
459 return list_of_texts_matching 432 return list_of_texts_matching
460 for m in self.texts: 433 for m in self.texts:
461 if m.hit_test(pos,dc): 434 if m.hit_test(pos,dc): list_of_texts_matching.append(m)
462 list_of_texts_matching.append(m)
463 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)
464 return list_of_texts_matching 436 return list_of_texts_matching
465 437
466 def hit_test_lines(self, pos, dc): 438 def hit_test_lines(self, pos, dc):
467 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)
469 if self.canvas.layers['fog'].use_fog == 1: 441 if self.canvas.layers['fog'].use_fog == 1:
470 if self.canvas.frame.session.role != "GM": 442 if self.canvas.frame.session.role != "GM":
471 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)
472 return list_of_lines_matching 444 return list_of_lines_matching
473 for m in self.lines: 445 for m in self.lines:
474 if m.hit_test(pos): 446 if m.hit_test(pos): list_of_lines_matching.append(m)
475 list_of_lines_matching.append(m)
476 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)
477 return list_of_lines_matching 448 return list_of_lines_matching
478 449
479 def find_line(self, pt): 450 def find_line(self, pt):
480 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)
551 def layerToXML(self, action="update"): 522 def layerToXML(self, action="update"):
552 """ format """ 523 """ format """
553 self.log.log("Enter whiteboard_layer->layerToXML(self, " + action + ")", ORPG_DEBUG) 524 self.log.log("Enter whiteboard_layer->layerToXML(self, " + action + ")", ORPG_DEBUG)
554 white_string = "" 525 white_string = ""
555 if self.lines: 526 if self.lines:
556 for l in self.lines: 527 for l in self.lines: white_string += l.toxml(action)
557 white_string += l.toxml(action)
558 if self.texts: 528 if self.texts:
559 for l in self.texts: 529 for l in self.texts: white_string += l.toxml(action)
560 white_string += l.toxml(action)
561 if len(white_string): 530 if len(white_string):
562 s = "<whiteboard" 531 s = "<whiteboard"
563 s += " serial='" + str(self.serial_number) + "'" 532 s += " serial='" + str(self.serial_number) + "'"
564 s += ">" 533 s += ">"
565 s += white_string 534 s += white_string
572 return "" 541 return ""
573 542
574 def layerTakeDOM(self, xml_dom): 543 def layerTakeDOM(self, xml_dom):
575 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)
576 serial_number = xml_dom.getAttribute('serial') 545 serial_number = xml_dom.getAttribute('serial')
577 if serial_number != "": 546 if serial_number != "": self.serial_number = int(serial_number)
578 self.serial_number = int(serial_number)
579 children = xml_dom._get_childNodes() 547 children = xml_dom._get_childNodes()
580 for l in children: 548 for l in children:
581 nodename = l._get_nodeName() 549 nodename = l._get_nodeName()
582 action = l.getAttribute("action") 550 action = l.getAttribute("action")
583 id = l.getAttribute('id') 551 id = l.getAttribute('id')
584 if action == "del": 552 if action == "del":
585 if nodename == 'line': 553 if nodename == 'line':
586 line = self.get_line_by_id(id) 554 line = self.get_line_by_id(id)
587 if line != None: 555 if line != None: self.lines.remove(line)
588 self.lines.remove(line) 556 else: self.log.log("Whiteboard error: Deletion of unknown line object attempted.", ORPG_GENERAL)
589 else:
590 self.log.log("Whiteboard error: Deletion of unknown line object attempted.", ORPG_GENERAL)
591 elif nodename == 'text': 557 elif nodename == 'text':
592 text = self.get_text_by_id(id) 558 text = self.get_text_by_id(id)
593 if text != None: 559 if text != None: self.texts.remove(text)
594 self.texts.remove(text) 560 else: self.log.log("Whiteboard error: Deletion of unknown text object attempted.", ORPG_GENERAL)
595 else: 561 else: self.log.log("Whiteboard error: Deletion of unknown whiteboard object attempted.", ORPG_GENERAL)
596 self.log.log("Whiteboard error: Deletion of unknown text object attempted.", ORPG_GENERAL)
597 else:
598 self.log.log("Whiteboard error: Deletion of unknown whiteboard object attempted.", ORPG_GENERAL)
599 elif action == "new": 562 elif action == "new":
600 if nodename == "line": 563 if nodename == "line":
601 try: 564 try:
602 line_string = l.getAttribute('line_string') 565 line_string = l.getAttribute('line_string')
603 upperleftx = l.getAttribute('upperleftx') 566 upperleftx = l.getAttribute('upperleftx')
605 lowerrightx = l.getAttribute('lowerrightx') 568 lowerrightx = l.getAttribute('lowerrightx')
606 lowerrighty = l.getAttribute('lowerrighty') 569 lowerrighty = l.getAttribute('lowerrighty')
607 upperleft = wx.Point(int(upperleftx),int(upperlefty)) 570 upperleft = wx.Point(int(upperleftx),int(upperlefty))
608 lowerright = wx.Point(int(lowerrightx),int(lowerrighty)) 571 lowerright = wx.Point(int(lowerrightx),int(lowerrighty))
609 color = l.getAttribute('color') 572 color = l.getAttribute('color')
610 if color == '#0000000': 573 if color == '#0000000': color = '#000000'
611 color = '#000000'
612 id = l.getAttribute('id') 574 id = l.getAttribute('id')
613 width = int(l.getAttribute('width')) 575 width = int(l.getAttribute('width'))
614 except: 576 except:
615 line_string = upperleftx = upperlefty = lowerrightx = lowerrighty = color = 0 577 line_string = upperleftx = upperlefty = lowerrightx = lowerrighty = color = 0
616 self.log.log(traceback.format_exc(), ORPG_GENERAL) 578 self.log.log(traceback.format_exc(), ORPG_GENERAL)
623 text_string = l.getAttribute('text_string') 585 text_string = l.getAttribute('text_string')
624 style = l.getAttribute('style') 586 style = l.getAttribute('style')
625 pointsize = l.getAttribute('pointsize') 587 pointsize = l.getAttribute('pointsize')
626 weight = l.getAttribute('weight') 588 weight = l.getAttribute('weight')
627 color = l.getAttribute('color') 589 color = l.getAttribute('color')
628 if color == '#0000000': 590 if color == '#0000000': color = '#000000'
629 color = '#000000'
630 id = l.getAttribute('id') 591 id = l.getAttribute('id')
631 posx = l.getAttribute('posx') 592 posx = l.getAttribute('posx')
632 posy = l.getAttribute('posy') 593 posy = l.getAttribute('posy')
633 pos = wx.Point(0,0) 594 pos = wx.Point(0,0)
634 pos.x = int(posx) 595 pos.x = int(posx)
640 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)
641 self.texts.append(text) 602 self.texts.append(text)
642 else: 603 else:
643 if nodename == "line": 604 if nodename == "line":
644 line = self.get_line_by_id(id) 605 line = self.get_line_by_id(id)
645 if line: 606 if line: line.takedom(l)
646 line.takedom(l) 607 else: self.log.log("Whiteboard error: Update of unknown line attempted.", ORPG_GENERAL)
647 else:
648 self.log.log("Whiteboard error: Update of unknown line attempted.", ORPG_GENERAL)
649 if nodename == "text": 608 if nodename == "text":
650 text = self.get_text_by_id(id) 609 text = self.get_text_by_id(id)
651 if text: 610 if text: text.takedom(l)
652 text.takedom(l) 611 else: self.log.log("Whiteboard error: Update of unknown text attempted.", ORPG_GENERAL)
653 else:
654 self.log.log("Whiteboard error: Update of unknown text attempted.", ORPG_GENERAL)
655 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)
656 #self.canvas.send_map_data() 613 #self.canvas.send_map_data()
657 614
658 def add_temp_line(self, line_string): 615 def add_temp_line(self, line_string):
659 line = WhiteboardLine(0, line_string, wx.Point(0,0), wx.Point(0,0), 616 line = WhiteboardLine(0, line_string, wx.Point(0,0), wx.Point(0,0),
660 color=self.color, width=self.width, log=self.log) 617 color=self.color, width=self.width, log=self.log)
661 self.lines.append(line) 618 self.lines.append(line)
662 return line 619 return line
663 620
664 def del_temp_line(self, line): 621 def del_temp_line(self, line):
665 if line: 622 if line: self.lines.remove(line)
666 self.lines.remove(line)