-
Notifications
You must be signed in to change notification settings - Fork 42
/
SceneEffectsCategoryToggle.cs
135 lines (125 loc) · 4.28 KB
/
SceneEffectsCategoryToggle.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
#if KK || KKS || AI || HS2
#define TMP
#endif
using System;
using UnityEngine.UI;
#if TMP
using Text = TMPro.TextMeshProUGUI;
#endif
namespace KKAPI.Studio.UI
{
/// <summary>
/// A container for the value of a toggle, associated label and toggle UI elements, and the setter method that triggers on value change.
/// </summary>
public class SceneEffectsToggleSet
{
private bool CurrentValue = false;
private bool EventsEnabled = true;
#region Backing Fields
private string _text;
#endregion
/// <summary>
/// Label UI element.
/// </summary>
public Text Label { get; set; }
/// <summary>
/// Toggle UI element.
/// </summary>
public Toggle Toggle { get; set; }
/// <summary>
/// Method called when the value of the toggle is changed.
/// </summary>
public Action<bool> Setter { get; set; }
/// <summary>
/// Initial state of the toggle.
/// </summary>
public bool InitialValue { get; set; } = false;
/// <summary>
/// Get or set the text of the label.
/// </summary>
public string Text
{
get => _text;
set
{
_text = value;
Label.text = value;
}
}
/// <summary>
/// Get or set the value of the toggle.
/// </summary>
public bool Value
{
get => GetValue();
set => SetValue(value);
}
/// <summary>
/// Create a new ToggleSet. Typically you want to use SceneEffectsCategory.AddToggleSet instead of creating these manually.
/// </summary>
/// <param name="label">Label UI element</param>
/// <param name="toggle">Toggle UI element</param>
/// <param name="text">Label text</param>
/// <param name="setter">Method that will be called on value change</param>
/// <param name="initialValue">Initial state of the toggle</param>
public SceneEffectsToggleSet(Text label, Toggle toggle, string text, Action<bool> setter, bool initialValue)
{
Label = label;
Toggle = toggle;
Setter = setter;
InitialValue = initialValue;
Value = InitialValue;
Text = text;
Toggle.isOn = Value;
Toggle.onValueChanged.RemoveAllListeners();
Toggle.onValueChanged.AddListener(delegate (bool value)
{
if (!EventsEnabled) return;
Value = value;
});
Label.gameObject.name = $"Label {Text}";
Toggle.gameObject.name = $"Toggle {Text}";
}
/// <summary>
/// Get the value of the toggle.
/// </summary>
/// <returns>Value of the toggle</returns>
public bool GetValue() => CurrentValue;
/// <summary>
/// Set the value of the toggle and trigger the Setter method.
/// </summary>
/// <param name="value">Value to set the toggle</param>
public void SetValue(bool value) => SetValue(value, true);
/// <summary>
/// Set the value of the toggle.
/// </summary>
/// <param name="value">Value to set the toggle</param>
/// <param name="triggerEvents">Whether to trigger the Setter method</param>
public void SetValue(bool value, bool triggerEvents)
{
EventsEnabled = false;
CurrentValue = value;
Toggle.isOn = value;
EventsEnabled = true;
if (triggerEvents)
Setter.Invoke(value);
}
/// <summary>
/// Reset the toggle to the initial value and trigger the Setter method.
/// </summary>
public void Reset() => Reset(true);
/// <summary>
/// Reset the toggle to the initial value.
/// </summary>
/// <param name="triggerEvents">Whether to trigger the Setter method</param>
public void Reset(bool triggerEvents)
{
EventsEnabled = false;
CurrentValue = InitialValue;
Toggle.isOn = InitialValue;
EventsEnabled = true;
if (triggerEvents)
Setter.Invoke(InitialValue);
}
}
}