[ease] [editor] Add a few key bindings to the editor



commit 4b4f8775d7dfc03aa1c7419948e6495dc77acfe1
Author: Nate Stedman <natesm gmail com>
Date:   Mon Jul 26 00:30:01 2010 -0400

    [editor] Add a few key bindings to the editor
    
    - Nudge things around with the arrow keys.
    - Shift causes actors to nudge more.
    - Remove actors with backspace/delete.
    - Added an iterable stage.

 src/ease-clutter-iterables.vala |    6 ++
 src/ease-editor-embed.vala      |  127 +++++++++++++++++++++++++++++++++++++--
 src/ease-editor-window.vala     |    3 +-
 src/ease-enums.vala             |   13 ++++
 4 files changed, 143 insertions(+), 6 deletions(-)
---
diff --git a/src/ease-clutter-iterables.vala b/src/ease-clutter-iterables.vala
index 42452bd..e02de15 100644
--- a/src/ease-clutter-iterables.vala
+++ b/src/ease-clutter-iterables.vala
@@ -55,3 +55,9 @@ public class Ease.ClutterIterableGroup : Clutter.Group, ClutterIterableContainer
 {
 }
 
+/**
+ * ClutterStage with { link ClutterIterableContainer} mixin.
+ */
+public class Ease.ClutterIterableStage : Clutter.Stage, ClutterIterableContainer
+{
+}
diff --git a/src/ease-editor-embed.vala b/src/ease-editor-embed.vala
index da0cda4..bfda5ed 100644
--- a/src/ease-editor-embed.vala
+++ b/src/ease-editor-embed.vala
@@ -27,7 +27,7 @@
  * EditorEmbed is a subclass of { link ScrollableEmbed}, and has both
  * horizontal and vertical scrollbars.
  */
-public class Ease.EditorEmbed : ScrollableEmbed
+public class Ease.EditorEmbed : ScrollableEmbed, UndoSource
 {
 	/**
 	 * The { link EditorWindow} that owns this EditorEmbed.
@@ -105,6 +105,11 @@ public class Ease.EditorEmbed : ScrollableEmbed
 	private float orig_h;
 	
 	/**
+	 * If the embed is currently receiving key events.
+	 */
+	private bool keys_connected = false;
+	
+	/**
 	 * The gtk background color identifier.
 	 */
 	private const string BG_COLOR = "bg_color:";
@@ -121,6 +126,16 @@ public class Ease.EditorEmbed : ScrollableEmbed
 	private const int HANDLE_COUNT = 8;
 	
 	/**
+	 * The number of pixels an actor moves when nudged.
+	 */
+	private const int NUDGE_PIXELS = 10;
+	
+	/**
+	 * The number of pixels an actor moves when nudged with shift held down.
+	 */
+	private const int NUDGE_SHIFT_PIXELS = 50;
+	
+	/**
 	 * The { link Document} linked with this EditorEmbed.
 	 */
 	private Document document;
@@ -214,6 +229,8 @@ public class Ease.EditorEmbed : ScrollableEmbed
 				reposition_group();
 			}
 		});
+		
+		connect_keys();
 	}
 
 	/**
@@ -239,6 +256,8 @@ public class Ease.EditorEmbed : ScrollableEmbed
 			is_editing = false;
 		}
 		
+		connect_keys();
+		
 		// clean up the previous slide
 		if (slide_actor != null)
 		{
@@ -391,6 +410,7 @@ public class Ease.EditorEmbed : ScrollableEmbed
 		// if this is a double click, edit the actor
 		if (event.click_count == 2)
 		{
+			disconnect_keys();
 			(sender as Actor).edit(this);
 			is_editing = true;
 			return true;
@@ -426,6 +446,8 @@ public class Ease.EditorEmbed : ScrollableEmbed
 		// deselect anything that is currently selected
 		deselect_actor();
 		
+		connect_keys();
+		
 		selected = sender as Actor;
 		
 		// make a new selection rectangle
@@ -451,6 +473,10 @@ public class Ease.EditorEmbed : ScrollableEmbed
 			handles[i].reposition(selection_rectangle);
 			contents.raise_child(handles[i], selection_rectangle);
 		}
+		
+		// when something is selected, the embed grabs key focus
+		set_can_focus(true);
+		grab_focus();
 	}
 	
 	/**
@@ -466,6 +492,7 @@ public class Ease.EditorEmbed : ScrollableEmbed
 			selected.end_edit(this);
 			is_editing = false;
 		}
+		connect_keys();
 		
 		// deselect
 		selected = null;
@@ -492,7 +519,7 @@ public class Ease.EditorEmbed : ScrollableEmbed
 			is_dragging = false;
 			Clutter.ungrab_pointer();
 			sender.motion_event.disconnect(actor_motion);
-			win.add_undo_action(move_undo);
+			undo(move_undo);
 		}
 		return true;
 	}
@@ -586,7 +613,7 @@ public class Ease.EditorEmbed : ScrollableEmbed
 			(sender as Handle).flip(false);
 			is_dragging = false;
 			sender.motion_event.disconnect(handle_motion);
-			win.add_undo_action(move_undo);
+			undo(move_undo);
 		}
 		
 		Clutter.ungrab_pointer();
@@ -663,7 +690,7 @@ public class Ease.EditorEmbed : ScrollableEmbed
 	}
 	
 	/**
-	 * Handles { link SlideActor.on_ease_actor_removed}. Deselects the current
+	 * Handles actor removal events. Deselects the current
 	 * { link Actor} if necessary, and disconnects handlers.
 	 */
 	public void on_ease_actor_removed(Actor actor)
@@ -675,7 +702,7 @@ public class Ease.EditorEmbed : ScrollableEmbed
 	}
 	
 	/**
-	 * Handles { link SlideActor.on_ease_actor_added}. Connects handlers.
+	 * Handles new actor events. Connects handlers.
 	 */
 	public void on_ease_actor_added(Actor actor)
 	{
@@ -683,5 +710,95 @@ public class Ease.EditorEmbed : ScrollableEmbed
 		actor.button_release_event.connect(actor_released);
 		actor.reactive = true;
 	}
+	
+	/**
+	 * Handles keypresses within the embed.
+	 */
+	public bool on_key_press_event(Gtk.Widget self, Gdk.EventKey event)
+	{
+		if (event.type == Gdk.EventType.KEY_RELEASE) return false;
+		
+		bool shift = (event.state & Gdk.ModifierType.SHIFT_MASK) != 0;
+		
+		switch (event.keyval)
+		{
+			case Key.UP:
+				if (selected == null) break;
+				if (is_editing) return true;
+				
+				undo(new UndoAction(selected.element, "y"));
+				selected.translate(0, shift ?
+				                      -NUDGE_SHIFT_PIXELS : -NUDGE_PIXELS);
+				position_selection();			
+				selected.element.parent.changed(selected.element.parent);
+				return true;
+			
+			case Key.DOWN:
+				if (selected == null) break;
+				if (is_editing) return true;
+				
+				undo(new UndoAction(selected.element, "y"));
+				selected.translate(0, shift ?
+				                      NUDGE_SHIFT_PIXELS : NUDGE_PIXELS);
+				position_selection();			
+				selected.element.parent.changed(selected.element.parent);
+				return true;
+				
+			case Key.LEFT:
+				if (selected == null) break;
+				if (is_editing) return true;
+				
+				undo(new UndoAction(selected.element, "x"));
+				selected.translate(shift ?
+				                   -NUDGE_SHIFT_PIXELS : -NUDGE_PIXELS, 0);
+				position_selection();			
+				selected.element.parent.changed(selected.element.parent);
+				return true;
+			
+			case Key.RIGHT:
+				if (selected == null) break;
+				if (is_editing) return true;
+				
+				undo(new UndoAction(selected.element, "x"));
+				selected.translate(shift ?
+				                   NUDGE_SHIFT_PIXELS : NUDGE_PIXELS, 0);
+				position_selection();			
+				selected.element.parent.changed(selected.element.parent);
+				return true;
+			
+			case Key.BACKSPACE:
+			case Key.DELETE:
+				if (selected == null) break;
+				
+				var slide = slide_actor.slide;
+				var i = slide.index_of(selected.element);
+				undo(new ElementRemoveUndoAction(slide.element_at(i)));
+				slide.remove_at(i);
+				
+				return true;
+		}
+		
+		return false;
+	}
+	
+	/**
+	 * Connects the key event handlers.
+	 */
+	public void connect_keys()
+	{
+		if (keys_connected) return;
+		keys_connected = true;
+		key_press_event.connect(on_key_press_event);
+	}
+	
+	/**
+	 * Disconnects the key event handlers.
+	 */
+	public void disconnect_keys()
+	{
+		if (!keys_connected) return;
+		keys_connected = false;
+		key_press_event.disconnect(on_key_press_event);
+	}
 }
 
diff --git a/src/ease-editor-window.vala b/src/ease-editor-window.vala
index 947abee..a161028 100644
--- a/src/ease-editor-window.vala
+++ b/src/ease-editor-window.vala
@@ -157,6 +157,7 @@ public class Ease.EditorWindow : Gtk.Window
 		// main editor
 		embed = new EditorEmbed(document, this);
 		(builder.get_object("Embed Align") as Gtk.Alignment).add(embed);
+		embed.undo.connect((action) => add_undo_action(action));
 		
 		// zoom slider
 		(builder.get_object("Zoom Slider Item") as Gtk.ToolItem).
@@ -229,7 +230,7 @@ public class Ease.EditorWindow : Gtk.Window
 	 *
 	 * @param action The new { link UndoItem}.
 	 */
-	public void add_undo_action(UndoItem action)
+	private void add_undo_action(UndoItem action)
 	{
 		undo.add_action(action);
 		undo.clear_redo();
diff --git a/src/ease-enums.vala b/src/ease-enums.vala
index 6c6f7e0..a19b06e 100644
--- a/src/ease-enums.vala
+++ b/src/ease-enums.vala
@@ -41,4 +41,17 @@ namespace Ease
 		EDITOR,
 		INSPECTOR
 	}
+	
+	/**
+	 * Key values.
+	 */
+	public enum Key
+	{
+		UP = 65362,
+		DOWN = 65364,
+		LEFT = 65361,
+		RIGHT = 65363,
+		BACKSPACE = 65288,
+		DELETE = 65535
+	}
 }



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