-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathGLControl.cs
More file actions
591 lines (509 loc) · 14.5 KB
/
GLControl.cs
File metadata and controls
591 lines (509 loc) · 14.5 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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using OpenTK;
using OpenTK.Input;
namespace GLGUI
{
public abstract class GLControl
{
public virtual string Name { get; set; }
public GLGui Gui { get; internal set; }
public GLControl Parent { get; internal set; }
public IEnumerable<GLControl> Controls { get { return controls; } }
public Rectangle Outer { get { return outer; } set { if (outer != value) { outer = value; Invalidate(); } } }
public Rectangle Inner { get { return inner; } protected set { if (inner != value) { lastInner = inner; inner = value; DoResize(); /*Invalidate();*/ } } }
public Size SizeMin { get { return sizeMin; } set { sizeMin = value; Invalidate(); } }
public Size SizeMax { get { return sizeMax; } set { sizeMax = value; Invalidate(); } }
public bool HasFocus { get; private set; }
public bool IsDragged { get { return isDragged || controls.Any(c => c.IsDragged); } }
public GLAnchorStyles Anchor { get { return anchor; } set { anchor = value; Invalidate(); } }
public bool AutoSize { get { return autoSize; } set { autoSize = value; Invalidate(); } }
public virtual GLContextMenu ContextMenu { get { return contextMenu; } set { contextMenu = value; } }
public bool HandleMouseEvents { get { return handleMouseEvents; } set { handleMouseEvents = value; } }
// derived from above properties:
public Point Location { get { return outer.Location; } set { Outer = new Rectangle(value, outer.Size); } }
public Size Size { get { return outer.Size; } set { Outer = new Rectangle(outer.Location, value); } }
public int X { get { return outer.X; } }
public int Y { get { return outer.Y; } }
public int Width { get { return outer.Width; } }
public int Height { get { return outer.Height; } }
public Point InnerOffset { get { return inner.Location; } }
public Size InnerSize { get { return inner.Size; } }
public int InnerWidth { get { return inner.Width; } }
public int InnerHeight { get { return inner.Height; } }
public delegate void RenderEventHandler(object sender, double timeDelta);
public delegate bool KeyEventHandler(object sender, KeyboardKeyEventArgs e);
public delegate bool KeyPressEventHandler(object sender, KeyPressEventArgs e);
public event RenderEventHandler Render;
public event EventHandler<MouseMoveEventArgs> MouseMove;
public event EventHandler<MouseButtonEventArgs> MouseDown;
public event EventHandler<MouseButtonEventArgs> MouseUp;
public event EventHandler<MouseWheelEventArgs> MouseWheel;
public event EventHandler MouseEnter;
public event EventHandler MouseLeave;
public event KeyEventHandler KeyDown;
public event KeyEventHandler KeyUp;
public event KeyPressEventHandler KeyPress;
public event EventHandler Focus;
public event EventHandler FocusLost;
public event EventHandler Resize;
protected Rectangle outer;
protected Size sizeMin, sizeMax;
protected bool isDragged = false;
protected GLAnchorStyles anchor = GLAnchorStyles.Left | GLAnchorStyles.Top;
private Rectangle inner;
private Rectangle lastInner;
private bool autoSize = false;
private readonly List<GLControl> controls = new List<GLControl>();
private static int idCounter = 0;
private bool visited = false;
private GLContextMenu contextMenu;
private bool handleMouseEvents = true;
private GLControl hoverChild;
private GLControl focusedChild;
protected GLControl(GLGui gui)
{
Gui = gui;
Name = GetType().Name + (idCounter++);
outer = new Rectangle(0, 0, 0, 0);
inner = new Rectangle(0, 0, 0, 0);
sizeMin = new Size(0, 0);
sizeMax = new Size(int.MaxValue, int.MaxValue);
}
public void Invalidate()
{
if (visited || Gui == null || Gui.LayoutSuspended)
return;
visited = true;
UpdateLayout();
if (Parent != null && Parent.autoSize)
Parent.Invalidate();
visited = false;
}
protected virtual void UpdateLayout()
{
if (autoSize)
{
if (controls.Count > 0)
{
outer.Width = Controls.Max(c => c.Outer.Right);
outer.Height = Controls.Max(c => c.Outer.Bottom);
}
else
{
outer.Width = 0;
outer.Height = 0;
}
}
outer.Width = Math.Min(Math.Max(outer.Width, sizeMin.Width), sizeMax.Width);
outer.Height = Math.Min(Math.Max(outer.Height, sizeMin.Height), sizeMax.Height);
Inner = new Rectangle(0, 0, outer.Width, outer.Height);
}
public virtual T Add<T>(T control) where T : GLControl
{
if (control.Parent != null)
{
string message = string.Format("{0} {1} is already a child of {2} {3}.",
control.GetType().Name, control.Name,
control.Parent.GetType().Name, control.Parent.Name);
throw new ArgumentException(message);
}
if (control is GLForm || control is GLContextMenu)
controls.Insert(0, control);
else
controls.Add(control);
control.Gui = Gui;
control.Parent = this;
control.Invalidate();
return control;
}
public virtual void Remove(GLControl control)
{
if (control.Parent == null)
return;
if (control.Parent != this)
{
string message = string.Format("{0} {1} is a child of {2} {3}.",
control.GetType().Name, control.Name,
control.Parent.GetType().Name, control.Parent.Name);
throw new ArgumentException(message);
}
controls.Remove(control);
control.Gui = null;
control.Parent = null;
}
public virtual void Clear()
{
Gui.SuspendLayout();
foreach(GLControl control in controls.ToArray())
Remove(control);
Gui.ResumeLayout();
}
protected Point ToControl(Point p)
{
p.X -= outer.X;
p.Y -= outer.Y;
GLControl c = Parent;
while (c != c.Parent)
{
p.X -= c.inner.X + c.outer.X;
p.Y -= c.inner.Y + c.outer.Y;
c = c.Parent;
}
return p;
}
protected Point ToViewport(Point p)
{
p.X += outer.X;
p.Y += outer.Y;
GLControl c = Parent;
while (c != c.Parent)
{
p.X += c.inner.X + c.outer.X;
p.Y += c.inner.Y + c.outer.Y;
c = c.Parent;
}
return p;
}
protected Rectangle ToViewport(Rectangle r)
{
r.X += outer.X;
r.Y += outer.Y;
GLControl c = Parent;
while (c != c.Parent)
{
r.X += c.inner.X + c.outer.X;
r.Y += c.inner.Y + c.outer.Y;
c = c.Parent;
}
return r;
}
internal void DoRender(Point absolutePosition, double timeDelta)
{
absolutePosition.X += outer.X;
absolutePosition.Y += outer.Y;
if (Render != null)
{
GLDraw.ControlRect = new Rectangle(absolutePosition, outer.Size);
GLDraw.ScissorRect.Intersect(GLDraw.ControlRect);
if (GLDraw.ScissorRect.Width != 0 && GLDraw.ScissorRect.Height != 0)
Render(this, timeDelta);
}
if (controls.Count > 0)
{
absolutePosition.X += inner.X;
absolutePosition.Y += inner.Y;
GLDraw.ControlRect = new Rectangle(absolutePosition, inner.Size);
GLDraw.ScissorRect.Intersect(GLDraw.ControlRect);
if (GLDraw.ScissorRect.Width != 0 && GLDraw.ScissorRect.Height != 0)
{
Rectangle scissorRect = GLDraw.ScissorRect;
for (int i = controls.Count - 1; i >= 0; i--)
{
controls[i].DoRender(absolutePosition, timeDelta);
GLDraw.ScissorRect = scissorRect;
}
}
}
}
internal bool DoMouseMove(MouseMoveEventArgs e)
{
if (Parent == null)
return false;
if (!isDragged)
{
Point im = new Point(e.X - inner.X, e.Y - inner.Y);
if (hoverChild != null && hoverChild.IsDragged)
{
hoverChild.DoMouseMove(new MouseMoveEventArgs(im.X - hoverChild.Outer.X, im.Y - hoverChild.Outer.Y, e.XDelta, e.YDelta));
return true;
}
if(inner.Contains(e.Position))
{
foreach (GLControl control in controls)
{
if (control.Outer.Contains(im))
{
if (control.DoMouseMove(new MouseMoveEventArgs(im.X - control.Outer.X, im.Y - control.Outer.Y, e.XDelta, e.YDelta)))
{
if (hoverChild != control)
{
if (hoverChild != null)
hoverChild.DoMouseLeave();
hoverChild = control;
hoverChild.DoMouseEnter();
}
return true;
}
}
}
}
if (hoverChild != null)
{
hoverChild.DoMouseLeave();
hoverChild = null;
}
}
if (MouseMove != null)
{
MouseMove(this, e);
return true;
}
if (MouseEnter != null || MouseLeave != null) // force correct MouseEnter/Leave handling
return true;
return handleMouseEvents;
}
internal bool DoMouseDown(MouseButtonEventArgs e)
{
if (Parent == null)
return false;
if (!isDragged)
{
Point im = new Point(e.X - inner.X, e.Y - inner.Y);
if (!HasFocus)
{
HasFocus = true;
if (Focus != null)
Focus(this, EventArgs.Empty);
}
if (inner.Contains(e.Position))
{
int i = 0;
foreach(GLControl control in controls)
{
if (control.Outer.Contains(im))
{
bool handled = control.DoMouseDown(new MouseButtonEventArgs(im.X - control.Outer.X, im.Y - control.Outer.Y, e.Button, e.IsPressed));
if (control is GLForm)
{
GLControl tmp = controls[i]; // move to front
controls.RemoveAt(i);
controls.Insert(0, tmp);
}
if (handled)
{
if (control != focusedChild)
{
if (focusedChild != null)
focusedChild.DoFocusLost();
focusedChild = control;
}
return true;
}
}
i++;
}
}
}
bool handledHere = false;
if (MouseDown != null)
{
MouseDown(this, e);
handledHere = true;
}
if (contextMenu != null && e.Button == MouseButton.Right)
{
Gui.OpenContextMenu(contextMenu, ToViewport(e.Position));
handledHere = true;
}
return handledHere || handleMouseEvents;
}
internal bool DoMouseUp(MouseButtonEventArgs e)
{
if (Parent == null)
return false;
if (!isDragged)
{
Point im = new Point(e.X - inner.X, e.Y - inner.Y);
if (hoverChild != null && hoverChild.IsDragged)
{
hoverChild.DoMouseUp(new MouseButtonEventArgs(im.X - hoverChild.Outer.X, im.Y - hoverChild.Outer.Y, e.Button, e.IsPressed));
return true;
}
if (inner.Contains(e.Position))
{
foreach(GLControl control in controls)
{
if (control.Outer.Contains(im))
{
if(control.DoMouseUp(new MouseButtonEventArgs(im.X - control.Outer.X, im.Y - control.Outer.Y, e.Button, e.IsPressed)))
return true;
}
}
}
}
if (MouseUp != null)
{
MouseUp(this, e);
return true;
}
return handleMouseEvents;
}
internal bool DoMouseWheel(MouseWheelEventArgs e)
{
if (Parent == null)
return false;
if (!isDragged)
{
Point im = new Point(e.X - inner.X, e.Y - inner.Y);
if (inner.Contains(e.Position))
{
foreach(GLControl control in controls)
{
if (control.Outer.Contains(im))
{
if (control.DoMouseWheel(new MouseWheelEventArgs(im.X - control.Outer.X, im.Y - control.Outer.Y, e.Value, e.Delta)))
return true;
}
}
}
}
if (MouseWheel != null)
{
MouseWheel(this, e);
return true;
}
return handleMouseEvents;
}
internal void DoMouseEnter()
{
if (Parent == null)
return;
Gui.Cursor = GLCursor.Default;
if (MouseEnter != null)
MouseEnter(this, EventArgs.Empty);
}
internal void DoMouseLeave()
{
if (Parent == null)
return;
if (hoverChild != null)
{
hoverChild.DoMouseLeave();
hoverChild = null;
}
if (MouseLeave != null)
MouseLeave(this, EventArgs.Empty);
}
internal bool DoKeyUp(KeyboardKeyEventArgs e)
{
if (Parent == null)
return false;
if (!isDragged)
{
foreach(GLControl control in controls)
{
if (control.HasFocus)
{
if (control.DoKeyUp(e))
return true;
}
}
}
if (KeyUp != null)
return KeyUp(this, e);
return false;
}
internal bool DoKeyDown(KeyboardKeyEventArgs e)
{
if (Parent == null)
return false;
if (!isDragged)
{
foreach(GLControl control in controls)
{
if (control.HasFocus)
{
if (control.DoKeyDown(e))
return true;
}
}
}
if (KeyDown != null)
return KeyDown(this, e);
return false;
}
internal bool DoKeyPress(KeyPressEventArgs e)
{
if (Parent == null)
return false;
if (!isDragged)
{
foreach(GLControl control in controls)
{
if (control.HasFocus)
{
if (control.DoKeyPress(e))
return true;
}
}
}
if (KeyPress != null)
return KeyPress(this, e);
return false;
}
internal void DoFocusLost()
{
if (Parent == null)
return;
if (HasFocus)
{
HasFocus = false;
if (FocusLost != null)
FocusLost(this, EventArgs.Empty);
}
foreach (GLControl control in controls)
if (control.HasFocus)
control.DoFocusLost();
}
private int subPixelDx = 0, subPixelDy = 0;
internal void DoResize()
{
if (Parent == null)
return;
if (Resize != null)
Resize(this, EventArgs.Empty);
int dx = inner.Width - lastInner.Width;
int dy = inner.Height - lastInner.Height;
foreach (GLControl control in controls)
{
var a = control.Anchor;
var o = control.Outer;
int l = o.Left, r = o.Right, t = o.Top, b = o.Bottom;
if ((a & (GLAnchorStyles.Left | GLAnchorStyles.Right)) == 0)
{
dx += control.subPixelDx; control.subPixelDx = Math.Sign(dx) * (dx & 1);
l += dx / 2;
r += dx / 2;
}
else
{
if ((a & GLAnchorStyles.Left) == 0)
l += dx;
if ((a & GLAnchorStyles.Right) != 0)
r += dx;
}
if ((a & (GLAnchorStyles.Top | GLAnchorStyles.Bottom)) == 0)
{
dy += control.subPixelDy; control.subPixelDy = Math.Sign(dy) * (dy & 1);
t += dy / 2;
b += dy / 2;
}
else
{
if ((a & GLAnchorStyles.Top) == 0)
t += dy;
if ((a & GLAnchorStyles.Bottom) != 0)
b += dy;
}
control.Outer = new Rectangle(l, t, r - l, b - t);
}
}
public override string ToString()
{
return Name;
}
}
}