forked from ReactUnity/core
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUIToolkitComponent.cs
More file actions
557 lines (459 loc) · 24.7 KB
/
UIToolkitComponent.cs
File metadata and controls
557 lines (459 loc) · 24.7 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
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
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
using System;
using System.Collections.Generic;
using Yoga;
using ReactUnity.Helpers;
using ReactUnity.Styling;
using ReactUnity.Types;
using UnityEngine;
using UnityEngine.UIElements;
#if UNITY_EDITOR
using UnityEditor.UIElements;
#endif
namespace ReactUnity.UIToolkit
{
public interface IUIToolkitComponent : IReactComponent
{
VisualElement Element { get; }
}
public interface IUIToolkitComponent<out T> : IReactComponent where T : VisualElement, new()
{
T Element { get; }
VisualElement TargetElement { get; }
}
public class UIToolkitComponent<T> : BaseReactComponent<UIToolkitContext>, IActivatableComponent, IUIToolkitComponent, IUIToolkitComponent<T> where T : VisualElement, new()
{
public T Element { get; protected set; }
VisualElement IUIToolkitComponent.Element => Element;
public virtual VisualElement TargetElement => Element;
public override float ClientWidth => Element.layout.width;
public override float ClientHeight => Element.layout.height;
public bool Disabled
{
get => !Element.enabledSelf;
set => Element.SetEnabled(!value);
}
protected Dictionary<Type, IManipulator> Manipulators = new Dictionary<Type, IManipulator>();
private string currentCursor = null;
protected UIToolkitComponent(T element, UIToolkitContext context, string tag) : base(context, tag, true)
{
ClassList = new UITClassList(this);
Element = element;
Element.userData = this;
RefreshName();
}
public UIToolkitComponent(UIToolkitContext context, string tag, bool isContainer = true) : base(context, tag, isContainer)
{
ClassList = new UITClassList(this);
Element = new T();
Element.userData = this;
RefreshName();
}
protected override void ApplyLayoutStylesSelf()
{
var computed = ComputedStyle;
TargetElement.style.flexDirection = StylingHelpers.GetStyleEnumCustom<FlexDirection>(computed, LayoutProperties.FlexDirection);
TargetElement.style.flexWrap = StylingHelpers.GetStyleEnumCustom<Wrap>(computed, LayoutProperties.Wrap);
TargetElement.style.flexGrow = StylingHelpers.GetStyleFloat(computed, LayoutProperties.FlexGrow);
TargetElement.style.flexShrink = StylingHelpers.GetStyleFloat(computed, LayoutProperties.FlexShrink);
TargetElement.style.width = StylingHelpers.GetStyleLength(computed, LayoutProperties.Width);
TargetElement.style.height = StylingHelpers.GetStyleLength(computed, LayoutProperties.Height);
TargetElement.style.flexBasis = StylingHelpers.GetStyleLength(computed, LayoutProperties.FlexBasis);
TargetElement.style.minWidth = StylingHelpers.GetStyleLength(computed, LayoutProperties.MinWidth);
TargetElement.style.minHeight = StylingHelpers.GetStyleLength(computed, LayoutProperties.MinHeight);
TargetElement.style.maxWidth = StylingHelpers.GetStyleLength(computed, LayoutProperties.MaxWidth);
TargetElement.style.maxHeight = StylingHelpers.GetStyleLength(computed, LayoutProperties.MaxHeight);
TargetElement.style.paddingBottom = StylingHelpers.GetStyleLengthDouble(computed, LayoutProperties.PaddingBottom, LayoutProperties.Padding);
TargetElement.style.paddingTop = StylingHelpers.GetStyleLengthDouble(computed, LayoutProperties.PaddingTop, LayoutProperties.Padding);
TargetElement.style.paddingLeft = StylingHelpers.GetStyleLengthDouble(computed, LayoutProperties.PaddingLeft, LayoutProperties.Padding);
TargetElement.style.paddingRight = StylingHelpers.GetStyleLengthDouble(computed, LayoutProperties.PaddingRight, LayoutProperties.Padding);
TargetElement.style.marginBottom = StylingHelpers.GetStyleLengthDouble(computed, LayoutProperties.MarginBottom, LayoutProperties.Margin);
TargetElement.style.marginTop = StylingHelpers.GetStyleLengthDouble(computed, LayoutProperties.MarginTop, LayoutProperties.Margin);
TargetElement.style.marginLeft = StylingHelpers.GetStyleLengthDouble(computed, LayoutProperties.MarginLeft, LayoutProperties.Margin);
TargetElement.style.marginRight = StylingHelpers.GetStyleLengthDouble(computed, LayoutProperties.MarginRight, LayoutProperties.Margin);
TargetElement.style.left = StylingHelpers.GetStyleLength(computed, LayoutProperties.Left);
TargetElement.style.right = StylingHelpers.GetStyleLength(computed, LayoutProperties.Right);
TargetElement.style.top = StylingHelpers.GetStyleLength(computed, LayoutProperties.Top);
TargetElement.style.bottom = StylingHelpers.GetStyleLength(computed, LayoutProperties.Bottom);
TargetElement.style.borderLeftWidth =
computed.borderLeftStyle == BorderStyle.None ? 0 :
StylingHelpers.GetStyleFloatDouble(computed, LayoutProperties.BorderLeftWidth, LayoutProperties.BorderWidth);
TargetElement.style.borderRightWidth =
computed.borderRightStyle == BorderStyle.None ? 0 :
StylingHelpers.GetStyleFloatDouble(computed, LayoutProperties.BorderRightWidth, LayoutProperties.BorderWidth);
TargetElement.style.borderTopWidth =
computed.borderTopStyle == BorderStyle.None ? 0 :
StylingHelpers.GetStyleFloatDouble(computed, LayoutProperties.BorderTopWidth, LayoutProperties.BorderWidth);
TargetElement.style.borderBottomWidth =
computed.borderBottomStyle == BorderStyle.None ? 0 :
StylingHelpers.GetStyleFloatDouble(computed, LayoutProperties.BorderBottomWidth, LayoutProperties.BorderWidth);
TargetElement.style.display = StylingHelpers.GetStyleEnumCustom<DisplayStyle>(computed, LayoutProperties.Display);
var pos = computed.position;
TargetElement.style.position = pos == PositionType.Relative ? Position.Relative : Position.Absolute;
TargetElement.style.overflow = StylingHelpers.GetStyleEnumCustom<Overflow>(computed, LayoutProperties.Overflow);
TargetElement.style.alignContent = StylingHelpers.GetStyleEnumCustom<Align>(computed, LayoutProperties.AlignContent);
TargetElement.style.alignItems = StylingHelpers.GetStyleEnumCustom<Align>(computed, LayoutProperties.AlignItems);
TargetElement.style.alignSelf = StylingHelpers.GetStyleEnumCustom<Align>(computed, LayoutProperties.AlignSelf);
TargetElement.style.justifyContent = StylingHelpers.GetStyleEnumCustom<Justify>(computed, LayoutProperties.JustifyContent);
}
protected override void ApplyStylesSelf()
{
var computed = ComputedStyle;
TargetElement.style.backgroundColor = StylingHelpers.GetStyleColor(computed, StyleProperties.backgroundColor);
TargetElement.style.color = StylingHelpers.GetStyleColor(computed, StyleProperties.color);
#if UNITY_2020_1_OR_NEWER
TargetElement.style.textOverflow = StylingHelpers.GetStyleEnumCustom<TextOverflow>(computed, StyleProperties.textOverflow);
#endif
TargetElement.style.visibility = StylingHelpers.GetStyleBoolToEnum(computed, StyleProperties.visibility, Visibility.Visible, Visibility.Hidden);
TargetElement.style.opacity = StylingHelpers.GetStyleFloat(computed, StyleProperties.opacity);
TargetElement.style.whiteSpace = StylingHelpers.GetStyleBoolToEnum(computed, StyleProperties.textWrap, WhiteSpace.Normal, WhiteSpace.NoWrap);
if (computed.HasValue(StyleProperties.fontSize)) TargetElement.style.fontSize = computed.fontSize;
else TargetElement.style.fontSize = StyleKeyword.Null;
TargetElement.style.borderBottomLeftRadius = StylingHelpers.GetStyleBorderRadius(computed, StyleProperties.borderBottomLeftRadius);
TargetElement.style.borderBottomRightRadius = StylingHelpers.GetStyleBorderRadius(computed, StyleProperties.borderBottomRightRadius);
TargetElement.style.borderTopLeftRadius = StylingHelpers.GetStyleBorderRadius(computed, StyleProperties.borderTopLeftRadius);
TargetElement.style.borderTopRightRadius = StylingHelpers.GetStyleBorderRadius(computed, StyleProperties.borderTopRightRadius);
TargetElement.style.borderBottomColor = StylingHelpers.GetStyleBorderColor(computed, StyleProperties.borderBottomColor);
TargetElement.style.borderTopColor = StylingHelpers.GetStyleBorderColor(computed, StyleProperties.borderTopColor);
TargetElement.style.borderLeftColor = StylingHelpers.GetStyleBorderColor(computed, StyleProperties.borderLeftColor);
TargetElement.style.borderRightColor = StylingHelpers.GetStyleBorderColor(computed, StyleProperties.borderRightColor);
#if UNITY_2022_3_OR_NEWER
TargetElement.style.backgroundPositionX = StylingHelpers.GetStyleBackgroundPosition(computed, StyleProperties.backgroundPositionX);
TargetElement.style.backgroundPositionY = StylingHelpers.GetStyleBackgroundPosition(computed, StyleProperties.backgroundPositionY);
TargetElement.style.backgroundSize = StylingHelpers.GetStyleBackgroundSize(computed, StyleProperties.backgroundSize);
TargetElement.style.backgroundRepeat = StylingHelpers.GetStyleBackgroundRepeat(computed, StyleProperties.backgroundRepeatX, StyleProperties.backgroundRepeatY);
TargetElement.style.letterSpacing = StylingHelpers.GetStyleFloat(computed, StyleProperties.letterSpacing).value;
TargetElement.style.wordSpacing = StylingHelpers.GetStyleFloat(computed, StyleProperties.wordSpacing).value;
#endif
TargetElement.style.unityTextOutlineColor = StylingHelpers.GetStyleColor(computed, StyleProperties.textStrokeColor).value;
TargetElement.style.unityTextOutlineWidth = StylingHelpers.GetStyleFloat(computed, StyleProperties.textStrokeWidth).value;
if (computed.HasValue(StyleProperties.backgroundImage))
{
var bg = computed.backgroundImage?.Get(0);
TargetElement.style.backgroundImage = null;
bg?.ResolveImage(Context, TargetElement.layout.size, tx => {
if (bg != ComputedStyle.backgroundImage?.Get(0)) return;
TargetElement.style.backgroundImage = tx?.Texture;
});
}
else TargetElement.style.backgroundImage = StyleKeyword.Null;
if (computed.HasValue(StyleProperties.fontStyle) || computed.HasValue(StyleProperties.fontWeight))
TargetElement.style.unityFontStyleAndWeight = StylingHelpers.ConvertFontStyle(computed.fontStyle, computed.fontWeight);
else TargetElement.style.unityFontStyleAndWeight = StyleKeyword.Null;
if (computed.HasValue(StyleProperties.backgroundImage) && computed.HasValue(StyleProperties.backgroundColor))
TargetElement.style.unityBackgroundImageTintColor = computed.backgroundColor;
else TargetElement.style.unityBackgroundImageTintColor = StyleKeyword.Null;
if (computed.HasValue(StyleProperties.textAlign))
{
if (StylingHelpers.TextAlignMap.TryGetValue(computed.textAlign, out var value)) TargetElement.style.unityTextAlign = value;
else TargetElement.style.unityTextAlign = TextAnchor.MiddleCenter;
}
else TargetElement.style.unityTextAlign = StyleKeyword.Null;
if (computed.HasValue(StyleProperties.fontFamily))
{
if (computed.fontFamily != null) computed.fontFamily?.Get(Context, x => {
if (x?.Font != null)
{
TargetElement.style.unityFont = x.Font;
#if REACT_TEXTCORE
TargetElement.style.unityFontDefinition = FontDefinition.FromFont(x.Font);
#endif
}
#if REACT_TMP
else if (x?.TmpFontAsset != null)
{
TargetElement.style.unityFont = x?.TmpFontAsset?.sourceFontFile;
#if REACT_TEXTCORE
TargetElement.style.unityFontDefinition = FontDefinition.FromFont(x?.TmpFontAsset?.sourceFontFile);
#endif
}
#endif
else TargetElement.style.unityFont = ResourcesHelper.DefaultFont;
#if REACT_TEXTCORE
if (x?.TextCoreFontAsset != null) TargetElement.style.unityFontDefinition = FontDefinition.FromSDFFont(x?.TextCoreFontAsset);
#endif
});
}
else TargetElement.style.unityFont = StyleKeyword.Null;
if (computed.HasValue(StyleProperties.cursor))
{
var cursor = ResourcesHelper.UtilityCursorClassPrefix + computed.cursor?.Get(0)?.Definition;
if (currentCursor != cursor)
{
if (currentCursor != null)
{
TargetElement.RemoveFromClassList(currentCursor);
currentCursor = null;
}
if (cursor != null)
{
currentCursor = cursor;
TargetElement.AddToClassList(currentCursor);
}
}
}
else if (currentCursor != null)
{
TargetElement.RemoveFromClassList(currentCursor);
currentCursor = null;
}
// Transforms
#if UNITY_6000_2_OR_NEWER
if (computed.HasValue(StyleProperties.scale)) TargetElement.style.scale = computed.scale;
else TargetElement.style.scale = Vector3.one;
if (computed.HasValue(StyleProperties.rotate)) TargetElement.style.rotate = Quaternion.Euler(computed.rotate);
else TargetElement.style.rotate = Quaternion.identity;
#else
if (computed.HasValue(StyleProperties.scale)) TargetElement.transform.scale = computed.scale;
else TargetElement.transform.scale = Vector3.one;
if (computed.HasValue(StyleProperties.rotate)) TargetElement.transform.rotation = Quaternion.Euler(computed.rotate);
else TargetElement.transform.rotation = Quaternion.identity;
#endif
Vector3 translate;
var size = TargetElement.layout.size;
var rect = new Vector2(float.IsNaN(size.x) ? 0 : size.x, float.IsNaN(size.y) ? 0 : size.y);
if (computed.HasValue(StyleProperties.translate))
{
var tran = computed.translate;
var scale = new Vector2(tran.X.Unit == YogaUnit.Percent ? rect.x / 100 : 1, tran.Y.Unit == YogaUnit.Percent ? rect.y / 100 : 1);
translate = new Vector2(tran.X.Value * scale.x, -tran.Y.Value * scale.y);
}
else translate = Vector3.zero;
var hasPivot = computed.HasValue(StyleProperties.transformOrigin);
if (hasPivot)
{
var origin = computed.transformOrigin;
var pivotX = origin.X.Unit == YogaUnit.Percent ? (origin.X.Value / 100) : origin.X.Unit == YogaUnit.Point ? (origin.X.Value / rect.x) : 0.5f;
var pivotY = origin.Y.Unit == YogaUnit.Percent ? (origin.Y.Value / 100) : origin.Y.Unit == YogaUnit.Point ? (origin.Y.Value / rect.y) : 0.5f;
var pivot = new Vector3(pivotX, pivotY, 0);
if (pivot == Vector3.zero)
{
#if UNITY_6000_2_OR_NEWER
TargetElement.style.translate = translate;
#else
TargetElement.transform.position = translate;
#endif
}
else
{
Vector3 deltaPosition = -pivot; // get change in pivot
deltaPosition.Scale(rect); // apply sizing
#if UNITY_6000_2_OR_NEWER
deltaPosition.Scale(TargetElement.resolvedStyle.scale.value); // apply scaling
deltaPosition = Quaternion.AngleAxis(TargetElement.resolvedStyle.rotate.angle.value, Vector3.forward) * deltaPosition; // apply rotation
#else
deltaPosition.Scale(TargetElement.transform.scale); // apply scaling
deltaPosition = TargetElement.transform.rotation * deltaPosition; // apply rotation
#endif
var counter = new Vector3(pivot.x, pivot.y, 0);
counter.Scale(rect);
var pos = deltaPosition + translate + counter;
#if UNITY_6000_2_OR_NEWER
TargetElement.style.translate = new Vector3(pos.x, pos.y, 0);
#else
TargetElement.transform.position = new Vector3(pos.x, pos.y, 0);
#endif
}
#if UNITY_2021_2_OR_NEWER
// TODO: Versions before 2021.2 does not have this property, so we need the above hack
// But using the official way should be faster to use for >2021.2
// So we should write a separate logic for before 2021.2 and after
TargetElement.style.transformOrigin = new TransformOrigin(0, 0, 0);
#endif
}
else
{
#if UNITY_6000_2_OR_NEWER
TargetElement.style.translate = translate;
#else
TargetElement.transform.position = translate;
#endif
}
TargetElement.pickingMode = computed.pointerEvents == PointerEvents.None ? PickingMode.Ignore : PickingMode.Position;
}
protected override void DestroySelf()
{
base.DestroySelf();
Element.RemoveFromHierarchy();
}
public override bool Pool()
{
if (!base.Pool()) return false;
Element.RemoveFromHierarchy();
return true;
}
#region Setters
public override Action AddEventListener(string eventName, Callback fun)
{
var shouldCapture = eventName != "onPointerCapture" && eventName != "onMouseCapture" && eventName.FastEndsWith("Capture");
if (shouldCapture) eventName = eventName.Substring(0, eventName.Length - 7);
var (register, unregister, priority) = EventHandlerMap.GetEventMethods(eventName);
if (register == null)
{
return base.AddEventListener(eventName, fun);
}
EventCallback<EventBase> callAction = (e) => fun.CallWithPriority(priority, e, this);
var trickle = shouldCapture ? TrickleDown.TrickleDown : TrickleDown.NoTrickleDown;
register.Invoke(Element, new object[] { callAction, trickle });
return () => unregister.Invoke(Element, new object[] { callAction, trickle });
}
public override void SetProperty(string property, object value)
{
switch (property)
{
case "focusable":
Element.focusable = Convert.ToBoolean(value);
return;
#if UNITY_EDITOR
case "bind":
if (value is UnityEditor.SerializedObject so) Element.Bind(so);
else Element.Unbind();
return;
#endif
case "tooltip":
Element.tooltip = value?.ToString();
return;
case "tabIndex":
Element.tabIndex = Convert.ToInt32(value);
return;
case "viewDataKey":
Element.viewDataKey = value?.ToString();
return;
case "disabled":
Disabled = Convert.ToBoolean(value);
return;
default:
base.SetProperty(property, value);
return;
}
}
protected override void ApplyName(string resolvedName)
{
Element.name = resolvedName;
}
#endregion
public override object GetComponent(Type type)
{
if (Manipulators.TryGetValue(type, out var val)) return val;
return null;
}
public override object AddComponent(Type type)
{
var instance = Activator.CreateInstance(type);
if (instance is IManipulator m)
{
TargetElement.AddManipulator(m);
Manipulators[type] = m;
}
return instance;
}
protected override bool InsertChild(IReactComponent child, int index = -1)
{
if (child is IUIToolkitComponent<VisualElement> u)
{
if (index >= 0) Element.Insert(index, u.Element);
else Element.Add(u.Element);
return true;
}
return false;
}
protected override bool DeleteChild(IReactComponent child)
{
if (child is IUIToolkitComponent<VisualElement> u)
{
if (u.Element.parent == Element)
{
Element.Remove(u.Element);
return true;
}
return false;
}
return false;
}
public void CaptureMouse()
{
MouseCaptureController.CaptureMouse(TargetElement);
}
public void ReleaseMouse()
{
MouseCaptureController.ReleaseMouse(TargetElement);
}
public bool HasMouseCapture()
{
return MouseCaptureController.HasMouseCapture(TargetElement);
}
public override bool UpdateOrder(int prev, int current)
{
var parent = (Parent as IUIToolkitComponent) ?? (Parent as Html.HtmlComponent)?.Proxy as IUIToolkitComponent;
var siblings = parent.Element;
var count = siblings.childCount;
var currentIndex = -1;
var layout = Element;
for (int i = 0; i < count; i++)
{
var sb = siblings[i];
if (sb == layout)
{
currentIndex = i;
break;
}
}
var expectedIndex = currentIndex;
if (current > prev)
{
for (int i = currentIndex + 1; i < count; i++)
{
var sb = siblings[i].userData as IReactComponent;
if (sb.CurrentOrder > current || (sb.CurrentOrder == current && sb.ParentIndex > ParentIndex)) break;
expectedIndex = i;
}
}
else
{
for (int i = currentIndex - 1; i >= 0; i--)
{
var sb = siblings[i].userData as IReactComponent;
if (sb.CurrentOrder < current || (sb.CurrentOrder == current && sb.ParentIndex < ParentIndex)) break;
expectedIndex = i;
}
}
if (expectedIndex != currentIndex)
{
siblings.RemoveAt(currentIndex);
siblings.Insert(expectedIndex, layout);
return true;
}
return false;
}
public virtual void Activate() { }
public class UITClassList : ClassList
{
private readonly IUIToolkitComponent<T> Component;
public UITClassList(UIToolkitComponent<T> component) : base(component)
{
Component = component;
}
internal override void OnAdd(string item)
{
base.OnAdd(item);
Component.TargetElement.AddToClassList(item);
}
internal override void OnRemove(string item)
{
base.OnRemove(item);
Component.TargetElement.RemoveFromClassList(item);
}
internal override void OnBeforeChange()
{
base.OnBeforeChange();
foreach (var item in Component.ClassList)
Component.TargetElement.RemoveFromClassList(item);
}
internal override void OnAfterChange()
{
base.OnAfterChange();
foreach (var item in Component.ClassList)
Component.TargetElement.AddToClassList(item);
}
}
}
}