[ease] Add basic text editing capabilities.



commit b8dedb96ca91aa91f856bd4092df7886acdc5e03
Author: Nate Stedman <natesm gmail com>
Date:   Thu Jun 10 02:03:38 2010 -0400

    Add basic text editing capabilities.

 src/Actor.vala           |   13 ++++++++++
 src/EditorEmbed.vala     |   61 ++++++++++++++++++++++++++++++----------------
 src/Main.vala            |    8 ++++++
 src/ScrollableEmbed.vala |    8 ++++++
 src/TextActor.vala       |   36 +++++++++++++++++++++++++++
 5 files changed, 105 insertions(+), 21 deletions(-)
---
diff --git a/src/Actor.vala b/src/Actor.vala
index 5e3e51c..26f6564 100644
--- a/src/Actor.vala
+++ b/src/Actor.vala
@@ -123,6 +123,19 @@ public abstract class Ease.Actor : Clutter.Group
 		element.height = height;	
 	}
 	
+	/**
+	 * Called when the actor should be edited. Subclasses should override this.
+	 *
+	 * @param sender The { link EditorEmbed} this Actor is on.
+	 */
+	public virtual void edit(EditorEmbed sender) {}
 	
+	/**
+	 * Called when the actor end editing. Subclasses with editing that is not
+	 * instant (popping up a dialog box) should override this.
+	 *
+	 * @param sender The { link EditorEmbed} this Actor is on.
+	 */
+	public virtual void end_edit(EditorEmbed sender) {}
 }
 
diff --git a/src/EditorEmbed.vala b/src/EditorEmbed.vala
index 11f64ea..e62d9e7 100644
--- a/src/EditorEmbed.vala
+++ b/src/EditorEmbed.vala
@@ -44,6 +44,11 @@ public class Ease.EditorEmbed : ScrollableEmbed
 	// the currently selected Actor
 	private Actor selected;
 	
+	/**
+	 * If the selected { link Actor} is being edited.
+	 */
+	public bool is_editing { get; set; }
+	
 	// if the selected Actor is being dragged
 	private bool is_dragging;
 	private bool is_drag_ready;
@@ -61,6 +66,7 @@ public class Ease.EditorEmbed : ScrollableEmbed
 	private const string BG_COLOR = "bg_color:";
 	private const string PREFIX = "#";
 	private const double SHADE_FACTOR = 0.9;
+	private const int HANDLE_COUNT = 8;
 	
 	private Document document;
 	public float zoom;
@@ -290,8 +296,16 @@ public class Ease.EditorEmbed : ScrollableEmbed
 	 */
 	public bool actor_clicked(Clutter.Actor sender, Clutter.ButtonEvent event)
 	{
-		// if the sender is already selected, drag it
-		if (sender == selected)
+		// if this is a double click, edit the actor
+		if (event.click_count == 2)
+		{
+			(sender as Actor).edit(this);
+			is_editing = true;
+			return true;
+		}
+		
+		// otherwise, if the sender is already selected, drag it
+		else if (sender == selected)
 		{
 			is_dragging = true;
 			is_drag_ready = false;
@@ -300,26 +314,23 @@ public class Ease.EditorEmbed : ScrollableEmbed
 			return true;
 		}
 		
+		// if editing another Actor, finish that edit
+		if (selected != null && is_editing)
+		{
+			selected.end_edit(this);
+			is_editing = false;
+		}
+		
 		// remove the selection rectangle and handles
 		if (selection_rectangle != null)
 		{
-			foreach (var h in handles)
-			{
-				//h.button_press_event.disconnect(handle_clicked);
-				//h.button_release_event.disconnect(handle_released);
-				
-				if (h.get_parent() == contents)
-				{	
-					contents.remove_actor(h);
-				}
-			}
 			if (selection_rectangle.get_parent() == contents)
 			{
 				contents.remove_actor(selection_rectangle);
 			}
 		}
 		
-		selected = (Actor)sender;
+		selected = sender as Actor;
 		
 		// make a new selection rectangle
 		selection_rectangle = new Clutter.Rectangle();
@@ -329,15 +340,23 @@ public class Ease.EditorEmbed : ScrollableEmbed
 		position_selection();
 		contents.add_actor(selection_rectangle);
 		
-		handles = new Handle[8];
-		for (int i = 0; i < 8; i++)
+		// place the handles
+		if (handles == null)
+		{
+			handles = new Handle[HANDLE_COUNT];
+			for (int i = 0; i < HANDLE_COUNT; i++)
+			{
+				handles[i] = new Handle((HandlePosition)i);
+				contents.add_actor(handles[i]);
+				handles[i].button_press_event.connect(handle_clicked);
+				handles[i].button_release_event.connect(handle_released);
+			}
+		}
+		
+		for (int i = 0; i < HANDLE_COUNT; i++)
 		{
-			handles[i] = new Handle((HandlePosition)i);
 			handles[i].reposition(selection_rectangle);
-			contents.add_actor(handles[i]);
-			
-			handles[i].button_press_event.connect(handle_clicked);
-			handles[i].button_release_event.connect(handle_released);
+			contents.raise_child(handles[i], selection_rectangle);
 		}
 		
 		return true;
@@ -421,7 +440,7 @@ public class Ease.EditorEmbed : ScrollableEmbed
 	 * @param event The corresponding Clutter.Event
 	 */
 	public bool handle_clicked(Clutter.Actor sender, Clutter.ButtonEvent event)
-	{
+	{	
 		(sender as Handle).flip();
 		is_dragging = true;
 		is_drag_ready = false;
diff --git a/src/Main.vala b/src/Main.vala
index 933e979..7d44a3a 100644
--- a/src/Main.vala
+++ b/src/Main.vala
@@ -30,6 +30,9 @@ public static class Ease.Main : GLib.Object
 	private static Gee.ArrayList<EditorWindow> windows;
 	private static WelcomeWindow welcome;
 	
+	private const int DOUBLE_CLICK_TIME = 1000;
+	private const int DOUBLE_CLICK_DISTANCE = 5;
+	
 	// options
 	static string play_filename;
 	static string[] filenames;
@@ -82,6 +85,11 @@ public static class Ease.Main : GLib.Object
 		Transitions.init();
 		OpenDialog.init();
 		windows = new Gee.ArrayList<EditorWindow>();
+		
+		// Clutter settings
+		var backend = Clutter.get_default_backend();
+		backend.set_double_click_time(DOUBLE_CLICK_TIME);
+		backend.set_double_click_distance(DOUBLE_CLICK_DISTANCE);
 	
 		// open editor windows for each argument specified
 		if (filenames != null)
diff --git a/src/ScrollableEmbed.vala b/src/ScrollableEmbed.vala
index 4b5b888..16eefe2 100644
--- a/src/ScrollableEmbed.vala
+++ b/src/ScrollableEmbed.vala
@@ -189,4 +189,12 @@ public class Ease.ScrollableEmbed : Gtk.HBox
 		viewport.width = allocation.width;
 		viewport.height = allocation.height;
 	}
+	
+	/**
+	 * Sets keyboard focus to this ScrollableEmbed's GtkClutter.Embed
+	 */
+	public void key_focus()
+	{
+		embed.grab_focus();
+	}
 }
diff --git a/src/TextActor.vala b/src/TextActor.vala
index 8fdbfe8..58d5972 100644
--- a/src/TextActor.vala
+++ b/src/TextActor.vala
@@ -53,5 +53,41 @@ public class Ease.TextActor : Actor
 		x = e.x;
 		y = e.y;
 	}
+	
+	/**
+	 * { inheritDoc}
+	 */
+	public override void edit(EditorEmbed sender)
+	{
+		// set text to editable
+		var text = contents as Clutter.Text;
+		text.editable = true;
+		text.reactive = true;
+		text.text_changed.connect(text_changed);
+		
+		// grab key focus
+		((Clutter.Stage)get_stage()).set_key_focus(text);
+		sender.key_focus();
+	}
+	
+	/**
+	 * { inheritDoc}
+	 */
+	public override void end_edit(EditorEmbed sender)
+	{
+		// release key focus
+		((Clutter.Stage)get_stage()).set_key_focus(null);
+		
+		// disable text editing
+		var text = contents as Clutter.Text;
+		text.editable = false;
+		text.reactive = false;
+		text.text_changed.disconnect(text_changed);
+	}
+	
+	private void text_changed(Clutter.Text sender)
+	{
+		element.set("text", sender.text);
+	}
 }
 



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]