-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathGLFlowLayout.cs
More file actions
128 lines (114 loc) · 3.62 KB
/
GLFlowLayout.cs
File metadata and controls
128 lines (114 loc) · 3.62 KB
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
using System;
using System.Drawing;
using System.Linq;
namespace GLGUI
{
public class GLFlowLayout : GLControl
{
public GLFlowDirection FlowDirection { get { return flowDirection; } set { flowDirection = value; Invalidate(); } }
public GLSkin.GLFlowLayoutSkin Skin { get { return skin; } set { skin = value; Invalidate(); } }
private GLFlowDirection flowDirection = GLFlowDirection.LeftToRight;
private GLSkin.GLFlowLayoutSkin skin;
private Rectangle background;
public GLFlowLayout(GLGui gui) : base(gui)
{
Render += OnRender;
skin = Gui.Skin.FlowLayout;
outer = new Rectangle(0, 0, 100, 100);
sizeMin = new Size(0, 0);
sizeMax = new Size(int.MaxValue, int.MaxValue);
}
private void Invalidate(object sender, EventArgs e)
{
Invalidate();
}
private void UpdatePositions()
{
int current = 0;
switch(FlowDirection)
{
case GLFlowDirection.LeftToRight:
foreach(GLControl control in Controls)
{
Rectangle o = control.Outer;
control.Outer = new Rectangle(current, 0, o.Width, o.Height);
current += o.Width + skin.Space;
}
break;
case GLFlowDirection.RightToLeft:
current = Inner.Width;
foreach(GLControl control in Controls)
{
Rectangle o = control.Outer;
current -= o.Width;
control.Outer = new Rectangle(current, 0, o.Width, o.Height);
current -= skin.Space;
}
break;
case GLFlowDirection.TopDown:
foreach(GLControl control in Controls)
{
Rectangle o = control.Outer;
control.Outer = new Rectangle(0, current, o.Width, o.Height);
current += o.Height + skin.Space;
}
break;
case GLFlowDirection.BottomUp:
current = Inner.Height;
foreach(GLControl control in Controls)
{
Rectangle o = control.Outer;
current -= o.Height;
control.Outer = new Rectangle(0, current, o.Width, o.Height);
current -= skin.Space;
}
break;
}
}
protected override void UpdateLayout()
{
UpdatePositions();
if (AutoSize)
{
if (Controls.Count() > 0)
{
outer.Width = Controls.Max(c => c.Outer.Right) - Controls.Min(c => c.Outer.Left) + skin.Padding.Horizontal + skin.Border.Horizontal;
outer.Height = Controls.Max(c => c.Outer.Bottom) - Controls.Min(c => c.Outer.Top) + skin.Padding.Vertical + skin.Border.Vertical;
}
else
{
outer.Width = skin.Padding.Horizontal + skin.Border.Horizontal;
outer.Height = skin.Padding.Vertical + skin.Border.Vertical;
}
}
outer.Width = Math.Min(Math.Max(outer.Width, sizeMin.Width), sizeMax.Width);
outer.Height = Math.Min(Math.Max(outer.Height, sizeMin.Height), sizeMax.Height);
background = new Rectangle(
skin.Border.Left, skin.Border.Top,
outer.Width - skin.Border.Horizontal, outer.Height - skin.Border.Vertical);
Inner = new Rectangle(
background.Left + skin.Padding.Left, background.Top + skin.Padding.Top,
background.Width - skin.Padding.Horizontal, background.Height - skin.Padding.Vertical);
if(flowDirection == GLFlowDirection.BottomUp || flowDirection == GLFlowDirection.RightToLeft)
UpdatePositions();
}
private void OnRender(object sender, double timeDelta)
{
GLDraw.Fill(ref skin.BorderColor);
GLDraw.FillRect(ref background, ref skin.BackgroundColor);
}
public override T Add<T>(T control)
{
base.Add(control);
control.Resize += Invalidate;
Invalidate();
return control;
}
public override void Remove(GLControl control)
{
base.Remove(control);
control.Resize -= Invalidate;
Invalidate();
}
}
}