-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathGUIContext.cs
547 lines (486 loc) · 15.9 KB
/
GUIContext.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
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
using System;
using System.Collections.Generic;
using OpenTK;
using OpenTK.Input;
namespace OpenTKGUI
{
/// <summary>
/// Context used to update controls. This context is not intended for any specific control and gives absolute positions and values
/// for all parameters.
/// </summary>
public abstract class GUIContext
{
/// <summary>
/// Gets or sets the control that currently has keyboard focus, or null if there is no such control.
/// </summary>
public abstract Control KeyboardFocus { get; set; }
/// <summary>
/// Gets or sets the control that currently has mouse focus, or null if there is no such control.
/// </summary>
public abstract Control MouseFocus { get; set; }
/// <summary>
/// Gets the state of the mouse, if possible.
/// </summary>
public abstract MouseState MouseState { get; }
/// <summary>
/// Gets the state of the keyboard, if possible.
/// </summary>
public abstract KeyboardState KeyboardState { get; }
/// <summary>
/// Creates a control context based on this context for a top-level control.
/// </summary>
public GUIControlContext CreateRootControlContext(Control Control, Point Offset)
{
return new GUIControlContext(this, Control, Offset);
}
}
/// <summary>
/// Context used to update a control. This control provides information specific to a single control.
/// </summary>
public class GUIControlContext
{
internal GUIControlContext(GUIContext Source, Control Control, Point Offset)
{
this._Source = Source;
this._Control = Control;
this._Offset = Offset;
}
/// <summary>
/// Gets the control this context is intended for.
/// </summary>
public Control Control
{
get
{
return this._Control;
}
}
/// <summary>
/// Gets the offset of this control from the root update context.
/// </summary>
public Point Offset
{
get
{
return this._Offset;
}
}
/// <summary>
/// Captures mouse focus.
/// </summary>
public void CaptureMouse()
{
this._Source.MouseFocus = this._Control;
}
/// <summary>
/// Releases mouse focus, if this control currently has it.
/// </summary>
public void ReleaseMouse()
{
this._Source.MouseFocus = null;
}
/// <summary>
/// Gets if this control has mouse focus.
/// </summary>
public bool HasMouse
{
get
{
return this._Source.MouseFocus == this._Control;
}
}
/// <summary>
/// Captures keyboard focus.
/// </summary>
public void CaptureKeyboard()
{
this._Source.KeyboardFocus = this._Control;
}
/// <summary>
/// Releases keyboard focus, if this control currently has it.
/// </summary>
public void ReleaseKeyboard()
{
this._Source.KeyboardFocus = null;
}
/// <summary>
/// Gets if this control has keyboard focus.
/// </summary>
public bool HasKeyboard
{
get
{
return this._Source.KeyboardFocus == this._Control;
}
}
/// <summary>
/// Searches for the closest ancestor of the specified type. Returns true on sucsess and false on failure.
/// </summary>
/// <param name="Offset">The current controls's offset from the ancestor.</param>
public bool FindAncestor<T>(out T Ancestor, out Point Offset)
where T : Control
{
GUIControlContext context;
if (this._FindAncestor<T>(out Ancestor, out context))
{
Offset = this._Offset - context._Offset;
return true;
}
Offset = new Point();
return false;
}
private bool _FindAncestor<T>(out T Ancestor, out GUIControlContext AncestorContext)
where T : Control
{
GUIControlContext parent = this._Parent;
if (parent == null)
{
Ancestor = null;
AncestorContext = null;
return false;
}
Ancestor = parent._Control as T;
if (Ancestor != null)
{
AncestorContext = parent;
return true;
}
return parent._FindAncestor<T>(out Ancestor, out AncestorContext);
}
/// <summary>
/// Gets the mouse state for the control.
/// </summary>
public MouseState MouseState
{
get
{
MouseState ms = this._Source.MouseState;
if (ms != null)
{
Point pos = ms.Position - this._Offset;
Control focus = this._Source.MouseFocus;
if (focus == this._Control)
{
return new _OffsetMouseState(this._Source.MouseState, pos);
}
if (!this._DisableMouse && focus == null)
{
if (this._Inside(ms.Position))
{
return new _OffsetMouseState(this._Source.MouseState, pos);
}
}
}
return null;
}
}
/// <summary>
/// Gets if the specified absolute point is inside the control.
/// </summary>
internal bool _Inside(Point Point)
{
if (new Rectangle(this._Control.Size).In(Point - this._Offset))
{
if (this._Parent != null)
{
return this._Parent._Inside(Point);
}
else
{
return true;
}
}
return false;
}
/// <summary>
/// Gets the keyboard state for the control.
/// </summary>
public KeyboardState KeyboardState
{
get
{
KeyboardState ks = this._Source.KeyboardState;
if (ks != null)
{
if (this._Source.KeyboardFocus == this._Control)
{
return ks;
}
}
return null;
}
}
/// <summary>
/// Gets a keyboard state for the control that doesn't require keyboard focus. This should not be
/// used for keyboard-intensive tasks such as typing as it may confuse the user. This would best be used
/// for modifiers to a mouse action.
/// </summary>
public KeyboardState SimpleKeyboardState
{
get
{
return this._Source.KeyboardState;
}
}
private class _OffsetMouseState : MouseState
{
public _OffsetMouseState(MouseState Source, Point Position)
{
this._Source = Source;
this._Position = Position;
}
public override Point Position
{
get
{
return this._Position;
}
}
public override bool IsButtonDown(MouseButton Button)
{
return this._Source.IsButtonDown(Button);
}
public override bool HasPushedButton(MouseButton Button)
{
return this._Source.HasPushedButton(Button);
}
public override bool HasReleasedButton(MouseButton Button)
{
return this._Source.HasReleasedButton(Button);
}
public override double Scroll
{
get
{
return this._Source.Scroll;
}
}
private Point _Position;
private MouseState _Source;
}
/// <summary>
/// Creates a control context for a child control of this control.
/// </summary>
/// <param name="Control">The child control.</param>
/// <param name="Offset">The offset of the child control.</param>
public GUIControlContext CreateChildContext(Control Control, Point Offset)
{
GUIControlContext cc = new GUIControlContext(this._Source, Control, this._Offset + Offset);
cc._DisableMouse = this._DisableMouse;
cc._Parent = this;
return cc;
}
/// <summary>
/// Creates a control context for a child control of this control. If DisableMouse is true, the child context will not
/// be able to access the mouse unless it has mouse focus.
/// </summary>
public GUIControlContext CreateChildContext(Control Control, Point Offset, bool DisableMouse)
{
GUIControlContext cc = new GUIControlContext(this._Source, Control, this._Offset + Offset);
cc._DisableMouse |= DisableMouse;
cc._Parent = this;
return cc;
}
private GUIContext _Source;
private GUIControlContext _Parent;
private bool _DisableMouse;
private Point _Offset;
private Control _Control;
}
/// <summary>
/// The state of the mouse at one time.
/// </summary>
public abstract class MouseState
{
/// <summary>
/// Gets the current position of the mouse.
/// </summary>
public abstract Point Position { get; }
/// <summary>
/// Gets if the specified mouse button is down.
/// </summary>
public abstract bool IsButtonDown(MouseButton Button);
/// <summary>
/// Gets if the mouse button has been pushed since the last update.
/// </summary>
public abstract bool HasPushedButton(MouseButton Button);
/// <summary>
/// Gets if the mouse button has been released since the last update.
/// </summary>
public abstract bool HasReleasedButton(MouseButton Button);
/// <summary>
/// Gets the amount (in "pixels") the mouse has scrolled since the last update. Positive numbers indicate
/// upward scrolling and negative numbers indicate downard scrolling.
/// </summary>
public virtual double Scroll
{
get
{
return 0.0;
}
}
}
/// <summary>
/// The state of the keyboard during an update, keeping track of changes since the last update.
/// </summary>
public abstract class KeyboardState
{
/// <summary>
/// Gets if the specified keyboard key is down.
/// </summary>
public abstract bool IsKeyDown(Key Key);
/// <summary>
/// Gets the keys events that were generated since the last update.
/// </summary>
public abstract IEnumerable<KeyEvent> Events { get; }
/// <summary>
/// Gets the characters that where typed since the last update.
/// </summary>
public abstract IEnumerable<char> Presses { get; }
}
/// <summary>
/// A type of event for a bottom.
/// </summary>
public enum ButtonEventType
{
/// <summary>
/// The button is pushed down.
/// </summary>
Down,
/// <summary>
/// The button is released.
/// </summary>
Up,
}
/// <summary>
/// An event for a key.
/// </summary>
public struct KeyEvent
{
public KeyEvent(Key Key, ButtonEventType Type)
{
this.Key = Key;
this.Type = Type;
}
/// <summary>
/// The key that is the subject of the event.
/// </summary>
public Key Key;
/// <summary>
/// The type of event this is.
/// </summary>
public ButtonEventType Type;
}
/// <summary>
/// A mouse state for a game window.
/// </summary>
public class WindowMouseState : MouseState
{
public WindowMouseState(GameWindow Window)
{
this._Device = Window.Mouse;
this._ButtonState = new bool[_ButtonAmount];
this._OldButtonState = new bool[_ButtonAmount];
}
/// <summary>
/// Updates the mouse state with new information produced by its associated device.
/// </summary>
public void Update()
{
this._OldWheel = this._NewWheel;
this._NewWheel = this._Device.WheelPrecise;
bool[] nbuttonstate = this._OldButtonState;
this._OldButtonState = this._ButtonState;
for (int t = 0; t < _ButtonAmount; t++)
{
nbuttonstate[t] = this._Device[(MouseButton)t];
}
this._ButtonState = nbuttonstate;
}
public override Point Position
{
get
{
return new Point(this._Device.X, this._Device.Y);
}
}
public override bool IsButtonDown(MouseButton Button)
{
return this._ButtonState[(int)Button];
}
public override bool HasPushedButton(MouseButton Button)
{
return !this._OldButtonState[(int)Button] && this._ButtonState[(int)Button];
}
public override bool HasReleasedButton(MouseButton Button)
{
return this._OldButtonState[(int)Button] && !this._ButtonState[(int)Button];
}
public override double Scroll
{
get
{
return (double)(this._NewWheel - this._OldWheel) * 10.0;
}
}
private const int _ButtonAmount = 12;
private MouseDevice _Device;
private float _OldWheel;
private float _NewWheel;
private bool[] _ButtonState;
private bool[] _OldButtonState;
}
/// <summary>
/// A keyboard state for a game window.
/// </summary>
public class WindowKeyboardState : KeyboardState
{
public WindowKeyboardState(GameWindow Window)
{
this._Device = Window.Keyboard;
this._KeyEvents = new List<KeyEvent>();
this._KeyPresses = new List<char>();
this._Device.KeyUp += delegate(object sender, KeyboardKeyEventArgs e)
{
this._KeyEvents.Add(new KeyEvent(e.Key, ButtonEventType.Up));
};
this._Device.KeyDown += delegate(object sender, KeyboardKeyEventArgs e)
{
this._KeyEvents.Add(new KeyEvent(e.Key, ButtonEventType.Down));
};
Window.KeyPress += delegate(object sender, KeyPressEventArgs e)
{
this._KeyPresses.Add(e.KeyChar);
};
}
/// <summary>
/// Updates the keyboard state by replacing old information used in an update.
/// </summary>
public void PostUpdate()
{
this._KeyEvents.Clear();
this._KeyPresses.Clear();
}
public override bool IsKeyDown(Key Key)
{
return this._Device[Key];
}
public override IEnumerable<KeyEvent> Events
{
get
{
return this._KeyEvents;
}
}
public override IEnumerable<char> Presses
{
get
{
return this._KeyPresses;
}
}
private List<KeyEvent> _KeyEvents;
private List<char> _KeyPresses;
private KeyboardDevice _Device;
}
}