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