From 80d50194176bcc14d6fb5d9cc8776933a230e88f Mon Sep 17 00:00:00 2001 From: Enrico Zini Date: Tue, 8 Dec 2009 14:34:33 +0000 Subject: [PATCH] Started to prototype a fisheye list --- src/fisheye.vala | 195 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 195 insertions(+) create mode 100644 src/fisheye.vala diff --git a/src/fisheye.vala b/src/fisheye.vala new file mode 100644 index 0000000..1d844d9 --- /dev/null +++ b/src/fisheye.vala @@ -0,0 +1,195 @@ +/* + * zavai - simple interface to the OpenMoko (or to the FSO stack) + * + * Copyright (C) 2009 Enrico Zini + * + * 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 + * http://citeseer.ist.psu.edu/sarkar92graphical.html. + */ + + /* + * 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