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.
Cache overview items
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 185 if (!is_null (MainWindow.get_toolbox ())) { 186 MainWindow.get_toolbox ().update_expanders (); 187 Toolbox.redraw_tool_box (); 188 } 189 190 parts.redraw (); 191 } 192 193 void set_new_background_image (GlyphCollection gc, BackgroundPartLabel bpl) { 194 Glyph g; 195 196 g = gc.get_current (); 197 bpl.selection.assigned_glyph = gc.get_name (); 198 bpl.label = gc.get_name (); 199 g.set_background_image (bpl.selection.image); 200 g.set_background_visible (true); 201 202 if (bpl.selection.image != null) { 203 ((!) bpl.selection.image).center_in_glyph (gc.get_current ()); 204 } 205 206 set_default_canvas (); 207 ZoomTool.zoom_full_background_image (); 208 MainWindow.get_toolbox ().update_expanders (); 209 } 210 211 public override Gee.ArrayList<Expander> get_expanders () { 212 return expanders; 213 } 214 215 void load_image () { 216 FileChooser fc = new FileChooser (); 217 fc.file_selected.connect ((fn) => { 218 if (fn != null) { 219 add_image_file ((!) fn); 220 } 221 }); 222 223 fc.add_extension ("png"); 224 fc.add_extension ("jpeg"); 225 fc.add_extension ("jpg"); 226 fc.add_extension ("gif"); 227 fc.add_extension ("tiff"); 228 fc.add_extension ("bmp"); 229 fc.add_extension ("svg"); 230 231 MainWindow.file_chooser (t_("Open"), fc, FileChooser.LOAD); 232 } 233 234 void add_image_file (string file_path) { 235 File f = File.new_for_path (file_path); 236 string fn = (!) f.get_basename (); 237 BackgroundImage image = new BackgroundImage (file_path); 238 int i; 239 240 i = fn.index_of ("."); 241 if (i > -1) { 242 fn = fn.substring (0, i); 243 } 244 245 image.name = fn; 246 247 add_image (image); 248 249 GlyphCanvas.redraw (); 250 MainWindow.get_toolbox ().update_expanders (); 251 Toolbox.redraw_tool_box (); 252 } 253 254 public void add_image (BackgroundImage image) { 255 LabelTool image_selection; 256 double xc, yc; 257 BackgroundTab bt; 258 Font font; 259 260 font = BirdFont.get_current_font (); 261 262 image_selection = new BackgroundSelectionLabel (image, image.name); 263 image_selection.select_action.connect ((t) => { 264 BackgroundTab background_tab = BackgroundTab.get_instance (); 265 BackgroundSelectionLabel bg = (BackgroundSelectionLabel) t; 266 267 if (!bg.deleted) { 268 background_tab.set_background_image (bg.img); 269 background_tab.set_background_visible (true); 270 ZoomTool.zoom_full_background_image (); 271 update_parts_list (bg.img); 272 GlyphCanvas.redraw (); 273 Toolbox.redraw_tool_box (); 274 } 275 276 set_default_canvas (); 277 }); 278 279 image_selection.select_action ((BackgroundSelectionLabel) image_selection); 280 281 image_selection.delete_action.connect ((t) => { 282 // don't invalidate the toolbox iterator 283 IdleSource idle = new IdleSource (); 284 idle.set_callback (() => { 285 BackgroundSelectionLabel bsl; 286 Font f = BirdFont.get_current_font (); 287 288 bsl = (BackgroundSelectionLabel) t; 289 bsl.deleted = true; 290 291 files.tool.remove (bsl); 292 f.background_images.remove (bsl.img); 293 294 MainWindow.get_current_glyph ().set_background_image (null); 295 296 MainWindow.get_toolbox ().update_expanders (); 297 set_default_canvas (); 298 image_selection.redraw (); 299 GlyphCanvas.redraw (); 300 return false; 301 }); 302 idle.attach (null); 303 }); 304 305 image_selection.has_delete_button = true; 306 307 files.add_tool (image_selection); 308 309 bt = BackgroundTab.get_instance (); 310 bt.set_background_image (image); 311 bt.set_background_visible (true); 312 ZoomTool.zoom_full_background_image (); 313 314 foreach (Tool t in files.tool) { 315 t.set_selected (false); 316 } 317 image_selection.set_selected (true); 318 319 bt.set_background_image (image); 320 bt.set_background_visible (true); 321 322 xc = image.img_middle_x; 323 yc = image.img_middle_y; 324 325 image.set_img_scale (0.2, 0.2); 326 327 image.img_middle_x = xc; 328 image.img_middle_y = yc; 329 330 image.center_in_glyph (); 331 ZoomTool.zoom_full_background_image (); 332 333 font.add_background_image (image); 334 } 335 336 public override Gee.ArrayList<string> get_displays () { 337 Gee.ArrayList<string> d = new Gee.ArrayList<string> (); 338 d.add ("Backgrounds"); 339 return d; 340 } 341 342 class BackgroundSelectionLabel : LabelTool { 343 public BackgroundImage img; 344 public bool deleted; 345 public BackgroundSelectionLabel (BackgroundImage img, string base_name) { 346 base (base_name); 347 this.img = img; 348 deleted = false; 349 } 350 } 351 352 class BackgroundPartLabel : LabelTool { 353 public bool deleted; 354 public BackgroundSelection selection; 355 public BackgroundPartLabel (BackgroundSelection selection, string base_name) { 356 base (base_name); 357 this.selection = selection; 358 deleted = false; 359 } 360 } 361 } 362 363 } 364