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; }
|
||||
ICollection<string> GetEventList();
|
||||
bool Bind (string eventId, IEventTarget target);
|
||||
void Unbind (string eventId, IEventTarget target);
|
||||
bool Bind (string eventId);
|
||||
void Unbind (string eventId);
|
||||
Gtk.Menu GetProviderSubMenu( EventManager.EventMenuData state, Gtk.ButtonPressEventHandler handler );
|
||||
void ProcessEvents();
|
||||
void ProcessEvents(EventManagerCallback callback);
|
||||
}
|
||||
|
||||
public interface IEventTarget
|
||||
|
|
@ -25,6 +25,10 @@ namespace DMX2
|
|||
bool FireEvent(EventData data);
|
||||
}
|
||||
|
||||
public delegate void EventManagerCallback(EventData data);
|
||||
public delegate void EventManagerMenuCallBack(object state, string eventId);
|
||||
|
||||
|
||||
public class EventManager
|
||||
{
|
||||
static public object StateKey = new object();
|
||||
|
|
@ -32,18 +36,24 @@ namespace DMX2
|
|||
|
||||
class eventBinding
|
||||
{
|
||||
private eventBinding(){}
|
||||
public eventBinding(string _id, IEventTarget _target, IEventProvider _provider){
|
||||
id=_id;
|
||||
target=_target;
|
||||
provider=_provider;
|
||||
List<IEventTarget> targets=new List<IEventTarget>();
|
||||
public void AddTarget(IEventTarget target)
|
||||
{
|
||||
if(!targets.Contains(target))
|
||||
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>();
|
||||
|
||||
public EventManager ()
|
||||
|
|
@ -55,25 +65,15 @@ namespace DMX2
|
|||
{
|
||||
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 EventMenuCallBack CallBack;
|
||||
public EventManagerMenuCallBack CallBack;
|
||||
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 ();
|
||||
EventMenuData evd = new EventMenuData();
|
||||
|
|
@ -103,47 +103,48 @@ namespace DMX2
|
|||
public void ProcessEvents ()
|
||||
{
|
||||
foreach (IEventProvider prov in providers) {
|
||||
prov.ProcessEvents();
|
||||
prov.ProcessEvents(new EventManagerCallback(EventCallBack));
|
||||
}
|
||||
}
|
||||
|
||||
public void UnregisterProvider (IEventProvider prov)
|
||||
{
|
||||
foreach (eventBinding b in bindings)
|
||||
if(b.provider == prov)
|
||||
b.provider = null;
|
||||
providers.Remove(prov);
|
||||
}
|
||||
|
||||
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) {
|
||||
if (prov.Bind (eventId, target)) {
|
||||
bindings.Add(new eventBinding(eventId,target,prov));
|
||||
return true;
|
||||
if(prov.Bind(eventId)) return true;
|
||||
}
|
||||
}
|
||||
bindings.Add(new eventBinding(eventId,target,null));
|
||||
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)
|
||||
{
|
||||
eventBinding b = findBinding(eventId,target);
|
||||
if(b==null)return;
|
||||
b.provider.Unbind(eventId,target);
|
||||
bindings.Remove (b);
|
||||
if (!bindings.ContainsKey (eventId))
|
||||
return;
|
||||
bindings [eventId].RemoveTarget (target);
|
||||
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 {
|
||||
public string internalName;
|
||||
public string description;
|
||||
List<IEventTarget> boundTargets=null;
|
||||
|
||||
public internalEvent(string _id, string _desc)
|
||||
{
|
||||
internalName=_id;
|
||||
description=_desc;
|
||||
}
|
||||
bool hasTargets;
|
||||
|
||||
public bool HasTargets {
|
||||
get {
|
||||
return boundTargets==null?false:boundTargets.Count>0;
|
||||
return hasTargets;
|
||||
}
|
||||
}
|
||||
public List<IEventTarget> BoundTargets {
|
||||
get {
|
||||
if(boundTargets==null) boundTargets = new List<IEventTarget>();
|
||||
return boundTargets;
|
||||
set {
|
||||
hasTargets = value;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Dictionary<string,internalEvent> eventlist = new Dictionary<string, internalEvent>();
|
||||
|
||||
|
||||
|
||||
IntPtr midi_seq_handle = IntPtr.Zero;
|
||||
|
||||
|
||||
|
|
@ -63,20 +57,20 @@ namespace DMX2
|
|||
return eventlist.Keys;
|
||||
}
|
||||
|
||||
bool IEventProvider.Bind (string eventId, IEventTarget target)
|
||||
bool IEventProvider.Bind (string eventId)
|
||||
{
|
||||
if (!eventlist.ContainsKey (eventId)) {
|
||||
// TODO : check if non received yet midi event...
|
||||
return false;
|
||||
}
|
||||
eventlist[eventId].BoundTargets.Add(target);
|
||||
//TODO
|
||||
eventlist[eventId].HasTargets = true;
|
||||
return true;
|
||||
}
|
||||
|
||||
void IEventProvider.Unbind (string eventId, IEventTarget target)
|
||||
void IEventProvider.Unbind (string eventId)
|
||||
{
|
||||
if (!eventlist.ContainsKey (eventId)) return;
|
||||
eventlist[eventId].BoundTargets.Remove(target);
|
||||
eventlist[eventId].HasTargets =false;
|
||||
return;
|
||||
}
|
||||
|
||||
|
|
@ -104,11 +98,11 @@ namespace DMX2
|
|||
|
||||
}
|
||||
|
||||
void IEventProvider.ProcessEvents ()
|
||||
void IEventProvider.ProcessEvents (EventManagerCallback callback)
|
||||
{
|
||||
int ret; IntPtr evPtr;
|
||||
while ((ret = snd_seq_event_input_pending(midi_seq_handle,1))>0) {
|
||||
ret=snd_seq_event_input(midi_seq_handle, out evPtr);
|
||||
IntPtr evPtr;
|
||||
while ((snd_seq_event_input_pending(midi_seq_handle,1))>0) {
|
||||
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_free_event(evPtr);
|
||||
|
||||
|
|
@ -148,8 +142,7 @@ namespace DMX2
|
|||
EventData evData = new EventData();
|
||||
evData.id = id;
|
||||
evData.value = (byte)value;
|
||||
foreach( IEventTarget t in eventlist[id].BoundTargets)
|
||||
t.FireEvent(evData);
|
||||
callback(evData);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -246,7 +246,7 @@ namespace DMX2
|
|||
void TirettePopup (object sender, ContextMenuEventArgs e)
|
||||
{
|
||||
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.Popup();
|
||||
}
|
||||
|
|
|
|||
|
|
@ -447,18 +447,18 @@ namespace DMX2
|
|||
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+))?",
|
||||
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+)?)?",
|
||||
System.Text.RegularExpressions.RegexOptions.Compiled);
|
||||
|
||||
public override void Command (string command)
|
||||
{
|
||||
lock (this) {
|
||||
var cmd = regexCommand1.Match(command);
|
||||
var cmd = regexCommandExec.Match(command);
|
||||
|
||||
if (cmd.Success) {
|
||||
if (cmd.Groups ["effet"].Success) {
|
||||
|
|
@ -481,7 +481,7 @@ namespace DMX2
|
|||
string[] commands = Conduite.Courante.SequenceurMaitre.GetCommands (this);
|
||||
|
||||
for (int i = 0; i < commands.Length; i++) {
|
||||
var cmd = regexCommand2.Match(commands[i]);
|
||||
var cmd = regexCommandProcess.Match(commands[i]);
|
||||
if(cmd.Success){
|
||||
int ef = int.Parse(cmd.Groups["effet"].Value);
|
||||
if (ef-1>index) {
|
||||
|
|
@ -500,7 +500,7 @@ namespace DMX2
|
|||
string[] commands = Conduite.Courante.SequenceurMaitre.GetCommands (this);
|
||||
|
||||
for (int i = 0; i < commands.Length; i++) {
|
||||
var cmd = regexCommand2.Match(commands[i]);
|
||||
var cmd = regexCommandProcess.Match(commands[i]);
|
||||
if(cmd.Success){
|
||||
int ef = int.Parse(cmd.Groups["effet"].Value);
|
||||
if (ef-1 == index)
|
||||
|
|
@ -525,7 +525,7 @@ namespace DMX2
|
|||
int b = index+2;
|
||||
|
||||
for (int i = 0; i < commands.Length; i++) {
|
||||
var cmd = regexCommand2.Match(commands[i]);
|
||||
var cmd = regexCommandProcess.Match(commands[i]);
|
||||
if(cmd.Success){
|
||||
int ef = int.Parse(cmd.Groups["effet"].Value);
|
||||
if (ef == a)
|
||||
|
|
|
|||
Loading…
Reference in a new issue