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
Circle boundaries heads/master
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.has_ctrl () && !KeyBindings.has_shift ()) { 40 zoom_area_begin_x = x; 41 zoom_area_begin_y = y; 42 Glyph g = MainWindow.get_current_glyph (); 43 g.zoom_area_is_visible = true; 44 } 45 }); 46 47 move_action.connect((self, x, y) => { 48 Glyph g = MainWindow.get_current_glyph (); 49 50 if (g.zoom_area_is_visible) { 51 g.show_zoom_area (zoom_area_begin_x, zoom_area_begin_y, x, y); 52 } 53 }); 54 55 release_action.connect((self, b, x, y) => { 56 Glyph g; 57 58 if (b == 1 && !KeyBindings.has_ctrl () && !KeyBindings.has_shift ()) { 59 store_current_view (); 60 61 g = MainWindow.get_current_glyph (); 62 63 if (zoom_area_begin_x == x && zoom_area_begin_y == y) { // zero width center this point but don't zoom in 64 g.set_center (x, y); 65 } else { 66 g.set_zoom_from_area (); 67 } 68 69 g.zoom_area_is_visible = false; 70 zoom_area_begin_x = -1; 71 zoom_area_begin_y = -1; 72 } 73 }); 74 75 draw_action.connect ((tool, cairo_context, glyph) => { 76 draw_zoom_area (cairo_context); 77 }); 78 } 79 80 public void draw_zoom_area (Context cr) { 81 Glyph g = MainWindow.get_current_glyph (); 82 83 if (g.zoom_area_is_visible) { 84 cr.save (); 85 cr.set_line_width (2.0); 86 Theme.color (cr, "Selection Border"); 87 88 cr.rectangle (Math.fmin (g.zoom_x1, g.zoom_x2), 89 Math.fmin (g.zoom_y1, g.zoom_y2), 90 Math.fabs (g.zoom_x1 - g.zoom_x2), 91 Math.fabs (g.zoom_y1 - g.zoom_y2)); 92 93 cr.stroke (); 94 cr.restore (); 95 } 96 } 97 98 public static void zoom_full_background_image () { 99 BackgroundImage bg; 100 Glyph g = MainWindow.get_current_glyph (); 101 int x, y; 102 103 g.reset_zoom (); 104 105 if (g.get_background_image () == null) { 106 return; 107 } 108 109 bg = (!) g.get_background_image (); 110 111 x = (int) (bg.img_offset_x); 112 y = (int) (bg.img_offset_y); 113 114 g.set_zoom_area (x, y, (int) (x + bg.size_margin * bg.img_scale_x), (int) (y + bg.size_margin * bg.img_scale_y)); 115 g.set_zoom_from_area (); 116 } 117 118 public void zoom_full_glyph () { 119 store_current_view (); 120 121 MainWindow.get_current_display ().zoom_min (); 122 } 123 124 /** Add an item to zoom view list. */ 125 public void store_current_view () { 126 if (views.size - 1 > view_index) { 127 int i = view_index + 1; 128 while (i != views.size - 1) { 129 views.remove_at (i); 130 } 131 } 132 133 views.add (MainWindow.get_current_tab ()); 134 view_index = (int) views.size - 1; 135 MainWindow.get_current_display ().store_current_view (); 136 } 137 138 /** Redo last zoom.*/ 139 public void next_view () { 140 if (view_index + 1 >= (int) views.size) { 141 return; 142 } 143 144 view_index++; 145 146 MainWindow.get_current_display ().next_view (); 147 GlyphCanvas.redraw (); 148 } 149 150 /** Undo last zoom. */ 151 public void previous_view () { 152 if (view_index == 0) { 153 return; 154 } 155 156 view_index--; 157 158 MainWindow.get_current_display ().restore_last_view (); 159 GlyphCanvas.redraw (); 160 } 161 } 162 163 } 164