summaryrefslogtreecommitdiff
path: root/Plugins/MonoGame.Extended/source/MonoGame.Extended.Gui/Controls/ComboBox.cs
blob: f8867d24ec02f2fe0ff1ea5e5f4a4aa2c569c13c (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
97
98
99
100
101
102
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;
using MonoGame.Extended.Input.InputListeners;
using MonoGame.Extended.TextureAtlases;

namespace MonoGame.Extended.Gui.Controls
{
    public class ComboBox : SelectorControl
    {
        public ComboBox()
        {
        }
        
        public bool IsOpen { get; set; }
        public TextureRegion2D DropDownRegion { get; set; }
        public Color DropDownColor { get; set; } = Color.White;

        public override bool OnKeyPressed(IGuiContext context, KeyboardEventArgs args)
        {
            if (args.Key == Keys.Enter)
                IsOpen = false;

            return base.OnKeyPressed(context, args);
        }

        public override bool OnPointerUp(IGuiContext context, PointerEventArgs args)
        {
            IsOpen = !IsOpen;
            return base.OnPointerUp(context, args);
        }

        protected override Rectangle GetListAreaRectangle(IGuiContext context)
        {
            return GetDropDownRectangle(context);
        }

        public override bool Contains(IGuiContext context, Point point)
        {
            return base.Contains(context, point) || IsOpen && GetListAreaRectangle(context).Contains(point);
        }

        public override Size GetContentSize(IGuiContext context)
        {
            var width = 0;
            var height = 0;

            foreach (var item in Items)
            {
                var itemSize = GetItemSize(context, item);

                if (itemSize.Width > width)
                    width = itemSize.Width;

                if (itemSize.Height > height)
                    height = itemSize.Height;
            }

            return new Size(width + ClipPadding.Width, height + ClipPadding.Height);
        }

        public override void Draw(IGuiContext context, IGuiRenderer renderer, float deltaSeconds)
        {
            base.Draw(context, renderer, deltaSeconds);

            if (IsOpen)
            {
                var dropDownRectangle = GetListAreaRectangle(context);

                if (DropDownRegion != null)
                {
                    renderer.DrawRegion(DropDownRegion, dropDownRectangle, DropDownColor);
                }
                else
                {
                    renderer.FillRectangle(dropDownRectangle, DropDownColor);
                    renderer.DrawRectangle(dropDownRectangle, BorderColor);
                }

                DrawItemList(context, renderer);
            }

            var selectedTextInfo = GetItemTextInfo(context, ContentRectangle, SelectedItem);

            if (!string.IsNullOrWhiteSpace(selectedTextInfo.Text))
                renderer.DrawText(selectedTextInfo.Font, selectedTextInfo.Text, selectedTextInfo.Position + TextOffset, selectedTextInfo.Color, selectedTextInfo.ClippingRectangle);
        }

        private Rectangle GetDropDownRectangle(IGuiContext context)
        {
            var dropDownRectangle = BoundingRectangle;

            dropDownRectangle.Y = dropDownRectangle.Y + dropDownRectangle.Height;
            dropDownRectangle.Height = (int) Items
                .Select(item => GetItemSize(context, item))
                .Select(itemSize => itemSize.Height)
                .Sum();

            return dropDownRectangle;
        }
    }
}