Started to prototype a fisheye list
authorEnrico Zini <enrico@enricozini.org>
Tue, 8 Dec 2009 14:34:33 +0000 (14:34 +0000)
committerEnrico Zini <enrico@enricozini.org>
Tue, 8 Dec 2009 14:34:33 +0000 (14:34 +0000)
src/fisheye.vala [new file with mode: 0644]

diff --git a/src/fisheye.vala b/src/fisheye.vala
new file mode 100644 (file)
index 0000000..1d844d9
--- /dev/null
@@ -0,0 +1,195 @@
+/*
+ * zavai - simple interface to the OpenMoko (or to the FSO stack)
+ *
+ * Copyright (C) 2009  Enrico Zini <enrico@enricozini.org>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ */
+
+using GLib;
+
+public class FisheyeList : Gtk.DrawingArea
+{
+       private string[] list;
+       protected int cur_el;
+
+       public FisheyeList()
+       {
+               list = new string[300];
+               for (int i = 0; i < 300; ++i)
+                       list[i] = "Antani %d".printf(i);
+
+               cur_el = 50;
+
+               add_events(Gdk.EventMask.POINTER_MOTION_MASK
+                        | Gdk.EventMask.BUTTON_PRESS_MASK
+                        | Gdk.EventMask.BUTTON_RELEASE_MASK);
+       }
+
+       /* Mouse button got pressed over widget */
+       public override bool button_press_event(Gdk.EventButton event)
+       {
+               // ...
+               return false;
+       }
+
+       /* Mouse button got released */
+       public override bool button_release_event(Gdk.EventButton event)
+       {
+               // ...
+               return false;
+       }
+
+       /* 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();
+               return false;
+       }
+
+       /* Widget is asked to draw itself */
+       public override bool expose_event (Gdk.EventExpose event) {
+
+               // Create a Cairo context
+               var cr = Gdk.cairo_create (this.window);
+
+               // Set clipping area in order to avoid unnecessary drawing
+               cr.rectangle(event.area.x, event.area.y,
+                               event.area.width, event.area.height);
+               cr.clip();
+
+               draw(cr);
+
+               return false;
+       }
+
+       protected double el_y(int idx)
+       {
+               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);
+               //stderr.printf("%d %f %f\n", idx, undy, pos);
+               return pos;
+       }
+
+       protected void draw(Cairo.Context context)
+       {
+               context.set_source_rgb(1, 1, 1);
+               context.paint();
+               context.set_source_rgb(0, 0, 0);
+
+               context.translate (0, 0);
+               context.scale(allocation.width, allocation.height);
+
+
+               // Layout items
+               // item_pos(idx) = idx/list.length
+
+               context.set_line_width (0.001);
+               context.select_font_face("Sans", Cairo.FontSlant.NORMAL, Cairo.FontWeight.NORMAL);
+               for (int idx = 0; idx < list.length; ++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;
+
+                       //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();
+
+                       context.set_source_rgba(0, 0, 0, 1);
+                       context.new_path();
+                       context.move_to(0, pos);
+                       context.set_font_size(y1-y0);
+                       context.text_path(list[idx]);
+                       context.fill();
+                       context.stroke();
+               }
+       }
+
+    /*
+     * The following function is adapted from Prefuse's FisheyeDistortion.java.
+     *
+     * A relevant annotation from Prefuse:
+     *
+     * For more details on this form of transformation, see Manojit Sarkar and 
+     * Marc H. Brown, "Graphical Fisheye Views of Graphs", in Proceedings of 
+     * CHI'92, Human Factors in Computing Systems, p. 83-91, 1992. Available
+     * online at <a href="http://citeseer.ist.psu.edu/sarkar92graphical.html">
+     * http://citeseer.ist.psu.edu/sarkar92graphical.html</a>. 
+     */
+
+    /*
+     * Distorts an item's coordinate.
+     * @param x the undistorted coordinate
+     * @param coordinate of the anchor or focus point
+     * @param d disortion factor
+     * @param min the beginning of the display
+     * @param max the end of the display
+     * @return the distorted coordinate
+     */
+    private double fisheye(double x, double a, double d, double min, double max)
+    {
+        if ( d != 0 ) {
+            bool left = x<a;
+            double v;
+           double m = (left ? a-min : max-a);
+            if ( m == 0 ) m = max-min;
+            v = Math.fabs(x - a) / m;
+            v = (d+1)/(d+(1/v));
+            return (left?-1:1)*m*v + a;
+        } else {
+            return x;
+        }
+    }
+}
+
+public class Fisheye : Gtk.Window
+{
+       public Fisheye()
+       {
+               title = "Fisheye";
+               destroy += Gtk.main_quit;
+
+               var list = new FisheyeList();
+               add(list);
+       }
+}
+
+static int main (string[] args) {
+       Gtk.init (ref args);
+
+       var fe = new Fisheye();
+       fe.set_size_request(300, 500);
+       fe.show_all();
+
+       Gtk.main();
+
+       return 0;
+}