示例#1
0
class _BoardCanvas(Canvas):
   def __init__(self):
      self._dragging = False
      Canvas.__init__(self, Width=WIDTH, Height=HEIGHT)
      canvasElement = self.getElement()
      #Register for canvas mouse events
      self._proc = Processing(DOM.getFirstChild(canvasElement))
      self._proc.setup = (lambda: self.setup_proc())
      self._proc.draw = None
      self._proc.init()

      DOM.sinkEvents(canvasElement, Event.MOUSEEVENTS)
      DOM.setEventListener(canvasElement, self)

      self._currentPointList = []

      self._x = None
      self._y = None

      self._callback_Redraw = None
      self._callback_AddStroke = None
      self._callback_DeleteStroke = None
      
   def onBrowserEvent(self, event):
      kind = DOM.eventGetType(event)
      x = DOM.eventGetClientX(event) - DOM.getAbsoluteLeft(self.getElement())
      y = DOM.eventGetClientY(event) - DOM.getAbsoluteTop(self.getElement())
      y = HEIGHT - y
      if kind == "mousedown":
         self.onMouseDown(x,y)
         self._dragging = True
      elif kind == "mousemove" and self._dragging:
         self.onMouseDrag(x,y)
      elif (kind == "mouseup" or kind == "mouseout") and self._dragging:
         self._dragging = False
         self.onMouseUp(x,y)

   def setup_proc(self):
      self._proc.size(WIDTH,HEIGHT)
      self._proc.background(255)
      self._proc.stroke("#000000")
      self._proc.smooth()
   def redraw(self):
      if self._callback_Redraw is not None:
         self.clear_board()
         self._callback_Redraw()
   def clear_board(self):
      self._proc.background(255)


   def onMouseDown(self, x, y):
      self._x = x
      self._y = y
      #self.drawCircle(x,y,radius=1)

      self._currentPointList.append(Point(x,y))

   def onMouseDrag(self, x, y):
      self.drawLine(self._x, self._y, x, y)
      self._x = x
      self._y = y
      #self.drawCircle(x,y,radius=1)
      self._currentPointList.append(Point(x,y))

   def onMouseUp(self, x, y):
      self.drawLine(self._x, self._y, x, y)
      self._x = None
      self._y = None

      if self._callback_AddStroke is not None:
         self._callback_AddStroke(self._currentPointList)
      if self._callback_Redraw is not None:
         self._callback_Redraw()
      self._currentPointList = []

   def setCallback_AddStroke(self, function):
      self._callback_AddStroke = function
   def setCallback_DeleteStroke(self, function):
      self._callback_DeleteStroke = function
   def setCallback_Redraw(self, function):
      self._callback_Redraw = function

   def drawLine(self, x1, y1, x2, y2, width=2, color="#000000"):
      _y1 = HEIGHT - y1
      _y2 = HEIGHT - y2
      self._proc.stroke(color)
      self._proc.strokeWeight(width)
      self._proc.line(x1,_y1,x2,_y2)
      #self._proc.stroke("#000000")
   
   def drawCircle(self, x, y, radius=1, color="#000000", fill="", width=1.0):
      "Draw a circle on the canvas at (x,y) with radius rad. Color should be 24 bit RGB string #RRGGBB. Empty string is transparent"
      _y = HEIGHT - y
      self._proc.stroke(color)
      self._proc.strokeWeight(width)
      if fill == "":
         self._proc.noFill()
      else:
         self._proc.fill(fill)
      self._proc.ellipse(x,_y,2*radius, 2*radius)
      #self._proc.stroke("#000000")

   def drawText (self, x, y, InText="", size=10, color="#000000"):
      logger.debug("drawing text '%s' at %s, %s" % (InText, x,y))
      self._proc.fill(color)
      _y = HEIGHT - y
      self._proc.text(InText, x, _y)