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