-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSelectableLabel.cs
91 lines (75 loc) · 2.81 KB
/
SelectableLabel.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
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UIElements;
namespace AssetBundleViewer
{
public class SelectableLabel : VisualElement
{
private static readonly string _sLabelUSS = "selectable-label";
private static readonly string _sLabelSelectedUSS = "selectable-label--selected";
private static readonly string _sLabelLabelUSS = "selectable-label-label";
private string _aUxmlResource;
private readonly StyleSheet _defaultStyles =
Resources.Load<StyleSheet>("DefaultStyleSheets/SelectableLabel");
private bool _isSelected;
private Label _label = new();
public string LabelText
{
get => _label.text;
set
{
_label.text = value;
_label.style.display = value != string.Empty ? DisplayStyle.Flex : DisplayStyle.None;
}
}
public SelectableLabel()
{
Add(_label);
AddToClassList(_sLabelUSS);
_label.AddToClassList(_sLabelLabelUSS);
this.AddManipulator(new Clickable(delegate ()
{
if (IsSelected)
IsSelected = false;
else
IsSelected = true;
}));
styleSheets.Add(_defaultStyles);
}
public bool IsSelected
{
get => _isSelected;
set
{
if (value)
AddToClassList(_sLabelSelectedUSS);
else
RemoveFromClassList(_sLabelSelectedUSS);
_isSelected = value;
}
}
public new class UxmlFactory : UxmlFactory<SelectableLabel, UxmlTraits> { }
public new class UxmlTraits : VisualElement.UxmlTraits
{
private readonly UxmlStringAttributeDescription _mLabelText = new()
{ name = "label-text", defaultValue = string.Empty };
private readonly UxmlStringAttributeDescription _mIconPath = new()
{ name = "icon-path", defaultValue = string.Empty };
private readonly UxmlBoolAttributeDescription _mIsSelected = new()
{ name = "Is-selected", defaultValue = false };
public override IEnumerable<UxmlChildElementDescription> uxmlChildElementsDescription
{
get { yield break; }
}
public override void Init(VisualElement ve, IUxmlAttributes bag, CreationContext cc)
{
base.Init(ve, bag, cc);
if (ve is SelectableLabel sLabel)
{
sLabel.IsSelected = _mIsSelected.GetValueFromBag(bag, cc);
sLabel.LabelText = _mLabelText.GetValueFromBag(bag, cc);
}
}
}
}
}