/
GuiDialog.cs
734 lines (624 loc) · 23.1 KB
/
GuiDialog.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
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
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
using System;
using System.Collections;
using System.Collections.Generic;
using Vintagestory.API.Common;
using Vintagestory.API.Datastructures;
using Vintagestory.API.MathTools;
namespace Vintagestory.API.Client
{
public abstract class GuiDialog : IDisposable
{
/// <summary>
/// Dialogue Composer for the GUIDialogue.
/// </summary>
public class DlgComposers : IEnumerable<KeyValuePair<string, GuiComposer>>
{
protected OrderedDictionary<string, GuiComposer> dialogComposers = new OrderedDictionary<string, GuiComposer>();
protected GuiDialog dialog;
/// <summary>
/// Returns all composers as a flat list
/// </summary>
public IEnumerable<GuiComposer> Values { get { return dialogComposers.Values; } }
/// <summary>
/// Constructor.
/// </summary>
/// <param name="dialog">The dialogue this composer belongs to.</param>
public DlgComposers(GuiDialog dialog)
{
this.dialog = dialog;
}
/// <summary>
/// Cleans up and clears the composers.
/// </summary>
public void ClearComposers()
{
foreach (var val in dialogComposers)
{
val.Value?.Dispose();
}
dialogComposers.Clear();
}
/// <summary>
/// Clean disposal method.
/// </summary>
public void Dispose()
{
foreach (var val in dialogComposers)
{
val.Value?.Dispose();
}
}
/// <summary>
/// Returns the composer for given composer name
/// </summary>
/// <param name="key"></param>
/// <returns></returns>
public GuiComposer this[string key]
{
get {
dialogComposers.TryGetValue(key, out GuiComposer val);
return val;
}
set {
dialogComposers[key] = value;
value.OnFocusChanged = dialog.OnFocusChanged;
}
}
IEnumerator IEnumerable.GetEnumerator()
{
return dialogComposers.GetEnumerator();
}
IEnumerator<KeyValuePair<string, GuiComposer>> IEnumerable<KeyValuePair<string, GuiComposer>>.GetEnumerator()
{
return dialogComposers.GetEnumerator();
}
/// <summary>
/// Checks to see if the key is located within the given dialogue composer.
/// </summary>
/// <param name="key">The key you are searching for.</param>
/// <returns>Do we have your key?</returns>
public bool ContainsKey(string key)
{
return dialogComposers.ContainsKey(key);
}
/// <summary>
/// Removes the given key and the corresponding value from the Dialogue Composer.
/// </summary>
/// <param name="key">The Key to remove.</param>
public void Remove(string key)
{
dialogComposers.Remove(key);
}
}
/// <summary>
/// The Instance of Dialogue Composer for this GUIDialogue.
/// </summary>
public DlgComposers Composers;
/// <summary>
/// A single composer for this GUIDialogue.
/// </summary>
public GuiComposer SingleComposer
{
get { return Composers["single"]; }
set { Composers["single"] = value; }
}
/// <summary>
/// Debug name. For debugging purposes.
/// </summary>
public virtual string DebugName
{
get { return GetType().Name; }
}
/// <summary>
/// The amount of depth required for this dialog. Default is 150. Required for correct z-ordering of dialogs.
/// </summary>
public virtual float ZSize => 150;
// First comes KeyDown event, opens the gui, then comes KeyPress event - this one we have to ignore
public bool ignoreNextKeyPress = false;
protected bool opened;
protected bool focused;
/// <summary>
/// Is the dialogue currently in focus?
/// </summary>
public virtual bool Focused { get { return focused; } }
/// <summary>
/// Can this dialog be focused?
/// </summary>
public virtual bool Focusable => true;
/// <summary>
/// Is this dialogue a dialogue or a HUD object?
/// </summary>
public virtual EnumDialogType DialogType { get { return EnumDialogType.Dialog; } }
/// <summary>
/// The event fired when this dialogue is opened.
/// </summary>
public event Action OnOpened;
/// <summary>
/// The event fired when this dialogue is closed.
/// </summary>
public event Action OnClosed;
protected ICoreClientAPI capi;
protected virtual void OnFocusChanged(bool on)
{
if (on == focused) return;
if (DialogType == EnumDialogType.Dialog && !opened) return;
if (on)
{
capi.Gui.RequestFocus(this);
} else
{
focused = false;
}
}
/// <summary>
/// Constructor for the GUIDialogue.
/// </summary>
/// <param name="capi">The Client API.</param>
public GuiDialog(ICoreClientAPI capi)
{
Composers = new DlgComposers(this);
this.capi = capi;
}
/// <summary>
/// Makes this gui pop up once a pre-set given key combination is set.
/// </summary>
public virtual void OnBlockTexturesLoaded()
{
string keyCombCode = ToggleKeyCombinationCode;
if (keyCombCode != null)
{
capi.Input.SetHotKeyHandler(keyCombCode, OnKeyCombinationToggle);
}
}
/// <summary>
///
/// </summary>
public virtual void OnLevelFinalize()
{
}
public virtual void OnOwnPlayerDataReceived() { }
/// <summary>
/// 0 = draw first, 1 = draw last. Used to enforce tooltips and held itemstack always drawn last to be visible.<br/>
/// Vanilla dialogs draw order:<br/>
/// Name tags: -0.1<br/>
/// Chat dialog: 0<br/>
/// Block Interaction help: 0.05<br/>
/// Worldmap HUD: 0.07<br/>
/// Default value for most other dialogs: 0.1<br/>
/// Worldmap Dialog: 0.11<br/>
/// Player and Chest inventories: 0.2<br/>
/// Various config/edit dialogs: 0.2<br/>
/// Handbook: 0.2<br/>
/// Escape menu: 0.89
/// </summary>
public virtual double DrawOrder { get { return 0.1; } }
/// <summary>
/// Determines the order on which dialog receives keyboard input first when the dialog is opened. 0 = handle inputs first, 9999 = handle inputs last.<br/>
/// Reference list:<br/>
/// 0: Escape menu<br/>
/// 0.5 (default): tick profiler, selection box editor, macro editor, survival&creative inventory, first launch info dialog, dead dialog, character dialog, etc.<br/>
/// 1: hotbar<br/>
/// 1.1: chat dialog
/// </summary>
public virtual double InputOrder { get { return 0.5; } }
/// <summary>
/// Should this dialogue de-register itself once it's closed? (Defaults to no)
/// </summary>
public virtual bool UnregisterOnClose { get { return false; } }
/// <summary>
/// Fires when the GUI is opened.
/// </summary>
public virtual void OnGuiOpened() {
}
/// <summary>
/// Fires when the GUI is closed.
/// </summary>
public virtual void OnGuiClosed() {
}
/// <summary>
/// Attempts to open this dialogue.
/// </summary>
/// <returns>Was this dialogue successfully opened?</returns>
public virtual bool TryOpen()
{
return TryOpen(true);
}
public virtual bool TryOpen(bool withFocus)
{
bool wasOpened = opened;
if (!capi.Gui.LoadedGuis.Contains(this))
{
capi.Gui.RegisterDialog(this);
}
opened = true;
if (DialogType == EnumDialogType.Dialog && withFocus)
{
capi.Gui.RequestFocus(this);
}
if (!wasOpened)
{
OnGuiOpened();
OnOpened?.Invoke();
capi.Gui.TriggerDialogOpened(this);
}
return true;
}
/// <summary>
/// Attempts to close this dialogue- triggering the OnCloseDialogue event.
/// </summary>
/// <returns>Was this dialogue successfully closed?</returns>
public virtual bool TryClose()
{
opened = false;
UnFocus();
OnGuiClosed();
OnClosed?.Invoke();
focused = false;
capi.Gui.TriggerDialogClosed(this);
return true;
}
/// <summary>
/// Unfocuses the dialogue.
/// </summary>
public virtual void UnFocus() {
focused = false;
}
/// <summary>
/// Focuses the dialog
/// </summary>
public virtual void Focus() {
if (!Focusable) return;
focused = true;
}
/// <summary>
/// If the dialogue is opened, this attempts to close it. If the dialogue is closed, this attempts to open it.
/// </summary>
public virtual void Toggle()
{
if (IsOpened())
{
TryClose();
} else
{
TryOpen();
}
}
/// <summary>
/// Is this dialogue opened?
/// </summary>
/// <returns>Whether this dialogue is opened or not.</returns>
public virtual bool IsOpened()
{
return opened;
}
/// <summary>
/// Is this dialogue opened in the given context?
/// </summary>
/// <param name="dialogComposerName">The composer context.</param>
/// <returns>Whether this dialogue was opened or not within the given context.</returns>
public virtual bool IsOpened(string dialogComposerName)
{
return IsOpened();
}
/// <summary>
/// This runs before the render. Local update method.
/// </summary>
/// <param name="deltaTime">The time that has elapsed.</param>
public virtual void OnBeforeRenderFrame3D(float deltaTime)
{
}
public string MouseOverCursor;
/// <summary>
/// This runs when the dialogue is ready to render all of the components.
/// </summary>
/// <param name="deltaTime">The time that has elapsed.</param>
public virtual void OnRenderGUI(float deltaTime)
{
foreach (var val in Composers)
{
val.Value.Render(deltaTime);
MouseOverCursor = val.Value.MouseOverCursor;
}
}
/// <summary>
/// This runs when the dialogue is finalizing and cleaning up all of the components.
/// </summary>
/// <param name="dt">The time that has elapsed.</param>
public virtual void OnFinalizeFrame(float dt)
{
foreach (var val in Composers)
{
val.Value.PostRender(dt);
}
}
internal virtual bool OnKeyCombinationToggle(KeyCombination viaKeyComb)
{
HotKey hotkey = capi.Input.GetHotKeyByCode(ToggleKeyCombinationCode);
if (hotkey == null) return false;
if (hotkey.KeyCombinationType == HotkeyType.CreativeTool && capi.World.Player.WorldData.CurrentGameMode != EnumGameMode.Creative) return false;
Toggle();
/*if (!viaKeyComb.Alt && !viaKeyComb.Ctrl && !viaKeyComb.Shift && viaKeyComb.KeyCode > 66)
{
ignoreNextKeyPress = true;
}*/
return true;
}
/// <summary>
/// Fires when keys are held down.
/// </summary>
/// <param name="args">The key or keys that were held down.</param>
public virtual void OnKeyDown(KeyEvent args)
{
foreach (GuiComposer composer in Composers.Values)
{
composer.OnKeyDown(args, focused);
if (args.Handled)
{
return;
}
}
HotKey hotkey = capi.Input.GetHotKeyByCode(ToggleKeyCombinationCode);
if (hotkey == null) return;
bool toggleKeyPressed = hotkey.DidPress(args, capi.World, capi.World.Player, true);
if (toggleKeyPressed && TryClose())
{
args.Handled = true;
return;
}
}
/// <summary>
/// Fires when the keys are pressed.
/// </summary>
/// <param name="args">The key or keys that were pressed.</param>
public virtual void OnKeyPress(KeyEvent args)
{
if (ignoreNextKeyPress)
{
ignoreNextKeyPress = false;
args.Handled = true;
return;
}
if (args.Handled) return;
foreach (GuiComposer composer in Composers.Values)
{
composer.OnKeyPress(args);
if (args.Handled) return;
}
}
/// <summary>
/// Fires when the keys are released.
/// </summary>
/// <param name="args">the key or keys that were released.</param>
public virtual void OnKeyUp(KeyEvent args) {
foreach (GuiComposer composer in Composers.Values)
{
composer.OnKeyUp(args);
if (args.Handled)
{
return;
}
}
}
/// <summary>
/// Fires explicitly when the Escape key is pressed and attempts to close the dialogue.
/// </summary>
/// <returns>Whether the dialogue was closed.</returns>
public virtual bool OnEscapePressed()
{
if (DialogType == EnumDialogType.HUD) return false;
return TryClose();
}
/// <summary>
/// Fires when the mouse enters the given slot.
/// </summary>
/// <param name="slot">The slot the mouse entered.</param>
/// <returns>Whether this event was handled.</returns>
public virtual bool OnMouseEnterSlot(ItemSlot slot) {
foreach (GuiComposer composer in Composers.Values)
{
if (composer.OnMouseEnterSlot(slot))
{
return true;
}
}
return false;
}
/// <summary>
/// Fires when the mouse leaves the slot.
/// </summary>
/// <param name="itemSlot">The slot the mouse entered.</param>
/// <returns>Whether this event was handled.</returns>
public virtual bool OnMouseLeaveSlot(ItemSlot itemSlot) {
foreach (GuiComposer composer in Composers.Values)
{
if (composer.OnMouseLeaveSlot(itemSlot))
{
return true;
}
}
return false;
}
/// <summary>
/// Fires when the mouse clicks within the slot.
/// </summary>
/// <param name="itemSlot">The slot that the mouse clicked in.</param>
/// <returns>Whether this event was handled.</returns>
public virtual bool OnMouseClickSlot(ItemSlot itemSlot) { return false; }
/// <summary>
/// Fires when a mouse button is held down.
/// </summary>
/// <param name="args">The mouse button or buttons in question.</param>
public virtual void OnMouseDown(MouseEvent args)
{
if (args.Handled) return;
foreach (GuiComposer composer in Composers.Values)
{
composer.OnMouseDown(args);
if (args.Handled)
{
return;
}
}
if (!IsOpened()) return;
foreach (GuiComposer composer in Composers.Values)
{
if (composer.Bounds.PointInside(args.X, args.Y))
{
args.Handled = true;
return;
}
}
}
/// <summary>
/// Fires when a mouse button is released.
/// </summary>
/// <param name="args">The mouse button or buttons in question.</param>
public virtual void OnMouseUp(MouseEvent args)
{
if (args.Handled) return;
foreach (GuiComposer composer in Composers.Values)
{
composer.OnMouseUp(args);
if (args.Handled) return;
}
foreach (GuiComposer composer in Composers.Values)
{
if (composer.Bounds.PointInside(args.X, args.Y))
{
args.Handled = true;
return;
}
}
}
/// <summary>
/// Fires when the mouse is moved.
/// </summary>
/// <param name="args">The mouse movements in question.</param>
public virtual void OnMouseMove(MouseEvent args)
{
if (args.Handled) return;
foreach (GuiComposer composer in Composers.Values)
{
composer.OnMouseMove(args);
if (args.Handled) return;
}
foreach (GuiComposer composer in Composers.Values)
{
if (composer.Bounds.PointInside(args.X, args.Y))
{
args.Handled = true;
break;
}
}
}
/// <summary>
/// Fires when the mouse wheel is scrolled.
/// </summary>
/// <param name="args"></param>
public virtual void OnMouseWheel(MouseWheelEventArgs args)
{
foreach (GuiComposer composer in Composers.Values)
{
composer.OnMouseWheel(args);
if (args.IsHandled) return;
}
if (focused)
{
foreach (GuiComposer composer in Composers.Values)
{
if (composer.Bounds.PointInside(capi.Input.MouseX, capi.Input.MouseY))
{
args.SetHandled(true);
}
}
}
}
/// <summary>
/// A check for whether the dialogue should recieve Render events.
/// </summary>
/// <returns>Whether the dialogue is opened or not.</returns>
public virtual bool ShouldReceiveRenderEvents()
{
return opened;
}
/// <summary>
/// A check for whether the dialogue should recieve keyboard events.
/// </summary>
/// <returns>Whether the dialogue is focused or not.</returns>
public virtual bool ShouldReceiveKeyboardEvents()
{
return focused;
}
/// <summary>
/// A check if the dialogue should recieve mouse events.
/// </summary>
/// <returns>Whether the mouse events should fire.</returns>
public virtual bool ShouldReceiveMouseEvents()
{
return IsOpened();
}
/// <summary>
/// Gets whether it is preferred for the mouse to be not grabbed while this dialog is opened.
/// If true (default), the Alt button needs to be held to manually grab the mouse.
/// </summary>
#pragma warning disable 0618
public virtual bool PrefersUngrabbedMouse =>
RequiresUngrabbedMouse();
#pragma warning restore 0618
[Obsolete("Use PrefersUngrabbedMouse instead")]
public virtual bool RequiresUngrabbedMouse()
{
return true;
}
/// <summary>
/// Gets whether ability to grab the mouse cursor is disabled while
/// this dialog is opened. For example, the escape menu. (Default: false)
/// </summary>
public virtual bool DisableMouseGrab => false;
// If true and gui element is opened then all keystrokes (except escape) are only received by this gui element
/// <summary>
/// Should this dialog (e.g. textbox) capture all the keyboard events except for escape.
/// </summary>
/// <returns></returns>
public virtual bool CaptureAllInputs()
{
return false;
}
/// <summary>
/// Should this dialog capture the raw mouse button clicks - useful for example for the settings menu itself (in case the user has unset them or set them to something crazy)
/// </summary>
/// <returns></returns>
public virtual bool CaptureRawMouse()
{
return false;
}
/// <summary>
/// Disposes the Dialog.
/// </summary>
public virtual void Dispose() {
Composers?.Dispose();
}
/// <summary>
/// Clears the composers.
/// </summary>
public void ClearComposers()
{
Composers?.ClearComposers();
}
/// <summary>
/// The key combination string that toggles this GUI object.
/// </summary>
public abstract string ToggleKeyCombinationCode { get; }
/// <summary>
/// Checks if the player is in range (pickingrange) of the given position
/// </summary>
/// <param name="pos"></param>
/// <returns>In range or no?</returns>
public virtual bool IsInRangeOf(Vec3d pos)
{
var playerEye = capi.World.Player.Entity.Pos.XYZ.Add(capi.World.Player.Entity.LocalEyePos);
double dist = pos.DistanceTo(playerEye);
return dist <= capi.World.Player.WorldData.PickingRange + 0.5;
}
}
}