The Birdfont Source Code


All Repositories / birdfont.git / blob – RSS feed

LayerLabel.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/LayerLabel.vala.
Bounding box for SVG rectangles
1 /* 2 Copyright (C) 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 SvgBird; 17 18 namespace BirdFont { 19 20 public class LayerLabel : Tool { 21 22 public bool selected_layer { get; set; } 23 public string label { get; set; } 24 25 public Layer layer; 26 Text label_text; 27 28 /** Add margin when layer is moves. */ 29 bool active_layer = false; 30 31 static TextArea? help_text_hide = null; 32 33 public LayerLabel (Layer layer) { 34 base (); 35 36 if (help_text_hide == null) { 37 update_description (); 38 } 39 40 this.layer = layer; 41 this.label = layer.name; 42 43 selected_layer = false; 44 45 set_text (); 46 47 panel_press_action.connect ((selected, button, tx, ty) => { 48 if (y <= ty <= y + h) { 49 if (tx >= w - 30) { 50 DrawingTools.deselect_layers (); 51 remove_layer (); 52 } else if (tx < 25) { 53 layer.visible = !layer.visible; 54 GlyphCanvas.redraw (); 55 BirdFont.get_current_font ().touch (); 56 MainWindow.get_current_glyph ().clear_active_paths (); 57 58 Glyph g = MainWindow.get_current_glyph (); 59 print ("\n\n"); 60 g.print_layers (g.layers); 61 } else { 62 active_layer = true; 63 select_layer (); 64 } 65 } else { 66 selected_layer = false; 67 } 68 }); 69 70 panel_double_click_action.connect ((selected, button, tx, ty) => { 71 if (y <= ty <= y + h) { 72 if (25 * Toolbox.get_scale () <= tx <= w - 30 * Toolbox.get_scale ()) { 73 set_layer_name (); 74 } 75 } 76 }); 77 78 panel_move_action.connect ((selected, button, tx, ty) => { 79 if (active_layer) { 80 if (ty > y) { 81 move_layer_down (); 82 } else if (ty < y - h) { 83 move_layer_up (); 84 } 85 86 MainWindow.get_toolbox ().update_expanders (); 87 redraw (); 88 } 89 90 if (y <= ty <= y + h) { 91 if (tx < 25 && help_text_hide != null) { 92 Help help = MainWindow.get_help (); 93 help.set_help_text ((!) help_text_hide); 94 } 95 } 96 97 return false; 98 }); 99 100 panel_release_action.connect ((selected, button, tx, ty) => { 101 active_layer = false; 102 }); 103 } 104 105 void update_description () { 106 help_text_hide = Help.create_help_text (t_("Hide the layer.")); 107 } 108 109 public override void clear_cache () { 110 base.clear_cache (); 111 update_description (); 112 } 113 114 void move_layer_up () { 115 int i; 116 Glyph g = MainWindow.get_current_glyph (); 117 118 // g.layers is ordered from bottom to top 119 i = DrawingTools.layer_tools.tool.size - g.current_layer - 1; 120 g.move_layer_up (); 121 122 DrawingTools.update_layers (); 123 124 if (i < 0) { 125 i = 0; 126 } 127 128 set_moving_label (i); 129 } 130 131 void move_layer_down () { 132 int i; 133 Glyph g = MainWindow.get_current_glyph (); 134 135 i = DrawingTools.layer_tools.tool.size - g.current_layer + 1; 136 g.move_layer_down (); 137 138 DrawingTools.update_layers (); 139 140 if (i >= DrawingTools.layer_tools.tool.size) { 141 i = DrawingTools.layer_tools.tool.size - 1; 142 } 143 144 set_moving_label (i); 145 } 146 147 void set_moving_label (int i) { 148 LayerLabel label; 149 int j = 0; 150 foreach (Tool layer in DrawingTools.layer_tools.tool) { 151 label = (LayerLabel) layer; 152 if (i == j) { 153 label.active_layer = true; 154 } 155 j++; 156 } 157 } 158 159 void set_text () { 160 double text_height; 161 162 label_text = new Text (); 163 label_text.set_text (label); 164 text_height = 17 * Toolbox.get_scale (); 165 label_text.set_font_size (text_height); 166 } 167 168 void set_layer_name () { 169 TextListener listener; 170 171 listener = new TextListener (t_("Layer"), layer.name, t_("Set")); 172 173 listener.signal_text_input.connect ((text) => { 174 layer.name = text; 175 label = text; 176 set_text (); 177 redraw (); 178 }); 179 180 listener.signal_submit.connect (() => { 181 TabContent.hide_text_input (); 182 }); 183 184 TabContent.show_text_input (listener); 185 } 186 187 public void select_layer () { 188 Glyph glyph = MainWindow.get_current_glyph (); 189 glyph.set_current_layer (layer); 190 DrawingTools.deselect_layers (); 191 selected_layer = true; 192 MainWindow.get_current_glyph ().clear_active_paths (); 193 GlyphCanvas.redraw (); 194 195 Font font = BirdFont.get_current_font (); 196 int index = glyph.get_layer_index (layer); 197 font.settings.set_setting (@"Active Layer $(glyph.get_name ())", @"$(index)"); 198 } 199 200 public void remove_layer () { 201 // remove layer after the click loop 202 IdleSource idle = new IdleSource (); 203 204 idle.set_callback (() => { 205 Glyph g = MainWindow.get_current_glyph (); 206 g.store_undo_state (); 207 g.layers.remove_layer (layer); 208 DrawingTools.update_layers (); 209 BirdFont.get_current_font ().touch (); 210 g.clear_active_paths (); 211 GlyphCanvas.redraw (); 212 return false; 213 }); 214 215 idle.attach (null); 216 } 217 218 public override void draw_tool (Context cr, double px, double py) { 219 Text visibility_icon; 220 double x = this.x - px; 221 double y = this.y - py; 222 double text_width; 223 double visibility_center_y; 224 string visibility; 225 226 // background 227 if (selected_layer) { 228 cr.save (); 229 Theme.color (cr, "Menu Background"); 230 // labels overlap with 2 pixels 231 cr.rectangle (0, y - 2 * Toolbox.get_scale (), w, h); 232 cr.fill (); 233 cr.restore (); 234 } 235 236 // tab label 237 cr.save (); 238 239 text_width = Toolbox.allocation_width; 240 text_width -= 30 * Toolbox.get_scale (); // delete button 241 text_width -= 20 * Toolbox.get_scale (); // visibility 242 243 label_text.truncate (text_width); 244 Theme.text_color (label_text, "Text Tool Box"); 245 label_text.draw_at_top (cr, x + 20 * Toolbox.get_scale (), y); 246 247 visibility = layer.visible ? "layer_visible" : "layer_hidden"; 248 visibility_icon = new Text (visibility, 30 * Toolbox.get_scale ()); 249 visibility_icon.load_font ("icons.bf"); 250 Theme.text_color (visibility_icon, "Text Tool Box"); 251 visibility_center_y = y + h / 2.0; 252 visibility_center_y -= visibility_icon.get_height () / 2.0; 253 visibility_center_y -= 2; 254 visibility_icon.draw_at_top (cr, x, visibility_center_y); 255 256 cr.restore (); 257 258 cr.save (); 259 Theme.color (cr, "Text Tool Box"); 260 cr.set_line_width (1); 261 cr.move_to (w - 20, y + h / 2 - 2.5 - 2); 262 cr.line_to (w - 25, y + h / 2 + 2.5 - 2); 263 cr.move_to (w - 20, y + h / 2 + 2.5 - 2); 264 cr.line_to (w - 25, y + h / 2 - 2.5 - 2); 265 cr.stroke (); 266 cr.restore (); 267 } 268 } 269 270 } 271