The Birdfont Source Code


All Repositories / birdfont.git / blob – RSS feed

LabelTool.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/LabelTool.vala.
Bounding box for SVG rectangles
1 /* 2 Copyright (C) 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 LabelTool : Tool { 20 21 public string label { 22 get { 23 return label_text.text; 24 } 25 26 set { 27 clear_cache (); 28 label_text.set_text (value); 29 } 30 } 31 32 public string number { 33 get { 34 return counter_number; 35 } 36 37 set { 38 clear_cache (); 39 counter_number = value; 40 } 41 } 42 43 string counter_number = ""; 44 45 public bool has_counter { get; set; } 46 public bool has_delete_button { get; set; } 47 public signal void delete_action (LabelTool self); 48 public string data = ""; 49 50 double counter_box_width = 24 * Toolbox.get_scale (); 51 double counter_box_height = 11 * Toolbox.get_scale (); 52 53 Text label_text; 54 55 Surface? selected_cache = null; 56 Surface? deselected_cache = null; 57 58 public LabelTool (string label) { 59 double text_height; 60 61 base (); 62 63 label_text = new Text (); 64 label_text.set_text (label); 65 66 this.label = label; 67 this.number = "-"; 68 69 text_height = 17 * Toolbox.get_scale (); 70 label_text.set_font_size (text_height); 71 72 has_delete_button = false; 73 has_counter = false; 74 75 panel_press_action.connect ((selected, button, tx, ty) => { 76 if (has_delete_button && y <= ty <= y + h && tx >= w - 30 * Toolbox.get_scale ()) { 77 delete_action (this); 78 } 79 }); 80 } 81 82 public override void clear_cache () { 83 base.clear_cache (); 84 selected_cache = null; 85 deselected_cache = null; 86 } 87 88 public override void draw_tool (Context cr, double px, double py) { 89 double x = this.x - px; 90 double y = this.y - py; 91 92 if (is_selected ()) { 93 94 if (selected_cache == null) { 95 selected_cache = Screen.create_background_surface ((int) w, (int) h + 2); 96 Context c = new Context ((!) selected_cache); 97 c.scale (1 / Screen.get_scale (), 1 / Screen.get_scale ()); 98 draw_tool_surface (c, x, 2, true); 99 } 100 101 cr.save (); 102 cr.set_antialias (Cairo.Antialias.NONE); 103 cr.set_source_surface ((!) selected_cache, 0, (int) y - 2); 104 cr.paint (); 105 cr.restore (); 106 } else { 107 108 if (deselected_cache == null) { 109 deselected_cache = Screen.create_background_surface ((int) w, (int) h + 2); 110 Context c = new Context ((!) deselected_cache); 111 c.scale (1 / Screen.get_scale (), 1 / Screen.get_scale ()); 112 draw_tool_surface (c, x, 2, false); 113 } 114 115 cr.save (); 116 cr.set_antialias (Cairo.Antialias.NONE); 117 cr.set_source_surface ((!) deselected_cache, 0, (int) y - 2); 118 cr.paint (); 119 cr.restore (); 120 } 121 } 122 123 public void draw_tool_surface (Context cr, double px, double py, bool selected) { 124 Text glyph_count; 125 double bgx, bgy; 126 double center_x, center_y; 127 double text_height; 128 double text_width; 129 double x = px; 130 double y = py; 131 132 // background 133 if (selected) { 134 cr.save (); 135 Theme.color (cr, "Menu Background"); 136 cr.rectangle (0, y - 2 * Toolbox.get_scale (), w, h); // labels overlap with 2 pixels 137 cr.fill (); 138 cr.restore (); 139 } 140 141 // tab label 142 cr.save (); 143 144 Theme.text_color (label_text, "Text Tool Box"); 145 146 text_width = Toolbox.allocation_width; 147 148 if (has_counter) { 149 text_width -= counter_box_width - 15; 150 } 151 152 if (has_delete_button) { 153 text_width -= 30; 154 } 155 156 label_text.truncate (text_width); 157 label_text.draw_at_top (cr, x, y); 158 cr.restore (); 159 160 // glyph count 161 if (has_counter) { 162 cr.save (); 163 bgx = Toolbox.allocation_width - counter_box_width - 9; 164 bgy = y + 2; 165 166 if (is_selected ()) { 167 Theme.color (cr, "Glyph Count Background 1"); 168 } else { 169 Theme.color (cr, "Glyph Count Background 2"); 170 } 171 172 draw_rounded_rectangle (cr, bgx, bgy, counter_box_width, counter_box_height, 3); 173 cr.fill (); 174 cr.restore (); 175 176 glyph_count = new Text (); 177 glyph_count.set_text (@"$(this.number)"); 178 text_height = 12; 179 180 glyph_count.set_font_size (text_height); 181 center_x = bgx + (counter_box_width / 2.0 - glyph_count.get_extent () / 2.0); 182 center_y = bgy + (counter_box_height / 2.0 + 5); 183 184 if (is_selected ()) { 185 Theme.text_color (glyph_count, "Text Foreground"); 186 } else { 187 Theme.text_color (glyph_count, "Text Foreground"); 188 } 189 190 glyph_count.set_font_size (text_height); 191 glyph_count.draw_at_baseline (cr, center_x, center_y); 192 } 193 194 if (has_delete_button) { 195 cr.save (); 196 Theme.color (cr, "Text Foreground"); 197 cr.set_line_width (1); 198 cr.move_to (w - 20, y + h / 2 - 2.5 - 2); 199 cr.line_to (w - 25, y + h / 2 + 2.5 - 2); 200 cr.move_to (w - 20, y + h / 2 + 2.5 - 2); 201 cr.line_to (w - 25, y + h / 2 - 2.5 - 2); 202 cr.stroke (); 203 cr.restore (); 204 } 205 } 206 } 207 208 } 209