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