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