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