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