Exemplo n.º 1
0
 def line(self, surface, color, point1, point2, width=1):
     """
     Draw line, and returns bounding Rect.
     Argument include surface to draw, color, point1, point2.
     Optional width argument of line.
     """
     surface.beginPath()
     surface.moveTo(*point1)
     surface.lineTo(*point2)
     surface.setLineWidth(width)
     if hasattr(color, 'a'):
         surface.setStrokeStyle(color)
     else:
         surface.setStrokeStyle(Color(color))
     surface.stroke()
     xpts = [pt[0] for pt in (point1, point2)]
     ypts = [pt[1] for pt in (point1, point2)]
     xmin, xmax = min(xpts), max(xpts)
     ymin, ymax = min(ypts), max(ypts)
     if surface._display:
         return surface._display._surface_rect.clip(
             Rect(xmin, ymin, xmax - xmin + 1, ymax - ymin + 1))
     else:
         return surface.get_rect().clip(
             Rect(xmin, ymin, xmax - xmin + 1, ymax - ymin + 1))
Exemplo n.º 2
0
 def circle(self, surface, color, position, radius, width=0):
     """
     Draw circular shape, and returns bounding Rect.
     Argument include surface to draw, color, position and radius.
     Optional width argument of outline, which defaults to 0 for filled shape.
     """
     surface.beginPath()
     surface.arc(position[0], position[1], radius, 0, 2 * _pi, False)
     if width:
         surface.setLineWidth(width)
         if hasattr(color, 'a'):
             surface.setStrokeStyle(color)
         else:
             surface.setStrokeStyle(Color(color))
         surface.stroke()
     else:
         if hasattr(color, 'a'):
             surface.setFillStyle(color)
         else:
             surface.setFillStyle(Color(color))
         surface.fill()
     if surface._display:
         return surface._display._surface_rect.clip(
             Rect(position[0] - radius, position[1] - radius, 2 * radius,
                  2 * radius))
     else:
         return surface.get_rect().clip(
             Rect(position[0] - radius, position[1] - radius, 2 * radius,
                  2 * radius))
Exemplo n.º 3
0
 def lines(self, surface, color, closed, pointlist, width=1):
     """
     Draw interconnected lines, and returns Rect bound.
     Argument include surface to draw, color, closed, and pointlist.
     Optional width argument of line.
     """
     surface.beginPath()
     surface.moveTo(*pointlist[0])
     for point in pointlist[1:]:
         surface.lineTo(*point)
     if closed:
         surface.closePath()
     surface.setLineWidth(width)
     if hasattr(color, 'a'):
         surface.setStrokeStyle(color)
     else:
         surface.setStrokeStyle(Color(color))
     surface.stroke()
     xpts = [pt[0] for pt in pointlist]
     ypts = [pt[1] for pt in pointlist]
     xmin, xmax = min(xpts), max(xpts)
     ymin, ymax = min(ypts), max(ypts)
     if surface._display:
         return surface._display._surface_rect.clip(
             Rect(xmin, ymin, xmax - xmin + 1, ymax - ymin + 1))
     else:
         return surface.get_rect().clip(
             Rect(xmin, ymin, xmax - xmin + 1, ymax - ymin + 1))
Exemplo n.º 4
0
 def fill(self, color=None, rect=None):
     """
     Fill surface with color.
     """
     if color is None:
         HTML5Canvas.fill(self)
         return
     if color:
         if self._fill_style != color:
             self._fill_style = color
             if hasattr(color, 'a'):
                 self.setFillStyle(color)
             else:
                 self.setFillStyle(Color(color))
         if not rect:
             _rect = Rect(0, 0, self.width, self.height)
         else:
             if self._display:
                 surface_rect = self._display._surface_rect
             else:
                 surface_rect = self.get_rect()
             if hasattr(rect, 'width'):
                 _rect = surface_rect.clip( rect )
             else:
                 _rect = surface_rect.clip( Rect(rect) )
             if not _rect.width or not _rect.height:
                 return _rect
         self.fillRect(_rect.x, _rect.y, _rect.width, _rect.height)
     else:
         _rect = Rect(0, 0, self.width, self.height)
         self.clear()
     return _rect
Exemplo n.º 5
0
 def polygon(self, surface, color, pointlist, width=0):
     """
     Draw polygon shape, and returns bounding Rect.
     Argument include surface to draw, color, and pointlist.
     Optional width argument of outline, which defaults to 0 for filled shape.
     """
     surface.beginPath()
     surface.moveTo(*pointlist[0])
     for point in pointlist[1:]:
         surface.lineTo(*point)
     surface.closePath()
     if width:
         surface.setLineWidth(width)
         if hasattr(color, 'a'):
             surface.setStrokeStyle(color)
         else:
             surface.setStrokeStyle(Color(color))
         surface.stroke()
     else:
         if hasattr(color, 'a'):
             surface.setFillStyle(color)
         else:
             surface.setFillStyle(Color(color))
         surface.fill()
     xpts = [pt[0] for pt in pointlist]
     ypts = [pt[1] for pt in pointlist]
     xmin, xmax = min(xpts), max(xpts)
     ymin, ymax = min(ypts), max(ypts)
     if surface._display:
         return surface._display._surface_rect.clip(
             Rect(xmin, ymin, xmax - xmin + 1, ymax - ymin + 1))
     else:
         return surface.get_rect().clip(
             Rect(xmin, ymin, xmax - xmin + 1, ymax - ymin + 1))
Exemplo n.º 6
0
 def get_rect(self, **attr):
     """
     Return rect of the surface.
     An optional keyword argument of the rect position.
     """
     rect = Rect(0, 0, self.width, self.height)
     for key in attr:
         rect.__setattr__(key,attr[key])
     return rect
Exemplo n.º 7
0
 def subsurface(self, rect):
     """
     Return Surface that represents a subsurface.
     The rect argument is the area of the subsurface.
     Argument can be 't'/'f' for data sync to/from subsurface.
     """
     if rect in ('t', 'f'):
         if not self._super_surface:
             return
         if rect == 't':
             self.drawImage(self._super_surface.canvas, self._offset[0],
                            self._offset[1], self.width, self.height, 0, 0,
                            self.width, self.height)
         else:
             self._super_surface.drawImage(self.canvas, self._offset[0],
                                           self._offset[1])
         return
     if hasattr(rect, 'width'):
         _rect = rect
     else:
         _rect = Rect(rect)
     surf_rect = self.get_rect()
     if not surf_rect.contains(_rect):
         raise ValueError('subsurface outside surface area')
     surface = self.getSubimage(_rect.x, _rect.y, _rect.width, _rect.height)
     surface._super_surface = self
     surface._offset = (_rect.x, _rect.y)
     surface._colorkey = self._colorkey
     return surface
Exemplo n.º 8
0
 def rect(self, surface, color, rect, width=0):
     """
     Draw rectangle shape, and returns bounding Rect.
     Argument include surface to draw, color, Rect.
     Optional width argument of outline, which defaults to 0 for filled shape.
     """
     if hasattr(rect, 'width'):
         _rect = rect
     else:
         _rect = Rect(rect)
     if width:
         surface.setLineWidth(width)
         if hasattr(color, 'a'):
             surface.setStrokeStyle(color)
         else:
             surface.setStrokeStyle(Color(color))
         surface.strokeRect(_rect.x, _rect.y, _rect.width, _rect.height)
     else:
         if hasattr(color, 'a'):
             surface.setFillStyle(color)
         else:
             surface.setFillStyle(Color(color))
         surface.fillRect(_rect.x, _rect.y, _rect.width, _rect.height)
     if surface._display:
         return surface._display._surface_rect.clip(_rect)
     else:
         return surface.get_rect().clip(_rect)
Exemplo n.º 9
0
 def arc(self, surface, color, rect, start_angle, stop_angle, width=1):
     """
     Draw arc shape, and returns bounding Rect.
     Argument include surface to draw, color, rect, start_angle, stop_angle.
     Optional width argument of outline.
     """
     if hasattr(rect, 'width'):
         _rect = rect
     else:
         _rect = Rect(rect)
     if _rect.width == _rect.height:
         surface.beginPath()
         surface.arc(_rect.x + int(_rect.width / 2),
                     _rect.y + int(_rect.height / 2), int(_rect.width / 2),
                     -start_angle, -stop_angle, True)
         if width:
             surface.setLineWidth(width)
             if hasattr(color, 'a'):
                 surface.setStrokeStyle(color)
             else:
                 surface.setStrokeStyle(Color(color))
             surface.stroke()
         else:
             surface.closePath()
             if hasattr(color, 'a'):
                 surface.setFillStyle(color)
             else:
                 surface.setFillStyle(Color(color))
             surface.fill()
     else:
         surface.saveContext()
         surface.translate(_rect.x + int(_rect.width / 2),
                           _rect.y + int(_rect.height / 2))
         if _rect.width >= _rect.height:
             surface.scale(_rect.width / (_rect.height * 1.0), 1)
             radius = _rect.height / 2
         else:
             surface.scale(1, _rect.height / (_rect.width * 1.0))
             radius = _rect.width / 2
         surface.beginPath()
         surface.arc(0, 0, radius, -start_angle, -stop_angle, True)
         if width:
             surface.setLineWidth(width)
             if hasattr(color, 'a'):
                 surface.setStrokeStyle(color)
             else:
                 surface.setStrokeStyle(Color(color))
             surface.stroke()
         else:
             surface.closePath()
             if hasattr(color, 'a'):
                 surface.setFillStyle(color)
             else:
                 surface.setFillStyle(Color(color))
             surface.fill()
         surface.restoreContext()
     if surface._display:
         return surface._display._surface_rect.clip(_rect)
     else:
         return surface.get_rect().clip(_rect)
Exemplo n.º 10
0
 def _get_rect(self):
     if self._rect_num < self._rect_len:
         return self._rect_list[self._rect_num]
     else:
         self._rect_list.append(Rect(0, 0, 0, 0))
         self._rect_len += 1
         return self._rect_list[self._rect_num]
Exemplo n.º 11
0
def ellipse(surface, color, rect, width=0):
    """
    Draw ellipse shape, and returns bounding Rect.
    Arguments include surface to draw, color, and rect.
    Optional width argument of outline, which defaults to 0 for filled shape.
    """
    if hasattr(rect, 'width'):
        _rect = rect
    else:
        _rect = Rect(rect)
    surface.saveContext()
    surface.translate(_rect.x + int(_rect.width / 2),
                      _rect.y + int(_rect.height / 2))
    if _rect.width >= _rect.height:
        surface.scale(_rect.width / (_rect.height * 1.0), 1)
        radius = int(_rect.height / 2)
    else:
        surface.scale(1, _rect.height / (_rect.width * 1.0))
        radius = int(_rect.width / 2)
    surface.beginPath()
    surface.arc(0, 0, radius, 0, 2 * _pi, False)
    if width:
        surface.setLineWidth(width)
        if surface._stroke_style != color:
            surface._stroke_style = color
            if hasattr(color, 'a'):
                surface.setStrokeStyle(color)
            else:
                surface.setStrokeStyle(Color(color))
        surface.stroke()
    else:
        if surface._fill_style != color:
            surface._fill_style = color
            if hasattr(color, 'a'):
                surface.setFillStyle(color)
            else:
                surface.setFillStyle(Color(color))
        surface.fill()
    surface.restoreContext()
    if not _return_rect:
        return None
    if surface._display:
        return surface._display._surface_rect.clip(_rect)
    else:
        return surface.get_rect().clip(_rect)
Exemplo n.º 12
0
 def fill(self, color=None, rect=None):
     """
     Fill surface with color.
     """
     if color is None:
         HTML5Canvas.fill(self)
         return None
     if self._fill_style != color:
         self._fill_style = color
         if hasattr(color, 'a'):
             self.setFillStyle(color)
         else:
             self.setFillStyle(Color(color))
     if not _return_rect:
         if rect is None:
             self.fillRect(0, 0, self.width, self.height)
         else:
             self.fillRect(rect[0], rect[1], rect[2], rect[3])
         return None
     if rect is None:
         _rect = Rect(0, 0, self.width, self.height)
         self.fillRect(_rect.x, _rect.y, _rect.width, _rect.height)
     else:
         if self._display:
             if hasattr(rect, 'width'):
                 _rect = self._display._surface_rect.clip(rect)
             else:
                 _rect_ = rectPool.get(rect[0], rect[1], rect[2], rect[3])
                 _rect = self._display._surface_rect.clip(_rect_)
                 rectPool.append(_rect_)
         else:
             surface_rect = rectPool.get(0, 0, self.width, self.height)
             if hasattr(rect, 'width'):
                 _rect = surface_rect.clip(rect)
             else:
                 _rect_ = rectPool.get(rect[0], rect[1], rect[2], rect[3])
                 _rect = surface_rect.clip(_rect_)
                 rectPool.append(_rect_)
             rectPool.append(surface_rect)
         if _rect.width and _rect.height:
             self.fillRect(_rect.x, _rect.y, _rect.width, _rect.height)
     return _rect
Exemplo n.º 13
0
 def test_eq(self):
     """
     Test if object __eq__ method is called.
     """
     return (Rect(0,0,20,20) == Rect(0,0,20,20))
Exemplo n.º 14
0
 def test_getattr(self):
     """
     Test if object __getattr__ method is called.
     """
     return (Rect(0,0,20,20).center == (10,10))
Exemplo n.º 15
0
 def get_rect(self, **attr):
     rect = Rect(0, 0, self.width, self.height)
     for key in attr:
         rect.__setattr__(key,attr[key])
     return rect