[Sugar-devel] [PATCH Paint Activity] Fixed aspect ratio mode for Shape tools (OLPC#3705)

Ayush Goyal ayush at seeta.in
Wed Oct 27 15:28:39 EDT 2010


Added fixed aspect ratio mode for line,ellipse and rectangle tool using Shift
key as mask.This allows drawing of straight lines & 45 degree lines from line 
tool,circle from ellipse tool and square from rectangle tool

Signed-off-by: Ayush Goyal <ayush at seeta.in>
---
 Area.py |   33 ++++++++++++++++++++++++++++++++-
 1 files changed, 32 insertions(+), 1 deletions(-)

diff --git a/Area.py b/Area.py
index ba06758..0bef28b 100644
--- a/Area.py
+++ b/Area.py
@@ -412,6 +412,12 @@ class Area(gtk.DrawingArea):
 
         coords = int(x), int(y)
 
+        if state & gtk.gdk.SHIFT_MASK:
+            if self.tool['name'] in ['rectangle', 'ellipse']:
+                coords = self._keep_selection_ratio(coords)
+            elif self.tool['name'] == 'line':
+                coords = self._keep_line_ratio(coords)
+
         if state & gtk.gdk.BUTTON1_MASK and self.pixmap != None:
             if self.tool['name'] == 'pencil':
                 self.d.brush(widget, coords, self.last,
@@ -530,11 +536,17 @@ class Area(gtk.DrawingArea):
             @param  event -- GdkEvent
         """
         coords = int(event.x), int(event.y)
+        if event.state & gtk.gdk.SHIFT_MASK:
+            if self.tool['name'] in ['rectangle', 'ellipse']:
+                coords = self._keep_selection_ratio(coords)
+            if self.tool['name'] == 'line':
+                coords = self._keep_line_ratio(coords)
+
         width, height = self.window.get_size()
         if self.desenha or self.sel_get_out:
             if self.tool['name'] == 'line':
                 self.pixmap.draw_line(self.gc_line, self.oldx, self.oldy,
-                    int(event.x), int(event.y))
+                                      coords[0], coords[1])
                 widget.queue_draw()
                 self.enableUndo(widget)
 
@@ -1411,3 +1423,22 @@ class Area(gtk.DrawingArea):
 
         return (self.oldx + sign(dx) * size,
                 self.oldy + sign(dy) * size)
+
+    def _keep_line_ratio(self, coords):
+
+        def sign(x):
+            return x and x / abs(x) or 0
+
+        dx = int(coords[0]) - self.oldx
+        dy = int(coords[1]) - self.oldy
+        size = max(abs(dx), abs(dy))
+
+        if abs(dx) > 0.5 * size and abs(dy) > 0.5 * size:
+            return (self.oldx + sign(dx) * size,
+                   self.oldy + sign(dy) * size)
+        elif abs(dx) < 0.5 * size and abs(dy) > 0.5 * size:
+            return (self.oldx,
+                   self.oldy + sign(dy) * size)
+        elif abs(dx) > 0.5 * size and abs(dy) < 0.5 * size:
+            return (self.oldx + sign(dx) * size,
+                   self.oldy)
-- 
1.7.1



More information about the Sugar-devel mailing list