forked from Unity-Technologies/UnityCsReference
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathToggle.cs
More file actions
137 lines (117 loc) · 4.52 KB
/
Toggle.cs
File metadata and controls
137 lines (117 loc) · 4.52 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
// Unity C# reference source
// Copyright (c) Unity Technologies. For terms of use, see
// https://unity3d.com/legal/licenses/Unity_Reference_Only_License
using System;
namespace UnityEngine.UIElements
{
public class Toggle : BaseField<bool>
{
public new class UxmlFactory : UxmlFactory<Toggle, UxmlTraits> {}
public new class UxmlTraits : BaseFieldTraits<bool, UxmlBoolAttributeDescription>
{
UxmlStringAttributeDescription m_Text = new UxmlStringAttributeDescription { name = "text" };
public override void Init(VisualElement ve, IUxmlAttributes bag, CreationContext cc)
{
base.Init(ve, bag, cc);
((Toggle)ve).text = m_Text.GetValueFromBag(bag, cc);
}
}
public new static readonly string ussClassName = "unity-toggle";
public new static readonly string labelUssClassName = ussClassName + "__label";
public new static readonly string inputUssClassName = ussClassName + "__input";
public static readonly string noTextVariantUssClassName = ussClassName + "--no-text";
public static readonly string checkmarkUssClassName = ussClassName + "__checkmark";
public static readonly string textUssClassName = ussClassName + "__text";
private Label m_Label;
public Toggle()
: this(null) {}
public Toggle(string label)
: base(label, null)
{
AddToClassList(ussClassName);
AddToClassList(noTextVariantUssClassName);
visualInput.AddToClassList(inputUssClassName);
labelElement.AddToClassList(labelUssClassName);
// Allocate and add the checkmark to the hierarchy
var checkMark = new VisualElement() { name = "unity-checkmark", pickingMode = PickingMode.Ignore };
checkMark.AddToClassList(checkmarkUssClassName);
visualInput.Add(checkMark);
// The picking mode needs to be Position in order to have the Pseudostate Hover applied...
visualInput.pickingMode = PickingMode.Position;
// Set-up the label and text...
text = null;
this.AddManipulator(new Clickable(OnClickEvent));
}
public string text
{
get { return m_Label?.text; }
set
{
if (!string.IsNullOrEmpty(value))
{
// Lazy allocation of label if needed...
if (m_Label == null)
{
m_Label = new Label
{
pickingMode = PickingMode.Ignore
};
m_Label.AddToClassList(textUssClassName);
RemoveFromClassList(noTextVariantUssClassName);
visualInput.Add(m_Label);
}
m_Label.text = value;
}
else if (m_Label != null)
{
Remove(m_Label);
AddToClassList(noTextVariantUssClassName);
m_Label = null;
}
}
}
public override void SetValueWithoutNotify(bool newValue)
{
if (newValue)
{
visualInput.pseudoStates |= PseudoStates.Checked;
pseudoStates |= PseudoStates.Checked;
}
else
{
visualInput.pseudoStates &= ~PseudoStates.Checked;
pseudoStates &= ~PseudoStates.Checked;
}
base.SetValueWithoutNotify(newValue);
}
void OnClickEvent(EventBase evt)
{
if ((evt as MouseUpEvent)?.button == (int)MouseButton.LeftMouse)
{
var mue = (MouseUpEvent)evt;
if (visualInput.ContainsPoint(visualInput.WorldToLocal(mue.mousePosition)))
{
OnClick();
}
}
}
void OnClick()
{
value = !value;
}
protected override void ExecuteDefaultActionAtTarget(EventBase evt)
{
base.ExecuteDefaultActionAtTarget(evt);
if (evt == null)
{
return;
}
if (((evt as KeyDownEvent)?.keyCode == KeyCode.KeypadEnter) ||
((evt as KeyDownEvent)?.keyCode == KeyCode.Return))
{
OnClick();
evt.StopPropagation();
}
}
}
}