-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathGLForm.cs
More file actions
292 lines (267 loc) · 10.1 KB
/
GLForm.cs
File metadata and controls
292 lines (267 loc) · 10.1 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
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
using System;
using System.Drawing;
using System.Linq;
using OpenTK.Input;
namespace GLGUI
{
public class GLForm : GLControl
{
public string Title { get { return title; } set { if (value != title) { title = value; Invalidate(); } } }
public bool Maximized { get { return maximized; } set { Maximize(value); } }
public GLSkin.GLFormSkin SkinActive { get { return skinActive; } set { skinActive = value; Invalidate(); } }
public GLSkin.GLFormSkin SkinInactive { get { return skinInactive; } set { skinInactive = value; Invalidate(); } }
private string title = "";
private GLFontText titleProcessed = new GLFontText();
private GLSkin.GLFormSkin skinActive, skinInactive;
private GLSkin.GLFormSkin skin;
private enum DragOperation { Move = 0, ResizeNW, ResizeN, ResizeNE, ResizeE, ResizeSE, ResizeS, ResizeSW, ResizeW }
private DragOperation dragOp;
private Rectangle moveClickRegion;
private Point mouseOffset;
private SizeF titleSize;
private int minHeight;
private DateTime lastClick;
private static GLCursor[] dragOpCursors = new GLCursor[]
{
GLCursor.SizeAll,
GLCursor.SizeNWSE, GLCursor.SizeNS, GLCursor.SizeNESW, GLCursor.SizeWE,
GLCursor.SizeNWSE, GLCursor.SizeNS, GLCursor.SizeNESW, GLCursor.SizeWE
};
public GLForm(GLGui gui) : base(gui)
{
Render += OnRender;
MouseDown += OnMouseDown;
MouseUp += OnMouseUp;
MouseMove += OnMouseMove;
MouseLeave += OnMouseLeave;
//MouseDoubleClick += OnMouseDoubleClick;
Focus += (s, e) => Invalidate();
FocusLost += (s, e) => Invalidate();
skinActive = Gui.Skin.FormActive;
skinInactive = Gui.Skin.FormInactive;
outer = new Rectangle(0, 0, 100, 100);
sizeMin = new Size(64, 32);
sizeMax = new Size(int.MaxValue, int.MaxValue);
}
protected override void UpdateLayout()
{
skin = HasFocus ? skinActive : skinInactive;
if (AutoSize)
{
if (Controls.Count() > 0)
{
outer.Width = Controls.Max(c => c.Outer.Right) + skin.Border.Horizontal;
outer.Height = Controls.Max(c => c.Outer.Bottom) + skin.Border.Vertical + (int)titleSize.Height + skin.Border.Top;
}
else
{
outer.Width = 0;
outer.Height = 0;
}
}
outer.Width = Math.Min(Math.Max(outer.Width, sizeMin.Width), sizeMax.Width);
int innerWidth = outer.Width - skin.Border.Horizontal;
titleSize = skin.Font.ProcessText(titleProcessed, title, GLFontAlignment.Left);
minHeight = Math.Max(sizeMin.Height, (int)titleSize.Height + skin.Border.Vertical + skin.Border.Top);
outer.Height = Math.Min(Math.Max(outer.Height, minHeight), sizeMax.Height);
if (Parent != null)
{
outer.X = Math.Max(Math.Min(outer.X, Parent.Inner.Width - outer.Width), 0);
outer.Y = Math.Max(Math.Min(outer.Y, Parent.Inner.Height - outer.Height), 0);
outer.Width = Math.Min(outer.Right, Parent.Inner.Width) - outer.X;
outer.Height = Math.Min(outer.Bottom, Parent.Inner.Height) - outer.Y;
}
Inner = new Rectangle(
skin.Border.Left, skin.Border.Top + (int)titleSize.Height + skin.Border.Top,
innerWidth, outer.Height - skin.Border.Vertical - (int)titleSize.Height - skin.Border.Top);
moveClickRegion = new Rectangle(skin.Border.Left, skin.Border.Top, Inner.Width, (int)titleSize.Height);
}
private void OnRender(object sender, double timeDelta)
{
GLDraw.Fill(ref skin.BorderColor);
GLDraw.FillRect(Inner, ref skin.BackgroundColor);
GLDraw.Text(titleProcessed, ref moveClickRegion, ref skin.Color);
}
private void StartDragOperation(DragOperation op, Point p)
{
if ((AutoSize || maximized) && op != DragOperation.Move)
return;
mouseOffset = p;
isDragged = true;
dragOp = op;
Gui.Cursor = dragOpCursors[(int)op];
}
private void OnMouseDown(object sender, MouseButtonEventArgs e)
{
if (e.Button == MouseButton.Left)
{
if (moveClickRegion.Contains(e.Position))
StartDragOperation (DragOperation.Move, e.Position);
else if(e.X < skin.Border.Left)
{
if (e.Y < skin.Border.Top)
StartDragOperation (DragOperation.ResizeNW, e.Position);
else if (e.Y >= outer.Height - skin.Border.Bottom)
StartDragOperation (DragOperation.ResizeSW, e.Position);
else
StartDragOperation (DragOperation.ResizeW, e.Position);
}
else if (e.X >= outer.Width - skin.Border.Right)
{
if (e.Y < skin.Border.Top)
StartDragOperation (DragOperation.ResizeNE, e.Position);
else if (e.Y >= outer.Height - skin.Border.Bottom)
StartDragOperation (DragOperation.ResizeSE, e.Position);
else
StartDragOperation (DragOperation.ResizeE, e.Position);
}
else if (e.Y < skin.Border.Top)
StartDragOperation (DragOperation.ResizeN, e.Position);
else if (e.Y >= outer.Height - skin.Border.Bottom)
StartDragOperation (DragOperation.ResizeS, e.Position);
}
justDoubleClicked = false;
}
private void OnMouseUp(object sender, MouseButtonEventArgs e)
{
if (e.Button == MouseButton.Left)
{
if (isDragged)
{
isDragged = false;
Gui.Cursor = GLCursor.Default;
}
var now = DateTime.Now;
if ((now - lastClick).TotalMilliseconds < 500.0)
OnMouseDoubleClick(this, e);
lastClick = now;
}
}
private void OnMouseMove(object sender, MouseEventArgs e)
{
if (isDragged)
{
Point p = e.Position;
if (Parent != null)
{
p.X = Math.Min(Math.Max(p.X + outer.X, 0), Parent.Inner.Width) - outer.X;
p.Y = Math.Min(Math.Max(p.Y + outer.Y, 0), Parent.Inner.Height) - outer.Y;
}
int dx = p.X - mouseOffset.X;
int dy = p.Y - mouseOffset.Y;
switch (dragOp)
{
case DragOperation.Move:
if (maximized && !justDoubleClicked)
{
if (Math.Max(dx, dy) > 16)
{
mouseOffset.X = restoreOuter.Width / 2;
Maximize(false);
}
}
else
Outer = new Rectangle(outer.X + dx, outer.Y + dy, outer.Width, outer.Height);
return;
case DragOperation.ResizeNW:
dx = outer.Width - Math.Min(Math.Max(outer.Width - dx, sizeMin.Width), sizeMax.Width);
dy = outer.Height - Math.Min(Math.Max(outer.Height - dy, minHeight), sizeMax.Height);
Outer = new Rectangle(outer.X + dx, outer.Y + dy, outer.Width - dx, outer.Height - dy);
return;
case DragOperation.ResizeN:
dy = outer.Height - Math.Min(Math.Max(outer.Height - dy, minHeight), sizeMax.Height);
Outer = new Rectangle(outer.X, outer.Y + dy, outer.Width, outer.Height - dy);
return;
case DragOperation.ResizeNE:
dx = Math.Min(Math.Max(p.X, sizeMin.Width), sizeMax.Width);
dy = outer.Height - Math.Min(Math.Max(outer.Height - dy, minHeight), sizeMax.Height);
Outer = new Rectangle(outer.X, outer.Y + dy, dx, outer.Height - dy);
return;
case DragOperation.ResizeE:
dx = Math.Min(Math.Max(p.X, sizeMin.Width), sizeMax.Width);
Outer = new Rectangle(outer.X, outer.Y, dx, outer.Height);
return;
case DragOperation.ResizeSE:
dx = Math.Min(Math.Max(p.X, sizeMin.Width), sizeMax.Width);
dy = Math.Min(Math.Max(p.Y, minHeight), sizeMax.Height);
Outer = new Rectangle(outer.X, outer.Y, dx, dy);
return;
case DragOperation.ResizeS:
dy = Math.Min(Math.Max(p.Y, minHeight), sizeMax.Height);
Outer = new Rectangle(outer.X, outer.Y, outer.Width, dy);
return;
case DragOperation.ResizeSW:
dx = outer.Width - Math.Min(Math.Max(outer.Width - dx, sizeMin.Width), sizeMax.Width);
dy = Math.Min(Math.Max(p.Y, minHeight), sizeMax.Height);
Outer = new Rectangle(outer.X + dx, outer.Y, outer.Width - dx, dy);
return;
case DragOperation.ResizeW:
dx = outer.Width - Math.Min(Math.Max(outer.Width - dx, sizeMin.Width), sizeMax.Width);
Outer = new Rectangle(outer.X + dx, outer.Y, outer.Width - dx, outer.Height);
return;
}
}
if (!AutoSize && !maximized)
{
if (e.X < skin.Border.Left)
{
if (e.Y < skin.Border.Top)
Gui.Cursor = GLCursor.SizeNWSE;
else if (e.Y >= outer.Height - skin.Border.Bottom)
Gui.Cursor = GLCursor.SizeNESW;
else
Gui.Cursor = GLCursor.SizeWE;
}
else if (e.X >= Outer.Width - skin.Border.Right)
{
if (e.Y < skin.Border.Top)
Gui.Cursor = GLCursor.SizeNESW;
else if (e.Y >= outer.Height - skin.Border.Bottom)
Gui.Cursor = GLCursor.SizeNWSE;
else
Gui.Cursor = GLCursor.SizeWE;
}
else if (e.Y < skin.Border.Top || e.Y >= outer.Height - skin.Border.Bottom)
Gui.Cursor = GLCursor.SizeNS;
else
Gui.Cursor = GLCursor.Default;
}
}
private void OnMouseLeave(object sender, EventArgs e)
{
Gui.Cursor = GLCursor.Default;
}
private bool justDoubleClicked = false;
private bool maximized = false;
private Rectangle restoreOuter;
private GLAnchorStyles restoreAnchor;
private void Maximize(bool maximize)
{
if (maximized == maximize)
return;
if (!maximize) // restore
{
maximized = false;
anchor = restoreAnchor;
outer = restoreOuter;
Invalidate();
}
else // maximize
{
maximized = true;
restoreOuter = outer;
restoreAnchor = anchor;
outer = Parent.Inner;
anchor = GLAnchorStyles.Left | GLAnchorStyles.Top | GLAnchorStyles.Right | GLAnchorStyles.Bottom;
Invalidate();
}
}
private void OnMouseDoubleClick(object sender, MouseEventArgs e)
{
if (!AutoSize && Parent != null && moveClickRegion.Contains(e.Position))
Maximize(!maximized);
Gui.Cursor = GLCursor.Default; // hack to avoid move operation cursor
justDoubleClicked = true;
}
}
}