Simplification de la gestion d'evenements
This commit is contained in:
parent
3fc814731f
commit
5f5a3f51d1
4 changed files with 70 additions and 76 deletions
|
|
@ -14,10 +14,10 @@ namespace DMX2
|
||||||
{
|
{
|
||||||
string MenuName{ get; }
|
string MenuName{ get; }
|
||||||
ICollection<string> GetEventList();
|
ICollection<string> GetEventList();
|
||||||
bool Bind (string eventId, IEventTarget target);
|
bool Bind (string eventId);
|
||||||
void Unbind (string eventId, IEventTarget target);
|
void Unbind (string eventId);
|
||||||
Gtk.Menu GetProviderSubMenu( EventManager.EventMenuData state, Gtk.ButtonPressEventHandler handler );
|
Gtk.Menu GetProviderSubMenu( EventManager.EventMenuData state, Gtk.ButtonPressEventHandler handler );
|
||||||
void ProcessEvents();
|
void ProcessEvents(EventManagerCallback callback);
|
||||||
}
|
}
|
||||||
|
|
||||||
public interface IEventTarget
|
public interface IEventTarget
|
||||||
|
|
@ -25,6 +25,10 @@ namespace DMX2
|
||||||
bool FireEvent(EventData data);
|
bool FireEvent(EventData data);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public delegate void EventManagerCallback(EventData data);
|
||||||
|
public delegate void EventManagerMenuCallBack(object state, string eventId);
|
||||||
|
|
||||||
|
|
||||||
public class EventManager
|
public class EventManager
|
||||||
{
|
{
|
||||||
static public object StateKey = new object();
|
static public object StateKey = new object();
|
||||||
|
|
@ -32,18 +36,24 @@ namespace DMX2
|
||||||
|
|
||||||
class eventBinding
|
class eventBinding
|
||||||
{
|
{
|
||||||
private eventBinding(){}
|
List<IEventTarget> targets=new List<IEventTarget>();
|
||||||
public eventBinding(string _id, IEventTarget _target, IEventProvider _provider){
|
public void AddTarget(IEventTarget target)
|
||||||
id=_id;
|
{
|
||||||
target=_target;
|
if(!targets.Contains(target))
|
||||||
provider=_provider;
|
targets.Add(target);
|
||||||
|
}
|
||||||
|
public void RemoveTarget(IEventTarget target)
|
||||||
|
{
|
||||||
|
targets.Remove(target);
|
||||||
|
}
|
||||||
|
public ICollection<IEventTarget> Targets {
|
||||||
|
get {
|
||||||
|
return targets;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
public string id;
|
|
||||||
public IEventTarget target;
|
|
||||||
public IEventProvider provider;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
List<eventBinding> bindings = new List<eventBinding>();
|
Dictionary<string,eventBinding> bindings = new Dictionary<string,eventBinding>();
|
||||||
List<IEventProvider> providers = new List<IEventProvider>();
|
List<IEventProvider> providers = new List<IEventProvider>();
|
||||||
|
|
||||||
public EventManager ()
|
public EventManager ()
|
||||||
|
|
@ -55,25 +65,15 @@ namespace DMX2
|
||||||
{
|
{
|
||||||
providers.Add (prov);
|
providers.Add (prov);
|
||||||
|
|
||||||
//List<string> evList = new List<string>(prov.GetEventList());
|
|
||||||
|
|
||||||
foreach (eventBinding b in bindings) {
|
|
||||||
if(b.provider == null)
|
|
||||||
{
|
|
||||||
if(prov.Bind(b.id,b.target))
|
|
||||||
b.provider = prov;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public delegate void EventMenuCallBack(object state, string eventId);
|
|
||||||
public class EventMenuData {
|
public class EventMenuData {
|
||||||
public EventMenuCallBack CallBack;
|
public EventManagerMenuCallBack CallBack;
|
||||||
public object state;
|
public object state;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Gtk.Menu GetMenu (object state, EventMenuCallBack callback)
|
public Gtk.Menu GetMenu (object state, EventManagerMenuCallBack callback)
|
||||||
{
|
{
|
||||||
Gtk.Menu menu = new Gtk.Menu ();
|
Gtk.Menu menu = new Gtk.Menu ();
|
||||||
EventMenuData evd = new EventMenuData();
|
EventMenuData evd = new EventMenuData();
|
||||||
|
|
@ -103,47 +103,48 @@ namespace DMX2
|
||||||
public void ProcessEvents ()
|
public void ProcessEvents ()
|
||||||
{
|
{
|
||||||
foreach (IEventProvider prov in providers) {
|
foreach (IEventProvider prov in providers) {
|
||||||
prov.ProcessEvents();
|
prov.ProcessEvents(new EventManagerCallback(EventCallBack));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void UnregisterProvider (IEventProvider prov)
|
public void UnregisterProvider (IEventProvider prov)
|
||||||
{
|
{
|
||||||
foreach (eventBinding b in bindings)
|
|
||||||
if(b.provider == prov)
|
|
||||||
b.provider = null;
|
|
||||||
providers.Remove(prov);
|
providers.Remove(prov);
|
||||||
}
|
}
|
||||||
|
|
||||||
public bool Bind (string eventId, IEventTarget target)
|
public bool Bind (string eventId, IEventTarget target)
|
||||||
{
|
{
|
||||||
|
if(!bindings.ContainsKey(eventId))
|
||||||
|
bindings.Add (eventId,new eventBinding());
|
||||||
|
bindings[eventId].AddTarget(target);
|
||||||
foreach (IEventProvider prov in providers) {
|
foreach (IEventProvider prov in providers) {
|
||||||
if (prov.Bind (eventId, target)) {
|
if(prov.Bind(eventId)) return true;
|
||||||
bindings.Add(new eventBinding(eventId,target,prov));
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
bindings.Add(new eventBinding(eventId,target,null));
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
eventBinding findBinding (string eventId, IEventTarget target)
|
|
||||||
{
|
|
||||||
foreach (eventBinding b in bindings) {
|
|
||||||
if (b.id == eventId && b.target == target) {
|
|
||||||
return b;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void Unbind (string eventId, IEventTarget target)
|
public void Unbind (string eventId, IEventTarget target)
|
||||||
{
|
{
|
||||||
eventBinding b = findBinding(eventId,target);
|
if (!bindings.ContainsKey (eventId))
|
||||||
if(b==null)return;
|
return;
|
||||||
b.provider.Unbind(eventId,target);
|
bindings [eventId].RemoveTarget (target);
|
||||||
bindings.Remove (b);
|
if (bindings [eventId].Targets.Count == 0) {
|
||||||
|
bindings.Remove(eventId);
|
||||||
|
foreach (IEventProvider prov in providers) {
|
||||||
|
prov.Unbind(eventId);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public void EventCallBack (EventData data)
|
||||||
|
{
|
||||||
|
if (bindings.ContainsKey (data.id)) {
|
||||||
|
foreach (IEventTarget target in bindings[data.id].Targets) {
|
||||||
|
target.FireEvent(data);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -10,30 +10,24 @@ namespace DMX2
|
||||||
class internalEvent {
|
class internalEvent {
|
||||||
public string internalName;
|
public string internalName;
|
||||||
public string description;
|
public string description;
|
||||||
List<IEventTarget> boundTargets=null;
|
|
||||||
|
|
||||||
public internalEvent(string _id, string _desc)
|
public internalEvent(string _id, string _desc)
|
||||||
{
|
{
|
||||||
internalName=_id;
|
internalName=_id;
|
||||||
description=_desc;
|
description=_desc;
|
||||||
}
|
}
|
||||||
|
bool hasTargets;
|
||||||
|
|
||||||
public bool HasTargets {
|
public bool HasTargets {
|
||||||
get {
|
get {
|
||||||
return boundTargets==null?false:boundTargets.Count>0;
|
return hasTargets;
|
||||||
}
|
}
|
||||||
}
|
set {
|
||||||
public List<IEventTarget> BoundTargets {
|
hasTargets = value;
|
||||||
get {
|
|
||||||
if(boundTargets==null) boundTargets = new List<IEventTarget>();
|
|
||||||
return boundTargets;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Dictionary<string,internalEvent> eventlist = new Dictionary<string, internalEvent>();
|
Dictionary<string,internalEvent> eventlist = new Dictionary<string, internalEvent>();
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
IntPtr midi_seq_handle = IntPtr.Zero;
|
IntPtr midi_seq_handle = IntPtr.Zero;
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -63,20 +57,20 @@ namespace DMX2
|
||||||
return eventlist.Keys;
|
return eventlist.Keys;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool IEventProvider.Bind (string eventId, IEventTarget target)
|
bool IEventProvider.Bind (string eventId)
|
||||||
{
|
{
|
||||||
if (!eventlist.ContainsKey (eventId)) {
|
if (!eventlist.ContainsKey (eventId)) {
|
||||||
// TODO : check if non received yet midi event...
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
eventlist[eventId].BoundTargets.Add(target);
|
//TODO
|
||||||
|
eventlist[eventId].HasTargets = true;
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
void IEventProvider.Unbind (string eventId, IEventTarget target)
|
void IEventProvider.Unbind (string eventId)
|
||||||
{
|
{
|
||||||
if (!eventlist.ContainsKey (eventId)) return;
|
if (!eventlist.ContainsKey (eventId)) return;
|
||||||
eventlist[eventId].BoundTargets.Remove(target);
|
eventlist[eventId].HasTargets =false;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -104,11 +98,11 @@ namespace DMX2
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void IEventProvider.ProcessEvents ()
|
void IEventProvider.ProcessEvents (EventManagerCallback callback)
|
||||||
{
|
{
|
||||||
int ret; IntPtr evPtr;
|
IntPtr evPtr;
|
||||||
while ((ret = snd_seq_event_input_pending(midi_seq_handle,1))>0) {
|
while ((snd_seq_event_input_pending(midi_seq_handle,1))>0) {
|
||||||
ret=snd_seq_event_input(midi_seq_handle, out evPtr);
|
snd_seq_event_input(midi_seq_handle, out evPtr);
|
||||||
snd_seq_event_t evS =(snd_seq_event_t) Marshal.PtrToStructure(evPtr,typeof(snd_seq_event_t));
|
snd_seq_event_t evS =(snd_seq_event_t) Marshal.PtrToStructure(evPtr,typeof(snd_seq_event_t));
|
||||||
snd_seq_free_event(evPtr);
|
snd_seq_free_event(evPtr);
|
||||||
|
|
||||||
|
|
@ -148,8 +142,7 @@ namespace DMX2
|
||||||
EventData evData = new EventData();
|
EventData evData = new EventData();
|
||||||
evData.id = id;
|
evData.id = id;
|
||||||
evData.value = (byte)value;
|
evData.value = (byte)value;
|
||||||
foreach( IEventTarget t in eventlist[id].BoundTargets)
|
callback(evData);
|
||||||
t.FireEvent(evData);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -246,7 +246,7 @@ namespace DMX2
|
||||||
void TirettePopup (object sender, ContextMenuEventArgs e)
|
void TirettePopup (object sender, ContextMenuEventArgs e)
|
||||||
{
|
{
|
||||||
Circuit c = e.Widget.Data[circuitKey] as Circuit;
|
Circuit c = e.Widget.Data[circuitKey] as Circuit;
|
||||||
Menu m = Conduite.Courante.EventManager.GetMenu(c,new EventManager.EventMenuCallBack(TirettePopupEnd));
|
Menu m = Conduite.Courante.EventManager.GetMenu(c,new EventManagerMenuCallBack(TirettePopupEnd));
|
||||||
m.ShowAll();
|
m.ShowAll();
|
||||||
m.Popup();
|
m.Popup();
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -447,18 +447,18 @@ namespace DMX2
|
||||||
effets.Add(Effet.Load(conduite,xe as System.Xml.XmlElement));
|
effets.Add(Effet.Load(conduite,xe as System.Xml.XmlElement));
|
||||||
}
|
}
|
||||||
|
|
||||||
static System.Text.RegularExpressions.Regex regexCommand1 = new System.Text.RegularExpressions.Regex(
|
static System.Text.RegularExpressions.Regex regexCommandExec = new System.Text.RegularExpressions.Regex(
|
||||||
@"(?<effet>\d+)(t(?<transition>\d+))?",
|
@"(?<effet>\d+)(t(?<transition>\d+))?",
|
||||||
System.Text.RegularExpressions.RegexOptions.Compiled);
|
System.Text.RegularExpressions.RegexOptions.Compiled);
|
||||||
|
|
||||||
static System.Text.RegularExpressions.Regex regexCommand2 = new System.Text.RegularExpressions.Regex(
|
static System.Text.RegularExpressions.Regex regexCommandProcess = new System.Text.RegularExpressions.Regex(
|
||||||
@"(?<effet>\d+)(?<params>(t\d+)?)?",
|
@"(?<effet>\d+)(?<params>(t\d+)?)?",
|
||||||
System.Text.RegularExpressions.RegexOptions.Compiled);
|
System.Text.RegularExpressions.RegexOptions.Compiled);
|
||||||
|
|
||||||
public override void Command (string command)
|
public override void Command (string command)
|
||||||
{
|
{
|
||||||
lock (this) {
|
lock (this) {
|
||||||
var cmd = regexCommand1.Match(command);
|
var cmd = regexCommandExec.Match(command);
|
||||||
|
|
||||||
if (cmd.Success) {
|
if (cmd.Success) {
|
||||||
if (cmd.Groups ["effet"].Success) {
|
if (cmd.Groups ["effet"].Success) {
|
||||||
|
|
@ -481,7 +481,7 @@ namespace DMX2
|
||||||
string[] commands = Conduite.Courante.SequenceurMaitre.GetCommands (this);
|
string[] commands = Conduite.Courante.SequenceurMaitre.GetCommands (this);
|
||||||
|
|
||||||
for (int i = 0; i < commands.Length; i++) {
|
for (int i = 0; i < commands.Length; i++) {
|
||||||
var cmd = regexCommand2.Match(commands[i]);
|
var cmd = regexCommandProcess.Match(commands[i]);
|
||||||
if(cmd.Success){
|
if(cmd.Success){
|
||||||
int ef = int.Parse(cmd.Groups["effet"].Value);
|
int ef = int.Parse(cmd.Groups["effet"].Value);
|
||||||
if (ef-1>index) {
|
if (ef-1>index) {
|
||||||
|
|
@ -500,7 +500,7 @@ namespace DMX2
|
||||||
string[] commands = Conduite.Courante.SequenceurMaitre.GetCommands (this);
|
string[] commands = Conduite.Courante.SequenceurMaitre.GetCommands (this);
|
||||||
|
|
||||||
for (int i = 0; i < commands.Length; i++) {
|
for (int i = 0; i < commands.Length; i++) {
|
||||||
var cmd = regexCommand2.Match(commands[i]);
|
var cmd = regexCommandProcess.Match(commands[i]);
|
||||||
if(cmd.Success){
|
if(cmd.Success){
|
||||||
int ef = int.Parse(cmd.Groups["effet"].Value);
|
int ef = int.Parse(cmd.Groups["effet"].Value);
|
||||||
if (ef-1 == index)
|
if (ef-1 == index)
|
||||||
|
|
@ -525,7 +525,7 @@ namespace DMX2
|
||||||
int b = index+2;
|
int b = index+2;
|
||||||
|
|
||||||
for (int i = 0; i < commands.Length; i++) {
|
for (int i = 0; i < commands.Length; i++) {
|
||||||
var cmd = regexCommand2.Match(commands[i]);
|
var cmd = regexCommandProcess.Match(commands[i]);
|
||||||
if(cmd.Success){
|
if(cmd.Success){
|
||||||
int ef = int.Parse(cmd.Groups["effet"].Value);
|
int ef = int.Parse(cmd.Groups["effet"].Value);
|
||||||
if (ef == a)
|
if (ef == a)
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue