The Birdfont Source Code


All Repositories / birdfont.git / blob – RSS feed

Tool.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/Tool.vala.
Cache overview items
1 /* 2 Copyright (C) 2012, 2014 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 Tool : Widget { 20 21 public double x = 0; 22 public double y = 0; 23 public double w; 24 public double h; 25 26 double scale; 27 28 public bool active = false; 29 public bool selected = false; 30 31 public Text icon_font; 32 33 public signal void select_action (Tool selected); 34 public signal void deselect_action (Tool selected); 35 36 public signal void press_action (Tool selected, int button, int x, int y); 37 public signal void double_click_action (Tool selected, int button, int x, int y); 38 public signal void move_action (Tool selected, int x, int y); 39 public signal void move_out_action (Tool selected); 40 public signal void release_action (Tool selected, int button, int x, int y); 41 42 /** Returns true if tool is listening for scroll wheel actions. */ 43 public signal bool scroll_wheel_up_action (Tool selected); 44 public signal bool scroll_wheel_down_action (Tool selected); 45 46 public signal void key_press_action (Tool selected, uint32 keyval); 47 public signal void key_release_action (Tool selected, uint32 keyval); 48 49 public signal void panel_press_action (Tool selected, uint button, double x, double y); 50 public signal void panel_release_action (Tool selected, uint button, double x, double y); 51 public signal void panel_double_click_action (Tool selected, uint button, double x, double y); 52 53 /** @return true is event is consumed. */ 54 public signal bool panel_move_action (Tool selected, double x, double y); 55 56 public signal void draw_action (Tool selected, Context cr, Glyph glyph); 57 58 public string name = ""; 59 60 static int next_id = 1; 61 62 int id; 63 64 public bool new_selection = false; 65 66 bool show_bg = true; 67 68 public string tip = ""; 69 70 public bool persistent = false; 71 public bool editor_events = false; 72 73 bool waiting_for_tooltip = false; 74 bool showing_this_tooltip = false; 75 static Tool active_tooltip = new Tool (); 76 77 public bool visible = true; 78 public bool is_tool_modifier = false; 79 80 public string icon_color = ""; 81 82 public signal void redraw_tool (); 83 84 /** Create tool with a certain name and load icon "name".png */ 85 public Tool (string? name = null, string tip = "") { 86 this.tip = tip; 87 88 icon_font = new Text (); 89 90 scale = MainWindow.units; 91 w = 33 * Toolbox.get_scale (); 92 h = (33 / 1.11) * Toolbox.get_scale (); 93 94 if (name != null) { 95 set_icon ((!) name); 96 this.name = (!) name; 97 } 98 99 id = next_id; 100 next_id++; 101 102 panel_press_action.connect ((self, button, x, y) => { 103 if (is_active ()) { 104 redraw (); 105 } 106 }); 107 108 select_action.connect ((self) => { 109 redraw (); 110 }); 111 112 deselect_action.connect ((self) => { 113 redraw (); 114 }); 115 116 move_out_action.connect ((self) => { 117 MainWindow.get_toolbox ().hide_tooltip (); 118 active_tooltip.showing_this_tooltip = false; 119 redraw (); 120 }); 121 122 panel_move_action.connect ((self, x, y) => { 123 if (is_active ()) { 124 wait_for_tooltip (); 125 redraw (); 126 } 127 return false; 128 }); 129 } 130 131 public virtual string get_tip () { 132 return tip; 133 } 134 135 public override void draw (Context cr) { 136 draw_tool (cr, 0, 0); 137 } 138 139 public void redraw () { 140 redraw_tool (); 141 Toolbox.redraw_tool_box (); 142 } 143 144 public override double get_height () { 145 return 33 * scale; 146 } 147 148 public override double get_width () { 149 return 33 * scale; 150 } 151 152 public void set_tool_visibility (bool v) { 153 visible = v; 154 } 155 156 public bool tool_is_visible () { 157 return visible; 158 } 159 160 void wait_for_tooltip () { 161 TimeoutSource timer_show; 162 int timeout_interval = 1500; 163 164 if (active_tooltip != this) { 165 if (active_tooltip.showing_this_tooltip) { 166 timeout_interval = 1; 167 } 168 169 active_tooltip.showing_this_tooltip = false; 170 showing_this_tooltip = false; 171 active_tooltip = this; 172 173 if (!waiting_for_tooltip) { 174 waiting_for_tooltip = true; 175 timer_show = new TimeoutSource (timeout_interval); 176 timer_show.set_callback (() => { 177 if (get_tip () != "" && active_tooltip.is_active () && !active_tooltip.showing_this_tooltip) { 178 show_tooltip (); 179 } 180 waiting_for_tooltip = false; 181 return waiting_for_tooltip; 182 }); 183 timer_show.attach (null); 184 } 185 } 186 } 187 188 public static void show_tooltip () { 189 TimeoutSource timer_hide; 190 Toolbox toolbox; 191 string tip; 192 string key_binding; 193 194 toolbox = MainWindow.get_toolbox (); 195 196 // hide tooltip label later 197 if (!active_tooltip.showing_this_tooltip) { 198 timer_hide = new TimeoutSource (1500); 199 timer_hide.set_callback (() => { 200 if (!active_tooltip.is_active ()) { 201 toolbox.hide_tooltip (); 202 active_tooltip.showing_this_tooltip = false; 203 active_tooltip = new Tool (); 204 } 205 return active_tooltip.showing_this_tooltip; 206 }); 207 timer_hide.attach (null); 208 } 209 210 active_tooltip.showing_this_tooltip = true; 211 212 tip = @"$(active_tooltip.get_tip ())"; 213 key_binding = active_tooltip.get_key_binding (); 214 215 if (key_binding != "") { 216 tip += " (" + key_binding + ")"; 217 } 218 219 toolbox.hide_tooltip (); 220 toolbox.show_tooltip (tip, (int) active_tooltip.x, (int) active_tooltip.y); 221 } 222 223 public string get_key_binding () { 224 ToolItem? ti = MainWindow.get_menu ().get_item_for_tool (this); 225 ToolItem t; 226 227 if (ti == null) { 228 warning ("No key binding for tool."); 229 return ""; 230 } 231 232 t = (!) ti; 233 return t.get_key_binding (); 234 } 235 236 public void set_icon (string name) { 237 bool found; 238 string icon_file; 239 240 icon_file = Theme.get_icon_file (); 241 icon_font = new Text ((!) name); 242 found = icon_font.load_font (icon_file); 243 icon_font.set_font_size (40 * Toolbox.get_scale ()); 244 245 if (!found) { 246 warning (@"Icon font for toolbox was not found. ($(icon_file))"); 247 } 248 } 249 250 public bool is_active () { 251 return active; 252 } 253 254 public void set_show_background (bool bg) { 255 show_bg = bg; 256 } 257 258 public int get_id () { 259 return id; 260 } 261 262 public string get_name () { 263 return name; 264 } 265 266 public bool is_selected () { 267 return selected; 268 } 269 270 public new bool is_over (double xp, double yp) { 271 bool r = (x <= xp <= x + w && y <= yp <= y + h); 272 return r; 273 } 274 275 public bool set_selected (bool a) { 276 new_selection = true; 277 selected = a; 278 set_active (a); 279 280 if (!a) { 281 deselect_action (this); 282 } 283 284 return true; 285 } 286 287 /** @return true if this tool changes state, */ 288 public bool set_active (bool ac) { 289 bool ret = (active != ac); 290 active = ac; 291 return ret; 292 } 293 294 public virtual void draw_tool (Context cr, double px, double py) { 295 double xt = x - px; 296 double yt = y - py; 297 298 double bgx, bgy; 299 double iconx, icony; 300 301 string border = "Button Border 3"; 302 string background = "Button Border 3"; 303 304 double scale = Toolbox.get_scale (); 305 306 cr.save (); 307 308 bgx = xt; 309 bgy = yt; 310 311 // Button in four states 312 if (selected) { 313 border = "Button Border 1"; 314 background = "Button Background 1"; 315 } 316 317 if (selected && active) { 318 border = "Button Border 2"; 319 background = "Button Background 2"; 320 } 321 322 if (!selected) { 323 border = "Button Border 3"; 324 background = "Button Background 3"; 325 } 326 327 if (!selected && active) { 328 border = "Button Border 4"; 329 background = "Button Background 4"; 330 } 331 332 Theme.color (cr, background); 333 draw_rounded_rectangle (cr, bgx, bgy, 34 * scale, 28 * scale, 4 * scale); 334 cr.fill (); 335 336 cr.set_line_width (1); 337 Theme.color (cr, border); 338 draw_rounded_rectangle (cr, bgx, bgy, 34 * scale, 28 * scale, 4 * scale); 339 cr.stroke (); 340 341 iconx = bgx + 1 + (w - 1) / 2 - icon_font.get_sidebearing_extent () / 2; 342 icony = bgy + 1 + (h - 1) / 2 - icon_font.get_height () / 2; 343 344 if (icon_color == "") { 345 if (!selected) { 346 Theme.text_color (icon_font, "Tool Foreground"); 347 } else { 348 Theme.text_color (icon_font, "Selected Tool Foreground"); 349 } 350 } else { 351 Theme.text_color (icon_font, icon_color); 352 } 353 354 icon_font.widget_x = iconx; 355 icon_font.widget_y = icony; 356 357 icon_font.draw (cr); 358 359 cr.restore (); 360 } 361 362 /** Run pending events in main loop before continuing. */ 363 public static void @yield () { 364 int t = 0; 365 TimeoutSource time = new TimeoutSource (500); 366 bool timeout; 367 unowned MainContext context; 368 bool acquired; 369 370 if (TestBirdFont.is_slow_test ()) { 371 timeout = false; 372 373 time.set_callback (() => { 374 timeout = true; 375 return false; 376 }); 377 378 time.attach (null); 379 } else { 380 timeout = true; 381 } 382 383 context = MainContext.default (); 384 acquired = context.acquire (); 385 386 if (unlikely (!acquired)) { 387 warning ("Failed to acquire main loop.\n"); 388 return; 389 } 390 391 while (context.pending () || TestBirdFont.is_slow_test ()) { 392 context.iteration (true); 393 t++; 394 395 if (!context.pending () && TestBirdFont.is_slow_test ()) { 396 if (timeout) break; 397 } 398 } 399 400 context.release (); 401 } 402 403 public void set_persistent (bool p) { 404 persistent = p; 405 } 406 407 public virtual void before_undo () { 408 } 409 410 public virtual void after_undo () { 411 } 412 413 } 414 415 } 416