[longomatch] Abtract key pressed events



commit 9e4e6a5e7ea5744bdad2852101861889120b3029
Author: Andoni Morales Alastruey <ylatuya gmail com>
Date:   Sun Dec 18 20:21:35 2011 +0100

    Abtract key pressed events

 LongoMatch.Core/Handlers/Handlers.cs           |    2 +
 LongoMatch.Core/Interfaces/GUI/IMainWindow.cs  |    2 +
 LongoMatch.GUI/Gui/MainWindow.cs               |   11 +++++++-
 LongoMatch.Services/Services/Core.cs           |    7 +---
 LongoMatch.Services/Services/HotKeysManager.cs |   35 +++++++++++++++++-------
 5 files changed, 41 insertions(+), 16 deletions(-)
---
diff --git a/LongoMatch.Core/Handlers/Handlers.cs b/LongoMatch.Core/Handlers/Handlers.cs
index f03b8eb..3fd18bf 100644
--- a/LongoMatch.Core/Handlers/Handlers.cs
+++ b/LongoMatch.Core/Handlers/Handlers.cs
@@ -126,5 +126,7 @@ namespace LongoMatch.Handlers
 	public delegate void UnitSelectedHandler (GameUnit gameUnit, TimelineNode unit);
 	public delegate void UnitAddedHandler (GameUnit gameUnit, int frame);
 	public delegate void UnitsDeletedHandler (GameUnit gameUnit, List<TimelineNode> unit);
+	
+	public delegate void KeyHandler (object sender, int key, int modifier);
 
 }
diff --git a/LongoMatch.Core/Interfaces/GUI/IMainWindow.cs b/LongoMatch.Core/Interfaces/GUI/IMainWindow.cs
index 418e8dc..761b469 100644
--- a/LongoMatch.Core/Interfaces/GUI/IMainWindow.cs
+++ b/LongoMatch.Core/Interfaces/GUI/IMainWindow.cs
@@ -70,6 +70,8 @@ namespace LongoMatch.Interfaces.GUI
 		event UnitsDeletedHandler UnitDeleted;
 		event UnitAddedHandler UnitAdded;
 		
+		event KeyHandler KeyPressed;
+		
 		void SetProject(Project project, ProjectType projectType, CaptureSettings props);
 		void AddPlay(Play play);
 		void UpdateSelectedPlay (Play play);
diff --git a/LongoMatch.GUI/Gui/MainWindow.cs b/LongoMatch.GUI/Gui/MainWindow.cs
index 6748523..dd6aa19 100644
--- a/LongoMatch.GUI/Gui/MainWindow.cs
+++ b/LongoMatch.GUI/Gui/MainWindow.cs
@@ -86,6 +86,8 @@ namespace LongoMatch.Gui
 		public event UnitSelectedHandler UnitSelected;
 		public event UnitsDeletedHandler UnitDeleted;
 		public event UnitAddedHandler UnitAdded;
+		
+		public event KeyHandler KeyPressed;
 
 		private static Project openedProject;
 		private ProjectType projectType;
@@ -286,7 +288,9 @@ namespace LongoMatch.Gui
 			guTimeline.UnitDeleted += EmitUnitDeleted;
 			guTimeline.UnitSelected += EmitUnitSelected;
 			guTimeline.UnitChanged += EmitUnitChanged;
-		}
+			
+			KeyPressEvent += (o, args) => (EmitKeyPressed(o, (int)args.Event.Key, (int)args.Event.Key));
+ 		}
 		
 		private void ConnectMenuSignals() {
 			SaveProjectAction.Activated += (o, e) => {EmitSaveProject();};
@@ -832,6 +836,11 @@ namespace LongoMatch.Gui
 			if (UnitChanged != null)
 				UnitChanged(gameUnit, unit, time);
 		}
+		
+		private void EmitKeyPressed(object sender, int key, int modifier) {
+			if (KeyPressed != null)
+				KeyPressed(sender, key, modifier);
+		}
 		#endregion
 	}
 }
diff --git a/LongoMatch.Services/Services/Core.cs b/LongoMatch.Services/Services/Core.cs
index 2efd7a3..74472bd 100644
--- a/LongoMatch.Services/Services/Core.cs
+++ b/LongoMatch.Services/Services/Core.cs
@@ -75,7 +75,7 @@ namespace LongoMatch.Services
 			eManager = new EventsManager(guiToolkit);
 
 			/* Start the hotkeys manager */
-			hkManager = new HotKeysManager();
+			hkManager = new HotKeysManager(guiToolkit.MainWindow);
 			hkManager.newMarkEvent += eManager.OnNewTag;
 
 			/* Start the rendering jobs manager */
@@ -138,11 +138,8 @@ namespace LongoMatch.Services
 		private static void OnOpenedProjectChanged (Project project, ProjectType projectType) {
 			if (project != null) {
 				hkManager.Categories=project.Categories;
-#if HAVE_GTK
-				mainWindow.KeyPressEvent -= hkManager.KeyListener;
 			} else {
-				mainWindow.KeyPressEvent += hkManager.KeyListener;
-#endif
+				hkManager.Categories=null;
 			}
 			
 			eManager.OpenedProject = project;
diff --git a/LongoMatch.Services/Services/HotKeysManager.cs b/LongoMatch.Services/Services/HotKeysManager.cs
index bdf0392..7b3bc31 100644
--- a/LongoMatch.Services/Services/HotKeysManager.cs
+++ b/LongoMatch.Services/Services/HotKeysManager.cs
@@ -19,6 +19,12 @@
 //
 //
 using System.Collections.Generic;
+#if HAVE_GTK
+using Gtk;
+using Gdk;
+#endif
+
+using LongoMatch.Interfaces.GUI;
 using LongoMatch.Store;
 using LongoMatch.Store.Templates;
 using LongoMatch.Handlers;
@@ -29,20 +35,26 @@ namespace LongoMatch.Services
 
 	public class HotKeysManager
 	{
-		private Dictionary<HotKey, Category> dic;
 		public event NewTagHandler newMarkEvent;
-
-		public HotKeysManager()
+		
+		Dictionary<HotKey, Category> dic;
+		bool ignoreKeys;
+		
+		public HotKeysManager(IMainWindow mainWindow)
 		{
 			dic = new Dictionary<HotKey,Category>();
+			mainWindow.KeyPressed += KeyListener;
 		}
 
 		// Set the active Hotkeys for the current project
 		public Categories Categories {
 			set {
 				dic.Clear();
-				if(value == null)
+				if(value == null) {
+					ignoreKeys = true;
 					return;
+				}
+				ignoreKeys = false;
 				foreach(Category cat in value) {
 					if(cat.HotKey.Defined &&
 					                !dic.ContainsKey(cat.HotKey))
@@ -51,19 +63,22 @@ namespace LongoMatch.Services
 			}
 		}
 
-		// Listen to key press events and fire a newMarkEvent event if the key combination
-		// is associated to a Category
-	/*	public void KeyListener(object sender, KeyPressEventArgs args) {
+		public void KeyListener(object sender, int key, int state) {
+			if (ignoreKeys)
+				return;
+			
+#if HAVE_GTK
 			Category cat = null;
 			HotKey hotkey = new HotKey();
 
-			hotkey.Key=args.Event.Key;
-			hotkey.Modifier=args.Event.State & (ModifierType.Mod1Mask | ModifierType.Mod5Mask | ModifierType.ShiftMask);
+			hotkey.Key= key;
+			hotkey.Modifier= (int) ((ModifierType)state & (ModifierType.Mod1Mask | ModifierType.Mod5Mask | ModifierType.ShiftMask));
 			if(dic.TryGetValue(hotkey, out cat)) {
 				if(newMarkEvent != null) {
 					newMarkEvent(cat);
 				}
+#endif
 			}
-		}*/
+		}
 	}
 }



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