CrabUI
Loading...
Searching...
No Matches
CUIFocusResolver.cs
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Reflection;
5using System.Diagnostics;
6
7using Barotrauma;
8using Microsoft.Xna.Framework;
9using Microsoft.Xna.Framework.Input;
10using Microsoft.Xna.Framework.Graphics;
11using System.IO;
12using EventInput;
13
14namespace CrabUI
15{
16 public class CUIFocusResolver
17 {
18 private CUIComponent focusedCUIComponent;
19 public CUIComponent FocusedCUIComponent
20 {
21 get => focusedCUIComponent;
22 set
23 {
24 CUIComponent oldFocused = focusedCUIComponent;
25 CUIComponent newFocused = value;
26
27 if (oldFocused == newFocused) return;
28
29 if (oldFocused != null)
30 {
31 oldFocused.Focused = false;
32 oldFocused.InvokeOnFocusLost();
33 }
34
35 if (newFocused != null)
36 {
37 newFocused.Focused = true;
38 newFocused.InvokeOnFocus();
39 }
40
41 if (oldFocused is IKeyboardSubscriber || newFocused is null)
42 {
43 OnVanillaIKeyboardSubscriberSet(null, true);
44 }
45
46 if (newFocused is IKeyboardSubscriber)
47 {
48 OnVanillaIKeyboardSubscriberSet((IKeyboardSubscriber)newFocused, true);
49 }
50
51 focusedCUIComponent = value;
52 }
53 }
54
55 public void OnVanillaIKeyboardSubscriberSet(IKeyboardSubscriber value, bool callFromCUI = false)
56 {
57 try
58 {
59 KeyboardDispatcher _ = GUI.KeyboardDispatcher;
60
61 IKeyboardSubscriber oldSubscriber = _._subscriber;
62 IKeyboardSubscriber newSubscriber = value;
63
64 if (newSubscriber == oldSubscriber) { return; }
65
66 // this case should be handled in CUI
67 if (!callFromCUI && oldSubscriber is CUIComponent && newSubscriber is null) { return; }
68
69 //CUI.Log($"new IKeyboardSubscriber {oldSubscriber} -> {newSubscriber}");
70
71 if (oldSubscriber != null)
72 {
73 TextInput.StopTextInput();
74 oldSubscriber.Selected = false;
75 }
76
77 if (oldSubscriber is CUIComponent component && newSubscriber is GUITextBox)
78 {
79 //TODO for some season TextInput doesn't loose focus here
80 component.InvokeOnFocusLost();
81 component.Focused = false;
82 focusedCUIComponent = null;
83 }
84
85 if (newSubscriber != null)
86 {
87 if (newSubscriber is GUITextBox box)
88 {
89 TextInput.SetTextInputRect(box.MouseRect);
90 TextInput.StartTextInput();
91 TextInput.SetTextInputRect(box.MouseRect);
92 }
93
94 if (newSubscriber is CUIComponent)
95 {
96 TextInput.StartTextInput();
97 }
98
99 newSubscriber.Selected = true;
100 }
101
102 _._subscriber = value;
103 }
104 catch (Exception e)
105 {
106 CUI.Error(e);
107 }
108 }
109
110
111
112 }
113}