Work on an unscaled drawing area
authorEnrico Zini <enrico@enricozini.org>
Tue, 8 Dec 2009 19:26:40 +0000 (19:26 +0000)
committerEnrico Zini <enrico@enricozini.org>
Tue, 8 Dec 2009 19:26:40 +0000 (19:26 +0000)
src/fisheye.vala

index 2c3610255e4fee67e0fae43119007c40b6494702..d6e9ae73086014c78fceb23538cd5eca146a5f07 100644 (file)
@@ -30,7 +30,7 @@ public class FisheyeList : Gtk.DrawingArea
        protected int focus_first;
        protected int focus_end;
        protected int focus_size;
-       protected double[] focus_starts;
+       protected int[] focus_starts;
        protected bool focus_locked;
        protected double focus_centre;
 
@@ -41,9 +41,9 @@ public class FisheyeList : Gtk.DrawingArea
                        list[i] = "Antani %d".printf(i);
 
                cur_el = 0;
-               focus_centre = 0.0;
+               focus_centre = 0;
                focus_size = 20;
-               focus_starts = new double[focus_size + 1];
+               focus_starts = new int[focus_size + 1];
                focus_locked = false;
                distortion_factor = 30;
                focus_layout();
@@ -72,10 +72,10 @@ public class FisheyeList : Gtk.DrawingArea
        public override bool motion_notify_event(Gdk.EventMotion event)
        {
                int old_cur_el = cur_el;
-               double x = event.x / allocation.width;
-               double y = event.y / allocation.height;
+               int x = (int)event.x;
+               int y = (int)event.y;
 
-               focus_locked = x < 0.5 && y >= focus_starts[0] && y < focus_starts[focus_end - focus_first];
+               focus_locked = x < allocation.width/2 && y >= focus_starts[0] && y < focus_starts[focus_end - focus_first];
 
                if (focus_locked)
                {
@@ -87,7 +87,7 @@ public class FisheyeList : Gtk.DrawingArea
                                }
 
                } else {
-                       cur_el = (int)Math.round(y * list.length);
+                       cur_el = y * list.length / allocation.height;
                        if (old_cur_el != cur_el)
                                focus_layout();
                }
@@ -117,22 +117,20 @@ public class FisheyeList : Gtk.DrawingArea
                return false;
        }
 
-       protected double el_y(int idx)
+       protected int el_y(int idx)
        {
-               double layout_min = 0;
-               double layout_max = 1;
                // Undistorted Y
-               double undy = (double)idx/list.length;
+               double undy = (double)idx * allocation.height / list.length;
                // Distorted position
-               double pos = fisheye(undy, focus_centre, distortion_factor, layout_min, layout_max);
+               double pos = fisheye(undy, focus_centre, distortion_factor, 0, allocation.height);
                //stderr.printf("%d %f %f\n", idx, undy, pos);
-               return pos;
+               return (int)Math.round(pos);
        }
 
        protected void focus_layout()
        {
                // Anchor point
-               focus_centre = (double)cur_el/list.length;
+               focus_centre = (double)cur_el*allocation.height/list.length;
 
                focus_first = cur_el > focus_size/2 ? cur_el-focus_size/2 : 0;
                focus_end = focus_first + focus_size;
@@ -141,11 +139,11 @@ public class FisheyeList : Gtk.DrawingArea
                // Compute starting positions for all items in focus
                for (int idx = focus_first; idx < focus_end; ++idx)
                {
-                       double posprev = idx == 0 ? 0 : el_y(idx-1);
-                       double pos = el_y(idx);
-                       double posnext = idx == list.length-1 ? 1 : el_y(idx+1);
-                       double y0 = (pos+posprev)/2;
-                       double y1 = (pos+posnext)/2;
+                       int posprev = idx == 0 ? 0 : el_y(idx-1);
+                       int pos = el_y(idx);
+                       int posnext = idx == list.length-1 ? 1 : el_y(idx+1);
+                       int y0 = (pos+posprev)/2;
+                       int y1 = (pos+posnext)/2;
 
                        focus_starts[idx - focus_first] = y0;
                        focus_starts[idx - focus_first + 1] = y1;
@@ -171,21 +169,17 @@ public class FisheyeList : Gtk.DrawingArea
                Gdk.cairo_set_source_color(context, style.bg[Gtk.StateType.NORMAL]);
                context.paint();
 
-               // Normalise coordinates
-               context.translate (0, 0);
-               context.scale(allocation.width, allocation.height);
-
                // Focus lock area
                Gdk.cairo_set_source_color(context, style.bg[Gtk.StateType.ACTIVE]);
                context.new_path();
-               context.rectangle(0, focus_starts[0], 0.5, focus_starts[focus_end - focus_first]);
+               context.rectangle(0, focus_starts[0], allocation.width/2, focus_starts[focus_end - focus_first]);
                context.fill();
                context.stroke();
 
                // Focus movement area
                Gdk.cairo_set_source_color(context, style.bg[Gtk.StateType.INSENSITIVE]);
                context.new_path();
-               context.rectangle(0.5, 0, 1, 1);
+               context.rectangle(allocation.width/2, 0, allocation.width, allocation.height);
                context.fill();
                context.stroke();
 
@@ -193,14 +187,14 @@ public class FisheyeList : Gtk.DrawingArea
                context.select_font_face(style.font_desc.get_family(), Cairo.FontSlant.NORMAL, Cairo.FontWeight.NORMAL);
                for (int idx = focus_first; idx < focus_end; ++idx)
                {
-                       double y0 = focus_starts[idx - focus_first];
-                       double y1 = focus_starts[idx - focus_first + 1];
+                       int y0 = focus_starts[idx - focus_first];
+                       int y1 = focus_starts[idx - focus_first + 1];
 
                        if (idx == cur_el)
                        {
                                Gdk.cairo_set_source_color(context, style.bg[Gtk.StateType.SELECTED]);
                                context.new_path();
-                               context.rectangle(0, y0, 1, y1-y0);
+                               context.rectangle(0, y0, allocation.width, y1-y0);
                                context.fill();
                                context.stroke();