forked from Unity-Technologies/UnityCsReference
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFocusController.cs
More file actions
222 lines (182 loc) · 6.68 KB
/
Copy pathFocusController.cs
File metadata and controls
222 lines (182 loc) · 6.68 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
// Unity C# reference source
// Copyright (c) Unity Technologies. For terms of use, see
// https://unity3d.com/legal/licenses/Unity_Reference_Only_License
using System;
using System.Collections.Generic;
namespace UnityEngine.Experimental.UIElements
{
public abstract class Focusable : CallbackEventHandler
{
protected Focusable()
{
m_FocusIndex = 0;
}
public abstract FocusController focusController { get; }
// See http://w3c.github.io/html/editing.html#the-tabindex-attribute
private int m_FocusIndex;
public int focusIndex
{
get { return m_FocusIndex; }
set { m_FocusIndex = value; }
}
public virtual bool canGrabFocus
{
get { return m_FocusIndex >= 0; }
}
public virtual void Focus()
{
if (focusController != null)
{
focusController.SwitchFocus(canGrabFocus ? this : null);
}
}
public virtual void Blur()
{
if (focusController != null && focusController.focusedElement == this)
{
focusController.SwitchFocus(null);
}
}
protected internal override void ExecuteDefaultAction(EventBase evt)
{
base.ExecuteDefaultAction(evt);
if (evt.GetEventTypeId() == MouseDownEvent.TypeId())
{
Focus();
}
if (focusController != null)
{
focusController.SwitchFocusOnEvent(evt);
}
}
}
public class FocusChangeDirection
{
static readonly FocusChangeDirection s_Unspecified = new FocusChangeDirection(-1);
public static FocusChangeDirection unspecified
{
get { return s_Unspecified; }
}
static readonly FocusChangeDirection s_None = new FocusChangeDirection(0);
public static FocusChangeDirection none
{
get { return s_None; }
}
protected static FocusChangeDirection lastValue { get { return s_None; } }
int m_Value;
protected FocusChangeDirection(int value)
{
m_Value = value;
}
public static implicit operator int(FocusChangeDirection fcd)
{
return fcd.m_Value;
}
}
public interface IFocusRing
{
FocusChangeDirection GetFocusChangeDirection(Focusable currentFocusable, EventBase e);
Focusable GetNextFocusable(Focusable currentFocusable, FocusChangeDirection direction);
}
public class FocusController
{
// https://w3c.github.io/uievents/#interface-focusevent
public FocusController(IFocusRing focusRing)
{
this.focusRing = focusRing;
focusedElement = null;
imguiKeyboardControl = 0;
}
IFocusRing focusRing { get; set; }
public Focusable focusedElement
{
get;
private set;
}
static void AboutToReleaseFocus(Focusable focusable, Focusable willGiveFocusTo, FocusChangeDirection direction)
{
using (FocusOutEvent e = FocusOutEvent.GetPooled(focusable, willGiveFocusTo, direction))
{
UIElementsUtility.eventDispatcher.DispatchEvent(e, null);
}
}
static void ReleaseFocus(Focusable focusable, Focusable willGiveFocusTo, FocusChangeDirection direction)
{
using (BlurEvent e = BlurEvent.GetPooled(focusable, willGiveFocusTo, direction))
{
UIElementsUtility.eventDispatcher.DispatchEvent(e, null);
}
}
static void AboutToGrabFocus(Focusable focusable, Focusable willTakeFocusFrom, FocusChangeDirection direction)
{
using (FocusInEvent e = FocusInEvent.GetPooled(focusable, willTakeFocusFrom, direction))
{
UIElementsUtility.eventDispatcher.DispatchEvent(e, null);
}
}
static void GrabFocus(Focusable focusable, Focusable willTakeFocusFrom, FocusChangeDirection direction)
{
using (FocusEvent e = FocusEvent.GetPooled(focusable, willTakeFocusFrom, direction))
{
UIElementsUtility.eventDispatcher.DispatchEvent(e, null);
}
}
internal void SwitchFocus(Focusable newFocusedElement)
{
SwitchFocus(newFocusedElement, FocusChangeDirection.unspecified);
}
void SwitchFocus(Focusable newFocusedElement, FocusChangeDirection direction)
{
if (newFocusedElement == focusedElement)
{
return;
}
var oldFocusedElement = focusedElement;
if (newFocusedElement == null || !newFocusedElement.canGrabFocus)
{
if (oldFocusedElement != null)
{
AboutToReleaseFocus(oldFocusedElement, newFocusedElement, direction);
focusedElement = null;
ReleaseFocus(oldFocusedElement, newFocusedElement, direction);
}
}
else if (newFocusedElement != oldFocusedElement)
{
if (oldFocusedElement != null)
{
AboutToReleaseFocus(oldFocusedElement, newFocusedElement, direction);
}
AboutToGrabFocus(newFocusedElement, oldFocusedElement, direction);
focusedElement = newFocusedElement;
if (oldFocusedElement != null)
{
ReleaseFocus(oldFocusedElement, newFocusedElement, direction);
}
GrabFocus(newFocusedElement, oldFocusedElement, direction);
}
}
public void SwitchFocusOnEvent(EventBase e)
{
FocusChangeDirection direction = focusRing.GetFocusChangeDirection(focusedElement, e);
if (direction != FocusChangeDirection.none)
{
Focusable f = focusRing.GetNextFocusable(focusedElement, direction);
SwitchFocus(f, direction);
}
}
internal int imguiKeyboardControl { get; set; }
internal void SyncIMGUIFocus(int imguiKeyboardControlID, IMGUIContainer imguiContainerHavingKeyboardControl)
{
imguiKeyboardControl = imguiKeyboardControlID;
if (imguiKeyboardControl != 0)
{
SwitchFocus(imguiContainerHavingKeyboardControl, FocusChangeDirection.unspecified);
}
else
{
SwitchFocus(null, FocusChangeDirection.unspecified);
}
}
}
}