The Birdfont Source Code


All Repositories / birdfont.git / blob – RSS feed

MoveTool.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/MoveTool.vala.
New color theme
1 /* 2 Copyright (C) 2012 2013 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 Math; 16 using Cairo; 17 18 namespace BirdFont { 19 20 public class MoveTool : Tool { 21 22 static bool move_path = false; 23 static bool moved = false; 24 static double last_x = 0; 25 static double last_y = 0; 26 27 static double selection_x = 0; 28 static double selection_y = 0; 29 static bool group_selection= false; 30 31 public static static double selection_box_width = 0; 32 public static static double selection_box_height = 0; 33 public static static double selection_box_center_x = 0; 34 public static static double selection_box_center_y = 0; 35 36 public signal void selection_changed (); 37 public signal void objects_moved (); 38 public signal void objects_deselected (); 39 40 public MoveTool (string n) { 41 base (n, t_("Move paths")); 42 43 select_action.connect((self) => { 44 Glyph glyph = MainWindow.get_current_glyph (); 45 glyph.close_path (); 46 }); 47 48 deselect_action.connect((self) => { 49 }); 50 51 press_action.connect((self, b, x, y) => { 52 press (b, x, y); 53 }); 54 55 release_action.connect((self, b, x, y) => { 56 release (b, x, y); 57 }); 58 59 move_action.connect ((self, x, y) => { 60 move (x, y); 61 }); 62 63 key_press_action.connect ((self, keyval) => { 64 key_down (keyval); 65 }); 66 67 draw_action.connect ((self, cr, glyph) => { 68 draw_actions (cr); 69 }); 70 } 71 72 public static void draw_actions (Context cr) { 73 if (group_selection) { 74 draw_selection_box (cr); 75 } 76 } 77 78 public void key_down (uint32 keyval) { 79 Glyph g = MainWindow.get_current_glyph (); 80 81 // delete selected paths 82 if (keyval == Key.DEL || keyval == Key.BACK_SPACE) { 83 foreach (Path p in g.active_paths) { 84 g.path_list.remove (p); 85 g.update_view (); 86 } 87 88 g.active_paths.clear (); 89 } 90 91 if (is_arrow_key (keyval)) { 92 move_selected_paths (keyval); 93 } 94 } 95 96 public void move (int x, int y) { 97 Glyph glyph = MainWindow.get_current_glyph (); 98 double dx = last_x - x; 99 double dy = last_y - y; 100 double p = PenTool.precision; 101 102 if (!move_path) { 103 return; 104 } 105 106 if (move_path && (fabs(dx) > 0 || fabs (dy) > 0)) { 107 moved = true; 108 foreach (Path path in glyph.active_paths) { 109 path.move (Glyph.ivz () * -dx * p, Glyph.ivz () * dy * p); 110 } 111 } 112 113 last_x = x; 114 last_y = y; 115 116 GlyphCanvas.redraw (); 117 118 update_selection_boundaries (); 119 120 if (glyph.active_paths.size > 0) { 121 objects_moved (); 122 } 123 124 BirdFont.get_current_font ().touch (); 125 } 126 127 public void release (int b, int x, int y) { 128 Glyph glyph = MainWindow.get_current_glyph (); 129 130 move_path = false; 131 132 if (GridTool.is_visible () && moved) { 133 tie_paths_to_grid (glyph); 134 } else if (GridTool.has_ttf_grid ()) { 135 foreach (Path p in glyph.active_paths) { 136 tie_path_to_ttf_grid (p); 137 } 138 } 139 140 if (group_selection) { 141 select_group (); 142 } 143 144 group_selection = false; 145 moved = false; 146 147 if (glyph.active_paths.size > 0) { 148 objects_moved (); 149 DrawingTools.resize_tool.signal_objects_rotated (); 150 } else { 151 objects_deselected (); 152 } 153 } 154 155 public void press (int b, int x, int y) { 156 Glyph glyph = MainWindow.get_current_glyph (); 157 158 glyph.store_undo_state (); 159 group_selection = false; 160 161 if (!glyph.is_over_selected_path (x, y)) { 162 if (!glyph.select_path (x, y)) { 163 glyph.clear_active_paths (); 164 } 165 } 166 167 move_path = true; 168 169 update_selection_boundaries (); 170 171 last_x = x; 172 last_y = y; 173 174 if (glyph.active_paths.size == 0) { 175 group_selection = true; 176 selection_x = x; 177 selection_y = y; 178 } 179 180 update_boundaries_for_selection (); 181 selection_changed (); 182 } 183 184 185 void select_group () { 186 double x1 = Glyph.path_coordinate_x (Math.fmin (selection_x, last_x)); 187 double y1 = Glyph.path_coordinate_y (Math.fmin (selection_y, last_y)); 188 double x2 = Glyph.path_coordinate_x (Math.fmax (selection_x, last_x)); 189 double y2 = Glyph.path_coordinate_y (Math.fmax (selection_y, last_y)); 190 Glyph glyph = MainWindow.get_current_glyph (); 191 192 glyph.clear_active_paths (); 193 194 foreach (Path p in glyph.path_list) { 195 if (p.xmin > x1 && p.xmax < x2 && p.ymin < y1 && p.ymax > y2) { 196 if (p.points.size > 0) { 197 glyph.add_active_path (p); 198 } 199 } 200 } 201 202 selection_changed (); 203 } 204 205 public static void update_selection_boundaries () { 206 get_selection_box_boundaries (out selection_box_center_x, 207 out selection_box_center_y, out selection_box_width, 208 out selection_box_height); 209 } 210 211 public void move_to_baseline () { 212 Glyph glyph = MainWindow.get_current_glyph (); 213 Font font = BirdFont.get_current_font (); 214 double x, y, w, h; 215 216 get_selection_box_boundaries (out x, out y, out w, out h); 217 218 foreach (Path path in glyph.active_paths) { 219 path.move (glyph.left_limit - x + w / 2, font.base_line - y + h / 2); 220 } 221 222 update_selection_boundaries (); 223 objects_moved (); 224 GlyphCanvas.redraw (); 225 } 226 227 static void draw_selection_box (Context cr) { 228 double x = Math.fmin (selection_x, last_x); 229 double y = Math.fmin (selection_y, last_y); 230 231 double w = Math.fabs (selection_x - last_x); 232 double h = Math.fabs (selection_y - last_y); 233 234 cr.save (); 235 236 Theme.color (cr, "Default Background"); 237 cr.set_line_width (2); 238 cr.rectangle (x, y, w, h); 239 cr.stroke (); 240 241 cr.restore (); 242 } 243 244 public static void get_selection_box_boundaries (out double x, out double y, out double w, out double h) { 245 double px, py, px2, py2; 246 Glyph glyph = MainWindow.get_current_glyph (); 247 248 px = 10000; 249 py = 10000; 250 px2 = -10000; 251 py2 = -10000; 252 253 foreach (Path p in glyph.active_paths) { 254 if (px > p.xmin) { 255 px = p.xmin; 256 } 257 258 if (py > p.ymin) { 259 py = p.ymin; 260 } 261 262 if (px2 < p.xmax) { 263 px2 = p.xmax; 264 } 265 266 if (py2 < p.ymax) { 267 py2 = p.ymax; 268 } 269 } 270 271 w = px2 - px; 272 h = py2 - py; 273 x = px + (w / 2); 274 y = py + (h / 2); 275 } 276 277 void move_selected_paths (uint key) { 278 Glyph glyph = MainWindow.get_current_glyph (); 279 double x, y; 280 281 x = 0; 282 y = 0; 283 284 switch (key) { 285 case Key.UP: 286 y = 1; 287 break; 288 case Key.DOWN: 289 y = -1; 290 break; 291 case Key.LEFT: 292 x = -1; 293 break; 294 case Key.RIGHT: 295 x = 1; 296 break; 297 default: 298 break; 299 } 300 301 foreach (Path path in glyph.active_paths) { 302 path.move (x * Glyph.ivz (), y * Glyph.ivz ()); 303 } 304 305 update_selection_boundaries (); 306 objects_moved (); 307 glyph.redraw_area (0, 0, glyph.allocation.width, glyph.allocation.height); 308 } 309 310 static void tie_path_to_ttf_grid (Path p) { 311 double sx, sy, qx, qy; 312 313 sx = p.xmax; 314 sy = p.ymax; 315 qx = p.xmin; 316 qy = p.ymin; 317 318 GridTool.ttf_grid_coordinate (ref sx, ref sy); 319 GridTool.ttf_grid_coordinate (ref qx, ref qy); 320 321 if (Math.fabs (qy - p.ymin) < Math.fabs (sy - p.ymax)) { 322 p.move (0, qy - p.ymin); 323 } else { 324 p.move (0, sy - p.ymax); 325 } 326 327 if (Math.fabs (qx - p.xmin) < Math.fabs (sx - p.xmax)) { 328 p.move (qx - p.xmin, 0); 329 } else { 330 p.move (sx - p.xmax, 0); 331 } 332 } 333 334 static void tie_paths_to_grid (Glyph g) { 335 double sx, sy, qx, qy; 336 double dx_min, dx_max, dy_min, dy_max;; 337 double maxx, maxy, minx, miny; 338 339 update_selection_boundaries (); 340 341 // tie to grid 342 maxx = selection_box_center_x + selection_box_width / 2; 343 maxy = selection_box_center_y + selection_box_height / 2; 344 minx = selection_box_center_x - selection_box_width / 2; 345 miny = selection_box_center_y - selection_box_height / 2; 346 347 sx = maxx; 348 sy = maxy; 349 qx = minx; 350 qy = miny; 351 352 GridTool.tie_coordinate (ref sx, ref sy); 353 GridTool.tie_coordinate (ref qx, ref qy); 354 355 dy_min = Math.fabs (qy - miny); 356 dy_max = Math.fabs (sy - maxy); 357 dx_min = Math.fabs (qx - minx); 358 dx_max = Math.fabs (sx - maxx); 359 360 foreach (Path p in g.active_paths) { 361 if (dy_min < dy_max) { 362 p.move (0, qy - miny); 363 } else { 364 p.move (0, sy - maxy); 365 } 366 367 if (dx_min < dx_max) { 368 p.move (qx - minx, 0); 369 } else { 370 p.move (sx - maxx, 0); 371 } 372 } 373 374 update_selection_boundaries (); 375 } 376 377 public static void update_boundaries_for_selection () { 378 Glyph glyph = MainWindow.get_current_glyph (); 379 foreach (Path p in glyph.active_paths) { 380 p.update_region_boundaries (); 381 } 382 } 383 384 public static void flip_vertical () { 385 flip (true); 386 } 387 388 public static void flip_horizontal () { 389 flip (false); 390 } 391 392 public static void flip (bool vertical) { 393 double xc, yc, xc2, yc2, w, h; 394 double dx, dy; 395 Glyph glyph = MainWindow.get_current_glyph (); 396 397 update_selection_boundaries (); 398 399 xc = selection_box_center_x; 400 yc = selection_box_center_y; 401 402 foreach (Path p in glyph.active_paths) { 403 if (vertical) { 404 p.flip_vertical (); 405 } else { 406 p.flip_horizontal (); 407 } 408 409 p.reverse (); 410 } 411 412 get_selection_box_boundaries (out xc2, out yc2, out w, out h); 413 414 dx = -(xc2 - xc); 415 dy = -(yc2 - yc); 416 417 foreach (Path p in glyph.active_paths) { 418 p.move (dx, dy); 419 } 420 421 update_selection_boundaries (); 422 423 BirdFont.get_current_font ().touch (); 424 } 425 426 public void select_all_paths () { 427 Glyph g = MainWindow.get_current_glyph (); 428 429 g.clear_active_paths (); 430 431 foreach (Path p in g.path_list) { 432 if (p.points.size > 0) { 433 g.add_active_path (p); 434 } 435 } 436 437 g.update_view (); 438 439 update_selection_boundaries (); 440 objects_moved (); 441 } 442 } 443 444 } 445