forked from Unity-Technologies/UnityCsReference
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBoxModelView.cs
More file actions
563 lines (470 loc) · 22.4 KB
/
Copy pathBoxModelView.cs
File metadata and controls
563 lines (470 loc) · 22.4 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
558
559
560
561
562
563
// 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 UnityEditor;
using UnityEngine;
using UnityEngine.UIElements;
using UnityEngine.UIElements.StyleSheets;
namespace UnityEditor.UIElements.Debugger
{
internal class BoxModelView : VisualElement
{
enum BoxType
{
Margin,
Border,
Padding,
Content
}
enum SideChanged
{
Left,
Right,
Top,
Bottom
}
private HighlightOverlayPainter m_OverlayPainter;
private VisualElement m_Container;
private VisualElement m_Layer1;
private VisualElement m_Layer2;
private BoxModelElement m_MarginBox;
private BoxModelElement m_BorderBox;
private BoxModelElement m_PaddingBox;
private ContentBox m_ContentBox;
private VisualElement m_TopTextFieldMarginContainer = new VisualElement();
private VisualElement m_TopTextFieldBorderContainer = new VisualElement();
private VisualElement m_TopTextFieldPaddingContainer = new VisualElement();
private VisualElement m_BottomTextFieldMarginContainer = new VisualElement();
private VisualElement m_BottomTextFieldBorderContainer = new VisualElement();
private VisualElement m_BottomTextFieldPaddingContainer = new VisualElement();
private VisualElement m_ContentSpacer = new VisualElement();
private VisualElement m_SelectedElement;
public VisualElement selectedElement
{
get { return m_SelectedElement; }
set { SelectElement(value); }
}
public BoxModelView()
{
AddToClassList("box-model-view");
visible = false;
m_OverlayPainter = new HighlightOverlayPainter();
m_Container = new VisualElement();
m_Container.AddToClassList("box-model-view-container");
m_Layer1 = new VisualElement() { name = "BoxModelViewLayer1"};
m_Layer2 = new VisualElement() { name = "BoxModelViewLayer2"};
m_Layer2.style.position = Position.Absolute;
m_Layer2.pickingMode = PickingMode.Ignore;
m_Layer2.StretchToParentSize();
m_TopTextFieldMarginContainer.pickingMode = PickingMode.Ignore;
m_TopTextFieldMarginContainer.AddToClassList("box-model-textfield-top-bottom-spacer");
m_BottomTextFieldMarginContainer.pickingMode = PickingMode.Ignore;
m_BottomTextFieldMarginContainer.AddToClassList("box-model-textfield-top-bottom-spacer");
m_TopTextFieldBorderContainer.pickingMode = PickingMode.Ignore;
m_TopTextFieldBorderContainer.AddToClassList("box-model-textfield-top-bottom-spacer");
m_BottomTextFieldBorderContainer.pickingMode = PickingMode.Ignore;
m_BottomTextFieldBorderContainer.AddToClassList("box-model-textfield-top-bottom-spacer");
m_TopTextFieldPaddingContainer.pickingMode = PickingMode.Ignore;
m_TopTextFieldPaddingContainer.AddToClassList("box-model-textfield-top-bottom-spacer");
m_BottomTextFieldPaddingContainer.pickingMode = PickingMode.Ignore;
m_BottomTextFieldPaddingContainer.AddToClassList("box-model-textfield-top-bottom-spacer");
m_ContentSpacer.pickingMode = PickingMode.Ignore;
m_ContentSpacer.AddToClassList("box-model-textfield-top-bottom-spacer");
m_Layer2.Add(m_TopTextFieldMarginContainer);
m_Layer2.Add(m_TopTextFieldBorderContainer);
m_Layer2.Add(m_TopTextFieldPaddingContainer);
m_Layer2.Add(m_ContentSpacer);
m_Layer2.Add(m_BottomTextFieldPaddingContainer);
m_Layer2.Add(m_BottomTextFieldBorderContainer);
m_Layer2.Add(m_BottomTextFieldMarginContainer);
m_ContentBox = new ContentBox();
m_ContentBox.AddToClassList("box-model");
m_ContentBox.AddToClassList("box-model-container-content");
m_ContentBox.RegisterCallback<MouseOverEvent, BoxType>(OnMouseOver, BoxType.Content);
m_PaddingBox = new BoxModelElement(BoxType.Padding, m_ContentBox,
m_TopTextFieldPaddingContainer, m_BottomTextFieldPaddingContainer);
m_PaddingBox.AddToClassList("box-model");
m_PaddingBox.AddToClassList("box-model-container-padding");
m_PaddingBox.RegisterCallback<MouseOverEvent, BoxType>(OnMouseOver, BoxType.Padding);
m_BorderBox = new BoxModelElement(BoxType.Border, m_PaddingBox,
m_TopTextFieldBorderContainer, m_BottomTextFieldBorderContainer);
m_BorderBox.AddToClassList("box-model");
m_BorderBox.AddToClassList("box-model-container-border");
m_BorderBox.RegisterCallback<MouseOverEvent, BoxType>(OnMouseOver, BoxType.Border);
m_MarginBox = new BoxModelElement(BoxType.Margin, m_BorderBox,
m_TopTextFieldMarginContainer, m_BottomTextFieldMarginContainer);
m_MarginBox.AddToClassList("box-model");
m_MarginBox.AddToClassList("box-model-container-margin");
m_MarginBox.RegisterCallback<MouseOverEvent, BoxType>(OnMouseOver, BoxType.Margin);
m_Layer1.Add(m_MarginBox);
m_Container.Add(m_Layer1);
m_Container.Add(m_Layer2);
var spacerLeft = new VisualElement() { style = { flexGrow = 1 } };
var spacerRight = new VisualElement() { style = { flexGrow = 1 } };
Add(spacerLeft);
Add(m_Container);
Add(spacerRight);
RegisterCallback<MouseOutEvent>(OnMouseOut);
}
public void Refresh(MeshGenerationContext mgc)
{
m_OverlayPainter.Draw(mgc);
m_MarginBox.SyncValues();
m_BorderBox.SyncValues();
m_PaddingBox.SyncValues();
m_ContentBox.SyncValues();
}
private void SelectElement(VisualElement ve)
{
m_SelectedElement = ve;
visible = m_SelectedElement != null;
m_MarginBox.SelectElement(ve);
m_BorderBox.SelectElement(ve);
m_PaddingBox.SelectElement(ve);
m_ContentBox.SelectElement(ve);
}
private void OnMouseOver(MouseOverEvent evt, BoxType boxType)
{
if (m_SelectedElement == null)
return;
m_OverlayPainter.ClearOverlay();
switch (boxType)
{
case BoxType.Margin:
m_OverlayPainter.AddOverlay(m_SelectedElement, OverlayContent.Margin);
m_MarginBox.RemoveFromClassList("box-model-white");
m_BorderBox.AddToClassList("box-model-white");
m_PaddingBox.AddToClassList("box-model-white");
m_ContentBox.AddToClassList("box-model-white");
break;
case BoxType.Border:
m_OverlayPainter.AddOverlay(m_SelectedElement, OverlayContent.Border);
m_BorderBox.RemoveFromClassList("box-model-white");
m_MarginBox.AddToClassList("box-model-white");
m_PaddingBox.AddToClassList("box-model-white");
m_ContentBox.AddToClassList("box-model-white");
break;
case BoxType.Padding:
m_OverlayPainter.AddOverlay(m_SelectedElement, OverlayContent.Padding);
m_MarginBox.AddToClassList("box-model-white");
m_PaddingBox.RemoveFromClassList("box-model-white");
m_BorderBox.AddToClassList("box-model-white");
m_ContentBox.AddToClassList("box-model-white");
break;
case BoxType.Content:
m_OverlayPainter.AddOverlay(m_SelectedElement, OverlayContent.Content);
m_ContentBox.RemoveFromClassList("box-model-white");
m_MarginBox.AddToClassList("box-model-white");
m_PaddingBox.AddToClassList("box-model-white");
m_BorderBox.AddToClassList("box-model-white");
break;
}
m_SelectedElement.panel?.visualTree.MarkDirtyRepaint();
evt.StopPropagation();
}
private void OnMouseOut(MouseOutEvent evt)
{
m_OverlayPainter.ClearOverlay();
m_MarginBox.RemoveFromClassList("box-model-white");
m_BorderBox.RemoveFromClassList("box-model-white");
m_PaddingBox.RemoveFromClassList("box-model-white");
m_ContentBox.RemoveFromClassList("box-model-white");
m_SelectedElement.panel?.visualTree.MarkDirtyRepaint();
evt.StopPropagation();
}
class BoxModelElement : VisualElement
{
private Label m_Title;
private VisualElement m_Left;
private VisualElement m_Right;
private VisualElement m_Center;
private VisualElement m_CenterTop;
private VisualElement m_CenterContent;
private VisualElement m_CenterBottom;
private IntegerField m_LeftTextField;
private IntegerField m_RightTextField;
private IntegerField m_TopTextField;
private IntegerField m_BottomTextField;
// For layout purpose since real TextField are in another layer
private IntegerField m_FakeTopTextField;
private IntegerField m_FakeBottomTextField;
private VisualElement m_SelectedElement;
public BoxModelElement(BoxType boxType, VisualElement content,
VisualElement topTextFieldContainer, VisualElement bottomTextFieldContainer)
{
string title = "";
switch (boxType)
{
case BoxType.Margin:
title = "margin";
break;
case BoxType.Border:
title = "border";
break;
case BoxType.Padding:
title = "padding";
break;
}
this.boxType = boxType;
m_Title = new Label(title);
m_Title.AddToClassList("box-model-title");
m_CenterContent = content;
m_CenterContent.AddToClassList("box-model-center-content");
m_LeftTextField = new IntegerField();
m_LeftTextField.AddToClassList("box-model-textfield");
m_LeftTextField.RegisterValueChangedCallback(e => OnTextFieldValueChanged(e, SideChanged.Left));
m_RightTextField = new IntegerField();
m_RightTextField.AddToClassList("box-model-textfield");
m_RightTextField.RegisterValueChangedCallback(e => OnTextFieldValueChanged(e, SideChanged.Right));
m_TopTextField = new IntegerField();
m_TopTextField.AddToClassList("box-model-textfield");
m_TopTextField.RegisterValueChangedCallback(e => OnTextFieldValueChanged(e, SideChanged.Top));
m_FakeTopTextField = new IntegerField();
m_FakeTopTextField.AddToClassList("box-model-textfield");
m_FakeTopTextField.visible = false;
m_BottomTextField = new IntegerField();
m_BottomTextField.AddToClassList("box-model-textfield");
m_BottomTextField.RegisterValueChangedCallback(e => OnTextFieldValueChanged(e, SideChanged.Bottom));
m_FakeBottomTextField = new IntegerField();
m_FakeBottomTextField.AddToClassList("box-model-textfield");
m_FakeBottomTextField.visible = false;
m_Left = new VisualElement();
m_Left.AddToClassList("box-model-side");
m_Left.Add(m_LeftTextField);
m_Right = new VisualElement();
m_Right.AddToClassList("box-model-side");
m_Right.Add(m_RightTextField);
m_Center = new VisualElement();
m_Center.AddToClassList("box-model-center");
m_CenterTop = new VisualElement();
m_CenterTop.AddToClassList("box-model-center-top");
m_CenterTop.Add(m_FakeTopTextField);
topTextFieldContainer.Add(m_TopTextField);
m_CenterBottom = new VisualElement();
m_CenterBottom.AddToClassList("box-model-center-bottom");
m_CenterBottom.Add(m_FakeBottomTextField);
bottomTextFieldContainer.Add(m_BottomTextField);
m_Center.Add(m_CenterTop);
m_Center.Add(m_CenterContent);
m_Center.Add(m_CenterBottom);
Add(m_Title);
Add(m_Left);
Add(m_Center);
Add(m_Right);
// Sync styles values
schedule.Execute(SyncValues).Every(100);
}
public BoxType boxType { get; private set; }
public void SelectElement(VisualElement ve)
{
m_SelectedElement = ve;
InitStyleValues();
}
private void InitStyleValues()
{
if (m_SelectedElement != null)
{
switch (boxType)
{
case BoxType.Margin:
InitOnMargins();
break;
case BoxType.Border:
InitOnBorders();
break;
case BoxType.Padding:
InitOnPaddings();
break;
}
}
else
{
m_LeftTextField.SetValueWithoutNotify(0);
m_RightTextField.SetValueWithoutNotify(0);
m_TopTextField.SetValueWithoutNotify(0);
m_BottomTextField.SetValueWithoutNotify(0);
}
}
private void InitOnMargins()
{
var selectedStyle = m_SelectedElement.resolvedStyle;
var value = (int)selectedStyle.marginLeft;
if (m_LeftTextField.value != value)
m_LeftTextField.SetValueWithoutNotify(value);
value = (int)selectedStyle.marginRight;
if (m_RightTextField.value != value)
m_RightTextField.SetValueWithoutNotify(value);
value = (int)selectedStyle.marginTop;
if (m_TopTextField.value != value)
m_TopTextField.SetValueWithoutNotify(value);
value = (int)selectedStyle.marginBottom;
if (m_BottomTextField.value != value)
m_BottomTextField.SetValueWithoutNotify(value);
}
private void InitOnBorders()
{
var selectedStyle = m_SelectedElement.resolvedStyle;
var value = (int)selectedStyle.borderLeftWidth;
if (m_LeftTextField.value != value)
m_LeftTextField.SetValueWithoutNotify(value);
value = (int)selectedStyle.borderRightWidth;
if (m_RightTextField.value != value)
m_RightTextField.SetValueWithoutNotify(value);
value = (int)selectedStyle.borderTopWidth;
if (m_TopTextField.value != value)
m_TopTextField.SetValueWithoutNotify(value);
value = (int)selectedStyle.borderBottomWidth;
if (m_BottomTextField.value != value)
m_BottomTextField.SetValueWithoutNotify(value);
}
private void InitOnPaddings()
{
var selectedStyle = m_SelectedElement.resolvedStyle;
var value = (int)selectedStyle.paddingLeft;
if (m_LeftTextField.value != value)
m_LeftTextField.SetValueWithoutNotify(value);
value = (int)selectedStyle.paddingRight;
if (m_RightTextField.value != value)
m_RightTextField.SetValueWithoutNotify(value);
value = (int)selectedStyle.paddingTop;
if (m_TopTextField.value != value)
m_TopTextField.SetValueWithoutNotify(value);
value = (int)selectedStyle.paddingBottom;
if (m_BottomTextField.value != value)
m_BottomTextField.SetValueWithoutNotify(value);
}
private void OnTextFieldValueChanged(ChangeEvent<int> evt, SideChanged sideChanged)
{
var selectedStyle = m_SelectedElement.style;
if (boxType == BoxType.Margin)
{
switch (sideChanged)
{
case SideChanged.Left:
selectedStyle.marginLeft = evt.newValue;
break;
case SideChanged.Right:
selectedStyle.marginRight = evt.newValue;
break;
case SideChanged.Top:
selectedStyle.marginTop = evt.newValue;
break;
case SideChanged.Bottom:
selectedStyle.marginBottom = evt.newValue;
break;
}
}
else if (boxType == BoxType.Border)
{
switch (sideChanged)
{
case SideChanged.Left:
selectedStyle.borderLeftWidth = evt.newValue;
break;
case SideChanged.Right:
selectedStyle.borderRightWidth = evt.newValue;
break;
case SideChanged.Top:
selectedStyle.borderTopWidth = evt.newValue;
break;
case SideChanged.Bottom:
selectedStyle.borderBottomWidth = evt.newValue;
break;
}
}
else if (boxType == BoxType.Padding)
{
switch (sideChanged)
{
case SideChanged.Left:
selectedStyle.paddingLeft = evt.newValue;
break;
case SideChanged.Right:
selectedStyle.paddingRight = evt.newValue;
break;
case SideChanged.Top:
selectedStyle.paddingTop = evt.newValue;
break;
case SideChanged.Bottom:
selectedStyle.paddingBottom = evt.newValue;
break;
}
}
}
public void SyncValues()
{
if (m_SelectedElement != null)
{
InitStyleValues();
}
}
}
class ContentBox : VisualElement
{
private VisualElement m_TextContainer;
private IntegerField m_WidthTextField;
private IntegerField m_HeightTextField;
private Label m_XLabel;
private VisualElement m_SelectedElement;
public ContentBox()
{
m_TextContainer = new VisualElement();
m_TextContainer.AddToClassList("box-model-content-text-container");
m_WidthTextField = new IntegerField();
m_WidthTextField.AddToClassList("box-model-textfield");
m_HeightTextField = new IntegerField();
m_HeightTextField.AddToClassList("box-model-textfield");
m_XLabel = new Label("x");
m_TextContainer.Add(m_WidthTextField);
m_TextContainer.Add(m_XLabel);
m_TextContainer.Add(m_HeightTextField);
m_WidthTextField.value = 0;
m_HeightTextField.value = 0;
m_WidthTextField.RegisterValueChangedCallback(OnWidthChanged);
m_HeightTextField.RegisterValueChangedCallback(OnHeightChanged);
Add(m_TextContainer);
// Sync styles values
schedule.Execute(SyncValues).Every(100);
}
public void SelectElement(VisualElement ve)
{
m_SelectedElement = ve;
if (ve != null)
{
m_WidthTextField.SetValueWithoutNotify((int)m_SelectedElement.contentRect.width);
m_HeightTextField.SetValueWithoutNotify((int)m_SelectedElement.contentRect.height);
}
else
{
m_WidthTextField.SetValueWithoutNotify(0);
m_HeightTextField.SetValueWithoutNotify(0);
}
}
private void OnWidthChanged(ChangeEvent<int> evt)
{
var style = m_SelectedElement.resolvedStyle;
var newValue = evt.newValue + style.paddingLeft + style.paddingRight + style.borderLeftWidth + style.borderRightWidth;
m_SelectedElement.style.width = newValue;
}
private void OnHeightChanged(ChangeEvent<int> evt)
{
var style = m_SelectedElement.resolvedStyle;
var newValue = evt.newValue + style.paddingTop + style.paddingBottom + style.borderTopWidth + style.borderBottomWidth;
m_SelectedElement.style.height = newValue;
}
public void SyncValues()
{
if (m_SelectedElement != null)
{
var value = (int)m_SelectedElement.contentRect.width;
if (m_WidthTextField.value != value)
m_WidthTextField.SetValueWithoutNotify(value);
value = (int)m_SelectedElement.contentRect.height;
if (m_HeightTextField.value != value)
m_HeightTextField.SetValueWithoutNotify(value);
}
}
}
}
}