The Birdfont Source Code


All Repositories / birdfont.git / blob – RSS feed

Toolbox.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/Toolbox.vala.
Revert "Stroke settings"
1 /* 2 Copyright (C) 2012 2014 2015 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 using Math; 17 18 namespace BirdFont { 19 20 public class Toolbox : GLib.Object { 21 public static ToolCollection current_set; 22 23 public static DrawingTools drawing_tools; 24 public static KerningTools kerning_tools; 25 public static PreviewTools preview_tools; 26 public static OverviewTools overview_tools; 27 public static BackgroundTools background_tools; 28 public static HiddenTools hidden_tools; 29 public static SpacingTools spacing_tools; 30 public static FileTools file_tools; 31 32 Tool current_tool; 33 34 public Tool press_tool; 35 36 public signal void redraw (int x, int y, int w, int h); 37 38 public static int allocation_width = 0; 39 public static int allocation_height = 0; 40 41 /** Scrolling with scroll wheel */ 42 bool scrolling_toolbox = false; 43 44 /** Scroll with touch pad. */ 45 bool scrolling_touch = false; 46 double scroll_y = 0; 47 48 public List<ToolCollection> tool_sets = new List<ToolCollection> (); 49 50 static double scale = 1; 51 52 string? tool_tip = null; 53 double tool_tip_x = 0; 54 double tool_tip_y = 0; 55 56 public Toolbox (GlyphCanvas glyph_canvas, TabBar tab_bar) { 57 current_tool = new Tool ("no_icon"); 58 press_tool = new Tool (null); 59 60 drawing_tools = new DrawingTools (glyph_canvas); 61 kerning_tools = new KerningTools (); 62 preview_tools = new PreviewTools (); 63 overview_tools = new OverviewTools (); 64 background_tools = new BackgroundTools (); 65 hidden_tools = new HiddenTools (); 66 spacing_tools = new SpacingTools (); 67 file_tools = new FileTools (); 68 69 tool_sets.append (drawing_tools); 70 tool_sets.append (kerning_tools); 71 tool_sets.append (preview_tools); 72 tool_sets.append (overview_tools); 73 tool_sets.append (background_tools); 74 // the menu has all the file_tools commands, it won't be added here 75 tool_sets.append (hidden_tools); // tools without a button 76 77 current_set = file_tools; 78 79 tab_bar.signal_tab_selected.connect ((tab) => { 80 string tab_name = tab.get_display ().get_name (); 81 set_toolbox_from_tab (tab_name, tab); 82 }); 83 84 update_expanders (); 85 } 86 87 public static void set_toolbox_from_tab (string tab_name, Tab? t = null) { 88 if (tab_name == "Spacing") { 89 current_set = (ToolCollection) spacing_tools; 90 } else if (tab_name == "Kerning") { 91 current_set = (ToolCollection) kerning_tools; 92 } else if (tab_name == "Preview") { 93 current_set = (ToolCollection) preview_tools; 94 } else if (tab_name == "Overview") { 95 current_set = (ToolCollection) overview_tools; 96 } else if (tab_name == "Backgrounds") { 97 current_set = (ToolCollection) background_tools; 98 } else if (t != null && ((!) t).get_display () is Glyph) { 99 current_set = (ToolCollection) drawing_tools; 100 } else { 101 current_set = (ToolCollection) file_tools; 102 } 103 104 MainWindow.get_toolbox ().update_expanders (); 105 redraw_tool_box (); 106 } 107 108 public static Tool get_move_tool () { 109 return DrawingTools.move_tool; 110 } 111 112 public static void set_object_stroke (double width) { 113 DrawingTools.object_stroke.set_value_round (width); 114 redraw_tool_box (); 115 } 116 117 public static void set_allocation (int w, int h) { 118 if (w != allocation_width || allocation_height != h) { 119 allocation_width = w; 120 allocation_height = h; 121 122 scale = 1; 123 124 Toolbox.redraw_tool_box (); 125 } 126 } 127 128 public void press (uint button, double x, double y) { 129 if (MenuTab.suppress_event) { 130 warn_if_test ("Event suppressed"); 131 return; 132 } 133 134 foreach (Expander exp in current_set.get_expanders ()) { 135 foreach (Tool t in exp.tool) { 136 if (t.tool_is_visible () && t.is_over (x, y)) { 137 t.panel_press_action (t, button, x, y); 138 press_tool = t; 139 } 140 } 141 } 142 143 scrolling_touch = true; 144 scroll_y = y; 145 } 146 147 public void release (uint button, double x, double y) { 148 bool active; 149 150 if (MenuTab.suppress_event) { 151 warn_if_test ("Event suppressed"); 152 return; 153 } 154 155 foreach (Expander exp in current_set.get_expanders ()) { 156 foreach (Tool t in exp.tool) { 157 if (t.tool_is_visible ()) { 158 active = t.is_over (x, y); 159 160 if (active) { 161 if (press_tool == t) { 162 select_tool (t); 163 } 164 } 165 166 t.panel_release_action (t, button, x, y); 167 } 168 } 169 } 170 171 scrolling_touch = false; 172 } 173 174 public void scroll_up (double x, double y) { 175 bool action = false; 176 177 if (MenuTab.suppress_event) { 178 warn_if_test ("Event suppressed"); 179 return; 180 } 181 182 if (!scrolling_toolbox) { 183 foreach (Expander exp in current_set.get_expanders ()) { 184 foreach (Tool t in exp.tool) { 185 if (t.tool_is_visible () && t.is_over (x, y)) { 186 action = t.scroll_wheel_up_action (t); 187 press_tool = t; 188 } 189 } 190 } 191 } 192 193 if (!action) { 194 scroll_current_set (35); 195 } 196 197 redraw_tool_box (); 198 } 199 200 void scroll_current_set (double d) { 201 current_set.scroll += d; 202 203 if (current_set.scroll > 0) { 204 current_set.scroll = 0; 205 } 206 207 if (current_set.content_height < allocation_height) { 208 current_set.scroll = 0; 209 } else if (current_set.content_height + current_set.scroll < allocation_height) { 210 current_set.scroll = allocation_height - current_set.content_height; 211 } 212 213 update_expanders (); 214 suppress_scroll (); 215 } 216 217 public void scroll_down (double x, double y) { 218 bool action = false; 219 220 if (!scrolling_toolbox) { 221 foreach (Expander exp in current_set.get_expanders ()) { 222 foreach (Tool t in exp.tool) { 223 if (t.tool_is_visible () && t.is_over (x, y)) { 224 action = t.scroll_wheel_down_action (t); 225 press_tool = t; 226 } 227 } 228 } 229 } 230 231 if (!action) { 232 scroll_current_set (-35); 233 } 234 235 redraw_tool_box (); 236 } 237 238 void suppress_scroll () { 239 scrolling_toolbox = true; 240 241 Timeout.add (2000, () => { 242 scrolling_toolbox = false; 243 return false; 244 }); 245 } 246 247 public void move (double x, double y) { 248 bool update; 249 bool a; 250 bool consumed = false; 251 bool active; 252 253 foreach (Expander exp in current_set.get_expanders ()) { 254 a = exp.is_over (x, y); 255 update = exp.set_active (a); 256 257 if (update) { 258 redraw ((int) exp.x - 10, (int) exp.y - 10, (int) (exp.x + exp.w + 10), (int) (exp.y + exp.h + 10)); 259 } 260 261 262 foreach (Tool t in exp.tool) { 263 if (t.tool_is_visible ()) { 264 active = t.is_over (x, y); 265 266 if (!active && t.is_active ()) { 267 t.move_out_action (t); 268 } 269 270 update = t.set_active (active); 271 272 if (update) { 273 redraw (0, 0, allocation_width, allocation_height); 274 } 275 276 if (t.panel_move_action (t, x, y)) { 277 consumed = true; 278 } 279 } 280 } 281 } 282 283 if (scrolling_touch && !consumed && BirdFont.android) { 284 scroll_current_set (y - scroll_y); 285 scroll_y = y; 286 redraw_tool_box (); 287 } 288 } 289 290 public static void redraw_tool_box () { 291 if (MenuTab.suppress_event) { 292 warn_if_test ("Don't redraw toolbox when background thread is running."); 293 return; 294 } 295 296 Toolbox t = MainWindow.get_toolbox (); 297 if (!is_null (t)) { 298 t.redraw (0, 0, allocation_width, allocation_height); 299 } 300 } 301 302 public void reset_active_tool () { 303 foreach (Expander exp in current_set.get_expanders ()) { 304 foreach (Tool t in exp.tool) { 305 t.set_active (false); 306 } 307 } 308 } 309 310 public Tool? get_active_tool () { 311 foreach (Expander exp in current_set.get_expanders ()) { 312 foreach (Tool t in exp.tool) { 313 if (t.is_active ()) { 314 return t; 315 } 316 } 317 } 318 319 return null; 320 } 321 322 public void set_current_tool (Tool tool) { 323 if (tool.editor_events) { 324 current_tool = tool; 325 } 326 } 327 328 public Tool get_current_tool () { 329 return current_tool; 330 } 331 332 public void select_tool (Tool tool) { 333 bool update; 334 335 foreach (Expander exp in current_set.get_expanders ()) { 336 foreach (Tool t in exp.tool) { 337 if (tool.get_id () == t.get_id ()) { 338 if (!t.tool_is_visible ()) { 339 warning ("Tool is hidden"); 340 } else { 341 update = false; 342 343 update = tool.set_selected (true); 344 if (tool.persistent) { 345 update = tool.set_active (true); 346 } 347 348 tool.select_action (tool); 349 350 if (update) { 351 redraw ((int) exp.x - 10, (int) exp.y - 10, allocation_width, (int) (allocation_height - exp.y + 10)); 352 } 353 354 set_current_tool (tool); 355 } 356 } 357 } 358 359 } 360 } 361 362 public Tool get_tool (string name) { 363 foreach (ToolCollection tc in tool_sets) { 364 foreach (Expander e in tc.get_expanders ()) { 365 foreach (Tool t in e.tool) { 366 if (t.get_name () == name) { 367 return t; 368 } 369 } 370 } 371 } 372 373 warning ("No tool found for name \"%s\".\n", name); 374 375 return new Tool ("no_icon"); 376 } 377 378 public static void set_tool_visible (string name, bool visible) { 379 Toolbox tb = MainWindow.get_toolbox (); 380 Tool t = tb.get_tool (name); 381 t.set_tool_visibility (visible); 382 tb.update_expanders (); 383 Toolbox.redraw_tool_box (); 384 } 385 386 public static void select_tool_by_name (string name) { 387 Toolbox b = MainWindow.get_toolbox (); 388 389 if (is_null (b)) { 390 return; 391 } 392 393 b.select_tool (b.get_tool (name)); 394 } 395 396 public static void set_scale (double s) { 397 scale = s; 398 } 399 400 public static double get_scale () { 401 return scale; 402 } 403 404 public void set_default_tool_size () { 405 foreach (ToolCollection t in tool_sets) { 406 foreach (Expander e in t.get_expanders ()) { 407 e.update_tool_position (); 408 } 409 } 410 } 411 412 public void update_expanders () { 413 double pos; 414 415 foreach (Expander e in current_set.get_expanders ()) { 416 e.set_scroll (current_set.scroll); 417 } 418 419 pos = 4 * get_scale (); 420 foreach (Expander e in current_set.get_expanders ()) { 421 if (e.visible) { 422 e.set_offset (pos); 423 pos += e.get_content_height () + 4 * get_scale (); 424 current_set.content_height = pos; 425 426 if (BirdFont.android) { 427 current_set.content_height *= 1.15; 428 } 429 } 430 } 431 432 foreach (Expander e in current_set.get_expanders ()) { 433 e.set_active (false); 434 } 435 } 436 437 private void draw_expanders (int w, int h, Context cr) { 438 foreach (Expander e in current_set.get_expanders ()) { 439 if (e.visible) { 440 e.draw (w, h, cr); 441 e.draw_content (w, h, cr); 442 } 443 } 444 } 445 446 public void draw (int w, int h, Context cr) { 447 cr.save (); 448 449 if (current_set is EmptySet) { 450 Theme.color (cr, "Empty Tool Box"); 451 } else { 452 Theme.color (cr, "Default Background"); 453 } 454 455 cr.rectangle (0, 0, w, h); 456 cr.set_line_width (0); 457 cr.fill (); 458 459 draw_expanders (w, h, cr); 460 461 cr.restore (); 462 463 draw_tool_tip (cr); 464 465 } 466 467 private void draw_tool_tip (Context cr) { 468 TextArea t; 469 470 if (tool_tip != null && tool_tip != "") { 471 t = new TextArea (17 * get_scale ()); 472 t.allocation = new WidgetAllocation.for_area (0, 0, allocation_width, allocation_height); 473 t.set_editable (false); 474 t.set_text ((!) tool_tip); 475 t.width = allocation_width - 20 * get_scale (); 476 t.min_height = 17 * get_scale (); 477 t.height = 17 * get_scale (); 478 479 t.layout (); 480 481 t.widget_x = 10 * get_scale (); 482 t.widget_y = tool_tip_y - t.height - 5 * get_scale (); 483 484 t.draw (cr); 485 } 486 } 487 488 public void hide_tooltip () { 489 if (tool_tip != null) { 490 tool_tip = null; 491 redraw_tool_box (); 492 } 493 } 494 495 public void show_tooltip (string tool_tip, double x, double y) { 496 if (tool_tip != "") { 497 this.tool_tip = tool_tip; 498 this.tool_tip_x = x; 499 this.tool_tip_y = y; 500 501 redraw_tool_box (); 502 } 503 } 504 505 public class EmptySet : ToolCollection { 506 Gee.ArrayList<Expander> expanders; 507 508 public EmptySet () { 509 expanders = new Gee.ArrayList<Expander> (); 510 } 511 512 public override Gee.ArrayList<Expander> get_expanders () { 513 return expanders; 514 } 515 } 516 517 } 518 519 } 520