/
RectTransform.bindings.cs
282 lines (239 loc) · 9.89 KB
/
RectTransform.bindings.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
// 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;
using UnityEngine.Bindings;
using UnityEngine.Scripting;
namespace UnityEngine
{
[Flags]
public enum DrivenTransformProperties
{
None = 0,
All = ~None,
AnchoredPositionX = 1 << 1,
AnchoredPositionY = 1 << 2,
AnchoredPositionZ = 1 << 3,
Rotation = 1 << 4,
ScaleX = 1 << 5,
ScaleY = 1 << 6,
ScaleZ = 1 << 7,
AnchorMinX = 1 << 8,
AnchorMinY = 1 << 9,
AnchorMaxX = 1 << 10,
AnchorMaxY = 1 << 11,
SizeDeltaX = 1 << 12,
SizeDeltaY = 1 << 13,
PivotX = 1 << 14,
PivotY = 1 << 15,
AnchoredPosition = AnchoredPositionX | AnchoredPositionY,
AnchoredPosition3D = AnchoredPositionX | AnchoredPositionY | AnchoredPositionZ,
Scale = ScaleX | ScaleY | ScaleZ,
AnchorMin = AnchorMinX | AnchorMinY,
AnchorMax = AnchorMaxX | AnchorMaxY,
Anchors = AnchorMin | AnchorMax,
SizeDelta = SizeDeltaX | SizeDeltaY,
Pivot = PivotX | PivotY
}
[NativeHeader("Editor/Src/Animation/AnimationModeSnapshot.h")]
[NativeHeader("Editor/Src/Undo/PropertyUndoManager.h")]
public struct DrivenRectTransformTracker
{
private List<RectTransform> m_Tracked;
internal static bool CanRecordModifications()
{
// The DrivenRectTransformTracker should not record undo by itself but always
// as part of another undoable action. We therefore prevent recording undo if there
// is no undo recordings yet. This fixes many situations where the main scene is
// marked as dirty without user interaction as a side effect the Add() and Clear()
// below being called either when rebuilding auto layouted during ui rendering or when
// layoutgroup is being disabled when merging Prefab instances. Fixes case 1268783.
return !IsInAnimationMode() && (IsUndoingOrRedoing() || HasUndoRecordObjects());
}
[FreeFunction("GetAnimationModeSnapshot().IsInAnimationMode")]
static extern bool IsInAnimationMode();
[FreeFunction("GetPropertyUndoManager().HasRecordings")]
static extern bool HasUndoRecordObjects();
[FreeFunction("GetPropertyUndoManager().IsUndoingOrRedoing")]
static extern bool IsUndoingOrRedoing();
private static bool s_BlockUndo;
public static void StopRecordingUndo() { s_BlockUndo = true; }
public static void StartRecordingUndo() { s_BlockUndo = false; }
public void Add(Object driver, RectTransform rectTransform, DrivenTransformProperties drivenProperties)
{
if (m_Tracked == null)
m_Tracked = new List<RectTransform>();
if (!Application.isPlaying && CanRecordModifications() && !s_BlockUndo)
RuntimeUndo.RecordObject(rectTransform, "Driving RectTransform");
rectTransform.drivenByObject = driver;
rectTransform.drivenProperties = rectTransform.drivenProperties | drivenProperties;
m_Tracked.Add(rectTransform);
}
[Obsolete("revertValues parameter is ignored. Please use Clear() instead.")]
public void Clear(bool revertValues)
{
Clear();
}
public void Clear()
{
if (m_Tracked != null)
{
for (int i = 0; i < m_Tracked.Count; i++)
{
if (m_Tracked[i] != null)
{
if (!Application.isPlaying && CanRecordModifications() && !s_BlockUndo)
RuntimeUndo.RecordObject(m_Tracked[i], "Driving RectTransform");
m_Tracked[i].drivenByObject = null;
m_Tracked[i].drivenProperties = DrivenTransformProperties.None;
}
}
m_Tracked.Clear();
}
}
}
[NativeHeader("Runtime/Transform/RectTransform.h"),
NativeClass("UI::RectTransform")]
public sealed class RectTransform : Transform
{
public enum Edge { Left = 0, Right = 1, Top = 2, Bottom = 3 }
public enum Axis { Horizontal = 0, Vertical = 1 }
public delegate void ReapplyDrivenProperties(RectTransform driven);
public static event ReapplyDrivenProperties reapplyDrivenProperties;
public extern Rect rect { get; }
public extern Vector2 anchorMin { get; set; }
public extern Vector2 anchorMax { get; set; }
public extern Vector2 anchoredPosition { get; set; }
public extern Vector2 sizeDelta { get; set; }
public extern Vector2 pivot { get; set; }
public Vector3 anchoredPosition3D
{
get
{
Vector2 pos2 = anchoredPosition;
return new Vector3(pos2.x, pos2.y, localPosition.z);
}
set
{
anchoredPosition = new Vector2(value.x, value.y);
Vector3 pos3 = localPosition;
pos3.z = value.z;
localPosition = pos3;
}
}
public Vector2 offsetMin
{
get
{
return anchoredPosition - Vector2.Scale(sizeDelta, pivot);
}
set
{
Vector2 offset = value - (anchoredPosition - Vector2.Scale(sizeDelta, pivot));
sizeDelta -= offset;
anchoredPosition += Vector2.Scale(offset, Vector2.one - pivot);
}
}
public Vector2 offsetMax
{
get
{
return anchoredPosition + Vector2.Scale(sizeDelta, Vector2.one - pivot);
}
set
{
Vector2 offset = value - (anchoredPosition + Vector2.Scale(sizeDelta, Vector2.one - pivot));
sizeDelta += offset;
anchoredPosition += Vector2.Scale(offset, pivot);
}
}
extern public Object drivenByObject { get; internal set; }
extern internal DrivenTransformProperties drivenProperties { get; set; }
[NativeMethod("UpdateIfTransformDispatchIsDirty")] public extern void ForceUpdateRectTransforms();
public void GetLocalCorners(Vector3[] fourCornersArray)
{
if (fourCornersArray == null || fourCornersArray.Length < 4)
{
Debug.LogError("Calling GetLocalCorners with an array that is null or has less than 4 elements.");
return;
}
Rect tmpRect = rect;
float x0 = tmpRect.x;
float y0 = tmpRect.y;
float x1 = tmpRect.xMax;
float y1 = tmpRect.yMax;
fourCornersArray[0] = new Vector3(x0, y0, 0f);
fourCornersArray[1] = new Vector3(x0, y1, 0f);
fourCornersArray[2] = new Vector3(x1, y1, 0f);
fourCornersArray[3] = new Vector3(x1, y0, 0f);
}
public void GetWorldCorners(Vector3[] fourCornersArray)
{
if (fourCornersArray == null || fourCornersArray.Length < 4)
{
Debug.LogError("Calling GetWorldCorners with an array that is null or has less than 4 elements.");
return;
}
GetLocalCorners(fourCornersArray);
Matrix4x4 mat = transform.localToWorldMatrix;
for (int i = 0; i < 4; i++)
fourCornersArray[i] = mat.MultiplyPoint(fourCornersArray[i]);
}
public void SetInsetAndSizeFromParentEdge(Edge edge, float inset, float size)
{
int axis = (edge == Edge.Top || edge == Edge.Bottom) ? 1 : 0;
bool end = (edge == Edge.Top || edge == Edge.Right);
// Set anchorMin and anchorMax to be anchored to the chosen edge.
float anchorValue = end ? 1 : 0;
Vector2 anchor = anchorMin;
anchor[axis] = anchorValue;
anchorMin = anchor;
anchor = anchorMax;
anchor[axis] = anchorValue;
anchorMax = anchor;
// Set size. Since anchors are together, size and sizeDelta means the same in this case.
Vector2 sizeD = sizeDelta;
sizeD[axis] = size;
sizeDelta = sizeD;
// Set inset.
Vector2 positionCopy = anchoredPosition;
positionCopy[axis] = end ? -inset - size * (1 - pivot[axis]) : inset + size * pivot[axis];
anchoredPosition = positionCopy;
}
public void SetSizeWithCurrentAnchors(Axis axis, float size)
{
int i = (int)axis;
Vector2 sizeD = sizeDelta;
sizeD[i] = size - GetParentSize()[i] * (anchorMax[i] - anchorMin[i]);
sizeDelta = sizeD;
}
[RequiredByNativeCode]
internal static void SendReapplyDrivenProperties(RectTransform driven)
{
reapplyDrivenProperties?.Invoke(driven);
}
// Return rect relative to lower left corner of parent rect
internal Rect GetRectInParentSpace()
{
Rect rectResult = rect;
Vector2 offset = offsetMin + Vector2.Scale(pivot, rectResult.size);
if (transform.parent)
{
RectTransform parentRect = transform.parent.GetComponent<RectTransform>();
if (parentRect)
offset += Vector2.Scale(anchorMin, parentRect.rect.size);
}
rectResult.x += offset.x;
rectResult.y += offset.y;
return rectResult;
}
private Vector2 GetParentSize()
{
RectTransform parentRect = parent as RectTransform;
if (!parentRect)
return Vector2.zero;
return parentRect.rect.size;
}
}
}