-
Notifications
You must be signed in to change notification settings - Fork 1
/
Context.cs
453 lines (388 loc) · 14.9 KB
/
Context.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
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
namespace AntTweakBar
{
/// <summary>
/// An AntTweakBar context, which logically maps to a unique window.
/// </summary>
public sealed class Context : IEnumerable<Bar>, IDisposable
{
private static object lk = new object();
private static Int32 contextCounter = 0;
/// <summary>
/// Gets this context's unique identifier, used to switch between contexts.
/// </summary>
internal Int32 Identifier { get { ThrowIfDisposed(); return identifier; } }
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
private readonly Int32 identifier;
/// <summary>
/// Gets this context's last set window size. This is used for release/reset.
/// </summary>
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
private Size size;
/// <summary>
/// Checks whether a graphics API requires a device pointer.
/// </summary>
private static bool RequiresDevicePointer(Tw.GraphicsAPI api)
{
return ((api == Tw.GraphicsAPI.D3D9)
|| (api == Tw.GraphicsAPI.D3D10)
|| (api == Tw.GraphicsAPI.D3D11));
}
/// <summary>
/// Creates a new AntTweakBar context. If there are no other active contexts, a graphics API to use must be provided.
/// </summary>
/// <param name="graphicsAPI">The graphics API you need to share with AntTweakBar.</param>
/// <param name="device">For Direct3D interop only, a pointer to the D3D device.</param>
public Context(Tw.GraphicsAPI graphicsAPI = Tw.GraphicsAPI.Unspecified, IntPtr device = default(IntPtr))
{
if (RequiresDevicePointer(graphicsAPI) && (device == IntPtr.Zero)) {
throw new ArgumentException("A valid device pointer is required for Direct3D interop.");
}
lock (lk)
{
if (contextCounter == 0)
{
if (graphicsAPI == Tw.GraphicsAPI.Unspecified) {
throw new ArgumentException("A graphics API must be specified for the initial context.");
}
Tw.Init(graphicsAPI, device);
}
identifier = contextCounter++;
}
}
/// <summary>
/// Draws every bar in this context.
/// </summary>
public void Draw()
{
Tw.SetCurrentWindow(Identifier);
Tw.Draw(); // Applies to all bars
}
/// <summary>
/// Releases all graphics resources used by this context. They can be reset with <see cref="AntTweakBar.Context.ResetResources"/>.
/// </summary>
/// <remarks>
/// This does not affect the bars and variables in this context in any way.
/// </remarks>
public void ReleaseResources()
{
HandleResize(Size.Empty);
}
/// <summary>
/// Resets all graphics resources used by this context.
/// </summary>
public void ResetResources()
{
HandleResize(size);
}
#region Event Handlers
/// <summary>
/// Notifies this context of a change in window size.
/// </summary>
/// <param name="size">The new window size.</param>
public void HandleResize(Size size)
{
Tw.SetCurrentWindow(Identifier);
Tw.WindowSize(size.Width, size.Height);
if (size != Size.Empty) {
this.size = size;
}
}
/// <summary>
/// Notifies this context of a mouse movement.
/// </summary>
/// <param name="point">The new cursor location.</param>
public bool HandleMouseMove(Point point)
{
Tw.SetCurrentWindow(Identifier);
return Tw.MouseMotion(point.X, point.Y);
}
/// <summary>
/// Notifies this context of a mouse scroll.
/// </summary>
/// <param name="pos">The new mouse wheel position.</param>
public bool HandleMouseWheel(int pos)
{
Tw.SetCurrentWindow(Identifier);
return Tw.MouseWheel(pos);
}
/// <summary>
/// Notifies this context of a mouse click.
/// </summary>
/// <param name="action">The kind of mouse action.</param>
/// <param name="button">The mouse button pressed.</param>
public bool HandleMouseClick(Tw.MouseAction action, Tw.MouseButton button)
{
Tw.SetCurrentWindow(Identifier);
return Tw.MouseClick(action, button);
}
/* I don't know if this input handling code is correct. It is actually NOT straightforward to
* convert from conventional KeyDown/KeyPressed/KeyUp events to AntTweakBar key presses. This
* may or may not be correct and was inspired from TwEventSFML.cpp, if you know how to fix it
* please issue a pull request or a patch, until then it looks satisfactory. Kind of.
*/
private Tw.KeyModifiers lastModifiers;
private bool ignoreKeyPress;
/// <summary>
/// All the special keys supported by AntTweakBar.
/// </summary>
private static IList<Tw.Key> SpecialKeys = new List<Tw.Key>() {
Tw.Key.Escape, Tw.Key.Return, Tw.Key.Tab, Tw.Key.Backspace, Tw.Key.PageUp, Tw.Key.PageDown,
Tw.Key.Up, Tw.Key.Down, Tw.Key.Left, Tw.Key.Right, Tw.Key.End, Tw.Key.Home, Tw.Key.Insert,
Tw.Key.Delete, Tw.Key.Space, Tw.Key.F1, Tw.Key.F2, Tw.Key.F3, Tw.Key.F4, Tw.Key.F5,
Tw.Key.F6, Tw.Key.F7, Tw.Key.F8, Tw.Key.F9, Tw.Key.F10, Tw.Key.F11, Tw.Key.F12,
Tw.Key.F13, Tw.Key.F14, Tw.Key.F15,
};
/// <summary>
/// Notifies this context that a character has been typed.
/// </summary>
/// <param name="character">The character typed.</param>
public bool HandleKeyPress(char character)
{
Tw.SetCurrentWindow(Identifier);
if (!ignoreKeyPress) {
if (((character & 0xFF) < 32) && (character != 0) && ((character & 0xFF00) == 0)) {
return Tw.KeyPressed((char)((character & 0xFF) + 'a' - 1), Tw.KeyModifiers.Ctrl | lastModifiers);
} else { // this is supposed to handle the Ctrl+letter combination properly (somehow)
return Tw.KeyPressed((char)(character & 0xFF), Tw.KeyModifiers.None);
}
} else {
ignoreKeyPress = false;
}
return false;
}
/// <summary>
/// Notifies this context that a key has been pressed.
/// </summary>
/// <param name="key">The key pressed.</param>
/// <param name="modifiers">The key modifiers.</param>
public bool HandleKeyDown(Tw.Key key, Tw.KeyModifiers modifiers)
{
Tw.SetCurrentWindow(Identifier);
lastModifiers = modifiers;
if (SpecialKeys.Contains(key)) {
ignoreKeyPress = true; // special keys
return Tw.KeyPressed(key, modifiers);
}
if (modifiers.HasFlag(Tw.KeyModifiers.Alt)) {
if ((key >= Tw.Key.A) && (key <= Tw.Key.Z)) {
ignoreKeyPress = true; // normal key shortcuts?
if (modifiers.HasFlag(Tw.KeyModifiers.Shift)) {
key = (Tw.Key)('A' + key - Tw.Key.A);
return Tw.KeyPressed(key, modifiers);
} else {
key = (Tw.Key)('a' + key - Tw.Key.A);
return Tw.KeyPressed(key, modifiers);
}
}
}
return false;
}
/// <summary>
/// Notifies this context that a key has been released.
/// </summary>
/// <param name="key">The key released.</param>
/// <param name="modifiers">The key modifiers.</param>
public bool HandleKeyUp(Tw.Key key, Tw.KeyModifiers modifiers)
{
lastModifiers = Tw.KeyModifiers.None;
ignoreKeyPress = false;
return false;
}
/// <summary>
/// The SFML event handler.
/// </summary>
public bool EventHandlerSFML(IntPtr sfmlEvent, byte majorVersion, byte minorVersion)
{
Tw.SetCurrentWindow(Identifier);
return Tw.EventSFML(sfmlEvent, majorVersion, minorVersion);
}
/// <summary>
/// The SDL event handler.
/// </summary>
public bool EventHandlerSDL(IntPtr sdlEvent, byte majorVersion, byte minorVersion)
{
Tw.SetCurrentWindow(Identifier);
return Tw.EventSDL(sdlEvent, majorVersion, minorVersion);
}
/// <summary>
/// The Windows event handle (for use in WndProc).
/// </summary>
public bool EventHandlerWin(IntPtr wnd, int msg, IntPtr wParam, IntPtr lParam)
{
Tw.SetCurrentWindow(Identifier);
return Tw.EventWin(wnd, msg, wParam, lParam);
}
#endregion
#region Customization
/// <summary>
/// Shows or hides the help bar for this context.
/// </summary>
/// <param name="visible">Whether the help bar should be visible.</param>
public void ShowHelpBar(Boolean visible)
{
Tw.SetCurrentWindow(Identifier); // one help bar per context
Tw.Define("TW_HELP visible=" + (visible ? "true" : "false"));
}
/// <summary>
/// Sets whether to draw or clip overlapping bars.
/// </summary>
/// <param name="overlap">If false, clips the contents of overlapping region (default).</param>
public void SetOverlap(Boolean overlap)
{
Tw.SetCurrentWindow(Identifier); // applies to this context
Tw.Define("GLOBAL overlap=" + (overlap ? "true" : "false"));
}
/// <summary>
/// Sets the icon position for all bars.
/// </summary>
/// <param name="position">The icon position (default is bottom left).</param>
public void SetIconPosition(BarIconPosition position)
{
Tw.SetCurrentWindow(Identifier);
Tw.Define("GLOBAL iconpos=" + position.ToString().ToLower());
}
/// <summary>
/// Sets the icon alignment for all bars.
/// </summary>
/// <param name="alignment">The icon alignment (default is vertical).</param>
public void SetIconAlignment(BarIconAlignment alignment)
{
Tw.SetCurrentWindow(Identifier);
Tw.Define("GLOBAL iconalign=" + alignment.ToString().ToLower());
}
/// <summary>
/// Sets the icon margin for all bars.
/// </summary>
/// <param name="margin">The icon margin as an x-y offset in pixels.</param>
public void SetIconMargin(Size margin)
{
Tw.SetCurrentWindow(Identifier);
Tw.Define(String.Format("GLOBAL iconmargin='{0} {1}'", margin.Width, margin.Height));
}
/// <summary>
/// Sets the font size for all bars. Note the fixed style font is not resizable.
/// </summary>
/// <param name="fontSize">The font size to use (default is medium).</param>
public void SetFontSize(BarFontSize fontSize)
{
int index = 0;
switch (fontSize) {
case BarFontSize.Small:
index = 1;
break;
case BarFontSize.Medium:
index = 2;
break;
case BarFontSize.Large:
index = 3;
break;
}
Tw.SetCurrentWindow(Identifier);
Tw.Define("GLOBAL fontsize=" + index);
}
/// <summary>
/// Sets the font style for all bars.
/// </summary>
/// <param name="fontStyle">The font style to use.</param>
public void SetFontStyle(BarFontStyle fontStyle)
{
Tw.SetCurrentWindow(Identifier);
Tw.Define("GLOBAL fontstyle=" + (fontStyle == BarFontStyle.Default ? "default" : "fixed"));
}
/// <summary>
/// Sets whether the bar font can be resized by the user.
/// </summary>
/// <param name="resizable">Whether the font can be resized.</param>
public void SetFontResizable(Boolean resizable)
{
Tw.SetCurrentWindow(Identifier);
Tw.Define("GLOBAL fontresizable=" + (resizable ? "true" : "false"));
}
/// <summary>
/// Sets a font scaling coefficient. Must be called once before initializing the first context.
/// </summary>
/// <param name="scale">The font scale to use.</param>
public static void SetFontScaling(Double scale)
{
Tw.Define("GLOBAL fontscaling=" + scale);
}
#endregion
#region IEnumerable
private readonly ICollection<Bar> bars = new HashSet<Bar>();
internal void Add(Bar bar)
{
bars.Add(bar);
}
internal void Remove(Bar bar)
{
bars.Remove(bar);
}
/// <summary>
/// Removes all bars in this context.
/// </summary>
public void Clear()
{
ThrowIfDisposed();
while (bars.Any()) {
bars.First().Dispose();
}
}
public IEnumerator<Bar> GetEnumerator()
{
return bars.GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
#endregion
#region IDisposable
~Context()
{
Dispose(false);
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
private void Dispose(bool disposing)
{
if (!disposed)
{
while (disposing && bars.Any()) {
bars.First().Dispose();
}
ReleaseResources();
lock (lk)
{
if (--contextCounter == 0) {
Tw.Terminate();
}
}
disposed = true;
}
}
private bool disposed = false;
/// <summary>
/// Throws an ObjectDisposedException if this context has been disposed.
/// </summary>
private void ThrowIfDisposed()
{
if (disposed) {
throw new ObjectDisposedException(GetType().FullName);
}
}
#endregion
public override String ToString()
{
return String.Format("[Context: {0} bar(s)]", bars.Count);
}
}
}