[gegl] lua-ui: add canvas ui for rotate, abstract out touch drawing



commit efd48d2aff583b82ab4754a093b8c084ed34b1bc
Author: Øyvind Kolås <pippin gimp org>
Date:   Sat Feb 23 11:31:28 2019 +0100

    lua-ui: add canvas ui for rotate, abstract out touch drawing
    
    Made gegl:crop symlink to gegl:rectangle and gegl:radial-gradient to
    gegl:linear-gradient, since these share properties and can share interactions
    at least as initial prorotyping.
    
    Also added gegl-rotate, origin is settable - but some parts of the ui/gegl
    transformation interaction are not fully aware of the origin leading to
    puzzling interaction.

 bin/lua/gegl_crop.lua            | 41 +---------------------------------------
 bin/lua/gegl_linear-gradient.lua | 16 +++++-----------
 bin/lua/gegl_radial-gradient.lua | 40 +--------------------------------------
 bin/lua/gegl_rectangle.lua       | 24 +++++++++++------------
 bin/lua/gegl_rotate.lua          | 40 +++++++++++++++++++++++++++++++++++++++
 bin/lua/gegl_translate.lua       |  9 ++-------
 bin/lua/init.lua                 | 24 +++++++++++++++++++++++
 bin/lua/viewer.lua               | 15 ---------------
 8 files changed, 84 insertions(+), 125 deletions(-)
---
diff --git a/bin/lua/gegl_crop.lua b/bin/lua/gegl_crop.lua
deleted file mode 100644
index c428f9d0d..000000000
--- a/bin/lua/gegl_crop.lua
+++ /dev/null
@@ -1,40 +0,0 @@
-local x = active:get_property("x").value
-local y = active:get_property("y").value
-
-cr:new_path()
-cr:arc(x, y, dim/2, 0, 3.1415 * 2)
-
-mrg:listen(Mrg.DRAG, function(ev)
-  if ev.type == Mrg.DRAG_MOTION then
-    local x = active:get_property("x").value
-    local y = active:get_property("y").value
-    x = x + ev.delta_x
-    y = y + ev.delta_y
-    active:set_property("y", GObject.Value(GObject.Type.DOUBLE, y))
-    active:set_property("x", GObject.Value(GObject.Type.DOUBLE, x))
-    ev:stop_propagate();
-  end
-end)
-
-cr:set_source_rgba(1,0,0,0.5)
-cr:fill()
-
-local w = active:get_property("width").value
-local h = active:get_property("height").value
-
-cr:new_path()
-cr:arc(x + w, y + h, dim/2, 0, 3.1415 * 2)
-mrg:listen(Mrg.DRAG, function(ev)
-  if ev.type == Mrg.DRAG_MOTION then
-    local w = active:get_property("width").value
-    local h = active:get_property("height").value
-    w = w + ev.delta_x
-    h = h + ev.delta_y
-    active:set_property("width", GObject.Value(GObject.Type.DOUBLE, w))
-    active:set_property("height", GObject.Value(GObject.Type.DOUBLE, h))
-    ev:stop_propagate();
-  end
-end)
-cr:set_source_rgba(1,0,0,0.5)
-cr:fill()
-
diff --git a/bin/lua/gegl_crop.lua b/bin/lua/gegl_crop.lua
new file mode 120000
index 000000000..58221d6aa
--- /dev/null
+++ b/bin/lua/gegl_crop.lua
@@ -0,0 +1 @@
+gegl_rectangle.lua
\ No newline at end of file
diff --git a/bin/lua/gegl_linear-gradient.lua b/bin/lua/gegl_linear-gradient.lua
index 32239ee22..0c3353043 100644
--- a/bin/lua/gegl_linear-gradient.lua
+++ b/bin/lua/gegl_linear-gradient.lua
@@ -1,10 +1,10 @@
 
 local start_x = active:get_property("start-x").value
 local start_y = active:get_property("start-y").value
+local end_x = active:get_property("end-x").value
+local end_y = active:get_property("end-y").value
 
-cr:new_path()
-cr:arc(start_x, start_y, dim/2, 0, 3.1415 * 2)
-
+touch_point(start_x, start_y)
 mrg:listen(Mrg.DRAG, function(ev)
   if ev.type == Mrg.DRAG_MOTION then
     local start_x = active:get_property("start-x").value
@@ -16,13 +16,8 @@ mrg:listen(Mrg.DRAG, function(ev)
     ev:stop_propagate();
   end
 end)
-cr:set_source_rgba(1,0,0,0.5)
-cr:fill()
 
-local end_x = active:get_property("end-x").value
-local end_y = active:get_property("end-y").value
-
-cr:arc(end_x, end_y, dim/2, 0, 3.1415 * 2)
+touch_point(end_x, end_y)
 mrg:listen(Mrg.DRAG, function(ev)
   if ev.type == Mrg.DRAG_MOTION then
     local end_x = active:get_property("end-x").value
@@ -34,6 +29,5 @@ mrg:listen(Mrg.DRAG, function(ev)
     ev:stop_propagate();
   end
 end)
-cr:set_source_rgba(1,0,0,0.5)
-cr:fill()
+cr:new_path()
 
diff --git a/bin/lua/gegl_radial-gradient.lua b/bin/lua/gegl_radial-gradient.lua
deleted file mode 100644
index 32239ee22..000000000
--- a/bin/lua/gegl_radial-gradient.lua
+++ /dev/null
@@ -1,39 +0,0 @@
-
-local start_x = active:get_property("start-x").value
-local start_y = active:get_property("start-y").value
-
-cr:new_path()
-cr:arc(start_x, start_y, dim/2, 0, 3.1415 * 2)
-
-mrg:listen(Mrg.DRAG, function(ev)
-  if ev.type == Mrg.DRAG_MOTION then
-    local start_x = active:get_property("start-x").value
-    local start_y = active:get_property("start-y").value
-    start_x = start_x + ev.delta_x
-    start_y = start_y + ev.delta_y
-    active:set_property("start-y", GObject.Value(GObject.Type.DOUBLE, start_y))
-    active:set_property("start-x", GObject.Value(GObject.Type.DOUBLE, start_x))
-    ev:stop_propagate();
-  end
-end)
-cr:set_source_rgba(1,0,0,0.5)
-cr:fill()
-
-local end_x = active:get_property("end-x").value
-local end_y = active:get_property("end-y").value
-
-cr:arc(end_x, end_y, dim/2, 0, 3.1415 * 2)
-mrg:listen(Mrg.DRAG, function(ev)
-  if ev.type == Mrg.DRAG_MOTION then
-    local end_x = active:get_property("end-x").value
-    local end_y = active:get_property("end-y").value
-    end_x = end_x + ev.delta_x
-    end_y = end_y + ev.delta_y
-    active:set_property("end-y", GObject.Value(GObject.Type.DOUBLE, end_y))
-    active:set_property("end-x", GObject.Value(GObject.Type.DOUBLE, end_x))
-    ev:stop_propagate();
-  end
-end)
-cr:set_source_rgba(1,0,0,0.5)
-cr:fill()
-
diff --git a/bin/lua/gegl_radial-gradient.lua b/bin/lua/gegl_radial-gradient.lua
new file mode 120000
index 000000000..3287c383f
--- /dev/null
+++ b/bin/lua/gegl_radial-gradient.lua
@@ -0,0 +1 @@
+gegl_linear-gradient.lua
\ No newline at end of file
diff --git a/bin/lua/gegl_rectangle.lua b/bin/lua/gegl_rectangle.lua
index c428f9d0d..1b0635f68 100644
--- a/bin/lua/gegl_rectangle.lua
+++ b/bin/lua/gegl_rectangle.lua
@@ -1,29 +1,28 @@
 local x = active:get_property("x").value
 local y = active:get_property("y").value
+local w = active:get_property("width").value
+local h = active:get_property("height").value
 
-cr:new_path()
-cr:arc(x, y, dim/2, 0, 3.1415 * 2)
-
+touch_point(x,y)
 mrg:listen(Mrg.DRAG, function(ev)
   if ev.type == Mrg.DRAG_MOTION then
     local x = active:get_property("x").value
     local y = active:get_property("y").value
+    local w = active:get_property("width").value
+    local h = active:get_property("height").value
     x = x + ev.delta_x
     y = y + ev.delta_y
+    w = w - ev.delta_x
+    h = h - ev.delta_y
     active:set_property("y", GObject.Value(GObject.Type.DOUBLE, y))
     active:set_property("x", GObject.Value(GObject.Type.DOUBLE, x))
+    active:set_property("width", GObject.Value(GObject.Type.DOUBLE, w))
+    active:set_property("height", GObject.Value(GObject.Type.DOUBLE, h))
     ev:stop_propagate();
   end
 end)
 
-cr:set_source_rgba(1,0,0,0.5)
-cr:fill()
-
-local w = active:get_property("width").value
-local h = active:get_property("height").value
-
-cr:new_path()
-cr:arc(x + w, y + h, dim/2, 0, 3.1415 * 2)
+touch_point(x+w,y+h)
 mrg:listen(Mrg.DRAG, function(ev)
   if ev.type == Mrg.DRAG_MOTION then
     local w = active:get_property("width").value
@@ -35,6 +34,5 @@ mrg:listen(Mrg.DRAG, function(ev)
     ev:stop_propagate();
   end
 end)
-cr:set_source_rgba(1,0,0,0.5)
-cr:fill()
+cr:new_path()
 
diff --git a/bin/lua/gegl_rotate.lua b/bin/lua/gegl_rotate.lua
new file mode 100644
index 000000000..1aa787ea8
--- /dev/null
+++ b/bin/lua/gegl_rotate.lua
@@ -0,0 +1,40 @@
+local origin_x = active:get_property("origin-x").value
+local origin_y = active:get_property("origin-y").value
+local degrees =  active:get_property("degrees").value
+
+local rotate_x = origin_x + (dim * 2) * math.cos (-degrees/360.0 * 3.141529 * 2)
+local rotate_y = origin_y + (dim * 2) * math.sin (-degrees/360.0 * 3.141529 * 2)
+
+cr:new_path()
+cr:move_to(origin_x, origin_y)
+cr:line_to(rotate_x, rotate_y)
+contrasty_stroke()
+
+
+touch_point(origin_x, origin_y)
+mrg:listen(Mrg.DRAG, function(ev)
+  if ev.type == Mrg.DRAG_MOTION then
+    local x = active:get_property("origin-x").value
+    local y = active:get_property("origin-y").value
+    x = x + ev.delta_x
+    y = y + ev.delta_y
+    active:set_property("origin-y", GObject.Value(GObject.Type.DOUBLE, y))
+    active:set_property("origin-x", GObject.Value(GObject.Type.DOUBLE, x))
+    ev:stop_propagate();
+  end
+end)
+cr:new_path()
+
+touch_point(rotate_x, rotate_y)
+mrg:listen(Mrg.DRAG, function(ev)
+  if ev.type == Mrg.DRAG_MOTION then
+    local new_degrees = math.atan2(ev.x-origin_x,ev.y-origin_y)*180/3.141529 - 90
+    if new_degrees < 0 then new_degrees = new_degrees + 360 end
+    active:set_property("degrees",
+           GObject.Value(GObject.Type.DOUBLE, new_degrees))
+    ev:stop_propagate();
+  end
+end)
+cr:new_path()
+
+
diff --git a/bin/lua/gegl_translate.lua b/bin/lua/gegl_translate.lua
index b680ba078..49d03317a 100644
--- a/bin/lua/gegl_translate.lua
+++ b/bin/lua/gegl_translate.lua
@@ -1,9 +1,7 @@
 local x = active:get_property("x").value
 local y = active:get_property("y").value
 
-cr:new_path()
-cr:arc(x, y, dim/2, 0, 3.1415 * 2)
-
+touch_point(x,y)
 mrg:listen(Mrg.DRAG, function(ev)
   if ev.type == Mrg.DRAG_MOTION then
     local x = active:get_property("x").value
@@ -15,7 +13,4 @@ mrg:listen(Mrg.DRAG, function(ev)
     ev:stop_propagate();
   end
 end)
-
-cr:set_source_rgba(1,0,0,0.5)
-cr:fill()
-
+cr:new_path()
diff --git a/bin/lua/init.lua b/bin/lua/init.lua
index 3368c876c..90c693ed2 100644
--- a/bin/lua/init.lua
+++ b/bin/lua/init.lua
@@ -117,3 +117,27 @@ int argvs_eval (const char *str);
 
 o = ffi.C.app_state()
 mrg = o.mrg
+
+
+function touch_point(x, y)
+  cr:new_path()
+  cr:arc(x, y, dim/2, 0, 3.1415 * 2)
+  cr:set_source_rgba(1,0,0,0.5)
+  cr:fill_preserve()
+end
+
+function contrasty_stroke()
+  local x0 = 6.0
+  local y0 = 6.0;
+  local x1 = 4.0
+  local y1 = 4.0;
+
+  x0, y0 = cr:device_to_user_distance (x0, y0)
+  x1, y1 = cr:device_to_user_distance (x1, y1)
+  cr:set_source_rgba (0,0,0,0.5);
+  cr:set_line_width (y0);
+  cr:stroke_preserve ()
+  cr:set_source_rgba (1,1,1,0.5);
+  cr:set_line_width (y1);
+  cr:stroke ();
+end
diff --git a/bin/lua/viewer.lua b/bin/lua/viewer.lua
index 1f911a844..6c30f4759 100644
--- a/bin/lua/viewer.lua
+++ b/bin/lua/viewer.lua
@@ -169,21 +169,6 @@ function draw_edit(x,y,w,h)
   cr:arc (x+0.5*w, y+0.5*h, h * .4, 0.0, 3.1415 * 2);
 end
 
-function contrasty_stroke()
-  local x0 = 6.0
-  local y0 = 6.0;
-  local x1 = 4.0
-  local y1 = 4.0;
-
-  x0, y0 = cr:device_to_user_distance (x0, y0)
-  x1, y1 = cr:device_to_user_distance (x1, y1)
-  cr:set_source_rgba (0,0,0,0.5);
-  cr:set_line_width (y0);
-  cr:stroke_preserve ()
-  cr:set_source_rgba (1,1,1,0.5);
-  cr:set_line_width (y1);
-  cr:stroke ();
-end
 
 -- ui_viewer
 


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]