150 lines
3.4 KiB
C#
150 lines
3.4 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
|
|
namespace DMX2
|
|
{
|
|
|
|
public struct EventData
|
|
{
|
|
public string id;
|
|
public byte value;
|
|
}
|
|
|
|
public interface IEventProvider
|
|
{
|
|
string MenuName{ get; }
|
|
ICollection<string> GetEventList();
|
|
bool Bind (string eventId, IEventTarget target);
|
|
void Unbind (string eventId, IEventTarget target);
|
|
Gtk.Menu GetProviderSubMenu( EventManager.EventMenuData state, Gtk.ButtonPressEventHandler handler );
|
|
void ProcessEvents();
|
|
}
|
|
|
|
public interface IEventTarget
|
|
{
|
|
bool FireEvent(EventData data);
|
|
}
|
|
|
|
public class EventManager
|
|
{
|
|
static public object StateKey = new object();
|
|
static public object EventIdKey = new object();
|
|
|
|
class eventBinding
|
|
{
|
|
private eventBinding(){}
|
|
public eventBinding(string _id, IEventTarget _target, IEventProvider _provider){
|
|
id=_id;
|
|
target=_target;
|
|
provider=_provider;
|
|
}
|
|
public string id;
|
|
public IEventTarget target;
|
|
public IEventProvider provider;
|
|
}
|
|
|
|
List<eventBinding> bindings = new List<eventBinding>();
|
|
List<IEventProvider> providers = new List<IEventProvider>();
|
|
|
|
public EventManager ()
|
|
{
|
|
|
|
}
|
|
|
|
public void RegisterProvider (IEventProvider 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 EventMenuCallBack CallBack;
|
|
public object state;
|
|
}
|
|
|
|
public Gtk.Menu GetMenu (object state, EventMenuCallBack callback)
|
|
{
|
|
Gtk.Menu menu = new Gtk.Menu ();
|
|
EventMenuData evd = new EventMenuData();
|
|
evd.CallBack = callback;
|
|
evd.state = state;
|
|
|
|
Gtk.ButtonPressEventHandler handler = new Gtk.ButtonPressEventHandler (HandleButtonPressEvent);
|
|
|
|
foreach (IEventProvider prov in providers) {
|
|
Gtk.MenuItem provitem = new Gtk.MenuItem(prov.MenuName);
|
|
provitem.Submenu = prov.GetProviderSubMenu(evd, handler);
|
|
menu.Add(provitem);
|
|
}
|
|
|
|
return menu;
|
|
|
|
}
|
|
|
|
void HandleButtonPressEvent (object o, Gtk.ButtonPressEventArgs args)
|
|
{
|
|
Gtk.MenuItem item = o as Gtk.MenuItem;
|
|
EventMenuData evd = item.Data[EventManager.StateKey] as EventMenuData;
|
|
string id = item.Data[EventManager.EventIdKey] as string;
|
|
evd.CallBack(evd.state,id);
|
|
}
|
|
|
|
public void ProcessEvents ()
|
|
{
|
|
foreach (IEventProvider prov in providers) {
|
|
prov.ProcessEvents();
|
|
}
|
|
}
|
|
|
|
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)
|
|
{
|
|
|
|
foreach (IEventProvider prov in providers) {
|
|
if (prov.Bind (eventId, target)) {
|
|
bindings.Add(new eventBinding(eventId,target,prov));
|
|
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);
|
|
}
|
|
}
|
|
}
|
|
|