-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathStackPanel.cs
160 lines (134 loc) · 5.01 KB
/
StackPanel.cs
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
////////////////////////////////////////////////////////////////
// //
// Neoforce Controls //
// //
////////////////////////////////////////////////////////////////
// //
// File: StackPanel.cs //
// //
// Version: 0.7 //
// //
// Date: 11/09/2010 //
// //
// Author: Tom Shane //
// //
////////////////////////////////////////////////////////////////
// //
// Copyright (c) by Tom Shane //
// //
////////////////////////////////////////////////////////////////
#region //// Using /////////////
////////////////////////////////////////////////////////////////////////////
using Microsoft.Xna.Framework;
using System.Collections.Generic;
using Microsoft.Xna.Framework.Graphics;
using System;
////////////////////////////////////////////////////////////////////////////
#endregion
namespace TomShane.Neoforce.Controls
{
public class StackPanel: Container
{
#region //// Fields ////////////
////////////////////////////////////////////////////////////////////////////
private Orientation orientation;
public Orientation Orientation
{
get { return this.orientation; }
set
{
this.orientation = value;
this.CalcLayout();
}
}
private bool autoRefresh;
/// <summary>
/// Should the stack panel refresh itself, when a control is added
/// </summary>
public bool AutoRefresh
{
get { return autoRefresh; }
set { autoRefresh = value; }
}
////////////////////////////////////////////////////////////////////////////
private TimeSpan refreshTimer;
private const int refreshTime = 300; //ms
#endregion
#region //// Constructors //////
////////////////////////////////////////////////////////////////////////////
public StackPanel(Manager manager, Orientation orientation): base(manager)
{
this.orientation = orientation;
this.Color = Color.Transparent;
this.autoRefresh = true;
refreshTimer = new TimeSpan(0, 0, 0, 0, refreshTime);
}
////////////////////////////////////////////////////////////////////////////
#endregion
#region //// Methods ///////////
////////////////////////////////////////////////////////////////////////////
private void CalcLayout()
{
int top = Top;
int left = Left;
foreach (Control c in ClientArea.Controls)
{
Margins m = c.Margins;
if (orientation == Orientation.Vertical)
{
top += m.Top;
c.Top = top;
top += c.Height;
top += m.Bottom;
c.Left = left;
}
if (orientation == Orientation.Horizontal)
{
left += m.Left;
c.Left = left;
left += c.Width;
left += m.Right;
c.Top = top;
}
}
}
////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////
protected override void DrawControl(Renderer renderer, Rectangle rect, GameTime gameTime)
{
base.DrawControl(renderer, rect, gameTime);
}
////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////
protected override void OnResize(ResizeEventArgs e)
{
CalcLayout();
base.OnResize(e);
}
////////////////////////////////////////////////////////////////////////////
protected internal override void Update(GameTime gameTime)
{
base.Update(gameTime);
if (autoRefresh)
{
refreshTimer = refreshTimer.Subtract(TimeSpan.FromMilliseconds(gameTime.ElapsedGameTime.TotalMilliseconds));
if (refreshTimer.TotalMilliseconds <= 0.00)
{
Refresh();
refreshTimer = new TimeSpan(0, 0, 0, 0, refreshTime);
}
}
}
public override void Add(Control control)
{
base.Add(control);
if (autoRefresh) Refresh();
}
public override void Add(Control control, bool client)
{
base.Add(control, client);
if (autoRefresh) Refresh();
}
#endregion
}
}