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.
Fix background tools selection
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 override void selected () { 73 // perform update after label selection is done 74 IdleSource idle = new IdleSource (); 75 idle.set_callback (() => { 76 foreach (Tool t in files.tool) { 77 BackgroundSelectionLabel bg = (BackgroundSelectionLabel) t; 78 79 if (bg.is_selected ()) { 80 update_parts_list (bg.img); 81 } 82 } 83 84 return false; 85 }); 86 idle.attach (null); 87 } 88 89 public void update_parts_list (BackgroundImage current_image) { 90 parts.tool.clear (); 91 92 foreach (BackgroundSelection selection in current_image.selections) { 93 add_part (selection); 94 } 95 96 parts.redraw (); 97 } 98 99 public void add_part (BackgroundSelection selection) { 100 BackgroundPartLabel label; 101 102 if (selection.assigned_glyph == null) { 103 label = new BackgroundPartLabel (selection, t_("Select Glyph")); 104 } else { 105 label = new BackgroundPartLabel (selection, (!) selection.assigned_glyph); 106 } 107 108 label.select_action.connect ((t) => { 109 BackgroundPartLabel bpl = (BackgroundPartLabel) t; 110 GlyphSelection gs = new GlyphSelection (); 111 112 gs.selected_glyph.connect ((gc) => { 113 GlyphCollection? pgc; 114 Font font = BirdFont.get_current_font (); 115 116 pgc = font.get_glyph_collection_by_name (bpl.selection.assigned_glyph); 117 if (pgc != null) { 118 ((!) pgc).get_current ().set_background_image (null); 119 } 120 121 set_new_background_image (gc, bpl); 122 }); 123 124 gs.open_new_glyph_signal.connect ((character) => { 125 GlyphCollection gc = gs.create_new_glyph (character); 126 set_new_background_image (gc, bpl); 127 }); 128 129 if (!bpl.deleted) { 130 GlyphCanvas.set_display (gs); 131 Toolbox.set_toolbox_from_tab ("Overview"); 132 } 133 }); 134 135 label.delete_action.connect ((t) => { 136 // don't invalidate the toolbox iterator 137 IdleSource idle = new IdleSource (); 138 idle.set_callback (() => { 139 GlyphCollection? gc; 140 BackgroundPartLabel bpl; 141 Font font = BirdFont.get_current_font (); 142 143 bpl = (BackgroundPartLabel) t; 144 bpl.deleted = true; 145 146 gc = font.get_glyph_collection_by_name (bpl.selection.assigned_glyph); 147 if (gc != null) { 148 ((!) gc).get_current ().set_background_image (null); 149 } 150 151 parts.tool.remove (bpl); 152 bpl.selection.parent_image.selections.remove (bpl.selection); 153 MainWindow.get_toolbox ().update_expanders (); 154 parts.clear_cache (); 155 set_default_canvas (); 156 Toolbox.redraw_tool_box (); 157 GlyphCanvas.redraw (); 158 159 return false; 160 }); 161 idle.attach (null); 162 }); 163 label.has_delete_button = true; 164 parts.add_tool (label, 0); 165 166 if (!is_null (MainWindow.get_toolbox ())) { 167 MainWindow.get_toolbox ().update_expanders (); 168 Toolbox.redraw_tool_box (); 169 } 170 171 parts.redraw (); 172 } 173 174 void set_new_background_image (GlyphCollection gc, BackgroundPartLabel bpl) { 175 Glyph g; 176 177 g = gc.get_current (); 178 bpl.selection.assigned_glyph = gc.get_name (); 179 bpl.label = gc.get_name (); 180 g.set_background_image (bpl.selection.image); 181 g.set_background_visible (true); 182 183 if (bpl.selection.image != null) { 184 ((!) bpl.selection.image).center_in_glyph (gc.get_current ()); 185 } 186 187 set_default_canvas (); 188 ZoomTool.zoom_full_background_image (); 189 MainWindow.get_toolbox ().update_expanders (); 190 } 191 192 public override Gee.ArrayList<Expander> get_expanders () { 193 return expanders; 194 } 195 196 void load_image () { 197 FileChooser fc = new FileChooser (); 198 fc.file_selected.connect ((fn) => { 199 if (fn != null) { 200 add_image_file ((!) fn); 201 } 202 }); 203 204 fc.add_extension ("png"); 205 fc.add_extension ("jpeg"); 206 fc.add_extension ("jpg"); 207 fc.add_extension ("gif"); 208 fc.add_extension ("tiff"); 209 fc.add_extension ("bmp"); 210 fc.add_extension ("svg"); 211 212 MainWindow.file_chooser (t_("Open"), fc, FileChooser.LOAD); 213 } 214 215 void add_image_file (string file_path) { 216 File f = File.new_for_path (file_path); 217 string fn = (!) f.get_basename (); 218 BackgroundImage image = new BackgroundImage (file_path); 219 int i; 220 221 i = fn.index_of ("."); 222 if (i > -1) { 223 fn = fn.substring (0, i); 224 } 225 226 image.name = fn; 227 228 add_image (image); 229 230 GlyphCanvas.redraw (); 231 MainWindow.get_toolbox ().update_expanders (); 232 Toolbox.redraw_tool_box (); 233 } 234 235 public void add_image (BackgroundImage image) { 236 LabelTool image_selection; 237 double xc, yc; 238 BackgroundTab bt; 239 Font font; 240 241 font = BirdFont.get_current_font (); 242 243 image_selection = new BackgroundSelectionLabel (image, image.name); 244 image_selection.select_action.connect ((t) => { 245 BackgroundTab background_tab = BackgroundTab.get_instance (); 246 BackgroundSelectionLabel bg = (BackgroundSelectionLabel) t; 247 248 if (!bg.deleted) { 249 background_tab.set_background_image (bg.img); 250 background_tab.set_background_visible (true); 251 ZoomTool.zoom_full_background_image (); 252 update_parts_list (bg.img); 253 GlyphCanvas.redraw (); 254 Toolbox.redraw_tool_box (); 255 } 256 257 set_default_canvas (); 258 }); 259 260 image_selection.select_action ((BackgroundSelectionLabel) image_selection); 261 262 image_selection.delete_action.connect ((t) => { 263 // don't invalidate the toolbox iterator 264 IdleSource idle = new IdleSource (); 265 idle.set_callback (() => { 266 BackgroundSelectionLabel bsl; 267 Font f = BirdFont.get_current_font (); 268 269 bsl = (BackgroundSelectionLabel) t; 270 bsl.deleted = true; 271 272 files.tool.remove (bsl); 273 f.background_images.remove (bsl.img); 274 275 MainWindow.get_current_glyph ().set_background_image (null); 276 277 MainWindow.get_toolbox ().update_expanders (); 278 set_default_canvas (); 279 image_selection.redraw (); 280 GlyphCanvas.redraw (); 281 return false; 282 }); 283 idle.attach (null); 284 }); 285 286 image_selection.has_delete_button = true; 287 288 files.add_tool (image_selection); 289 290 bt = BackgroundTab.get_instance (); 291 bt.set_background_image (image); 292 bt.set_background_visible (true); 293 ZoomTool.zoom_full_background_image (); 294 295 foreach (Tool t in files.tool) { 296 t.set_selected (false); 297 } 298 image_selection.set_selected (true); 299 300 bt.set_background_image (image); 301 bt.set_background_visible (true); 302 303 xc = image.img_middle_x; 304 yc = image.img_middle_y; 305 306 image.set_img_scale (0.2, 0.2); 307 308 image.img_middle_x = xc; 309 image.img_middle_y = yc; 310 311 image.center_in_glyph (); 312 ZoomTool.zoom_full_background_image (); 313 314 font.add_background_image (image); 315 } 316 317 public override Gee.ArrayList<string> get_displays () { 318 Gee.ArrayList<string> d = new Gee.ArrayList<string> (); 319 d.add ("Backgrounds"); 320 return d; 321 } 322 323 class BackgroundSelectionLabel : LabelTool { 324 public BackgroundImage img; 325 public bool deleted; 326 public BackgroundSelectionLabel (BackgroundImage img, string base_name) { 327 base (base_name); 328 this.img = img; 329 deleted = false; 330 } 331 } 332 333 class BackgroundPartLabel : LabelTool { 334 public bool deleted; 335 public BackgroundSelection selection; 336 public BackgroundPartLabel (BackgroundSelection selection, string base_name) { 337 base (base_name); 338 this.selection = selection; 339 deleted = false; 340 } 341 } 342 } 343 344 } 345