Fisheye with focus lock area
authorEnrico Zini <enrico@enricozini.org>
Tue, 8 Dec 2009 18:19:41 +0000 (18:19 +0000)
committerEnrico Zini <enrico@enricozini.org>
Tue, 8 Dec 2009 18:19:41 +0000 (18:19 +0000)
src/fisheye.vala

index 24fefeb30e91e6a380cdf570af3ea4dc89326027..7a8ccf0721e3427e317176bbd2d810e8f01658f5 100644 (file)
@@ -24,6 +24,15 @@ public class FisheyeList : Gtk.DrawingArea
 {
        private string[] list;
        protected int cur_el;
+       protected double distortion_factor;
+
+       // Number of items shown before and after the focus element
+       protected int focus_first;
+       protected int focus_end;
+       protected int focus_size;
+       protected double[] focus_starts;
+       protected bool focus_locked;
+       protected double focus_centre;
 
        public FisheyeList()
        {
@@ -31,7 +40,13 @@ public class FisheyeList : Gtk.DrawingArea
                for (int i = 0; i < 300; ++i)
                        list[i] = "Antani %d".printf(i);
 
-               cur_el = 50;
+               cur_el = 0;
+               focus_centre = 0.0;
+               focus_size = 20;
+               focus_starts = new double[focus_size + 1];
+               focus_locked = false;
+               distortion_factor = 30;
+               focus_layout();
 
                add_events(Gdk.EventMask.POINTER_MOTION_MASK
                         | Gdk.EventMask.BUTTON_PRESS_MASK
@@ -41,13 +56,14 @@ public class FisheyeList : Gtk.DrawingArea
        /* Mouse button got pressed over widget */
        public override bool button_press_event(Gdk.EventButton event)
        {
-               // ...
+               stderr.printf("Mouse pressed on %d %s\n", cur_el, list[cur_el]);
                return false;
        }
 
        /* Mouse button got released */
        public override bool button_release_event(Gdk.EventButton event)
        {
+               stderr.printf("Mouse released on %d %s\n", cur_el, list[cur_el]);
                // ...
                return false;
        }
@@ -55,9 +71,33 @@ public class FisheyeList : Gtk.DrawingArea
        /* Mouse pointer moved over widget */
        public override bool motion_notify_event(Gdk.EventMotion event)
        {
-               cur_el = (int)Math.round(event.y * list.length / allocation.height);
-               stderr.printf("MOTION %f %f CE %d\n", event.x, event.y, cur_el);
-               queue_draw();
+               int old_cur_el = cur_el;
+               double x = event.x / allocation.width;
+               double y = event.y / allocation.height;
+
+               focus_locked = x > 0.5 && y >= focus_starts[0] && y < focus_starts[focus_end - focus_first];
+
+               if (focus_locked)
+               {
+                       for (int idx = focus_first; idx < focus_end; ++idx)
+                               if (y < focus_starts[idx-focus_first+1])
+                               {
+                                       cur_el = idx;
+                                       break;
+                               }
+
+               } else {
+                       cur_el = (int)Math.round(y * list.length);
+                       if (old_cur_el != cur_el)
+                               focus_layout();
+               }
+
+               //stderr.printf("MOTION %f %f CE %d\n", event.x, event.y, cur_el);
+               if (old_cur_el != cur_el)
+               {
+                       queue_draw();
+                       old_cur_el = cur_el;
+               }
                return false;
        }
 
@@ -81,58 +121,80 @@ public class FisheyeList : Gtk.DrawingArea
        {
                double layout_min = 0;
                double layout_max = 1;
-               double distortion_factor = 30;
-               // Anchor point
-               double anchor = (double)cur_el/list.length;
                // Undistorted Y
                double undy = (double)idx/list.length;
                // Distorted position
-               double pos = fisheye(undy, anchor, distortion_factor, layout_min, layout_max);
+               double pos = fisheye(undy, focus_centre, distortion_factor, layout_min, layout_max);
                //stderr.printf("%d %f %f\n", idx, undy, pos);
                return pos;
        }
 
+       protected void focus_layout()
+       {
+               // Anchor point
+               focus_centre = (double)cur_el/list.length;
+
+               focus_first = cur_el > focus_size/2 ? cur_el-focus_size/2 : 0;
+               focus_end = focus_first + focus_size;
+               if (focus_end >= list.length) focus_end = list.length;
+
+               // 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;
+
+                       focus_starts[idx - focus_first] = y0;
+                       focus_starts[idx - focus_first + 1] = y1;
+               }
+       }
+
        protected void draw(Cairo.Context context)
        {
+               // White background (FIXME: get from style)
                context.set_source_rgb(1, 1, 1);
                context.paint();
                context.set_source_rgb(0, 0, 0);
 
+               // Normalise coordinates
                context.translate (0, 0);
                context.scale(allocation.width, allocation.height);
 
-
-               // Layout items
-               // item_pos(idx) = idx/list.length
-
+               // Paint items around focus
                context.set_line_width (0.001);
                context.select_font_face("Sans", Cairo.FontSlant.NORMAL, Cairo.FontWeight.NORMAL);
-               int start = cur_el > 20 ? cur_el-20 : 0;
-               int end = cur_el < list.length - 20 ? cur_el + 20 : list.length;
-               for (int idx = start; idx < end; ++idx)
+               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;
+                       double y0 = focus_starts[idx - focus_first];
+                       double y1 = focus_starts[idx - focus_first + 1];
 
-                       //stderr.printf("  %f->%f  (%f)\n", y0, y1, y1-y0);
-
-                       context.set_source_rgba(idx%2, 1-idx%2, 0, 1);
-                       context.new_path();
-                       context.rectangle(0, y0, 1, y1-y0);
-                       context.fill();
-                       context.stroke();
+                       if (idx == cur_el)
+                       {
+                               context.set_source_rgba(0.4, 0.4, 1, 1);
+                               context.new_path();
+                               context.rectangle(0, y0, 1, y1-y0);
+                               context.fill();
+                               context.stroke();
+                       }
 
                        context.set_source_rgba(0, 0, 0, 1);
                        context.new_path();
-                       context.move_to(0, pos);
+                       context.move_to(0, (y1+y0)/2);
                        context.set_font_size(y1-y0);
                        context.text_path(list[idx]);
                        context.fill();
                        context.stroke();
                }
+
+               // Paint focus lock area
+               context.set_source_rgba(0.8, 1, 1, 0.8);
+               context.new_path();
+               context.rectangle(0.5, focus_starts[0], 1, focus_starts[focus_end - focus_first]);
+               context.fill();
+               context.stroke();
        }
 
     /*