Commit 84cfe17
Delete background selections
Changed files (8)
modifiedicons/select_background.pngmodifiedlibbirdfont/BackgroundImage.valaaddedlibbirdfont/BackgroundSelection.valaaddedlibbirdfont/BackgroundSelectionTool.valamodifiedlibbirdfont/BackgroundTools.valamodifiedlibbirdfont/CutBackgroundTool.valamodifiedlibbirdfont/Expander.valamodifiedlibbirdfont/LabelTool.vala
Diff
Binary files a/icons/select_background.png and b/icons/select_background.png differ@@ -20,7 +20,8 @@ namespace BirdFont { public class BackgroundImage { public string name = "";- + public Gee.ArrayList<BackgroundSelection> selections;+ public double img_x = 0; public double img_y = 0; @@ -92,6 +93,7 @@ public class BackgroundImage { public BackgroundImage (string file_name) { path = file_name;+ selections = new Gee.ArrayList<BackgroundSelection> (); } public BackgroundImage copy () {@@ -108,10 +110,18 @@ public class BackgroundImage { bg.threshold = threshold; bg.high_contrast = high_contrast; bg.trace_resolution = trace_resolution;+ + foreach (BackgroundSelection b in selections) {+ bg.selections.add (b);+ } return bg; } + public void add_selection (BackgroundSelection bs) {+ selections.add (bs);+ }+ public void set_trace_simplification (double s) { simplification = s; }new file mode 100644@@ -0,0 +1,41 @@+/*+ Copyright (C) 2014 Johan Mattsson++ This library is free software; you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as + published by the Free Software Foundation; either version 3 of the + License, or (at your option) any later version.++ This library is distributed in the hope that it will be useful, but + WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details.+*/++namespace BirdFont {++public class BackgroundSelection : GLib.Object {+ + public Glyph? assigned_glyph;+ public BackgroundImage image;+ public BackgroundImage parent_image;+ + public double x;+ public double y;+ public double w;+ public double h;+ + public BackgroundSelection (BackgroundImage img, BackgroundImage parent_img,+ double x, double y, double w, double h) {+ + assigned_glyph = null;+ parent_image = parent_img;+ image = img;+ this.x = x;+ this.y = y;+ this.w = w;+ this.h = h;+ }+}++}new file mode 100644@@ -0,0 +1,84 @@+/*+ Copyright (C) 2014 Johan Mattsson++ This library is free software; you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as + published by the Free Software Foundation; either version 3 of the + License, or (at your option) any later version.++ This library is distributed in the hope that it will be useful, but + WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details.+*/++using Cairo;+using Math;++namespace BirdFont {++public class BackgroundSelectionTool : CutBackgroundTool {+ + public BackgroundSelectionTool () {+ base ("select_background", t_("Select Background"));+ editor_events = true;+ persistent = true;+ self_destination = false;+ + new_image.connect (add_new_image);++ select_action.connect ((t) => {+ GlyphCanvas.redraw ();+ });++ draw_action.connect ((self, cr, glyph) => {+ double x, y, w, h;+ BackgroundImage bg;+ + if (glyph.get_background_image () == null) {+ warning ("No image");+ return;+ }+ + // draw a border around each selection+ bg = (!) glyph.get_background_image ();+ foreach (BackgroundSelection bs in bg.selections) {+ x = Glyph.reverse_path_coordinate_x (bs.x);+ y = Glyph.reverse_path_coordinate_y (bs.y);+ w = Glyph.reverse_path_coordinate_x (bs.x + bs.w) - x;+ h = Glyph.reverse_path_coordinate_y (bs.y + bs.h) - y;+ + cr.save ();+ cr.set_line_width (2.0);+ cr.set_source_rgba (237 / 255.0, 67 / 255.0, 0, 1);+ cr.rectangle (x, y, w, h);+ cr.stroke ();++ cr.arc (x + w, y + h, 9.0, 0, 2 * PI);+ cr.fill ();+ + cr.restore ();+ }+ });+ }+ + public void add_new_image (BackgroundImage image,+ double x, double y, double w, double h) {+ + BackgroundImage bg;+ BackgroundSelection selection;++ if (MainWindow.get_current_glyph ().get_background_image () == null) {+ warning ("No image");+ return;+ }+ + bg = (!) MainWindow.get_current_glyph ().get_background_image ();+ selection = new BackgroundSelection (image, bg, x, y, w, h);+ + bg.add_selection (selection);+ Toolbox.background_tools.add_part (selection);+ }+}++}@@ -16,9 +16,11 @@ namespace BirdFont { public class BackgroundTools : ToolCollection { Expander files;+ Expander parts; public Gee.ArrayList<Expander> expanders = new Gee.ArrayList<Expander> (); public BackgroundTools () {+ BackgroundSelectionTool select_background = new BackgroundSelectionTool (); Expander background_selection = new Expander (t_("Images")); Expander background_tools = new Expander (); @@ -30,6 +32,10 @@ public class BackgroundTools : ToolCollection { files.set_persistent (true); files.set_unique (true); + parts = new Expander (t_("Parts"));+ parts.set_persistent (true);+ parts.set_unique (true);+ LabelTool add_new_image = new LabelTool (t_("Add")); add_new_image.select_action.connect ((t) => { load_image ();@@ -38,13 +44,12 @@ public class BackgroundTools : ToolCollection { LabelTool remove_image = new LabelTool (t_("Remove")); background_selection.add_tool (remove_image);- - Tool select_background = new Tool ("select_background", t_("Select Background"));- select_background.select_action.connect ((self) => {++ select_background.select_action.connect ((t) => {+ foreach (Tool tool in background_tools.tool) {+ tool.set_selected (false);+ } });- - select_background.editor_events = true;- select_background.persistent = true; background_tools.add_tool (select_background); background_tools.add_tool (DrawingTools.move_background);@@ -57,10 +62,48 @@ public class BackgroundTools : ToolCollection { expanders.add (DrawingTools.guideline_tools); expanders.add (background_selection); expanders.add (files);+ expanders.add (parts); } public void remove_images () { files.tool.clear ();+ parts.tool.clear ();+ }++ public void add_part (BackgroundSelection selection) {+ BackgroundPartLabel label;+ label = new BackgroundPartLabel (selection, t_("No Glyph Selected"));+ label.select_action.connect ((t) => {+ BackgroundPartLabel bpl = (BackgroundPartLabel) t;++ });+ label.delete_action.connect ((t) => {+ // don't invalidate the toolbox iterator+ IdleSource idle = new IdleSource (); + idle.set_callback (() => {+ Glyph g;+ BackgroundPartLabel bpl;+ + bpl = (BackgroundPartLabel) t;+ + if (bpl.selection.assigned_glyph != null){+ g = (!) bpl.selection.assigned_glyph;+ g.set_background_image (null);+ }+ + parts.tool.remove (bpl);+ bpl.selection.parent_image.selections.remove (bpl.selection);+ MainWindow.get_toolbox ().update_expanders ();+ Toolbox.redraw_tool_box ();+ GlyphCanvas.redraw ();+ return false;+ });+ idle.attach (null);+ });+ label.has_delete_button = true;+ parts.add_tool (label, 0);+ MainWindow.get_toolbox ().update_expanders ();+ Toolbox.redraw_tool_box (); } public override Gee.ArrayList<Expander> get_expanders () {@@ -95,6 +138,7 @@ public class BackgroundTools : ToolCollection { add_image (image); font.add_background_image (image); GlyphCanvas.redraw ();+ MainWindow.get_toolbox ().update_expanders (); Toolbox.redraw_tool_box (); } @@ -103,10 +147,10 @@ public class BackgroundTools : ToolCollection { double xc, yc; BackgroundTab bt; - image_selection = new BackgroundSelection (image, image.name);+ image_selection = new BackgroundSelectionLabel (image, image.name); image_selection.select_action.connect ((t) => { BackgroundTab background_tab = BackgroundTab.get_instance ();- BackgroundSelection bg = (BackgroundSelection) t;+ BackgroundSelectionLabel bg = (BackgroundSelectionLabel) t; background_tab.set_background_image (bg.img); background_tab.set_background_visible (true); ZoomTool.zoom_full_background_image ();@@ -139,13 +183,21 @@ public class BackgroundTools : ToolCollection { ZoomTool.zoom_full_background_image (); } - class BackgroundSelection : LabelTool {+ class BackgroundSelectionLabel : LabelTool { public BackgroundImage img;- public BackgroundSelection (BackgroundImage img, string base_name) {+ public BackgroundSelectionLabel (BackgroundImage img, string base_name) { base (base_name); this.img = img; } }++ class BackgroundPartLabel : LabelTool {+ public BackgroundSelection selection;+ public BackgroundPartLabel (BackgroundSelection selection, string base_name) {+ base (base_name);+ this.selection = selection;+ }+ } } }@@ -1,5 +1,5 @@ /*- Copyright (C) 2012 Johan Mattsson+ Copyright (C) 2012 2014 Johan Mattsson This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as @@ -28,10 +28,15 @@ public class CutBackgroundTool : Tool { bool is_set = false; bool is_done = false; - public signal void new_image (BackgroundImage file);- - public CutBackgroundTool (string name) {- base (name, t_("Crop background image"));+ public signal void new_image (BackgroundImage file, + double coordinate_x, double coordinate_y, double width, double height);+ + public bool self_destination = true;+ + public CutBackgroundTool (string name, string? tool_tip = null) {+ string tt;+ tt = (tool_tip == null) ? t_("Crop background image") : (!) tool_tip;+ base (name, tt); select_action.connect((self) => { });@@ -85,7 +90,7 @@ public class CutBackgroundTool : Tool { }); draw_action.connect ((self, cr, glyph) => {- if (is_visible) {+ if (is_visible && x1 - x2 != 0 && y1 - y2 != 0) { // draw a border around the selection cr.save (); cr.set_line_width (2.0);@@ -107,16 +112,21 @@ public class CutBackgroundTool : Tool { } }); - new_image.connect ((file) => {- Glyph glyph = MainWindow.get_current_glyph ();- TabBar tb = MainWindow.get_tab_bar ();- - glyph.store_undo_state ();+ new_image.connect ((file, x, y, w, h) => {+ Glyph glyph;+ TabBar tb; - glyph.set_background_image (file);- tb.select_tab_name (glyph.get_name ());+ if (self_destination) { + glyph = MainWindow.get_current_glyph ();+ tb = MainWindow.get_tab_bar (); - glyph.set_background_visible (true);+ glyph.store_undo_state ();+ + glyph.set_background_image (file);+ tb.select_tab_name (glyph.get_name ());+ + glyph.set_background_visible (true);+ } }); }@@ -216,6 +226,7 @@ public class CutBackgroundTool : Tool { File img_file_next; string fn; double wc, hc;+ double px, py, w, h; img_dir = get_child (f.get_backgrounds_folder (), "parts"); @@ -265,7 +276,12 @@ public class CutBackgroundTool : Tool { newbg.img_scale_x = original_bg.img_scale_x; newbg.img_scale_y = original_bg.img_scale_y; - new_image (newbg);+ px = Glyph.path_coordinate_x (fmin (x1, x2));+ py = Glyph.path_coordinate_y (fmin (y1, y2));+ w = Glyph.path_coordinate_x (fmax (x1, x2)) - px;+ h = Glyph.path_coordinate_y (fmax (y1, y2)) - py;+ + new_image (newbg, px, py, w, h); #endif } }@@ -155,8 +155,14 @@ public class Expander : GLib.Object { update_tool_position (); } - public void add_tool (Tool t) {- tool.add (t);+ public void add_tool (Tool t, int position = -1) {+ if (position < 0) {+ tool.add (t);+ } else {+ return_if_fail (position <= tool.size);+ tool.insert (position, t);+ }+ update_tool_position (); t.select_action.connect ((selected) => {@@ -22,6 +22,8 @@ public class LabelTool : Tool { public string label { get; set; } public string number { get; set; } public bool has_counter { get; set; }+ public bool has_delete_button { get; set; }+ public signal void delete_action (LabelTool self); public LabelTool (string label) { base ();@@ -29,10 +31,14 @@ public class LabelTool : Tool { this.label = label; this.number = "-"; + has_delete_button = false; has_counter = false; counter_background = Icons.get_icon ("overview_counter.png"); panel_press_action.connect ((selected, button, tx, ty) => {+ if (has_delete_button && y <= ty <= y + h && tx >= w - 30) {+ delete_action (this);+ } }); panel_move_action.connect ((selected, button, tx, ty) => {@@ -100,6 +106,16 @@ public class LabelTool : Tool { cr.restore (); } + if (has_delete_button) {+ cr.save ();+ cr.set_line_width (1);+ cr.move_to (w - 20, y + h / 2 - 2.5 + 2);+ cr.line_to (w - 25, y + h / 2 + 2.5 + 2);+ cr.move_to (w - 20, y + h / 2 + 2.5 + 2);+ cr.line_to (w - 25, y + h / 2 - 2.5 + 2); + cr.stroke ();+ cr.restore ();+ } } }