This repository was archived by the owner on Apr 29, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 256
/
Copy pathUIWidgetsPanel.cs
420 lines (350 loc) · 15.3 KB
/
UIWidgetsPanel.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
using System.Collections.Generic;
using Unity.UIWidgets.async;
using Unity.UIWidgets.editor;
using Unity.UIWidgets.external.simplejson;
using Unity.UIWidgets.foundation;
using Unity.UIWidgets.ui;
using Unity.UIWidgets.widgets;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
using RawImage = UnityEngine.UI.RawImage;
using Rect = UnityEngine.Rect;
using Texture = UnityEngine.Texture;
namespace Unity.UIWidgets.engine {
public class UIWidgetWindowAdapter : WindowAdapter {
readonly UIWidgetsPanel _uiWidgetsPanel;
bool _needsPaint;
protected override void updateSafeArea() {
this._padding = this._uiWidgetsPanel.viewPadding;
this._viewInsets = this._uiWidgetsPanel.viewInsets;
}
protected override bool hasFocus() {
return EventSystem.current != null &&
EventSystem.current.currentSelectedGameObject == this._uiWidgetsPanel.gameObject;
}
public override void scheduleFrame(bool regenerateLayerTree = true) {
base.scheduleFrame(regenerateLayerTree);
this._needsPaint = true;
}
public UIWidgetWindowAdapter(UIWidgetsPanel uiWidgetsPanel) {
this._uiWidgetsPanel = uiWidgetsPanel;
}
public override void OnGUI(Event evt) {
if (this.displayMetricsChanged()) {
this._needsPaint = true;
}
if (evt.type == EventType.Repaint) {
if (!this._needsPaint) {
return;
}
this._needsPaint = false;
}
base.OnGUI(evt);
}
protected override Surface createSurface() {
return new WindowSurfaceImpl(this._uiWidgetsPanel.applyRenderTexture);
}
public override GUIContent titleContent {
get { return new GUIContent(this._uiWidgetsPanel.gameObject.name); }
}
protected override float queryDevicePixelRatio() {
return this._uiWidgetsPanel.devicePixelRatio;
}
protected override int queryAntiAliasing() {
return this._uiWidgetsPanel.antiAliasing;
}
protected override Vector2 queryWindowSize() {
var rect = this._uiWidgetsPanel.rectTransform.rect;
// Here we use ReferenceEquals instead of "==" due to
// https://blogs.unity3d.com/2014/05/16/custom-operator-should-we-keep-it/
// In short, "==" is overloaded for UnityEngine.Object and will bring performance issues
if (!ReferenceEquals(this._uiWidgetsPanel.canvas, null)) {
var size = new Vector2(rect.width, rect.height) *
this._uiWidgetsPanel.canvas.scaleFactor / this._uiWidgetsPanel.devicePixelRatio;
size.x = Mathf.Round(size.x);
size.y = Mathf.Round(size.y);
return size;
}
return new Vector2(0, 0);
}
public Offset windowPosToScreenPos(Offset windowPos) {
Camera camera = null;
var canvas = this._uiWidgetsPanel.canvas;
if (canvas.renderMode != RenderMode.ScreenSpaceCamera) {
camera = canvas.GetComponent<GraphicRaycaster>().eventCamera;
}
var pos = new Vector2(windowPos.dx, windowPos.dy);
pos = pos * this.queryDevicePixelRatio() / this._uiWidgetsPanel.canvas.scaleFactor;
var rectTransform = this._uiWidgetsPanel.rectTransform;
var rect = rectTransform.rect;
pos.x += rect.min.x;
pos.y = rect.max.y - pos.y;
var worldPos = rectTransform.TransformPoint(new Vector2(pos.x, pos.y));
var screenPos = RectTransformUtility.WorldToScreenPoint(camera, worldPos);
return new Offset(screenPos.x, Screen.height - screenPos.y);
}
}
[RequireComponent(typeof(RectTransform))]
public class UIWidgetsPanel : RawImage, IPointerDownHandler, IPointerUpHandler, IDragHandler,
IPointerEnterHandler, IPointerExitHandler, WindowHost {
static Event _repaintEvent;
[Tooltip("set to zero if you want to use the default device pixel ratio of the target platforms; otherwise the " +
"device pixel ratio will be forced to the given value on all devices.")]
[SerializeField] protected float devicePixelRatioOverride;
[Tooltip("set to true will enable the hardware anti-alias feature, which will improve the appearance of the UI greatly but " +
"making it much slower. Enable it only when seriously required.")]
[SerializeField] protected bool hardwareAntiAliasing = false;
WindowAdapter _windowAdapter;
Texture _texture;
Vector2 _lastMouseMove;
readonly HashSet<int> _enteredPointers = new HashSet<int>();
bool _viewMetricsCallbackRegistered;
bool _mouseEntered {
get { return !this._enteredPointers.isEmpty(); }
}
DisplayMetrics _displayMetrics;
const int mouseButtonNum = 3;
void _handleViewMetricsChanged(string method, List<JSONNode> args) {
this._windowAdapter.onViewMetricsChanged();
this._displayMetrics.onViewMetricsChanged();
}
protected virtual void InitWindowAdapter() {
D.assert(this._windowAdapter == null);
this._windowAdapter = new UIWidgetWindowAdapter(this);
this._windowAdapter.OnEnable();
}
protected override void OnEnable() {
base.OnEnable();
//Disable the default touch -> mouse event conversion on mobile devices
Input.simulateMouseWithTouches = false;
this._displayMetrics = DisplayMetricsProvider.provider();
this._displayMetrics.OnEnable();
this._enteredPointers.Clear();
if (_repaintEvent == null) {
_repaintEvent = new Event {type = EventType.Repaint};
}
this.InitWindowAdapter();
Widget root;
using (this._windowAdapter.getScope()) {
root = this.createWidget();
}
this._windowAdapter.attachRootWidget(root);
this._lastMouseMove = Input.mousePosition;
}
public float devicePixelRatio {
get {
return this.devicePixelRatioOverride > 0
? this.devicePixelRatioOverride
: this._displayMetrics.devicePixelRatio;
}
}
public int antiAliasing {
get { return this.hardwareAntiAliasing ? Window.defaultAntiAliasing : 0; }
}
public WindowPadding viewPadding {
get { return this._displayMetrics.viewPadding; }
}
public WindowPadding viewInsets {
get { return this._displayMetrics.viewInsets; }
}
protected override void OnDisable() {
D.assert(this._windowAdapter != null);
this._windowAdapter.OnDisable();
this._windowAdapter = null;
base.OnDisable();
}
protected virtual Widget createWidget() {
return null;
}
public void recreateWidget() {
Widget root;
using (this._windowAdapter.getScope()) {
root = this.createWidget();
}
this._windowAdapter.attachRootWidget(root);
}
internal void applyRenderTexture(Rect screenRect, Texture texture, Material mat) {
this.texture = texture;
this.material = mat;
}
protected virtual void Update() {
this._displayMetrics.Update();
UIWidgetsMessageManager.ensureUIWidgetsMessageManagerIfNeeded();
#if UNITY_ANDROID
if (Input.GetKeyDown(KeyCode.Escape)) {
this._windowAdapter.withBinding(() => { WidgetsBinding.instance.handlePopRoute(); });
}
#endif
if (!this._viewMetricsCallbackRegistered) {
this._viewMetricsCallbackRegistered = true;
UIWidgetsMessageManager.instance?.AddChannelMessageDelegate("ViewportMatricsChanged",
this._handleViewMetricsChanged);
}
if (this._mouseEntered) {
if (this._lastMouseMove.x != Input.mousePosition.x || this._lastMouseMove.y != Input.mousePosition.y) {
this.handleMouseMovement();
}
}
this._lastMouseMove = Input.mousePosition;
if (this._mouseEntered) {
this.handleMouseScroll();
}
D.assert(this._windowAdapter != null);
this._windowAdapter.Update();
this._windowAdapter.OnGUI(_repaintEvent);
}
void OnGUI() {
this._displayMetrics.OnGUI();
if (Event.current.type == EventType.KeyDown || Event.current.type == EventType.KeyUp) {
this._windowAdapter.OnGUI(Event.current);
}
}
void handleMouseMovement() {
var pos = this.getPointPosition(Input.mousePosition);
if (pos == null) {
return;
}
this._windowAdapter.postPointerEvent(new PointerData(
timeStamp: Timer.timespanSinceStartup,
change: PointerChange.hover,
kind: PointerDeviceKind.mouse,
device: this.getMouseButtonDown(),
physicalX: pos.Value.x,
physicalY: pos.Value.y
));
}
void handleMouseScroll() {
if (Input.mouseScrollDelta.y != 0 || Input.mouseScrollDelta.x != 0) {
var scaleFactor = this.canvas.scaleFactor;
var pos = this.getPointPosition(Input.mousePosition);
if (pos == null) {
return;
}
this._windowAdapter.onScroll(Input.mouseScrollDelta.x * scaleFactor,
Input.mouseScrollDelta.y * scaleFactor,
pos.Value.x,
pos.Value.y,
InputUtils.getScrollButtonKey());
}
}
int getMouseButtonDown() {
//default mouse button key = left mouse button
var defaultKey = 0;
for (int key = 0; key < mouseButtonNum; key++) {
if (Input.GetMouseButton(key)) {
defaultKey = key;
break;
}
}
return InputUtils.getMouseButtonKey(defaultKey);
}
public void OnPointerDown(PointerEventData eventData) {
var position = this.getPointPosition(eventData);
if (position == null) {
return;
}
EventSystem.current.SetSelectedGameObject(this.gameObject, eventData);
this._windowAdapter.postPointerEvent(new PointerData(
timeStamp: Timer.timespanSinceStartup,
change: PointerChange.down,
kind: InputUtils.getPointerDeviceKind(eventData),
device: InputUtils.getPointerDeviceKey(eventData),
physicalX: position.Value.x,
physicalY: position.Value.y
));
}
public void OnPointerUp(PointerEventData eventData) {
var position = this.getPointPosition(eventData);
if (position == null) {
return;
}
this._windowAdapter.postPointerEvent(new PointerData(
timeStamp: Timer.timespanSinceStartup,
change: PointerChange.up,
kind: InputUtils.getPointerDeviceKind(eventData),
device: InputUtils.getPointerDeviceKey(eventData),
physicalX: position.Value.x,
physicalY: position.Value.y
));
}
Camera getActiveCamera() {
//refer to: https://zhuanlan.zhihu.com/p/37127981
Camera eventCamera = null;
if (this.canvas.renderMode != RenderMode.ScreenSpaceOverlay) {
eventCamera = this.canvas.GetComponent<GraphicRaycaster>().eventCamera;
}
return eventCamera;
}
Vector2? getPointPosition(PointerEventData eventData) {
Camera camera = this.getActiveCamera();
Vector2 localPoint;
RectTransformUtility.ScreenPointToLocalPointInRectangle(this.rectTransform, eventData.position,
camera, out localPoint);
var scaleFactor = this.canvas.scaleFactor;
localPoint.x = (localPoint.x - this.rectTransform.rect.min.x) * scaleFactor;
localPoint.y = (this.rectTransform.rect.max.y - localPoint.y) * scaleFactor;
return localPoint;
}
Vector2? getPointPosition(Vector2 position) {
Vector2 localPoint;
Camera eventCamera = this.getActiveCamera();
RectTransformUtility.ScreenPointToLocalPointInRectangle(this.rectTransform, position,
eventCamera, out localPoint);
var scaleFactor = this.canvas.scaleFactor;
localPoint.x = (localPoint.x - this.rectTransform.rect.min.x) * scaleFactor;
localPoint.y = (this.rectTransform.rect.max.y - localPoint.y) * scaleFactor;
return localPoint;
}
public void OnDrag(PointerEventData eventData) {
var position = this.getPointPosition(eventData);
if (position == null) {
return;
}
this._windowAdapter.postPointerEvent(new PointerData(
timeStamp: Timer.timespanSinceStartup,
change: PointerChange.move,
kind: InputUtils.getPointerDeviceKind(eventData),
device: InputUtils.getPointerDeviceKey(eventData),
physicalX: position.Value.x,
physicalY: position.Value.y
));
}
public void OnPointerEnter(PointerEventData eventData) {
var position = this.getPointPosition(eventData);
if (position == null) {
return;
}
var pointerKey = InputUtils.getPointerDeviceKey(eventData);
this._enteredPointers.Add(pointerKey);
this._lastMouseMove = eventData.position;
this._windowAdapter.postPointerEvent(new PointerData(
timeStamp: Timer.timespanSinceStartup,
change: PointerChange.hover,
kind: InputUtils.getPointerDeviceKind(eventData),
device: pointerKey,
physicalX: position.Value.x,
physicalY: position.Value.y
));
}
public void OnPointerExit(PointerEventData eventData) {
var position = this.getPointPosition(eventData);
if (position == null) {
return;
}
var pointerKey = InputUtils.getPointerDeviceKey(eventData);
this._enteredPointers.Remove(pointerKey);
this._windowAdapter.postPointerEvent(new PointerData(
timeStamp: Timer.timespanSinceStartup,
change: PointerChange.hover,
kind: InputUtils.getPointerDeviceKind(eventData),
device: pointerKey,
physicalX: position.Value.x,
physicalY: position.Value.y
));
}
public Window window {
get { return this._windowAdapter; }
}
}
}