The Birdfont Source Code


All Repositories / birdfont.git / blob – RSS feed

BackgroundTools.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/BackgroundTools.vala.
Ignore case in key bindings
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 namespace BirdFont { 16 17 public class BackgroundTools : ToolCollection { 18 19 public BackgroundSelectionTool select_background; 20 21 Expander files; 22 Expander parts; 23 public Gee.ArrayList<Expander> expanders = new Gee.ArrayList<Expander> (); 24 25 public BackgroundTools () { 26 Expander background_tools = new Expander (t_("Background Image")); 27 Expander background_selection = new Expander (t_("Images")); 28 29 Expander font_name = new Expander (); 30 font_name.add_tool (new FontName ()); 31 32 select_background = new BackgroundSelectionTool (); 33 34 files = new Expander (t_("Files")); 35 files.set_persistent (true); 36 files.set_unique (true); 37 38 parts = new Expander (t_("Parts")); 39 parts.set_persistent (true); 40 parts.set_unique (true); 41 42 background_tools.add_tool (select_background); 43 44 LabelTool add_new_image = new LabelTool (t_("Add")); 45 add_new_image.select_action.connect ((t) => { 46 load_image (); 47 }); 48 background_selection.add_tool (add_new_image); 49 50 background_tools.add_tool (DrawingTools.move_background); 51 background_tools.add_tool (DrawingTools.move_canvas); 52 background_tools.add_tool (DrawingTools.background_scale); 53 54 expanders.add (font_name); 55 expanders.add (background_tools); 56 expanders.add (DrawingTools.zoombar_tool); 57 expanders.add (DrawingTools.guideline_tools); 58 expanders.add (background_selection); 59 expanders.add (files); 60 expanders.add (parts); 61 } 62 63 public void remove_images () { 64 files.tool.clear (); 65 parts.tool.clear (); 66 } 67 68 void set_default_canvas () { 69 MainWindow.get_tab_bar ().select_tab_name ("Backgrounds"); 70 } 71 72 public void update_parts_list (BackgroundImage current_image) { 73 parts.tool.clear (); 74 75 foreach (BackgroundSelection selection in current_image.selections) { 76 add_part (selection); 77 } 78 79 parts.redraw (); 80 } 81 82 public void add_part (BackgroundSelection selection) { 83 BackgroundPartLabel label; 84 85 if (selection.assigned_glyph == null) { 86 label = new BackgroundPartLabel (selection, t_("Select Glyph")); 87 } else { 88 label = new BackgroundPartLabel (selection, (!) selection.assigned_glyph); 89 } 90 91 label.select_action.connect ((t) => { 92 BackgroundPartLabel bpl = (BackgroundPartLabel) t; 93 GlyphSelection gs = new GlyphSelection (); 94 95 gs.selected_glyph.connect ((gc) => { 96 GlyphCollection? pgc; 97 Font font = BirdFont.get_current_font (); 98 99 pgc = font.get_glyph_collection_by_name (bpl.selection.assigned_glyph); 100 if (pgc != null) { 101 ((!) pgc).get_current ().set_background_image (null); 102 } 103 104 set_new_background_image (gc, bpl); 105 }); 106 107 gs.open_new_glyph_signal.connect ((character) => { 108 GlyphCollection gc = gs.create_new_glyph (character); 109 set_new_background_image (gc, bpl); 110 }); 111 112 if (!bpl.deleted) { 113 GlyphCanvas.set_display (gs); 114 Toolbox.set_toolbox_from_tab ("Overview"); 115 } 116 }); 117 118 label.delete_action.connect ((t) => { 119 // don't invalidate the toolbox iterator 120 IdleSource idle = new IdleSource (); 121 idle.set_callback (() => { 122 GlyphCollection? gc; 123 BackgroundPartLabel bpl; 124 Font font = BirdFont.get_current_font (); 125 126 bpl = (BackgroundPartLabel) t; 127 bpl.deleted = true; 128 129 gc = font.get_glyph_collection_by_name (bpl.selection.assigned_glyph); 130 if (gc != null) { 131 ((!) gc).get_current ().set_background_image (null); 132 } 133 134 parts.tool.remove (bpl); 135 bpl.selection.parent_image.selections.remove (bpl.selection); 136 MainWindow.get_toolbox ().update_expanders (); 137 set_default_canvas (); 138 Toolbox.redraw_tool_box (); 139 GlyphCanvas.redraw (); 140 141 return false; 142 }); 143 idle.attach (null); 144 }); 145 label.has_delete_button = true; 146 parts.add_tool (label, 0); 147 148 if (!is_null (MainWindow.get_toolbox ())) { 149 MainWindow.get_toolbox ().update_expanders (); 150 Toolbox.redraw_tool_box (); 151 } 152 153 parts.redraw (); 154 } 155 156 void set_new_background_image (GlyphCollection gc, BackgroundPartLabel bpl) { 157 Glyph g; 158 159 g = gc.get_current (); 160 bpl.selection.assigned_glyph = gc.get_name (); 161 bpl.label = gc.get_name (); 162 g.set_background_image (bpl.selection.image); 163 g.set_background_visible (true); 164 165 if (bpl.selection.image != null) { 166 ((!) bpl.selection.image).center_in_glyph (gc.get_current ()); 167 } 168 169 set_default_canvas (); 170 ZoomTool.zoom_full_background_image (); 171 } 172 173 public override Gee.ArrayList<Expander> get_expanders () { 174 return expanders; 175 } 176 177 void load_image () { 178 FileChooser fc = new FileChooser (); 179 fc.file_selected.connect ((fn) => { 180 if (fn != null) { 181 add_image_file ((!) fn); 182 } 183 }); 184 185 MainWindow.file_chooser (t_("Open"), fc, FileChooser.LOAD); 186 } 187 188 void add_image_file (string file_path) { 189 File f = File.new_for_path (file_path); 190 string fn = (!) f.get_basename (); 191 BackgroundImage image = new BackgroundImage (file_path); 192 int i; 193 194 i = fn.index_of ("."); 195 if (i > -1) { 196 fn = fn.substring (0, i); 197 } 198 199 image.name = fn; 200 201 add_image (image); 202 203 GlyphCanvas.redraw (); 204 MainWindow.get_toolbox ().update_expanders (); 205 Toolbox.redraw_tool_box (); 206 } 207 208 public void add_image (BackgroundImage image) { 209 LabelTool image_selection; 210 double xc, yc; 211 BackgroundTab bt; 212 Font font; 213 214 font = BirdFont.get_current_font (); 215 216 image_selection = new BackgroundSelectionLabel (image, image.name); 217 image_selection.select_action.connect ((t) => { 218 BackgroundTab background_tab = BackgroundTab.get_instance (); 219 BackgroundSelectionLabel bg = (BackgroundSelectionLabel) t; 220 221 if (!bg.deleted) { 222 background_tab.set_background_image (bg.img); 223 background_tab.set_background_visible (true); 224 ZoomTool.zoom_full_background_image (); 225 update_parts_list (bg.img); 226 GlyphCanvas.redraw (); 227 Toolbox.redraw_tool_box (); 228 } 229 230 set_default_canvas (); 231 }); 232 233 image_selection.delete_action.connect ((t) => { 234 // don't invalidate the toolbox iterator 235 IdleSource idle = new IdleSource (); 236 idle.set_callback (() => { 237 BackgroundSelectionLabel bsl; 238 Font f = BirdFont.get_current_font (); 239 240 bsl = (BackgroundSelectionLabel) t; 241 bsl.deleted = true; 242 243 files.tool.remove (bsl); 244 f.background_images.remove (bsl.img); 245 246 MainWindow.get_current_glyph ().set_background_image (null); 247 248 MainWindow.get_toolbox ().update_expanders (); 249 set_default_canvas (); 250 image_selection.redraw (); 251 GlyphCanvas.redraw (); 252 return false; 253 }); 254 idle.attach (null); 255 }); 256 257 image_selection.has_delete_button = true; 258 259 files.add_tool (image_selection); 260 261 bt = BackgroundTab.get_instance (); 262 bt.set_background_image (image); 263 bt.set_background_visible (true); 264 ZoomTool.zoom_full_background_image (); 265 266 foreach (Tool t in files.tool) { 267 t.set_selected (false); 268 } 269 image_selection.set_selected (true); 270 271 bt.set_background_image (image); 272 bt.set_background_visible (true); 273 274 xc = image.img_middle_x; 275 yc = image.img_middle_y; 276 277 image.set_img_scale (0.2, 0.2); 278 279 image.img_middle_x = xc; 280 image.img_middle_y = yc; 281 282 image.center_in_glyph (); 283 ZoomTool.zoom_full_background_image (); 284 285 font.add_background_image (image); 286 } 287 288 public override Gee.ArrayList<string> get_displays () { 289 Gee.ArrayList<string> d = new Gee.ArrayList<string> (); 290 d.add ("Backgrounds"); 291 return d; 292 } 293 294 class BackgroundSelectionLabel : LabelTool { 295 public BackgroundImage img; 296 public bool deleted; 297 public BackgroundSelectionLabel (BackgroundImage img, string base_name) { 298 base (base_name); 299 this.img = img; 300 deleted = false; 301 } 302 } 303 304 class BackgroundPartLabel : LabelTool { 305 public bool deleted; 306 public BackgroundSelection selection; 307 public BackgroundPartLabel (BackgroundSelection selection, string base_name) { 308 base (base_name); 309 this.selection = selection; 310 deleted = false; 311 } 312 } 313 } 314 315 } 316