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