-
Notifications
You must be signed in to change notification settings - Fork 2.5k
/
MaskField.cs
683 lines (594 loc) · 25.1 KB
/
MaskField.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
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
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
// 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 System.Text;
using Unity.Properties;
using UnityEngine;
using UnityEngine.Pool;
using UnityEngine.UIElements;
namespace UnityEditor.UIElements
{
/// <summary>
/// Base class implementing the shared functionality for editing bit mask values. For more information, refer to [[wiki:UIE-uxml-element-MaskField|UXML element MaskField]].
/// </summary>
public abstract class BaseMaskField<TChoice> : BasePopupField<TChoice, string>
{
internal static readonly BindingId choicesMasksProperty = nameof(choicesMasks);
internal abstract TChoice MaskToValue(int newMask);
internal abstract int ValueToMask(TChoice value);
static readonly int s_NothingIndex = 0;
static readonly int s_EverythingIndex = 1;
static readonly int s_TotalIndex = 2;
static readonly string s_MixedLabel = L10n.Tr("Mixed...");
static readonly string s_EverythingLabel = L10n.Tr("Everything");
static readonly string s_NothingLabel = L10n.Tr("Nothing");
// This is the list of string representing all the user choices
List<string> m_UserChoices;
// This is the list of masks for every specific choice... if null, the mask will be computed with the default values
// More details about this :
// In IMGUI, the MaskField is only allowing the creation of the field with an array of choices that will have a mask
// based on power of 2 value starting from 1.
// However, the LayerMaskField is created based on the Layers and do not necessarily has power of 2 consecutive masks.
// Therefore, this specific internal field (in IMGUI...) is requiring a specific array to contain the mask value of the
// actual list of choices.
List<int> m_UserChoicesMasks;
// This is containing a mask to cover all the choices from the list. Computed with the help of m_UserChoicesMasks
// or based on the power of 2 mask values.
int m_FullChoiceMask;
internal int fullChoiceMask => m_FullChoiceMask;
internal BaseMaskField(string label) : base(label)
{
textElement.RegisterCallback<GeometryChangedEvent>(OnTextElementGeometryChanged);
m_AutoCloseMenu = false;
}
private void OnTextElementGeometryChanged(GeometryChangedEvent evt)
{
var mask = ValueToMask(value);
switch (mask)
{
case 0:
case ~0:
// Don't do anything for Nothing or Everything
break;
default:
// Mixed values
if (!IsPowerOf2(mask))
{
// If the current text is "Mixed..." and we now have more space, we might need to check if the
// actual values would fit.
// If the current label contains the actual values and we now have less space, we might need to
// change it to "Mixed..."
if (textElement.text == s_MixedLabel && evt.oldRect.width < evt.newRect.width
|| textElement.text != s_MixedLabel && evt.oldRect.width > evt.newRect.width)
{
textElement.text = GetMixedString();
}
}
break;
}
}
/// <summary>
/// The list of choices to display in the popup menu.
/// </summary>
public override List<string> choices
{
get { return m_UserChoices; }
set
{
if (value == null)
{
throw new ArgumentNullException(nameof(value));
}
// Keep the original list in a separate user list ...
if (m_UserChoices == null)
{
m_UserChoices = new List<string>();
}
else
{
m_UserChoices.Clear();
}
m_UserChoices.AddRange(value);
// Now, add the nothing and everything choices...
if (m_Choices == null)
{
m_Choices = new List<string>();
}
else
{
m_Choices.Clear();
}
ComputeFullChoiceMask();
m_Choices.Add(GetNothingName());
m_Choices.Add(GetEverythingName());
m_Choices.AddRange(m_UserChoices);
// Make sure to update the text displayed
SetValueWithoutNotify(rawValue);
NotifyPropertyChanged(choicesProperty);
}
}
internal virtual string GetNothingName()
{
return s_NothingLabel;
}
internal virtual string GetEverythingName()
{
return s_EverythingLabel;
}
/// <summary>
/// The list of list of masks for every specific choice to display in the popup menu.
/// </summary>
[CreateProperty]
public virtual List<int> choicesMasks
{
get { return m_UserChoicesMasks; }
set
{
if (value == null)
{
m_UserChoicesMasks = null;
}
else
{
// Keep the original list in a separate user list ...
if (m_UserChoicesMasks == null)
{
m_UserChoicesMasks = new List<int>();
}
else
{
m_UserChoicesMasks.Clear();
}
m_UserChoicesMasks.AddRange(value);
ComputeFullChoiceMask();
// Make sure to update the text displayed
SetValueWithoutNotify(rawValue);
}
NotifyPropertyChanged(choicesMasksProperty);
}
}
void ComputeFullChoiceMask()
{
// Compute the full mask for all the items... it is not necessarily ~0 (which is all bits set to 1)
if (m_UserChoices.Count == 0)
{
m_FullChoiceMask = 0;
}
else
{
if ((m_UserChoicesMasks != null) && (m_UserChoicesMasks.Count == m_UserChoices.Count))
{
if (m_UserChoices.Count >= (sizeof(int) * 8))
{
m_FullChoiceMask = ~0;
}
else
{
m_FullChoiceMask = 0;
foreach (int itemMask in m_UserChoicesMasks)
{
if (itemMask == ~0)
{
continue;
}
m_FullChoiceMask |= itemMask;
}
}
}
else
{
if (m_UserChoices.Count >= (sizeof(int) * 8))
{
m_FullChoiceMask = ~0;
}
else
{
m_FullChoiceMask = (1 << m_UserChoices.Count) - 1;
}
}
}
}
// Trick to get the number of selected values...
// A power of 2 number means only 1 selected...
internal bool IsPowerOf2(int itemIndex)
{
return ((itemIndex & (itemIndex - 1)) == 0);
}
internal override string GetValueToDisplay()
{
return GetDisplayedValue(ValueToMask(value));
}
internal override string GetListItemToDisplay(TChoice item)
{
return GetDisplayedValue(ValueToMask(item));
}
internal string GetDisplayedValue(int itemIndex)
{
var newValueToShowUser = "";
switch (itemIndex)
{
case 0:
newValueToShowUser = m_Choices[s_NothingIndex];
break;
case ~0:
newValueToShowUser = m_Choices[s_EverythingIndex];
break;
default:
// Show up the right selected value
if (IsPowerOf2(itemIndex))
{
var indexOfValue = 0;
if (m_UserChoicesMasks != null)
{
// Find the actual index of the selected choice...
foreach (int itemMask in m_UserChoicesMasks)
{
if (itemMask != ~0 && ((itemMask & itemIndex) == itemIndex))
{
indexOfValue = m_UserChoicesMasks.IndexOf(itemMask);
break;
}
}
}
else
{
while ((1 << indexOfValue) != itemIndex)
{
indexOfValue++;
}
}
// To get past the Nothing + Everything choices...
indexOfValue += s_TotalIndex;
if (indexOfValue < m_Choices.Count)
{
newValueToShowUser = m_Choices[indexOfValue];
}
}
else
{
if (m_UserChoicesMasks != null)
{
// Check if there's a name defined for this value
for (int i = 0; i < m_UserChoicesMasks.Count; i++)
{
var itemMask = m_UserChoicesMasks[i];
if (itemMask == itemIndex)
{
var index = i + s_TotalIndex;
newValueToShowUser = m_Choices[index];
break;
}
}
}
if (string.IsNullOrEmpty(newValueToShowUser))
{
newValueToShowUser = GetMixedString();
}
}
break;
}
return newValueToShowUser;
}
private string GetMixedString()
{
var sb = GenericPool<StringBuilder>.Get();
foreach (var item in m_Choices)
{
var maskOfItem = GetMaskValueOfItem(item);
if (!IsItemSelected(maskOfItem))
{
continue;
}
if (sb.Length > 0)
{
sb.Append(", ");
}
sb.Append(item);
}
var mixedString = sb.ToString();
var minSize = textElement.MeasureTextSize(mixedString, 0, MeasureMode.Undefined, 0, MeasureMode.Undefined);
// If text doesn't fit, we use "Mixed..."
if (float.IsNaN(textElement.resolvedStyle.width) || minSize.x > textElement.resolvedStyle.width)
{
mixedString = s_MixedLabel;
}
sb.Clear();
GenericPool<StringBuilder>.Release(sb);
return mixedString;
}
public override TChoice value
{
get => base.value;
set
{
// We need to convert the value to an accepted mask value so that the comparision with the old value works (UUM-56605)
// For example, if the value is null, we need to convert it to the mask value of the Nothing choice or it will be considered as different.
base.value = MaskToValue(UpdateMaskIfEverything(ValueToMask(value)));
}
}
public override void SetValueWithoutNotify(TChoice newValue)
{
base.SetValueWithoutNotify(MaskToValue(UpdateMaskIfEverything(ValueToMask(newValue))));
}
internal override void AddMenuItems(IGenericMenu menu)
{
if (menu == null)
{
throw new ArgumentNullException(nameof(menu));
}
foreach (var item in m_Choices)
{
var maskOfItem = GetMaskValueOfItem(item);
var isSelected = IsItemSelected(maskOfItem) && !showMixedValue;
menu.AddItem(GetListItemToDisplay(MaskToValue(maskOfItem)), isSelected, () => ChangeValueFromMenu(item));
}
}
private bool IsItemSelected(int maskOfItem)
{
int valueMask = ValueToMask(value);
if(maskOfItem == 0)
return valueMask == 0;
return (maskOfItem & valueMask) == maskOfItem;
}
private void UpdateMenuItems()
{
var menu = m_GenericMenu as GenericDropdownMenu;
if (menu == null)
return;
foreach (var item in m_Choices)
{
var maskOfItem = GetMaskValueOfItem(item);
var isSelected = IsItemSelected(maskOfItem);
menu.UpdateItem(GetListItemToDisplay(MaskToValue(maskOfItem)), isSelected);
}
}
// Based on the current mask, this is updating the value of the actual mask to use vs the full mask.
// This is returning ~0 if all the values are selected...
int UpdateMaskIfEverything(int currentMask)
{
var newMask = currentMask;
// If the mask is full, put back the Everything flag.
if (m_FullChoiceMask != 0)
{
if ((currentMask & m_FullChoiceMask) == m_FullChoiceMask)
{
newMask = ~0;
}
else
{
newMask &= m_FullChoiceMask;
}
}
return newMask;
}
internal void ChangeValueFromMenu(string menuItem)
{
var newMask = showMixedValue ? 0 : ValueToMask(value);
var maskFromItem = GetMaskValueOfItem(menuItem);
switch (maskFromItem)
{
// Nothing
case 0:
newMask = 0;
break;
// Everything
case ~0:
newMask = ~0;
break;
default:
// Make sure to have only the real selected one...
//newMask &= m_FullChoiceMask;
// Add or remove the newly selected...
if ((newMask & maskFromItem) == maskFromItem)
{
newMask &= ~maskFromItem;
}
else
{
newMask |= maskFromItem;
}
// If the mask is full, put back the Everything flag.
newMask = UpdateMaskIfEverything(newMask);
break;
}
// Finally, make sure to update the value of the mask...
value = MaskToValue(newMask);
UpdateMenuItems();
}
// Returns the mask to be used for the item...
int GetMaskValueOfItem(string item)
{
int maskValue;
var indexOfItem = m_Choices.IndexOf(item);
switch (indexOfItem)
{
case 0: // Nothing
maskValue = 0;
break;
case 1: // Everything
maskValue = ~0;
break;
default: // All others
if (indexOfItem > 0)
{
if ((m_UserChoicesMasks != null) && (m_UserChoicesMasks.Count == m_UserChoices.Count))
{
maskValue = m_UserChoicesMasks[(indexOfItem - s_TotalIndex)];
}
else
{
maskValue = 1 << (indexOfItem - s_TotalIndex);
}
}
else
{
// If less than 0, it means the item was not found...
maskValue = 0;
}
break;
}
return maskValue;
}
}
/// <summary>
/// Make a field for masks.
/// </summary>
public class MaskField : BaseMaskField<int>
{
internal override int MaskToValue(int newMask) => newMask;
internal override int ValueToMask(int value) => value;
[UnityEngine.Internal.ExcludeFromDocs, Serializable]
public new class UxmlSerializedData : BaseMaskField<int>.UxmlSerializedData
{
#pragma warning disable 649
[SerializeField] List<string> choices;
[SerializeField, UxmlIgnore, HideInInspector] UxmlAttributeFlags choices_UxmlAttributeFlags;
#pragma warning restore 649
public override object CreateInstance() => new MaskField();
public override void Deserialize(object obj)
{
base.Deserialize(obj);
// Assigning null value throws.
if (ShouldWriteAttributeValue(choices_UxmlAttributeFlags) && choices != null)
{
var e = (MaskField)obj;
e.choices = choices;
}
}
}
/// <summary>
/// Instantiates a <see cref="MaskField"/> using the data read from a UXML file.
/// </summary>
[Obsolete("UxmlFactory is deprecated and will be removed. Use UxmlElementAttribute instead.", false)]
public new class UxmlFactory : UxmlFactory<MaskField, UxmlTraits> {}
/// <summary>
/// Defines <see cref="UxmlTraits"/> for the <see cref="MaskField"/>.
/// </summary>
[Obsolete("UxmlTraits is deprecated and will be removed. Use UxmlElementAttribute instead.", false)]
public new class UxmlTraits : BasePopupField<int, UxmlIntAttributeDescription>.UxmlTraits
{
UxmlStringAttributeDescription m_MaskChoices = new UxmlStringAttributeDescription { name = "choices" };
UxmlIntAttributeDescription m_MaskValue = new UxmlIntAttributeDescription { name = "value" };
/// <summary>
/// Initializes the <see cref="UxmlTraits"/> for <see cref="MaskField"/>.
/// </summary>
/// <param name="ve">The VisualElement that will be populated.</param>
/// <param name="bag">The bag from where the attributes are taken.</param>
/// <param name="cc">The creation context, unused.</param>
public override void Init(VisualElement ve, IUxmlAttributes bag, CreationContext cc)
{
var f = (MaskField)ve;
string choicesFromBag = m_MaskChoices.GetValueFromBag(bag, cc);
var listOfChoices = UxmlUtility.ParseStringListAttribute(choicesFromBag);
if (listOfChoices != null && listOfChoices.Count > 0)
{
f.choices = listOfChoices;
}
// The mask is simply an int
f.SetValueWithoutNotify(m_MaskValue.GetValueFromBag(bag, cc));
base.Init(ve, bag, cc);
}
}
/// <summary>
/// Callback that provides a string representation used to display the selected value.
/// </summary>
public virtual Func<string, string> formatSelectedValueCallback
{
get { return m_FormatSelectedValueCallback; }
set
{
m_FormatSelectedValueCallback = value;
textElement.text = GetValueToDisplay();
}
}
/// <summary>
/// Callback that provides a string representation used to populate the popup menu.
/// </summary>
public virtual Func<string, string> formatListItemCallback
{
get { return m_FormatListItemCallback; }
set { m_FormatListItemCallback = value; }
}
internal override string GetListItemToDisplay(int itemIndex)
{
string displayedValue = GetDisplayedValue(itemIndex);
if (ShouldFormatListItem(itemIndex))
displayedValue = m_FormatListItemCallback(displayedValue);
return displayedValue;
}
internal override string GetValueToDisplay()
{
string displayedValue = showMixedValue ? mixedValueString : GetDisplayedValue(rawValue);
if (ShouldFormatSelectedValue())
displayedValue = m_FormatSelectedValueCallback(displayedValue);
return displayedValue;
}
internal bool ShouldFormatListItem(int itemIndex)
{
return itemIndex != 0 && itemIndex != -1 && m_FormatListItemCallback != null;
}
internal bool ShouldFormatSelectedValue()
{
return rawValue != 0 && rawValue != -1 && m_FormatSelectedValueCallback != null && IsPowerOf2(rawValue);
}
/// <summary>
/// USS class name of elements of this type.
/// </summary>
public new static readonly string ussClassName = "unity-mask-field";
/// <summary>
/// USS class name of labels in elements of this type.
/// </summary>
public new static readonly string labelUssClassName = ussClassName + "__label";
/// <summary>
/// USS class name of input elements in elements of this type.
/// </summary>
public new static readonly string inputUssClassName = ussClassName + "__input";
/// <summary>
/// Initializes and returns an instance of MaskField.
/// </summary>
/// <param name="choices">A list of choices to populate the field.</param>
/// <param name="defaultValue">The initial mask value for this field.</param>
/// <param name="formatSelectedValueCallback">A callback to format the selected value. Unity calls this method automatically when a new value is selected in the field..</param>
/// <param name="formatListItemCallback">The initial mask value this field should use. Unity calls this method automatically when displaying choices for the field.</param>
public MaskField(List<string> choices, int defaultMask, Func<string, string> formatSelectedValueCallback = null, Func<string, string> formatListItemCallback = null)
: this(null, choices, defaultMask, formatSelectedValueCallback, formatListItemCallback)
{
}
/// <summary>
/// Initializes and returns an instance of MaskField.
/// </summary>
/// <param name="label">The text to use as a label for the field.</param>
/// <param name="choices">A list of choices to populate the field.</param>
/// <param name="defaultValue">The initial mask value for this field.</param>
/// <param name="formatSelectedValueCallback">A callback to format the selected value. Unity calls this method automatically when a new value is selected in the field..</param>
/// <param name="formatListItemCallback">The initial mask value this field should use. Unity calls this method automatically when displaying choices for the field.</param>
public MaskField(string label, List<string> choices, int defaultMask, Func<string, string> formatSelectedValueCallback = null, Func<string, string> formatListItemCallback = null)
: this(label)
{
this.choices = choices;
m_FormatListItemCallback = formatListItemCallback;
m_FormatSelectedValueCallback = formatSelectedValueCallback;
SetValueWithoutNotify(defaultMask);
this.formatListItemCallback = formatListItemCallback;
this.formatSelectedValueCallback = formatSelectedValueCallback;
}
/// <summary>
/// Initializes and returns an instance of MaskField.
/// </summary>
public MaskField()
: this(null) {}
/// <summary>
/// Initializes and returns an instance of MaskField.
/// </summary>
/// <param name="label">The text to use as a label for the field.</param>
public MaskField(string label)
: base(label)
{
AddToClassList(ussClassName);
labelElement.AddToClassList(labelUssClassName);
visualInput.AddToClassList(inputUssClassName);
}
}
}