]> git.r.bdr.sh - rbdr/super-polarity/blob - Super Polarity/Widget.cs
65c3fd23d4f36bb01caabe5a264c71de78b4337c
[rbdr/super-polarity] / Super Polarity / Widget.cs
1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5
6 namespace SuperPolarity
7 {
8 class Widget
9 {
10 public IList<Widget> Children;
11 public Dictionary<string, List<Action<float>>> Listeners;
12
13 public virtual void AppendChild(Widget widget)
14 {
15 Children.Add(widget);
16 }
17
18 public virtual void Bind(string eventName, Action<float> eventListener)
19 {
20 List<Action<float>> newListenerList;
21 List<Action<float>> listenerList;
22 bool foundListeners;
23
24 if (!Listeners.ContainsKey(eventName))
25 {
26 newListenerList = new List<Action<float>>();
27 Listeners.Add(eventName, newListenerList);
28 }
29
30 foundListeners = Listeners.TryGetValue(eventName, out listenerList);
31
32 listenerList.Add(eventListener);
33 }
34
35 public virtual void Unbind(string eventName, Action<float> eventListener)
36 {
37 // NOT YET IMPLEMENTED;
38 }
39
40 public virtual void Dispatch(string eventName, float value)
41 {
42 List<Action<float>> listenerList;
43 bool foundListeners;
44
45 foundListeners = Listeners.TryGetValue(eventName, out listenerList);
46
47 if (!foundListeners)
48 {
49 return;
50 }
51
52 foreach (Action<float> method in listenerList)
53 {
54 method(value);
55 }
56 }
57 }
58 }