From a22447e2e7b3b2f49e6a00d4d60c6adf6ecf6203 Mon Sep 17 00:00:00 2001 From: tzim Date: Fri, 15 Nov 2013 10:00:52 +0000 Subject: [PATCH] Enregistrement des attaches evenements --- DMX-2.0/Conduite.cs | 11 +++- DMX-2.0/DMX-2.0.csproj | 2 +- DMX-2.0/EventManager.cs | 51 ++++++++++++++- ...ontextMenuHelper.cs => HelperFunctions.cs} | 11 ++++ DMX-2.0/MainWindow.cs | 5 +- DMX-2.0/MidiEventProvider.cs | 10 ++- DMX-2.0/SequenceurLineaire.cs | 63 ++++++++++++++++--- DMX-2.0/gtk-gui/DMX2.SeqLinUI.cs | 1 + DMX-2.0/gtk-gui/gui.stetic | 1 + 9 files changed, 137 insertions(+), 18 deletions(-) rename DMX-2.0/{ContextMenuHelper.cs => HelperFunctions.cs} (89%) diff --git a/DMX-2.0/Conduite.cs b/DMX-2.0/Conduite.cs index 48249a3..b4ef3a4 100644 --- a/DMX-2.0/Conduite.cs +++ b/DMX-2.0/Conduite.cs @@ -11,7 +11,9 @@ namespace DMX2 public class Conduite : IComparer, IDisposable { - public static Conduite Courante = null; + static Conduite courante = null; + + public static Conduite Courante { get { return courante; } } Timer timer = null; DateTime dernierTick; @@ -32,6 +34,11 @@ namespace DMX2 public Conduite() { + + if(courante!= null) + courante.Dispose(); + courante = this; + timer = new Timer(new TimerCallback(TimerTick),this, 1000,10); derniereMaj = dernierTick=DateTime.Now; @@ -242,6 +249,8 @@ namespace DMX2 public void Dispose() { + if(courante==this) + courante = null; disposed=true; if(timer!=null) timer.Dispose(); diff --git a/DMX-2.0/DMX-2.0.csproj b/DMX-2.0/DMX-2.0.csproj index 4b86a90..cd5e7be 100644 --- a/DMX-2.0/DMX-2.0.csproj +++ b/DMX-2.0/DMX-2.0.csproj @@ -106,7 +106,7 @@ - + diff --git a/DMX-2.0/EventManager.cs b/DMX-2.0/EventManager.cs index 27fa8c4..4580d8f 100644 --- a/DMX-2.0/EventManager.cs +++ b/DMX-2.0/EventManager.cs @@ -25,6 +25,9 @@ namespace DMX2 public interface IEventTarget { bool FireEvent(EventData data); + void Bind(string id); + void Unbind(string id); + IEnumerable IDs { get; } } public delegate void EventManagerCallback(EventData data); @@ -32,7 +35,8 @@ namespace DMX2 public class actionEventTarget : IEventTarget { public delegate bool EventAction (EventData data); - EventAction action; + EventAction action; + List eventIds = new List(); public actionEventTarget(EventAction _action) { action=_action; @@ -42,6 +46,23 @@ namespace DMX2 { return action(data); } + + void IEventTarget.Bind (string id) + { + if(!eventIds.Contains(id)) + eventIds.Add(id); + } + + void IEventTarget.Unbind (string id) + { + eventIds.Remove(id); + } + + IEnumerable IEventTarget.IDs { + get { + return eventIds; + } + } #endregion } @@ -102,7 +123,7 @@ namespace DMX2 foreach (IEventProvider prov in providers) { Gtk.MenuItem provitem = new Gtk.MenuItem(prov.MenuName); provitem.Submenu = prov.GetProviderSubMenu(evd, handler); - menu.Add(provitem); + if(provitem.Submenu!=null) menu.Add(provitem); } Gtk.MenuItem itemNone = new Gtk.MenuItem("Aucun"); @@ -140,6 +161,7 @@ namespace DMX2 if(!bindings.ContainsKey(eventId)) bindings.Add (eventId,new eventBinding()); bindings[eventId].AddTarget(target); + target.Bind(eventId); foreach (IEventProvider prov in providers) { if(prov.Bind(eventId)) return true; } @@ -164,6 +186,8 @@ namespace DMX2 prov.Unbind(eventId); } } + + target.Unbind(eventId); } public void EventCallBack (EventData data) @@ -175,6 +199,29 @@ namespace DMX2 } } } + + public static bool SaveBindings (System.Xml.XmlElement xmlParent, IEventTarget target) + { + bool ret=false; + System.Xml.XmlElement xmlB; + foreach (string id in target.IDs) { + ret=true; + xmlParent.AppendChild(xmlB = xmlParent.OwnerDocument.CreateElement("EventBinding")); + xmlB.SetAttribute("id",id); + } + return ret; + } + + public static string[] LoadBindings (System.Xml.XmlElement xmlParent) + { + var all=xmlParent.GetElementsByTagName("EventBinding"); + string[] ret = new string[all.Count]; int index=0; + foreach (var xb in all) { + ret[index++] = (xb as System.Xml.XmlElement).GetAttribute("id"); + } + return ret; + } + } } diff --git a/DMX-2.0/ContextMenuHelper.cs b/DMX-2.0/HelperFunctions.cs similarity index 89% rename from DMX-2.0/ContextMenuHelper.cs rename to DMX-2.0/HelperFunctions.cs index 4f0463e..cb4d0c7 100644 --- a/DMX-2.0/ContextMenuHelper.cs +++ b/DMX-2.0/HelperFunctions.cs @@ -2,9 +2,20 @@ using System; using Gdk; using GLib; using Gtk; +using System.Xml; namespace DMX2 { + + public static class XmlHelpers { + public static string TryGetAttribute (this XmlElement element, string name, string defaultval) + { + if(!element.HasAttribute(name)) return defaultval; + return element.GetAttribute(name); + } + } + + public class ContextMenuEventArgs : EventArgs { private Widget widget; diff --git a/DMX-2.0/MainWindow.cs b/DMX-2.0/MainWindow.cs index 5f71908..cf555db 100644 --- a/DMX-2.0/MainWindow.cs +++ b/DMX-2.0/MainWindow.cs @@ -282,7 +282,7 @@ namespace DMX2 dlg.VBox.Add (new Label("Nom de la nouvelle Conduite :")); dlg.VBox.Add (entry); dlg.VBox.ShowAll (); entry.ActivatesDefault=true; if ((ResponseType)dlg.Run () == ResponseType.Ok) { - Conduite.Courante = new Conduite (); + new Conduite (); Conduite.Courante.Name = entry.Text; } MajWidgets(); @@ -293,7 +293,6 @@ namespace DMX2 protected void OnCloseActionActivated (object sender, EventArgs e) { Conduite.Courante.Dispose(); - Conduite.Courante= null; MajWidgets(); } @@ -399,8 +398,6 @@ namespace DMX2 return; } - Conduite.Courante = cond; - conduiteFile = openFile; } catch (IOException) { diff --git a/DMX-2.0/MidiEventProvider.cs b/DMX-2.0/MidiEventProvider.cs index c423821..a3f3a83 100644 --- a/DMX-2.0/MidiEventProvider.cs +++ b/DMX-2.0/MidiEventProvider.cs @@ -50,8 +50,7 @@ namespace DMX2 bool IEventProvider.Bind (string eventId) { - // TODO : check if "MIDI ..." - return true; + return eventId.StartsWith("MIDI-"); } void IEventProvider.Unbind (string eventId) @@ -62,6 +61,7 @@ namespace DMX2 Gtk.Menu IEventProvider.GetProviderSubMenu (EventManager.EventMenuData state, Gtk.ButtonPressEventHandler handler) { + if(!connected) return null; Gtk.Menu retmenu = new Gtk.Menu (); if (levent != null) { Gtk.MenuItem lmenuitem = new Gtk.MenuItem ("Dernier"); @@ -96,6 +96,7 @@ namespace DMX2 } EventData last; internalEvent levent=null; + bool connected=false; void IEventProvider.ProcessEvents (EventManagerCallback callback) { @@ -108,6 +109,9 @@ namespace DMX2 string id=null, description=null; int value=0; switch (evS.type) { + case snd_seq_event_type_t.SND_SEQ_EVENT_PORT_SUBSCRIBED: + connected = true; + continue; case snd_seq_event_type_t.SND_SEQ_EVENT_CONTROLLER: id= string.Format("MIDI-CTRL-C{0}P{1}",evS.data_ev_ctrl.channel,evS.data_ev_ctrl.param); description = string.Format("Controller Ch {0} Param {1}",evS.data_ev_ctrl.channel,evS.data_ev_ctrl.param); @@ -133,7 +137,7 @@ namespace DMX2 Info.Publish(string.Format ("event {0}", evS.type) ); continue; } - + connected=true; if(id!=null) { if(!eventlist.ContainsKey(id)) diff --git a/DMX-2.0/SequenceurLineaire.cs b/DMX-2.0/SequenceurLineaire.cs index b9191f6..233b9ca 100644 --- a/DMX-2.0/SequenceurLineaire.cs +++ b/DMX-2.0/SequenceurLineaire.cs @@ -103,7 +103,7 @@ namespace DMX2 } #region EventTargets - class circuitEventTarget : IEventTarget { + class circuitEventTarget : IEventTarget { Circuit circuit; Dictionary valeursrecues= new Dictionary(); SequenceurLineaire seq; @@ -141,6 +141,23 @@ namespace DMX2 return true; } + + void IEventTarget.Bind (string id) + { + valeursrecues[id] = 0; + } + + void IEventTarget.Unbind (string id) + { + valeursrecues.Remove(id); + } + + IEnumerable IEventTarget.IDs { + get { + return valeursrecues.Keys; + } + } + } #endregion @@ -490,7 +507,6 @@ namespace DMX2 return; } Conduite.Courante.EventManager.Bind(eventId,masterEventTarget); - } public void BindEffetSuivantEvent (string eventId) @@ -514,16 +530,30 @@ namespace DMX2 public override void Save (System.Xml.XmlElement parent) { System.Xml.XmlElement el = parent.OwnerDocument.CreateElement ("SequenceurLineaire"); - System.Xml.XmlElement xmlC; + System.Xml.XmlElement xmlEl; parent.AppendChild (el); el.SetAttribute ("id", ID.ToString ()); el.SetAttribute ("name", Name); - el.SetAttribute ("master", master.ToString ()); + //el.SetAttribute ("master", master.ToString ()); + + el.AppendChild(xmlEl = parent.OwnerDocument.CreateElement ("Master")); + xmlEl.SetAttribute("value",master.ToString()); + EventManager.SaveBindings(xmlEl,masterEventTarget); + + xmlEl = parent.OwnerDocument.CreateElement ("EffetSuivant"); + if(EventManager.SaveBindings(xmlEl,goNextEventTarget )) el.AppendChild(xmlEl); + + xmlEl = parent.OwnerDocument.CreateElement ("EffetPrecedent"); + if(EventManager.SaveBindings(xmlEl,goBackEventTarget )) el.AppendChild(xmlEl); foreach (Circuit c in circuitsSeq) { - el.AppendChild(xmlC = parent.OwnerDocument.CreateElement ("CircuitSeq")); - xmlC.SetAttribute("id",c.ID.ToString()); + el.AppendChild(xmlEl = parent.OwnerDocument.CreateElement ("CircuitSeq")); + xmlEl.SetAttribute("id",c.ID.ToString()); + + if(targets.ContainsKey(c)) + EventManager.SaveBindings(xmlEl,targets[c]); + } foreach (Effet ef in effets) { @@ -558,15 +588,34 @@ namespace DMX2 private void LoadSeq (Conduite conduite, System.Xml.XmlElement el) { + System.Xml.XmlElement xmlE; + ID = int.Parse (el.GetAttribute ("id")); Name = el.GetAttribute ("name"); - master = int.Parse (el.GetAttribute ("master")); + + if ((xmlE = el["Master"]) != null) { + master = int.Parse (xmlE.TryGetAttribute("value","100")); + foreach(string id in EventManager.LoadBindings(xmlE)) + BindMasterEvent(id); + } + else master = int.Parse (el.TryGetAttribute("master","100")); + + if ((xmlE = el["EffetSuivant"])!= null) + foreach(string id in EventManager.LoadBindings(xmlE)) + BindEffetSuivantEvent(id); + + if ((xmlE = el["EffetPrecedent"])!= null) + foreach(string id in EventManager.LoadBindings(xmlE)) + BindEffetSuivantEvent(id); + foreach (var xc in el.GetElementsByTagName("CircuitSeq")) { System.Xml.XmlElement xcir = xc as System.Xml.XmlElement; Circuit c = conduite.GetCircuitByID (int.Parse (xcir.GetAttribute ("id"))); circuitsSeq.Add (c); AjouteCircuit (c); + foreach(string id in EventManager.LoadBindings (xcir)) + BindCircuitEvent(c,id); } foreach (var xe in el.GetElementsByTagName("Effet")) diff --git a/DMX-2.0/gtk-gui/DMX2.SeqLinUI.cs b/DMX-2.0/gtk-gui/DMX2.SeqLinUI.cs index fb4ea0f..2591721 100644 --- a/DMX-2.0/gtk-gui/DMX2.SeqLinUI.cs +++ b/DMX-2.0/gtk-gui/DMX2.SeqLinUI.cs @@ -266,6 +266,7 @@ namespace DMX2 this.moveDownAction.Activated += new global::System.EventHandler (this.OnMoveDownActionActivated); this.circuitsAction.Activated += new global::System.EventHandler (this.OnCircuitsActionActivated); this.closeAction.Activated += new global::System.EventHandler (this.OnCloseActionActivated); + this.seqMasterScale.ValueChanged += new global::System.EventHandler (this.OnSeqMasterScaleValueChanged); this.zoneWid.SizeAllocated += new global::Gtk.SizeAllocatedHandler (this.OnZoneWidSizeAllocated); } } diff --git a/DMX-2.0/gtk-gui/gui.stetic b/DMX-2.0/gtk-gui/gui.stetic index 73961ab..1a655ec 100644 --- a/DMX-2.0/gtk-gui/gui.stetic +++ b/DMX-2.0/gtk-gui/gui.stetic @@ -817,6 +817,7 @@ au sequenceur True 0 Right + 1