forked from ProwlEngine/Prowl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSelectHandler.cs
184 lines (160 loc) · 5.37 KB
/
SelectHandler.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
using Prowl.Runtime;
namespace Prowl.Editor;
public static class GlobalSelectHandler
{
public static event Action<object>? OnGlobalSelectObject;
public static event Action<object>? OnGlobalDeselectObject;
public static void Select(object obj) => OnGlobalSelectObject?.Invoke(obj);
public static void Deselect(object obj) => OnGlobalDeselectObject?.Invoke(obj);
}
/// <summary>
/// A General Purpose Selection Handler for the Editor
/// </summary>
/// <typeparam name="T">The Type you want to select, Must be of type 'Class'</typeparam>
public class SelectHandler<T> where T : class
{
bool selectedThisFrame = false;
List<T> selected = new();
SortedList<int, T> previousFrameSelectables;
SortedList<int, T> selectables = new();
int lastSelectedIndex = -1;
public bool SelectedThisFrame => selectedThisFrame;
public List<T> Selected => selected;
public int Count => selected.Count;
public event Action<T>? OnSelectObject;
public event Action<T>? OnDeselectObject;
private Func<T, bool> CheckIsDestroyed;
private Func<T, T, bool> Equals;
public SelectHandler(Func<T, bool> checkIsDestroyed, Func<T, T, bool> equals)
{
CheckIsDestroyed = checkIsDestroyed;
Equals = equals;
}
public void StartFrame()
{
// Clear dead references
for (int i = 0; i < selected.Count; i++) {
if (CheckIsDestroyed.Invoke(selected[i])) {
selected.RemoveAt(i);
i--;
}
}
selectedThisFrame = false;
previousFrameSelectables = selectables;
selectables = new();
if (lastSelectedIndex == -1 && selected.Count > 0) {
SetSelectedIndex((T)selected[0]);
}
}
public void SetSelection(T obj)
{
Clear();
selected.Add(obj);
selectedThisFrame = true;
OnSelectObject?.Invoke(obj);
GlobalSelectHandler.Select(obj);
SetSelectedIndex(obj);
}
public void SetSelection(T[] objs)
{
Clear();
selectedThisFrame = true;
for (int i = 0; i < objs.Length; i++) {
selected.Add(objs[i]);
OnSelectObject?.Invoke(objs[i]);
GlobalSelectHandler.Select(objs[i]);
}
if(objs.Length > 0)
SetSelectedIndex(objs[0]);
}
public bool IsSelected(T obj)
{
for (int i = 0; i < selected.Count; i++) {
if (Equals.Invoke(selected[i], obj))
return true;
}
return false;
}
public void Foreach(Action<T> value)
{
var copy = new List<T>(selected);
copy.ForEach((weak) => {
value?.Invoke((T)weak);
});
}
public void AddSelectableAtIndex(int index, T obj) => selectables.Add(index, obj);
public void Select(int index, T obj)
{
int prevLastIndex = lastSelectedIndex;
Select(obj);
if (prevLastIndex != index && index != -1 && lastSelectedIndex != -1)
{
if (prevLastIndex != -1 && Input.GetKey(Silk.NET.Input.Key.ShiftLeft))
{
// Bulk Select
for (int i = Math.Min(prevLastIndex, index); i <= Math.Max(prevLastIndex, index); i++)
{
if (previousFrameSelectables.TryGetValue(i, out var o))
{
if (!CheckIsDestroyed.Invoke(o))
{
// Always additive so we cant call Select(o) here as that checks if ctrl is down
selected.Add(o);
selectedThisFrame = true;
OnSelectObject?.Invoke(o);
GlobalSelectHandler.Select(o);
}
}
}
}
}
lastSelectedIndex = index;
}
public void SelectIfNot(T obj)
{
if (!IsSelected(obj))
Select(obj);
}
public void Select(T obj)
{
selectedThisFrame = true;
if (Input.GetKey(Silk.NET.Input.Key.ControlLeft)) {
// Additive
if (IsSelected(obj)) {
for (int i = 0; i < selected.Count; i++) {
if (Equals.Invoke(selected[i], obj)) {
selected.RemoveAt(i);
break;
}
}
OnDeselectObject?.Invoke(obj);
GlobalSelectHandler.Deselect(obj);
} else {
selected.Add(obj);
OnSelectObject?.Invoke(obj);
GlobalSelectHandler.Select(obj);
}
} else SetSelection(obj);
SetSelectedIndex(obj);
}
private void SetSelectedIndex(T entity)
{
if (previousFrameSelectables == null) return;
// if sorted has this value using reference equals, set lastSelectedIndex to the index of it
for (int i = 0; i < previousFrameSelectables.Count; i++) {
if (Equals.Invoke(previousFrameSelectables.Values[i], entity)) {
lastSelectedIndex = previousFrameSelectables.Keys[i];
break;
}
}
}
public void Clear()
{
foreach (var item in selected) {
OnDeselectObject?.Invoke(item);
GlobalSelectHandler.Deselect(item);
}
selected.Clear();
lastSelectedIndex = -1;
}
}