forked from Unity-Technologies/UnityCsReference
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFlowLayout.cs
327 lines (286 loc) · 10.9 KB
/
FlowLayout.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
// Unity C# reference source
// Copyright (c) Unity Technologies. For terms of use, see
// https://unity3d.com/legal/licenses/Unity_Reference_Only_License
using UnityEngine;
using UnityEditor;
using System.Collections.Generic;
using System.Collections;
using System.Reflection;
using UnityEditorInternal;
namespace UnityEditor
{
class FlowLayout : GUILayoutGroup
{
struct LineInfo
{
public float minSize, maxSize;
public float start, size;
public int topBorder, bottomBorder;
}
int m_Lines;
LineInfo[] m_LineInfo;
public override void CalcWidth()
{
bool hasMinWidth = minWidth != 0;
base.CalcWidth();
if (isVertical)
{
// DONT
}
else
{
// Margin handling is somewhat different from other controls.
// Since we don't know what will wrap where we'll just take the Min margin of all child elements
if (!hasMinWidth)
{
minWidth = 0;
foreach (GUILayoutEntry i in entries)
{
// Here we should probably include margins of the element, but that does seem kind of annoying
minWidth = Mathf.Max(m_ChildMinWidth, i.minWidth);
}
}
}
}
public override void SetHorizontal(float x, float width)
{
// Apply the base. Now everything is in one line (or column), and all we need is to insert the linebreaks
base.SetHorizontal(x, width);
if (resetCoords)
x = 0;
if (isVertical)
{
Debug.LogError("Wordwrapped vertical group. Don't. Just Don't");
}
else
{ // we're horizontally laid out:
// apply margins/padding here
// If we have a style, adjust the sizing to take care of padding (if we don't the horizontal margins have been propagated fully up the hierarchy)...
// Set the positions
m_Lines = 0;
float pulledOffset = 0; // How far we need to pull each item back.
foreach (GUILayoutEntry i in entries)
{
if (i.rect.xMax - pulledOffset > x + width)
{
// TODO: When we move a line back, we should re-expand
pulledOffset = i.rect.x - i.marginLeft;
m_Lines++;
}
i.SetHorizontal(i.rect.x - pulledOffset, i.rect.width);
i.rect.y = m_Lines;
}
m_Lines++;
}
}
public override void CalcHeight()
{
if (entries.Count == 0)
{
maxHeight = minHeight = 0;
return;
}
m_ChildMinHeight = m_ChildMaxHeight = 0;
int _topMarginMin = 0, _bottomMarginMin = 0;
m_StretchableCountY = 0;
if (isVertical)
{
}
else
{
m_LineInfo = new LineInfo[m_Lines];
for (int i = 0; i < m_Lines; i++)
{
m_LineInfo[i].topBorder = 10000;
m_LineInfo[i].bottomBorder = 10000;
}
// Figure out border values for each line
foreach (GUILayoutEntry i in entries)
{
i.CalcHeight();
int j = (int)i.rect.y;
m_LineInfo[j].minSize = Mathf.Max(i.minHeight, m_LineInfo[j].minSize);
m_LineInfo[j].maxSize = Mathf.Max(i.maxHeight, m_LineInfo[j].maxSize);
m_LineInfo[j].topBorder = Mathf.Min(i.marginTop, m_LineInfo[j].topBorder);
m_LineInfo[j].bottomBorder = Mathf.Min(i.marginBottom, m_LineInfo[j].bottomBorder);
}
for (int i = 0; i < m_Lines; i++)
{
m_ChildMinHeight += m_LineInfo[i].minSize;
m_ChildMaxHeight += m_LineInfo[i].maxSize;
}
// Add in the the extra lines
for (int i = 1; i < m_Lines; i++)
{
float space = Mathf.Max(m_LineInfo[i - 1].bottomBorder, m_LineInfo[i].topBorder);
m_ChildMinHeight += space;
m_ChildMaxHeight += space;
}
_topMarginMin = m_LineInfo[0].topBorder;
_bottomMarginMin = m_LineInfo[m_LineInfo.Length - 1].bottomBorder;
}
// Do the dance between children & parent for haggling over how many empty pixels to have
float firstPadding, lastPadding;
m_MarginTop = _topMarginMin;
m_MarginBottom = _bottomMarginMin;
firstPadding = lastPadding = 0;
minHeight = Mathf.Max(minHeight, m_ChildMinHeight + firstPadding + lastPadding);
if (maxHeight == 0)
{
stretchHeight += m_StretchableCountY + (style.stretchHeight ? 1 : 0);
maxHeight = m_ChildMaxHeight + firstPadding + lastPadding;
}
else
{
stretchHeight = 0;
}
maxHeight = Mathf.Max(maxHeight, minHeight);
}
public override void SetVertical(float y, float height)
{
if (entries.Count == 0)
{
base.SetVertical(y, height);
return;
}
if (isVertical)
{
base.SetVertical(y, height);
}
else
{
if (resetCoords)
y = 0;
float clientY, clientHeight;
clientY = y - marginTop;
clientHeight = y + marginVertical;
// Figure out how to distribute the elements between the different lines
float heightToDistribute = clientHeight - spacing * (m_Lines - 1);
float minMaxScale = 0;
if (m_ChildMinHeight != m_ChildMaxHeight)
minMaxScale = Mathf.Clamp((heightToDistribute - m_ChildMinHeight) / (m_ChildMaxHeight - m_ChildMinHeight), 0, 1);
float lineY = clientY;
for (int i = 0; i < m_Lines; i++)
{
if (i > 0)
lineY += Mathf.Max(m_LineInfo[i].topBorder, m_LineInfo[i - 1].bottomBorder);
m_LineInfo[i].start = lineY;
m_LineInfo[i].size = Mathf.Lerp(m_LineInfo[i].minSize, m_LineInfo[i].maxSize, minMaxScale);
lineY += m_LineInfo[i].size + spacing;
}
foreach (GUILayoutEntry i in entries)
{
LineInfo li = m_LineInfo[(int)i.rect.y];
if (i.stretchHeight != 0)
i.SetVertical(li.start + i.marginTop, li.size - i.marginVertical);
else
i.SetVertical(li.start + i.marginTop, Mathf.Clamp(li.size - i.marginVertical, i.minHeight, i.maxHeight));
}
}
}
}
// @TODO Make this serialize
// @TODO Handle animate-from nothing (with fade?)
// @TODO Figure out how to implement fade-away of contents
// @TODO Switch a bunch of others to use this
internal class GUISlideGroup
{
internal static GUISlideGroup current = null;
Dictionary<int, Rect> animIDs = new Dictionary<int, Rect>();
const float kLerp = .1f;
const float kSnap = .5f;
public void Begin()
{
if (current != null)
{
Debug.LogError("You cannot nest animGroups");
return;
}
current = this;
}
public void End()
{
current = null;
}
public void Reset()
{
current = null;
animIDs.Clear();
}
public Rect BeginHorizontal(int id, params GUILayoutOption[] options)
{
SlideGroupInternal g = (SlideGroupInternal)GUILayoutUtility.BeginLayoutGroup(GUIStyle.none, options, typeof(SlideGroupInternal));
g.SetID(this, id);
g.isVertical = false;
return g.m_FinalRect;
}
public void EndHorizontal()
{
GUILayoutUtility.EndLayoutGroup();
}
public Rect GetRect(int id, Rect r)
{
bool dummy;
if (Event.current.type != EventType.Repaint)
return r;
return GetRect(id, r, out dummy);
}
Rect GetRect(int id, Rect r, out bool changed)
{
if (!animIDs.ContainsKey(id))
{
animIDs.Add(id, r);
changed = false;
return r;
}
Rect current = animIDs[id];
if (current.y != r.y || current.height != r.height || current.x != r.x || current.width != r.width)
{
float lerp = kLerp;
if (Mathf.Abs(current.y - r.y) > kSnap)
r.y = Mathf.Lerp(current.y, r.y, lerp);
if (Mathf.Abs(current.height - r.height) > kSnap)
r.height = Mathf.Lerp(current.height, r.height, lerp);
if (Mathf.Abs(current.x - r.x) > kSnap)
r.x = Mathf.Lerp(current.x, r.x, lerp);
if (Mathf.Abs(current.width - r.width) > kSnap)
r.width = Mathf.Lerp(current.width, r.width, lerp);
animIDs[id] = r;
changed = true;
HandleUtility.Repaint();
}
else
changed = false;
return r;
}
class SlideGroupInternal : GUILayoutGroup
{
int m_ID;
GUISlideGroup m_Owner;
#pragma warning disable 649
internal Rect m_FinalRect;
public void SetID(GUISlideGroup owner, int id)
{
m_ID = id;
m_Owner = owner;
}
public override void SetHorizontal(float x, float width)
{
m_FinalRect.x = x;
m_FinalRect.width = width;
base.SetHorizontal(x, width);
}
public override void SetVertical(float y, float height)
{
m_FinalRect.y = y;
m_FinalRect.height = height;
Rect r = new Rect(rect.x, y, rect.width, height);
bool changed;
r = m_Owner.GetRect(m_ID, r, out changed);
if (changed)
base.SetHorizontal(r.x, r.width);
base.SetVertical(r.y, r.height);
}
}
}
} // namespace