The Birdfont Source Code


All Repositories / birdfont.git / blob – RSS feed

ZoomTool.vala in libbirdfont

This file is a part of the Birdfont project.

Contributing

Send patches or pull requests to johan.mattsson.m@gmail.com.
Clone this repository: git clone https://github.com/johanmattssonm/birdfont.git

Revisions

View the latest version of libbirdfont/ZoomTool.vala.
Fix zero length handles
1 /* 2 Copyright (C) 2012 Johan Mattsson 3 4 This library is free software; you can redistribute it and/or modify 5 it under the terms of the GNU Lesser General Public License as 6 published by the Free Software Foundation; either version 3 of the 7 License, or (at your option) any later version. 8 9 This library is distributed in the hope that it will be useful, but 10 WITHOUT ANY WARRANTY; without even the implied warranty of 11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 12 Lesser General Public License for more details. 13 */ 14 15 using Cairo; 16 17 namespace BirdFont { 18 19 public class ZoomTool : Tool { 20 21 int zoom_area_begin_x = -1; 22 int zoom_area_begin_y = -1; 23 24 int view_index = 0; 25 Gee.ArrayList<Tab> views; 26 27 public ZoomTool (string n) { 28 base (n, "Zoom"); 29 30 views = new Gee.ArrayList<Tab> (); 31 32 select_action.connect((self) => { 33 }); 34 35 select_action.connect((self) => { 36 }); 37 38 press_action.connect((self, b, x, y) => { 39 if (b == 1 && KeyBindings.modifier != CTRL) { 40 zoom_area_begin_x = x; 41 zoom_area_begin_y = y; 42 } 43 }); 44 45 move_action.connect((self, x, y) => { 46 Glyph g; 47 48 if (zoom_area_begin_x > 0) { 49 g = MainWindow.get_current_glyph (); 50 g.show_zoom_area (zoom_area_begin_x, zoom_area_begin_y, x, y); 51 } 52 }); 53 54 release_action.connect((self, b, x, y) => { 55 Glyph g; 56 57 if (b == 1 && KeyBindings.modifier != CTRL) { 58 store_current_view (); 59 60 g = MainWindow.get_current_glyph (); 61 62 if (zoom_area_begin_x == x && zoom_area_begin_y == y) { // zero width center this point but don't zoom in 63 g.set_center (x, y); 64 } else { 65 g.set_zoom_from_area (); 66 } 67 68 zoom_area_begin_x = -1; 69 zoom_area_begin_y = -1; 70 } 71 }); 72 73 draw_action.connect ((tool, cairo_context, glyph) => { 74 draw_zoom_area (cairo_context); 75 }); 76 } 77 78 public void draw_zoom_area (Context cr) { 79 Glyph g = MainWindow.get_current_glyph (); 80 81 if (g.zoom_area_is_visible) { 82 cr.save (); 83 cr.set_line_width (2.0); 84 Theme.color (cr, "Selection Border"); 85 86 cr.rectangle (Math.fmin (g.zoom_x1, g.zoom_x2), 87 Math.fmin (g.zoom_y1, g.zoom_y2), 88 Math.fabs (g.zoom_x1 - g.zoom_x2), 89 Math.fabs (g.zoom_y1 - g.zoom_y2)); 90 91 cr.stroke (); 92 cr.restore (); 93 } 94 } 95 96 public static void zoom_full_background_image () { 97 BackgroundImage bg; 98 Glyph g = MainWindow.get_current_glyph (); 99 int x, y; 100 101 g.reset_zoom (); 102 103 if (g.get_background_image () == null) { 104 return; 105 } 106 107 bg = (!) g.get_background_image (); 108 109 x = (int) (bg.img_offset_x); 110 y = (int) (bg.img_offset_y); 111 112 g.set_zoom_area (x, y, (int) (x + bg.size_margin * bg.img_scale_x), (int) (y + bg.size_margin * bg.img_scale_y)); 113 g.set_zoom_from_area (); 114 } 115 116 public void zoom_full_glyph () { 117 store_current_view (); 118 119 MainWindow.get_current_display ().zoom_min (); 120 } 121 122 /** Add an item to zoom view list. */ 123 public void store_current_view () { 124 if (views.size - 1 > view_index) { 125 int i = view_index + 1; 126 while (i != views.size - 1) { 127 views.remove_at (i); 128 } 129 } 130 131 views.add (MainWindow.get_current_tab ()); 132 view_index = (int) views.size - 1; 133 MainWindow.get_current_display ().store_current_view (); 134 } 135 136 /** Redo last zoom.*/ 137 public void next_view () { 138 if (view_index + 1 >= (int) views.size) { 139 return; 140 } 141 142 view_index++; 143 144 MainWindow.get_current_display ().next_view (); 145 GlyphCanvas.redraw (); 146 } 147 148 /** Undo last zoom. */ 149 public void previous_view () { 150 if (view_index == 0) { 151 return; 152 } 153 154 view_index--; 155 156 MainWindow.get_current_display ().restore_last_view (); 157 GlyphCanvas.redraw (); 158 } 159 } 160 161 } 162