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.
Tool tip for beziér tool modifiers
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 override void draw (Context cr) { 132 draw_tool (cr, 0, 0); 133 } 134 135 public void redraw () { 136 redraw_tool (); 137 Toolbox.redraw_tool_box (); 138 } 139 140 public override double get_height () { 141 return 33 * scale; 142 } 143 144 public override double get_width () { 145 return 33 * scale; 146 } 147 148 public void set_tool_visibility (bool v) { 149 visible = v; 150 } 151 152 public bool tool_is_visible () { 153 return visible; 154 } 155 156 void wait_for_tooltip () { 157 TimeoutSource timer_show; 158 int timeout_interval = 1500; 159 160 if (active_tooltip != this) { 161 if (active_tooltip.showing_this_tooltip) { 162 timeout_interval = 1; 163 } 164 165 active_tooltip.showing_this_tooltip = false; 166 showing_this_tooltip = false; 167 active_tooltip = this; 168 169 if (!waiting_for_tooltip) { 170 waiting_for_tooltip = true; 171 timer_show = new TimeoutSource (timeout_interval); 172 timer_show.set_callback (() => { 173 if (tip != "" && active_tooltip.is_active () && !active_tooltip.showing_this_tooltip) { 174 show_tooltip (); 175 } 176 waiting_for_tooltip = false; 177 return waiting_for_tooltip; 178 }); 179 timer_show.attach (null); 180 } 181 } 182 } 183 184 public static void show_tooltip () { 185 TimeoutSource timer_hide; 186 Toolbox toolbox; 187 string tip; 188 string key_binding; 189 190 toolbox = MainWindow.get_toolbox (); 191 192 // hide tooltip label later 193 if (!active_tooltip.showing_this_tooltip) { 194 timer_hide = new TimeoutSource (1500); 195 timer_hide.set_callback (() => { 196 if (!active_tooltip.is_active ()) { 197 toolbox.hide_tooltip (); 198 active_tooltip.showing_this_tooltip = false; 199 active_tooltip = new Tool (); 200 } 201 return active_tooltip.showing_this_tooltip; 202 }); 203 timer_hide.attach (null); 204 } 205 206 active_tooltip.showing_this_tooltip = true; 207 208 tip = @"$(active_tooltip.tip)"; 209 key_binding = active_tooltip.get_key_binding (); 210 211 if (key_binding != "") { 212 tip += " (" + key_binding + ")"; 213 } 214 215 toolbox.hide_tooltip (); 216 toolbox.show_tooltip (tip, (int) active_tooltip.x, (int) active_tooltip.y); 217 } 218 219 public string get_key_binding () { 220 ToolItem? ti = MainWindow.get_menu ().get_item_for_tool (this); 221 ToolItem t; 222 223 if (ti == null) { 224 warning ("No key binding for tool."); 225 return ""; 226 } 227 228 t = (!) ti; 229 return t.get_key_binding (); 230 } 231 232 public void set_icon (string name) { 233 bool found; 234 string icon_file; 235 236 icon_file = Theme.get_icon_file (); 237 icon_font = new Text ((!) name); 238 found = icon_font.load_font (icon_file); 239 icon_font.use_cache (true); 240 icon_font.set_font_size (40 * Toolbox.get_scale ()); 241 242 if (!found) { 243 warning (@"Icon font for toolbox was not found. ($(icon_file))"); 244 } 245 } 246 247 public bool is_active () { 248 return active; 249 } 250 251 public void set_show_background (bool bg) { 252 show_bg = bg; 253 } 254 255 public int get_id () { 256 return id; 257 } 258 259 public string get_name () { 260 return name; 261 } 262 263 public bool is_selected () { 264 return selected; 265 } 266 267 public string get_tip () { 268 return tip; 269 } 270 271 public new bool is_over (double xp, double yp) { 272 bool r = (x <= xp <= x + w && y <= yp <= y + h); 273 return r; 274 } 275 276 public bool set_selected (bool a) { 277 new_selection = true; 278 selected = a; 279 set_active (a); 280 281 if (!a) { 282 deselect_action (this); 283 } 284 285 return true; 286 } 287 288 /** @return true if this tool changes state, */ 289 public bool set_active (bool ac) { 290 bool ret = (active != ac); 291 active = ac; 292 return ret; 293 } 294 295 public virtual void draw_tool (Context cr, double px, double py) { 296 double xt = x - px; 297 double yt = y - py; 298 299 double bgx, bgy; 300 double iconx, icony; 301 302 string border = "Button Border 3"; 303 string background = "Button Border 3"; 304 305 double scale = Toolbox.get_scale (); 306 307 cr.save (); 308 309 bgx = xt; 310 bgy = yt; 311 312 // Button in four states 313 if (selected) { 314 border = "Button Border 1"; 315 background = "Button Background 1"; 316 } 317 318 if (selected && active) { 319 border = "Button Border 2"; 320 background = "Button Background 2"; 321 } 322 323 if (!selected) { 324 border = "Button Border 3"; 325 background = "Button Background 3"; 326 } 327 328 if (!selected && active) { 329 border = "Button Border 4"; 330 background = "Button Background 4"; 331 } 332 333 Theme.color (cr, background); 334 draw_rounded_rectangle (cr, bgx, bgy, 34 * scale, 28 * scale, 4 * scale); 335 cr.fill (); 336 337 cr.set_line_width (1); 338 Theme.color (cr, border); 339 draw_rounded_rectangle (cr, bgx, bgy, 34 * scale, 28 * scale, 4 * scale); 340 cr.stroke (); 341 342 iconx = bgx + 1 + (w - 1) / 2 - icon_font.get_sidebearing_extent () / 2; 343 icony = bgy - 1 + (h - 1) / 2 - icon_font.get_height () / 2; 344 345 if (icon_color == "") { 346 if (!selected) { 347 Theme.text_color (icon_font, "Tool Foreground"); 348 } else { 349 Theme.text_color (icon_font, "Selected Tool Foreground"); 350 } 351 } else { 352 Theme.text_color (icon_font, icon_color); 353 } 354 355 icon_font.widget_x = iconx; 356 icon_font.widget_y = icony; 357 358 icon_font.draw (cr); 359 360 cr.restore (); 361 } 362 363 /** Run pending events in main loop before continuing. */ 364 public static void @yield () { 365 int t = 0; 366 TimeoutSource time = new TimeoutSource (500); 367 bool timeout; 368 unowned MainContext context; 369 bool acquired; 370 371 if (TestBirdFont.is_slow_test ()) { 372 timeout = false; 373 374 time.set_callback (() => { 375 timeout = true; 376 return false; 377 }); 378 379 time.attach (null); 380 } else { 381 timeout = true; 382 } 383 384 context = MainContext.default (); 385 acquired = context.acquire (); 386 387 if (unlikely (!acquired)) { 388 warning ("Failed to acquire main loop.\n"); 389 return; 390 } 391 392 while (context.pending () || TestBirdFont.is_slow_test ()) { 393 context.iteration (true); 394 t++; 395 396 if (!context.pending () && TestBirdFont.is_slow_test ()) { 397 if (timeout) break; 398 } 399 } 400 401 context.release (); 402 } 403 404 public void set_persistent (bool p) { 405 persistent = p; 406 } 407 408 public virtual void before_undo () { 409 } 410 411 public virtual void after_undo () { 412 } 413 414 } 415 416 } 417