-
Notifications
You must be signed in to change notification settings - Fork 878
Expand file tree
/
Copy pathVFXBitField.cs
More file actions
145 lines (123 loc) · 4.82 KB
/
Copy pathVFXBitField.cs
File metadata and controls
145 lines (123 loc) · 4.82 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
using UnityEngine;
using UnityEngine.UIElements;
using UnityEditor.UIElements;
using System.Collections.Generic;
using System.Runtime.InteropServices;
namespace UnityEditor.VFX
{
class BitFieldAttribute : System.Attribute
{
}
}
namespace UnityEditor.VFX.UI
{
abstract class VFXBitField<T, U> : VFXControl<U>
{
protected VisualElement[] m_Buttons;
protected VisualElement m_Background;
protected Texture2D m_BitImage;
protected Texture2D m_BitBkgndImage;
protected Label m_Label;
public VFXBitField()
{
m_Buttons = new VisualElement[Marshal.SizeOf(typeof(T)) * 8];
m_Background = new VisualElement() { name = "background" };
m_Label = new Label() { name = "tip" };
Add(m_Label);
Add(m_Background);
var buttonContainer = new VisualElement() { name = "button-container", pickingMode = PickingMode.Ignore };
Add(buttonContainer);
for (int i = 0; i < m_Buttons.Length; ++i)
{
var button = new VisualElement();
button.style.flexGrow = button.style.flexShrink = 1;
button.style.marginRight = 1;
SetupListener(button, i);
buttonContainer.Add(button);
m_Buttons[i] = button;
}
VisualElement backgroundItem = null;
for (int i = 0; i < m_Buttons.Length; ++i)
{
backgroundItem = new VisualElement();
backgroundItem.style.flexGrow = backgroundItem.style.flexShrink = 1;
if (i != m_Buttons.Length - 1)
backgroundItem.style.paddingLeft = 1;
SetupBkgnd(backgroundItem, i);
m_Background.Add(backgroundItem);
}
m_Buttons[m_Buttons.Length - 1].style.marginRight = 0;
RegisterCallback<CustomStyleResolvedEvent>(OnCustomStyleResolved);
this.AddManipulator(new ContextualMenuManipulator(BuildContextualMenu));
}
public void BuildContextualMenu(ContextualMenuPopulateEvent evt)
{
evt.menu.AppendAction("Check All", CheckAll, DropdownMenuAction.AlwaysEnabled);
evt.menu.AppendAction("Check None", CheckNone, DropdownMenuAction.AlwaysEnabled);
}
protected abstract void CheckAll(DropdownMenuAction a);
protected abstract void CheckNone(DropdownMenuAction a);
void SetupListener(VisualElement button, int index)
{
button.AddManipulator(new Clickable(() => ValueToggled(index)));
button.RegisterCallback<MouseEnterEvent>(e => m_Label.text = index.ToString());
button.RegisterCallback<MouseLeaveEvent>(e => m_Label.text = "");
}
void SetupBkgnd(VisualElement button, int index)
{
button.RegisterCallback<MouseEnterEvent>(e => m_Label.text = index.ToString());
button.RegisterCallback<MouseLeaveEvent>(e => m_Label.text = "");
}
protected abstract void ValueToggled(int i);
static readonly CustomStyleProperty<Texture2D> s_BitImage = new CustomStyleProperty<Texture2D>("--bit-image");
static readonly CustomStyleProperty<Texture2D> s_BitBkgndImage = new CustomStyleProperty<Texture2D>("--bit-bkgnd-image");
private void OnCustomStyleResolved(CustomStyleResolvedEvent e)
{
var customStyle = e.customStyle;
customStyle.TryGetValue(s_BitImage, out m_BitImage);
customStyle.TryGetValue(s_BitBkgndImage, out m_BitBkgndImage);
for (int i = 0; i < m_Background.childCount - 1; ++i)
m_Background.ElementAt(i).style.backgroundImage = m_BitBkgndImage;
ValueToGUI(true);
}
bool m_Indeterminate;
public override bool indeterminate
{
get
{
return m_Indeterminate;
}
set
{
m_Indeterminate = value;
foreach (var button in m_Buttons)
{
button.visible = !m_Indeterminate;
}
}
}
}
class VFX32BitField : VFXBitField<uint, long>
{
protected override void ValueToGUI(bool force)
{
uint value = (uint)this.value;
for (int i = 0; i < m_Buttons.Length; ++i)
{
m_Buttons[i].style.backgroundImage = (value & 1u << i) != 0 ? m_BitImage : null;
}
}
protected override void ValueToggled(int i)
{
value = value ^ (1u << i);
}
protected override void CheckAll(DropdownMenuAction a)
{
value = 0xFFFFFFFF;
}
protected override void CheckNone(DropdownMenuAction a)
{
value = 0;
}
}
}