summaryrefslogtreecommitdiff
path: root/Plugins/MonoGame.Extended/source/MonoGame.Extended.Gui/Controls/DockPanel.cs
blob: 7dfe1e6905e5131b3e84b84fdde2a5216bd06e10 (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
103
104
using System;
using Microsoft.Xna.Framework;

namespace MonoGame.Extended.Gui.Controls
{
    public enum Dock
    {
        Left, Right, Top, Bottom
    }

    public class DockPanel : LayoutControl
    {
        public override Size GetContentSize(IGuiContext context)
        {
            var size = new Size();

            for (var i = 0; i < Items.Count; i++)
            {
                var control = Items[i];
                var actualSize = control.CalculateActualSize(context);

                if (LastChildFill && i == Items.Count - 1)
                {
                    size.Width += actualSize.Width;
                    size.Height += actualSize.Height;
                }
                else
                {
                    var dock = control.GetAttachedProperty(DockProperty) as Dock? ?? Dock.Left;

                    switch (dock)
                    {
                        case Dock.Left:
                        case Dock.Right:
                            size.Width += actualSize.Width;
                            break;
                        case Dock.Top:
                        case Dock.Bottom:
                            size.Height += actualSize.Height;
                            break;
                        default:
                            throw new ArgumentOutOfRangeException();
                    }
                }
            }

            return size;
        }

        protected override void Layout(IGuiContext context, Rectangle rectangle)
        {
            for (var i = 0; i < Items.Count; i++)
            {
                var control = Items[i];

                if (LastChildFill && i == Items.Count - 1)
                {
                    PlaceControl(context, control, rectangle.X, rectangle.Y, rectangle.Width, rectangle.Height);
                }
                else
                {
                    var actualSize = control.CalculateActualSize(context);
                    var dock = control.GetAttachedProperty(DockProperty) as Dock? ?? Dock.Left;

                    switch (dock)
                    {
                        case Dock.Left:
                            PlaceControl(context, control, rectangle.Left, rectangle.Top, actualSize.Width, rectangle.Height);
                            rectangle.X += actualSize.Width;
                            rectangle.Width -= actualSize.Width;
                            break;
                        case Dock.Right:
                            PlaceControl(context, control, rectangle.Right - actualSize.Width, rectangle.Top, actualSize.Width, rectangle.Height);
                            rectangle.Width -= actualSize.Width;
                            break;
                        case Dock.Top:
                            PlaceControl(context, control, rectangle.Left, rectangle.Top, rectangle.Width, actualSize.Height);
                            rectangle.Y += actualSize.Height;
                            rectangle.Height -= actualSize.Height;
                            break;
                        case Dock.Bottom:
                            PlaceControl(context, control, rectangle.Left, rectangle.Bottom - actualSize.Height, rectangle.Width, actualSize.Height);
                            rectangle.Height -= actualSize.Height;
                            break;
                        default:
                            throw new ArgumentOutOfRangeException();
                    }
                }
            }
        }

        public const string DockProperty = "Dock";
        
        public override Type GetAttachedPropertyType(string propertyName)
        {
            if (string.Equals(DockProperty, propertyName, StringComparison.OrdinalIgnoreCase))
                return typeof(Dock);

            return base.GetAttachedPropertyType(propertyName);
        }

        public bool LastChildFill { get; set; } = true;
    }
}