Commit c9e6ef00 authored by Taddeus Kroes's avatar Taddeus Kroes

Added margin to bounding box.

parent 3d940d14
...@@ -33,10 +33,11 @@ class Rectangle(mt.RectangularArea): ...@@ -33,10 +33,11 @@ class Rectangle(mt.RectangularArea):
class Polygon(BoundingBoxArea): class Polygon(BoundingBoxArea):
def __init__(self, x, y, points, color=BLUE, border_color=RED): def __init__(self, x, y, points, margin=0, color=BLUE, border_color=RED):
super(Polygon, self).__init__(x, y, points) super(Polygon, self).__init__(x, y, points)
self.fill_color = color self.fill_color = color
self.border_color = border_color self.border_color = border_color
self.margin = margin
self.on_drag(self.handle_drag) self.on_drag(self.handle_drag)
self.on_pinch(self.handle_pinch) self.on_pinch(self.handle_pinch)
...@@ -59,10 +60,17 @@ class Polygon(BoundingBoxArea): ...@@ -59,10 +60,17 @@ class Polygon(BoundingBoxArea):
self.update_bounds() self.update_bounds()
refresh() refresh()
def contains(self, x, y):
m = self.margin
return self.x - m <= x < self.x + self.width + m \
and self.y - m <= y < self.y + self.height + m
def draw(self, cr): def draw(self, cr):
# Draw bounding box # Draw bounding box
if draw_bounding_boxes: if draw_bounding_boxes:
cr.rectangle(self.x, self.y, self.width, self.height) m = self.margin
cr.rectangle(self.x - m, self.y - m,
self.width + 2 * m, self.height + 2 * m)
cr.set_source_rgb(*self.border_color) cr.set_source_rgb(*self.border_color)
cr.set_line_width(3) cr.set_line_width(3)
cr.stroke() cr.stroke()
...@@ -205,7 +213,7 @@ def on_show(window): ...@@ -205,7 +213,7 @@ def on_show(window):
# Create blue rectangle # Create blue rectangle
x, y, w, h = 0, 0, 250, 150 x, y, w, h = 0, 0, 250, 150
rect = Polygon(x, y, [(0, 0), (0, h), (w, h), (w, 0)]) rect = Polygon(x, y, [(0, 0), (0, h), (w, h), (w, 0)], margin=20)
draw_objects.append(rect) draw_objects.append(rect)
root.add_area(rect) root.add_area(rect)
...@@ -215,7 +223,8 @@ def on_show(window): ...@@ -215,7 +223,8 @@ def on_show(window):
# Create green triangle # Create green triangle
x, y, w = 400, 400, 200 x, y, w = 400, 400, 200
h = triangle_height(w) h = triangle_height(w)
triangle = Polygon(x, y, [(0, h), (w, h), (w / 2, 0)], color=GREEN) triangle = Polygon(x, y, [(0, h), (w, h), (w / 2, 0)],
margin=20, color=GREEN)
draw_objects.append(triangle) draw_objects.append(triangle)
root.add_area(triangle) root.add_area(triangle)
...@@ -245,8 +254,8 @@ if __name__ == '__main__': ...@@ -245,8 +254,8 @@ if __name__ == '__main__':
# Parse arguments # Parse arguments
parser = create_parser() parser = create_parser()
parser.add_argument('-f', '--fullscreen', action='store_true', default=False, parser.add_argument('-f', '--fullscreen', action='store_true',
help='run in fullscreen initially') default=False, help='run in fullscreen initially')
args = parse_args(parser) args = parse_args(parser)
fullscreen = args.fullscreen fullscreen = args.fullscreen
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment