-
Notifications
You must be signed in to change notification settings - Fork 44
/
Copy pathCursorAPI.cs
154 lines (138 loc) · 4.21 KB
/
CursorAPI.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
#if UNITY_WEBGL && !UNITY_EDITOR
#define REACT_WEBCURSOR
#endif
using System.Collections.Generic;
using ReactUnity.Types;
using UnityEngine;
namespace ReactUnity.Helpers
{
public class CursorAPI
{
#if REACT_WEBCURSOR
static HashSet<string> WebGLCursorNames = new HashSet<string>()
{
"auto",
"default",
"none",
"context-menu",
"help",
"pointer",
"progress",
"wait",
"cell",
"crosshair",
"text",
"vertical-text",
"alias",
"copy",
"move",
"no-drop",
"not-allowed",
"e-resize",
"n-resize",
"ne-resize",
"nw-resize",
"s-resize",
"se-resize",
"sw-resize",
"w-resize",
"ew-resize",
"ns-resize",
"nesw-resize",
"nwse-resize",
"col-resize",
"row-resize",
"all-scroll",
"zoom-in",
"zoom-out",
"grab",
"grabbing",
};
#endif
ICssValueList<Types.Cursor> Current;
ReactContext Context;
List<IReactComponent> Components = new List<IReactComponent>();
public CursorAPI(ReactContext context)
{
Context = context;
}
public void Push(IReactComponent cmp)
{
var top = Components.Count > 0 ? Components[Components.Count - 1] : null;
if (top == cmp)
{
SetCursor(cmp?.ComputedStyle?.cursor);
}
else
{
Components.Remove(cmp);
Components.Add(cmp);
Refresh();
}
}
public void Pop(IReactComponent cmp)
{
Components.Remove(cmp);
Refresh();
}
public void Refresh()
{
var cmp = Components.Count > 0 ? Components[Components.Count - 1] : null;
SetCursor(cmp?.ComputedStyle?.cursor);
}
void SetCursor(ICssValueList<Types.Cursor> cursor)
{
if (Current == cursor) return;
Current = cursor;
UnityEngine.Cursor.SetCursor(null, Vector2.zero, CursorMode.Auto);
#if REACT_WEBCURSOR
setWebGLCursor("");
#endif
TrySetCursor(cursor, 0);
}
private void TrySetCursor(ICssValueList<Types.Cursor> cursor, int ind)
{
if (cursor == null || !cursor.Any || cursor.Count <= ind) return;
if (Current != cursor) return;
if (Context == null) return;
var item = cursor.Get(ind);
if (item.Image != null)
{
item.Image.Get(Context, tx => {
if (Current != cursor) return;
if (tx) UnityEngine.Cursor.SetCursor(tx, item.Offset, CursorMode.Auto);
#if REACT_WEBCURSOR
else if (item.Image.Type == AssetReferenceType.Url) setWebGLCursor(item.Definition);
#endif
else TrySetCursor(cursor, ind + 1);
});
}
else if (!string.IsNullOrWhiteSpace(item.Name))
{
#if REACT_WEBCURSOR
if (WebGLCursorNames.Contains(item.Name))
{
setWebGLCursor(item.Name);
return;
}
#endif
var set = Context.CursorSet;
var ct = set?.Cursors?.GetValueOrDefault(item.Name);
if (ct != null) UnityEngine.Cursor.SetCursor(ct.Cursor, ct.Hotspot, CursorMode.Auto);
else
{
#if REACT_WEBCURSOR
setWebGLCursor(item.Name);
#else
TrySetCursor(cursor, ind + 1);
#endif
}
}
else TrySetCursor(cursor, ind + 1);
}
#if REACT_WEBCURSOR
[System.Runtime.InteropServices.DllImport("__Internal")]
private static extern void setWebGLCursor(string cursor);
#endif
}
}