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 image selection offset
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_("Select 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 font_name.draw_separator = false; 32 33 select_background = new BackgroundSelectionTool (); 34 35 files = new Expander (t_("Files")); 36 files.set_persistent (true); 37 files.set_unique (true); 38 39 parts = new Expander (t_("Parts")); 40 parts.set_persistent (true); 41 parts.set_unique (true); 42 43 background_tools.add_tool (select_background); 44 45 LabelTool add_new_image = new LabelTool (t_("Add")); 46 add_new_image.select_action.connect ((t) => { 47 load_image (); 48 }); 49 background_selection.add_tool (add_new_image); 50 51 background_tools.add_tool (DrawingTools.move_background); 52 background_tools.add_tool (DrawingTools.move_canvas); 53 background_tools.add_tool (DrawingTools.background_scale); 54 55 expanders.add (font_name); 56 expanders.add (background_tools); 57 expanders.add (DrawingTools.zoombar_tool); 58 expanders.add (DrawingTools.view_tools); 59 expanders.add (DrawingTools.guideline_tools); 60 expanders.add (background_selection); 61 expanders.add (files); 62 expanders.add (parts); 63 } 64 65 public void remove_images () { 66 files.tool.clear (); 67 parts.tool.clear (); 68 } 69 70 void set_default_canvas () { 71 MainWindow.get_tab_bar ().select_tab_name ("Backgrounds"); 72 } 73 74 public void update_parts_list (BackgroundImage current_image) { 75 parts.tool.clear (); 76 77 foreach (BackgroundSelection selection in current_image.selections) { 78 add_part (selection); 79 } 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 set_default_canvas (); 138 Toolbox.redraw_tool_box (); 139 GlyphCanvas.redraw (); 140 141 return false; 142 }); 143 idle.attach (null); 144 }); 145 label.has_delete_button = true; 146 parts.add_tool (label, 0); 147 148 if (!is_null (MainWindow.get_toolbox ())) { 149 MainWindow.get_toolbox ().update_expanders (); 150 Toolbox.redraw_tool_box (); 151 } 152 } 153 154 void set_new_background_image (GlyphCollection gc, BackgroundPartLabel bpl) { 155 bpl.selection.assigned_glyph = gc.get_name (); 156 bpl.label = gc.get_name (); 157 gc.get_current ().set_background_image (bpl.selection.image); 158 159 if (bpl.selection.image != null) { 160 ((!) bpl.selection.image).center_in_glyph (gc.get_current ()); 161 } 162 163 set_default_canvas (); 164 ZoomTool.zoom_full_background_image (); 165 } 166 167 public override Gee.ArrayList<Expander> get_expanders () { 168 return expanders; 169 } 170 171 void load_image () { 172 FileChooser fc = new FileChooser (); 173 fc.file_selected.connect ((fn) => { 174 if (fn != null) { 175 add_image_file ((!) fn); 176 } 177 }); 178 179 MainWindow.file_chooser (t_("Open"), fc, FileChooser.LOAD); 180 } 181 182 void add_image_file (string file_path) { 183 File f = File.new_for_path (file_path); 184 string fn = (!) f.get_basename (); 185 BackgroundImage image = new BackgroundImage (file_path); 186 int i; 187 188 i = fn.index_of ("."); 189 if (i > -1) { 190 fn = fn.substring (0, i); 191 } 192 193 image.name = fn; 194 195 add_image (image); 196 197 GlyphCanvas.redraw (); 198 MainWindow.get_toolbox ().update_expanders (); 199 Toolbox.redraw_tool_box (); 200 } 201 202 public void add_image (BackgroundImage image) { 203 LabelTool image_selection; 204 double xc, yc; 205 BackgroundTab bt; 206 Font font; 207 208 font = BirdFont.get_current_font (); 209 210 image_selection = new BackgroundSelectionLabel (image, image.name); 211 image_selection.select_action.connect ((t) => { 212 BackgroundTab background_tab = BackgroundTab.get_instance (); 213 BackgroundSelectionLabel bg = (BackgroundSelectionLabel) t; 214 215 if (!bg.deleted) { 216 background_tab.set_background_image (bg.img); 217 background_tab.set_background_visible (true); 218 ZoomTool.zoom_full_background_image (); 219 update_parts_list (bg.img); 220 GlyphCanvas.redraw (); 221 Toolbox.redraw_tool_box (); 222 } 223 224 set_default_canvas (); 225 }); 226 227 image_selection.delete_action.connect ((t) => { 228 // don't invalidate the toolbox iterator 229 IdleSource idle = new IdleSource (); 230 idle.set_callback (() => { 231 BackgroundSelectionLabel bsl; 232 Font f = BirdFont.get_current_font (); 233 234 bsl = (BackgroundSelectionLabel) t; 235 bsl.deleted = true; 236 237 files.tool.remove (bsl); 238 f.background_images.remove (bsl.img); 239 240 MainWindow.get_current_glyph ().set_background_image (null); 241 242 MainWindow.get_toolbox ().update_expanders (); 243 set_default_canvas (); 244 Toolbox.redraw_tool_box (); 245 GlyphCanvas.redraw (); 246 return false; 247 }); 248 idle.attach (null); 249 }); 250 251 image_selection.has_delete_button = true; 252 253 files.add_tool (image_selection); 254 255 bt = BackgroundTab.get_instance (); 256 bt.set_background_image (image); 257 bt.set_background_visible (true); 258 ZoomTool.zoom_full_background_image (); 259 260 foreach (Tool t in files.tool) { 261 t.set_selected (false); 262 } 263 image_selection.set_selected (true); 264 265 bt.set_background_image (image); 266 bt.set_background_visible (true); 267 268 xc = image.img_middle_x; 269 yc = image.img_middle_y; 270 271 image.set_img_scale (0.2, 0.2); 272 273 image.img_middle_x = xc; 274 image.img_middle_y = yc; 275 276 image.center_in_glyph (); 277 ZoomTool.zoom_full_background_image (); 278 279 font.add_background_image (image); 280 } 281 282 class BackgroundSelectionLabel : LabelTool { 283 public BackgroundImage img; 284 public bool deleted; 285 public BackgroundSelectionLabel (BackgroundImage img, string base_name) { 286 base (base_name); 287 this.img = img; 288 deleted = false; 289 } 290 } 291 292 class BackgroundPartLabel : LabelTool { 293 public bool deleted; 294 public BackgroundSelection selection; 295 public BackgroundPartLabel (BackgroundSelection selection, string base_name) { 296 base (base_name); 297 this.selection = selection; 298 deleted = false; 299 } 300 } 301 } 302 303 } 304