Clone in a terminal:
Commit 37ea6aa
Johan Mattsson - 2014-11-12T14:43:55+00:00
37ea6aa58fd57e128bfe0db374a16763f0cb267ejohan.mattsson.m@gmail.com

Add a zoom bar

Changed files (6)

Diff

diff --git a/libbirdfont/BackgroundTools.vala b/libbirdfont/BackgroundTools.valaindex 95b8498..06bfb7e 100644--- a/libbirdfont/BackgroundTools.vala+++ b/libbirdfont/BackgroundTools.vala@@ -226,6 +226,8 @@ public class BackgroundTools : ToolCollection  { 			 				files.tool.remove (bsl); 				f.background_images.remove (bsl.img);+				+				MainWindow.get_current_glyph ().set_background_image (null);  				MainWindow.get_toolbox ().update_expanders (); 				set_default_canvas ();diff --git a/libbirdfont/DrawingTools.vala b/libbirdfont/DrawingTools.valaindex 7670a99..2646e7f 100644--- a/libbirdfont/DrawingTools.vala+++ b/libbirdfont/DrawingTools.vala@@ -91,6 +91,8 @@ public class DrawingTools : ToolCollection  { 	Tool flip_vertical; 	Tool flip_horizontal; 	+	public ZoomBar zoom_bar;+	 	public DrawingTools (GlyphCanvas main_glyph_canvas) { 		glyph_canvas = main_glyph_canvas; 		@@ -840,6 +842,27 @@ public class DrawingTools : ToolCollection  { 		guideline_tools.add_tool (new_grid);  		// Zoom tools +		zoom_bar = new ZoomBar ();+		zoom_bar.new_zoom.connect ((z) => {+			Glyph g = MainWindow.get_current_glyph ();+			double zoom = 20 * z + 1;+			double xc, yc, nxc, nyc;++			xc = Glyph.path_coordinate_x (Glyph.xc ());+			yc = Glyph.path_coordinate_y (Glyph.yc ());+						+			g.set_zoom (zoom);++			nxc = Glyph.path_coordinate_x (Glyph.xc ());+			nyc = Glyph.path_coordinate_y (Glyph.yc ());+			+			g.view_offset_x -= nxc - xc;+			g.view_offset_y += nyc - yc;+						+			GlyphCanvas.redraw ();+		});+		view_tools.add_tool (zoom_bar);+		 		Tool zoom_in = new Tool ("zoom_in", t_("Zoom in"), '+', CTRL); 		zoom_in.select_action.connect ((self) => { 			zoom_tool.store_current_view ();diff --git a/libbirdfont/EditPoint.vala b/libbirdfont/EditPoint.valaindex 55f5d2c..6dd0999 100644--- a/libbirdfont/EditPoint.vala+++ b/libbirdfont/EditPoint.vala@@ -527,8 +527,8 @@ public class EditPoint : GLib.Object { 		 		ivz = 1 / g.view_zoom; 		-		xc = (g.allocation.width / 2.0);-		yc = (g.allocation.height / 2.0);+		xc = g.allocation.width / 2.0;+		yc = g.allocation.height / 2.0; 				 		x *= ivz; 		y *= ivz;diff --git a/libbirdfont/Expander.vala b/libbirdfont/Expander.valaindex 3281bb2..78e743a 100644--- a/libbirdfont/Expander.vala+++ b/libbirdfont/Expander.vala@@ -88,6 +88,8 @@ public class Expander : GLib.Object { 		double yt = y + scroll + margin_small; 		bool new_row = false; 		bool has_visible_tools = false;+		Tool previous;+		bool first_row;  		foreach (Tool t in tool) { 			if (t.tool_is_visible ()) {@@ -100,9 +102,12 @@ public class Expander : GLib.Object { 			content_height = 0; 			return; 		}-+		 		foreach (Tool t in tool) {-			if (t is LabelTool) {+			if (t is ZoomBar) {+				t.w = Toolbox.allocation_width * scale;+				t.h = 7 * scale;+			} else if (t is LabelTool) { 				t.w = Toolbox.allocation_width * scale; 				t.h = 15 * scale; 			} else if (t is FontName) {@@ -128,20 +133,35 @@ public class Expander : GLib.Object { 			content_height += 10 * scale + HEADLINE_MARGIN; 		} 		-		foreach (Tool t in tool) {-			if (t.tool_is_visible ()) {-				if (new_row) {-					content_height += t.h + margin_small; -					xt = x;-					yt += t.h + margin_small;+		if (tool.size > 0) {+			previous = tool.get (0);+			first_row = true;+			foreach (Tool t in tool) {+				if (t.tool_is_visible ()) {+					new_row = xt + t.w > Toolbox.allocation_width - margin_small;+					+					if (t is ZoomBar) { // add extra space around zoom bar+						content_height += 30 * scale;+					}+					+					if (previous is ZoomBar) {+						yt += margin_small;+					}+					+					if (new_row && !first_row) {+						content_height += previous.h + margin_small; +						xt = x;+						yt += previous.h + margin_small;+					}+				+					t.x = xt;+					t.y = yt;+				+					xt += t.w + margin_small;+					+					previous = t;+					first_row = false; 				}-			-				t.x = xt;-				t.y = yt;-			-				xt += t.w + margin_small;--				new_row = xt + t.w > Toolbox.allocation_width - margin_small; 			} 		} 	}diff --git a/libbirdfont/Glyph.vala b/libbirdfont/Glyph.valaindex 1438798..91f0c94 100644--- a/libbirdfont/Glyph.vala+++ b/libbirdfont/Glyph.vala@@ -23,7 +23,8 @@ public class Glyph : FontDisplay { 	bool background_image_visible = true; 	 	// Glyph zoom level-	public double view_zoom = 0.01;+	public double view_zoom = 0.1;+	 	public double view_offset_x = 0; 	public double view_offset_y = 0; 	Gee.ArrayList<ZoomView> zoom_list = new Gee.ArrayList<ZoomView> ();@@ -100,7 +101,7 @@ public class Glyph : FontDisplay { 		path_list.add (new Path ()); 		 		add_help_lines ();-+		 		left_limit = -28; 		right_limit = 28; 	}@@ -351,6 +352,12 @@ public class Glyph : FontDisplay { 		if (!is_null (MainWindow.native_window)) { 			MainWindow.native_window.set_scrollbar_size (0); 		}+		+		update_zoom_bar ();+	}+	+	void update_zoom_bar () {+		Toolbox.drawing_tools.zoom_bar.set_zoom ((view_zoom - 1) / 20); 	} 	 	public void remove_lines () {@@ -1168,6 +1175,7 @@ public class Glyph : FontDisplay { 		set_zoom_area (10, 10, allocation.width - 10, allocation.height - 10); 		set_zoom_from_area (); 		update_view ();+		update_zoom_bar (); 	} 	 	public override void zoom_out () {@@ -1176,10 +1184,12 @@ public class Glyph : FontDisplay { 		set_zoom_area (-n, -n, allocation.width + n, allocation.height + n); 		set_zoom_from_area (); 		update_view ();+		update_zoom_bar (); 	} 	 	public override void zoom_max () { 		default_zoom ();+		update_zoom_bar (); 	} 	 	public override void zoom_min () {@@ -1215,7 +1225,7 @@ public class Glyph : FontDisplay { 		zoom_out (); // add some margin 		 		redraw_area (0, 0, allocation.width, allocation.height);-		+		update_zoom_bar (); 	}  	public override void store_current_view () {@@ -1249,6 +1259,8 @@ public class Glyph : FontDisplay { 		view_offset_y = z.y; 		view_zoom = z.zoom; 		allocation = z.allocation;+		+		update_zoom_bar (); 	}  	public override void next_view () {@@ -1265,7 +1277,9 @@ public class Glyph : FontDisplay { 		view_offset_x = z.x; 		view_offset_y = z.y; 		view_zoom = z.zoom;-		allocation = z.allocation;		+		allocation = z.allocation;+		+		update_zoom_bar ();		 	} 	 	public override void reset_zoom () {@@ -1275,6 +1289,7 @@ public class Glyph : FontDisplay { 		set_zoom (1); 		 		store_current_view ();+		update_zoom_bar (); 	} 	 	/** Get x-height or top line. */@@ -1329,7 +1344,7 @@ public class Glyph : FontDisplay { 		return true; 	} 	-	private void set_zoom (double z)+	public void set_zoom (double z) 		requires (z > 0) 	{		 		view_zoom = z;diff --git a/libbirdfont/ZoomBar.vala b/libbirdfont/ZoomBar.valanew file mode 100644index 0000000..06d84bf--- /dev/null+++ b/libbirdfont/ZoomBar.vala@@ -0,0 +1,117 @@+/*+    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 ZoomBar : Tool {++	double zoom_level = 1 / 3.0;+	bool update_zoom = false;+	+	public signal void new_zoom (double zoom_level);+	+	public ZoomBar () {+		base ();+		+		panel_press_action.connect ((selected, button, tx, ty) => {+			if (y <= ty <= y + h) {+				set_zoom_from_mouse (tx);+				update_zoom = true;+			}+		});++		panel_move_action.connect ((selected, button, tx, ty) => {+			if (update_zoom) {+				set_zoom_from_mouse (tx);+			}+			+			return true;+		});+		+		panel_release_action.connect ((selected, button, tx, ty) => {+			update_zoom = false;+		});+	}+	+	/** Zoom level from 0 to 1. */+	public void set_zoom (double z) {+		zoom_level = z;+		Toolbox.redraw_tool_box ();+	}+	+	void set_zoom_from_mouse (double tx) {+		double margin = w * 0.1;+		double bar_width = w - margin - x;+		+		tx -= x;+		zoom_level = tx / bar_width;+		+		if (zoom_level > 1) {+			zoom_level = 1;+		}++		if (zoom_level < 0) {+			zoom_level = 0;+		}+		+		set_zoom (zoom_level);+		new_zoom (zoom_level);+	}+	+	public override void draw (Context cr) {+		double margin = w * 0.1;+		double bar_width = w - margin - x;+		+		// filled+		cr.save ();+		cr.set_source_rgba (26 / 255.0, 30 / 255.0, 32 / 255.0, 1);+		draw_bar (cr);+		cr.fill ();+		cr.restore ();+		+		// remove non filled parts+		cr.save ();+		cr.set_source_rgba (51 / 255.0, 54 / 255.0, 59 / 255.0, 1);+		cr.rectangle (x + bar_width * zoom_level, y, w, h);+		cr.fill ();+		cr.restore ();+		+		// border+		cr.save ();+		cr.set_source_rgba (26 / 255.0, 30 / 255.0, 32 / 255.0, 1);+		cr.set_line_width (0.8);+		draw_bar (cr);+		cr.stroke ();+		cr.restore ();+	}+	+	void draw_bar (Context cr) {+		double scale = Toolbox.get_scale ();+		double height = h;+		double radius = height / 2;+		double margin = w * 0.1;+	+		cr.move_to (x + radius, y + height);+		cr.arc (x + radius, y + radius, radius, PI / 2, 3 * (PI / 2));+		cr.line_to (w - margin - radius, y);+		cr.arc (w - margin - radius, y + radius, radius, 3 * (PI / 2), 5 * (PI / 2));+		cr.line_to (x + radius, y + height);+		cr.close_path ();			+	}+}++}

Contact

Email: johan.mattsson.m@gmail.com
Mastodon: typo.social/@birdfont
GitHub: johanmattssonm
LinkedIn: johan-mattsson-2ab324112