Clone in a terminal:
Commit 3907f61
Johan Mattsson - 2012-08-14T16:47:03+00:00
3907f617ce00c1bac2f8060043f63fd45cd52f85johan.mattsson.m@gmail.com

insert new point on path

Changed files (5)

Diff

diff --git a/src/Glyph.vala b/src/Glyph.valaindex b094470..e5fc294 100644--- a/src/Glyph.vala+++ b/src/Glyph.vala@@ -570,7 +570,12 @@ class Glyph : FontDisplay { 			move_offset_y = view_offset_y; 		} else { 			Tool t = MainWindow.get_toolbox ().get_current_tool ();-			t.press_action (t, (int) e.button, (int) e.x, (int) e.y);+			+			if (e.type == EventType.BUTTON_PRESS) {+				t.press_action (t, (int) e.button, (int) e.x, (int) e.y);+			} else if (e.type == EventType.2BUTTON_PRESS) {+				t.double_click_action (t, (int) e.button, (int) e.x, (int) e.y);+			} 		} 		 		MainWindow.hide_cursor ();@@ -1616,6 +1621,41 @@ class Glyph : FontDisplay { 		 		return img; 	}++	/** Split curve in two parts and add a new point in between. */+	public void insert_new_point_on_path (double x, double y) {+		double min, distance;+		Path? p = null;+		EditPoint ep = new EditPoint ();+		EditPoint lep = new EditPoint ();+		+		double xt;+		double yt;++		xt = x * ivz () - xc () + view_offset_x;+		yt = yc () - y * ivz () - view_offset_y;+		+		min = double.MAX;+		+		delete_invisible_paths ();+		+		foreach (Path pp in path_list) {+			pp.get_closest_point_on_path (lep, xt, yt);+			distance = Math.sqrt (Math.pow (xt - ep.x, 2) + Math.pow (yt - ep.y, 2));+			+			if (distance < min) {+				min = distance;+				p = pp;+				ep = lep;+			}+		}++		if (p == null) {+			return;+		}++		((!)p).insert_new_point_on_path (ep);+	} }  }diff --git a/src/Path.vala b/src/Path.valaindex fbb01d2..99ddbc2 100644--- a/src/Path.vala+++ b/src/Path.vala@@ -867,9 +867,7 @@ class Path { 		 		second_last_point = last_point; 		last_point = p;-				-		// CPU hog update_region_boundries ();-		+ 		return np; 		 	}@@ -1030,7 +1028,51 @@ class Path { 		 		return true; 	}-	++	public void insert_new_point_on_path (EditPoint ep) {+		EditPoint start, stop;+		double x0, x1, y0, y1;+		double px, py;+		+		double position, t, d, min;+		double steps = 100;++		start = ep.get_prev ().data;+		stop = ep.get_next ().data;++		add_point_after (ep, ep.get_prev ());++		min = double.MAX;++		position = 0.5;++		for (int i = 0; i < steps; i++) {+			t = i / steps;+			+			px = bezier_path (t, start.x, start.get_right_handle ().x (), stop.get_left_handle ().x (), stop.x);+			py = bezier_path (t, start.y, start.get_right_handle ().y (), stop.get_left_handle ().y (), stop.y);+			+			d = Math.sqrt (Math.pow (ep.x - px, 2) + Math.pow (ep.y - py, 2));+			+			if (d < min) {+				min = d;+				position = t;+			}+		}+			+		bezier_vector (position, start.x, start.get_right_handle ().x (), stop.get_left_handle ().x (), stop.x, out x0, out x1);+		bezier_vector (position, start.y, start.get_right_handle ().y (), stop.get_left_handle ().y (), stop.y, out y0, out y1);++		ep.get_left_handle ().set_point_type (PointType.CURVE);+		ep.get_left_handle ().move_to_coordinate (x0, y0);+		+		ep.get_right_handle ().set_point_type (PointType.CURVE);+		ep.get_right_handle ().move_to_coordinate (x1, y1);++		stop.get_left_handle ().length *= 1 - position;+		start.get_right_handle ().length *= position;+	}+			 	/** Get a point on the this path closest to x and y coordinates. */ 	public void get_closest_point_on_path (EditPoint edit_point, double x, double y) { 		return_if_fail (points.length () != 0);diff --git a/src/PenTool.vala b/src/PenTool.valaindex 1cde884..308d1db 100644--- a/src/PenTool.vala+++ b/src/PenTool.vala@@ -60,9 +60,12 @@ class PenTool : Tool { 		press_action.connect((self, b, x, y) => { 			last_point_x = x; 			last_point_y = y;-			+ 			press (b, x, y); 		});+		+		double_click_action.connect((self, b, x, y) => {+		});  		release_action.connect((self, b, ix, iy) => { 			double x = ix;@@ -179,6 +182,11 @@ class PenTool : Tool { 			return; 		} +		if (insert_new_point_on_path_selected ()) {+			glyph.insert_new_point_on_path (x, y);+			return;+		}+ 		if (is_new_point_on_path_selected ()) { 			new_point_on_path_at (x, y); 			return;	@@ -338,6 +346,11 @@ class PenTool : Tool { 		return t.is_selected (); 	} +	public static bool insert_new_point_on_path_selected () {+		Tool t = MainWindow.get_toolbox ().get_tool ("insert_point_on_path");+		return t.is_selected ();+	}+ 	public static bool is_new_point_on_path_selected () { 		Tool t = MainWindow.get_toolbox ().get_tool ("new_point_on_path"); 		return t.is_selected ();diff --git a/src/Tool.vala b/src/Tool.valaindex ed4208b..5dcc07a 100644--- a/src/Tool.vala+++ b/src/Tool.vala@@ -49,6 +49,7 @@ class Tool : GLib.Object { 	public signal void deselect_action (Tool selected); 	 	public signal void press_action (Tool selected, int button, int x, int y);+	public signal void double_click_action (Tool selected, int button, int x, int y); 	public signal void move_action (Tool selected, int x, int y); 	public signal void release_action (Tool selected, int button, int x, int y); 	diff --git a/src/Toolbox.vala b/src/Toolbox.valaindex b2a7e39..70e6e2f 100644--- a/src/Toolbox.vala+++ b/src/Toolbox.vala@@ -108,7 +108,14 @@ class Toolbox : DrawingArea { 			}); 		draw_tool_modifiers.add_tool (corner_tool); -		Tool new_point_on_path = new Tool ("new_point_on_path", "Add new point on path", 'n');+		Tool insert_point_on_path = new Tool ("insert_point_on_path", "Add new point on path", 'n');+		insert_point_on_path.select_action.connect((self) => {+				select_draw_tool ();+				pen_tool.begin_from_new_point_on_path ();+			});+		draw_tool_modifiers.add_tool (insert_point_on_path);+		+		Tool new_point_on_path = new Tool ("new_point_on_path", "Begin new path from point on path", 'n'); 		new_point_on_path.select_action.connect((self) => { 				select_draw_tool (); 				pen_tool.begin_from_new_point_on_path ();@@ -617,9 +624,9 @@ class Toolbox : DrawingArea { 					} 				} 			}-+			 			return true;-		});+		});	 				 		button_release_event.connect ((se, e)=> { 			release (e.button, e.x, e.y);

Contact

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