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
Circle boundaries heads/master
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 * Screen.get_scale ()), (int) ((h + 2) * Screen.get_scale ())); 96 Context c = new Context ((!) selected_cache); 97 c.scale (Screen.get_scale (), Screen.get_scale ()); 98 draw_tool_surface (c, x, 2, true); 99 } 100 101 cr.save (); 102 cr.scale (1 / Screen.get_scale (), 1 / Screen.get_scale ()); 103 cr.set_antialias (Cairo.Antialias.NONE); 104 cr.set_source_surface ((!) selected_cache, 0, (int) ((y - 2) * Screen.get_scale ())); 105 cr.paint (); 106 cr.restore (); 107 } else { 108 109 if (deselected_cache == null) { 110 deselected_cache = Screen.create_background_surface ((int) (w * Screen.get_scale ()), (int) ((h + 2) * Screen.get_scale ())); 111 Context c = new Context ((!) deselected_cache); 112 c.scale (Screen.get_scale (), Screen.get_scale ()); 113 draw_tool_surface (c, x, 2, false); 114 } 115 116 cr.save (); 117 cr.scale (1 / Screen.get_scale (), 1 / Screen.get_scale ()); 118 cr.set_antialias (Cairo.Antialias.NONE); 119 cr.set_source_surface ((!) deselected_cache, 0, (int) ((y - 2) * Screen.get_scale ())); 120 cr.paint (); 121 cr.restore (); 122 } 123 } 124 125 public void draw_tool_surface (Context cr, double px, double py, bool selected) { 126 Text glyph_count; 127 double bgx, bgy; 128 double center_x, center_y; 129 double text_height; 130 double text_width; 131 double x = px; 132 double y = py; 133 134 // background 135 if (selected) { 136 cr.save (); 137 Theme.color (cr, "Menu Background"); 138 cr.rectangle (0, y - 2 * Toolbox.get_scale (), w, h); // labels overlap with 2 pixels 139 cr.fill (); 140 cr.restore (); 141 } 142 143 // tab label 144 cr.save (); 145 146 Theme.text_color (label_text, "Text Tool Box"); 147 148 text_width = Toolbox.allocation_width; 149 150 if (has_counter) { 151 text_width -= counter_box_width - 15; 152 } 153 154 if (has_delete_button) { 155 text_width -= 30; 156 } 157 158 label_text.truncate (text_width); 159 label_text.draw_at_top (cr, x, y); 160 cr.restore (); 161 162 // glyph count 163 if (has_counter) { 164 cr.save (); 165 bgx = Toolbox.allocation_width - counter_box_width - 9; 166 bgy = y + 2; 167 168 if (is_selected ()) { 169 Theme.color (cr, "Glyph Count Background 1"); 170 } else { 171 Theme.color (cr, "Glyph Count Background 2"); 172 } 173 174 draw_rounded_rectangle (cr, bgx, bgy, counter_box_width, counter_box_height, 3); 175 cr.fill (); 176 cr.restore (); 177 178 glyph_count = new Text (); 179 glyph_count.set_text (@"$(this.number)"); 180 text_height = 12; 181 182 glyph_count.set_font_size (text_height); 183 center_x = bgx + (counter_box_width / 2.0 - glyph_count.get_extent () / 2.0); 184 center_y = bgy + (counter_box_height / 2.0 + 5); 185 186 if (is_selected ()) { 187 Theme.text_color (glyph_count, "Text Foreground"); 188 } else { 189 Theme.text_color (glyph_count, "Text Foreground"); 190 } 191 192 glyph_count.set_font_size (text_height); 193 glyph_count.draw_at_baseline (cr, center_x, center_y); 194 } 195 196 if (has_delete_button) { 197 cr.save (); 198 Theme.color (cr, "Text Foreground"); 199 cr.set_line_width (1); 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.move_to (w - 20, y + h / 2 + 2.5 - 2); 203 cr.line_to (w - 25, y + h / 2 - 2.5 - 2); 204 cr.stroke (); 205 cr.restore (); 206 } 207 } 208 } 209 210 } 211