-
Notifications
You must be signed in to change notification settings - Fork 85
/
DropDownOption.cs
118 lines (96 loc) · 4.04 KB
/
DropDownOption.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
namespace TrafficManager.UI.Helpers {
using ICities;
using ColossalFramework.UI;
using TrafficManager.State;
using CSUtil.Commons;
using System;
using System.Linq;
using TrafficManager.API.Traffic.Enums;
using TrafficManager.Util;
public class DropDownOption<TEnum> : SerializableUIOptionBase<TEnum, UIDropDown, DropDownOption<TEnum>>
where TEnum : struct, Enum, IConvertible {
private UILabel _dropdownLabel;
public DropDownOption(string fieldName, Options.PersistTo scope = Options.PersistTo.Savegame)
: base(fieldName, scope) { }
/* Data */
private static TEnum[] values_ = Enum.GetValues(typeof(TEnum)) as TEnum[];
private static string[] names_ = Enum.GetNames(typeof(TEnum));
private static string[] keys_ =
names_.
Where(item => item != "MaxValue").
Select(LocaleKeyAttribute.GetKey<TEnum>).
ToArray();
private string[] GetTranslatedItems() =>
keys_.Select(Translate).ToArray();
private static int IndexOf(TEnum value) =>
Array.FindIndex(values_, item => item.Equals(value));
protected void InvokeOnValueChanged(int index) {
TEnum val = values_[index];
InvokeOnValueChanged(val);
}
public override void Load(byte data) {
unchecked {
Value = (TEnum)(IConvertible)(int)data;
}
}
public override byte Save() => Value.ToByte(null);
/* UI */
public override TEnum Value {
get => base.Value;
set {
if (values_.Contains(value)) {
base.Value = value;
if (Shortcuts.IsMainThread()) {
if (HasUI) {
_ui.selectedIndex = IndexOf(value);
}
} else {
SimulationManager.instance
.m_ThreadingWrapper
.QueueMainThread(() => {
if (HasUI) {
_ui.selectedIndex = IndexOf(value);
}
});
}
} else {
Log.Error($"unrecognised value:{value} for enum:{typeof(TEnum).Name}");
}
}
}
public override DropDownOption<TEnum> AddUI(UIHelperBase container) {
_ui = container.AddDropdown(
text: Translate(Label) + ":",
options: GetTranslatedItems(),
defaultSelection: IndexOf(Value),
eventCallback: InvokeOnValueChanged) as UIDropDown;
_ui.width = 350;
_ui.parent.width = 350; //UIDropDown is added to the UIPanel which also require resize for correct tooltip interactions
_dropdownLabel = _ui.parent.Find<UILabel>("Label");
UpdateTooltip();
UpdateReadOnly();
return this;
}
protected override void UpdateLabel() {
if (!HasUI) return;
_ui.text = Translate(Label) + ":";
_ui.items = GetTranslatedItems();
}
protected override void UpdateTooltip() {
if (!HasUI) return;
//UIDropDown parent(UIPanel) handles tooltip
_ui.parent.tooltip = IsInScope
? $"{_tooltip}"
: Translate(INGAME_ONLY_SETTING);
}
protected override void UpdateReadOnly() {
if (!HasUI) return;
var readOnly = !IsInScope || _readOnly;
Log._Debug($"DropDownOption.UpdateReadOnly() - `{FieldName}` is {(readOnly ? "read-only" : "writeable")}");
_ui.isInteractive = !readOnly;
_ui.opacity = readOnly ? 0.3f : 1f;
// parent is UIPanel containing text label and dropdown
_dropdownLabel.opacity = readOnly ? 0.3f : 1f;
}
}
}