aboutsummaryrefslogtreecommitdiff
path: root/SuperPolarity/Widget.cs
blob: 3db42d943b51e6f0553643e0ba1cf0e284daf438 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace SuperPolarity
{
    class Widget
    {
        public List<Widget> Children;
        public Dictionary<string, List<Action<float>>> Listeners;

        public Vector2 Position;
        public SuperPolarity Game;

        protected bool Active;

        public Widget(SuperPolarity game, Vector2 position)
        {
            Game = game;
            Position = position;
            Active = false;
            Children = new List<Widget>();
            Listeners = new Dictionary<string, List<Action<float>>>();
        }

        public void Activate()
        {
            Active = true;
        }

        public void Deactivate()
        {
            Active = false;
        }

        public virtual void AppendChild(Widget widget)
        {
            Children.Add(widget);
        }

        public virtual void Bind(string eventName, Action<float> eventListener)
        {
            List<Action<float>> newListenerList;
            List<Action<float>> listenerList;
            bool foundListeners;

            if (!Listeners.ContainsKey(eventName))
            {
                newListenerList = new List<Action<float>>();
                Listeners.Add(eventName, newListenerList);
            }

            foundListeners = Listeners.TryGetValue(eventName, out listenerList);

            listenerList.Add(eventListener);
        }

        public virtual void Unbind(string eventName, Action<float> eventListener)
        {
            // NOT YET IMPLEMENTED;
        }

        public virtual void Dispatch(string eventName, float value)
        {
            List<Action<float>> listenerList;
            bool foundListeners;

            foundListeners = Listeners.TryGetValue(eventName, out listenerList);

            if (!foundListeners)
            {
                return;
            }

            foreach (Action<float> method in listenerList)
            {
                method(value);
            }
        }

        public virtual void Update(GameTime gameTime)
        {
        }

        public virtual void Draw(SpriteBatch spriteBatch)
        {
        }
    }
}