aboutsummaryrefslogtreecommitdiff
path: root/V3/Widgets/AbstractMenu.cs
blob: f179b5e38397809f4274d13d8a65482be728132d (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
93
94
95
96
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using System.Collections.Generic;
using System.Linq;
using V3.Input;

namespace V3.Widgets
{
    /// <summary>
    /// An abstract menu that handles the updating and drawing of widgtes.
    /// </summary>
    public abstract class AbstractMenu : IMenu
    {
        public List<IWidget> Widgets { get; } = new List<IWidget>();
        public Vector2 Size { get; private set; }
        public Vector2 Position { get; private set; }

        private readonly GraphicsDeviceManager mGraphicsDeviceManager;

        protected AbstractMenu(GraphicsDeviceManager graphicsDeviceManager)
        {
            mGraphicsDeviceManager = graphicsDeviceManager;
        }

        public void HandleMouseEvent(IMouseEvent mouseEvent)
        {
            foreach (var clickable in Widgets.OfType<IClickable>())
                clickable.HandleMouseEvent(mouseEvent);
        }

        public void Draw(SpriteBatch spriteBatch)
        {
            UpdateWidgetRelativePositions();
            UpdateWidgetAbsolutePositions();
            spriteBatch.Begin();
            Widgets.ForEach(w => w.Draw(spriteBatch));
            spriteBatch.End();
        }

        public void Update()
        {
            ResetClicked();
            UpdateMouseSelection();
            UpdateWidgetSizes();
        }

        protected abstract void UpdateWidgetSizes();

        protected abstract void UpdateWidgetRelativePositions();

        protected abstract Vector2 GetTotalSize();

        private void UpdateWidgetAbsolutePositions()
        {
            var viewport = mGraphicsDeviceManager.GraphicsDevice.Viewport;
            Size = GetTotalSize();
            var viewportSize = new Vector2(viewport.Bounds.Width, viewport.Bounds.Height);
            Position = (viewportSize - Size) / 2;
            Widgets.ForEach(w => w.Position = w.Position + Position);
        }

        private void ResetClicked()
        {
            foreach (var clickable in Widgets.OfType<IClickable>())
                clickable.IsClicked = false;
        }

        private void UpdateMouseSelection()
        {
            var position = Mouse.GetState().Position;
            foreach (var selectable in Widgets.OfType<ISelectable>())
                selectable.IsSelected = selectable.CheckSelected(position);
        }

        protected static void MakeWidgetsSameSize(IEnumerable<IWidget> widgets)
        {
            var xMax = 0f;
            var yMax = 0f;

            var widgetsCopy = widgets as IList<IWidget> ?? widgets.ToList();
            foreach (var widget in widgetsCopy)
            {
                var size = widget.GetMinimumSize();
                if (size.X > xMax)
                    xMax = size.X;
                if (size.Y > yMax)
                    yMax = size.Y;
            }

            var sizeMax = new Vector2(xMax, yMax);
            foreach (var widget in widgetsCopy)
                widget.Size = sizeMax;
        }
    }
}