aboutsummaryrefslogtreecommitdiff
path: root/source/ui/elements/Container.cs
blob: 90809996b3c96e5c782e2683e936e6f49bfc59fc (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
using System.Collections.Generic;
using Celesteia.Game.Input;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using MonoGame.Extended.Input;

namespace Celesteia.UI.Elements {
    public class Container : Element, IContainer
    {
        private List<IElement> Children;

        public Container(Rect rect) {
            SetRect(rect);
            Children = new List<IElement>();
        }

        public void AddChild(IElement element) {
            Children.Add(element);
            element.SetParent(this);
        }

        public List<IElement> GetChildren() => Children;

        public override void Draw(SpriteBatch spriteBatch)
        {
            Children.ForEach(element => { if (element.GetEnabled()) element.Draw(spriteBatch); });
        }

        private Point _mousePosition;

        public override void Update(GameTime gameTime, out bool clickedAnything)
        {
            clickedAnything = false;
            if (!UIReferences.GUIEnabled) return;

            foreach (IElement element in Children) {
                element.Update(gameTime, out clickedAnything);
            }

            _mousePosition = MouseHelper.Position;

            if (MouseHelper.Pressed(MouseButton.Left))  clickedAnything = ResolveMouseDown(MouseButton.Left);
            else if (MouseHelper.Pressed(MouseButton.Right))  clickedAnything = ResolveMouseDown(MouseButton.Right);

            if (MouseHelper.Released(MouseButton.Left)) clickedAnything = ResolveMouseUp(MouseButton.Left);
            else if (MouseHelper.Released(MouseButton.Right)) clickedAnything = ResolveMouseUp(MouseButton.Right);

            ResolveMouseOver();
        }

        public bool ResolveMouseDown(MouseButton button) {
            bool clicked = false;
            Children.FindAll(x => x is Clickable).ForEach(element => {
                if (!element.GetEnabled()) return;
                Clickable clickable = element as Clickable;

                if (clicked = clickable.GetRectangle().Contains(_mousePosition))
                    clickable.OnMouseDown(button, _mousePosition);
            });
            return clicked;
        }

        public bool ResolveMouseUp(MouseButton button) {
            bool clicked = false;
            Children.FindAll(x => x is Clickable).ForEach(element => {
                if (!element.GetEnabled()) return;
                Clickable clickable = element as Clickable;

                if (clickable.GetRectangle().Contains(_mousePosition)) {
                    clicked = true;
                    clickable.OnMouseUp(button, _mousePosition);
                }
            });
            return clicked;
        }

        public void ResolveMouseOver() {
            Children.ForEach(element => {
                if (!element.GetEnabled()) return;
                bool over = element.GetRectangle().Contains(_mousePosition);
                if (over && !element.GetMouseOver()) {
                    element.OnMouseIn();
                } else if (!over && element.GetMouseOver()) element.OnMouseOut();
            });
        }

        public void Dispose() {
            Children.Clear();
        }
    }
}