-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSettingsMenuAPI.cs
More file actions
107 lines (86 loc) · 4.98 KB
/
Copy pathSettingsMenuAPI.cs
File metadata and controls
107 lines (86 loc) · 4.98 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
using BigMenuLib.Elements.SettingsMenu;
using BigMenuLib.Templates;
using UnityEngine;
using Object = UnityEngine.Object;
namespace BigMenuLib;
/// <summary>
/// The API used for creating UI inside the settings menu.
/// </summary>
/// <remarks>All <c>SettingsMenuAPI.Create...()</c> methods should be called from inside an event (e.g. <see cref="Loaded"/>). An exception will be thrown otherwise.</remarks>
public static class SettingsMenuAPI
{
/// <summary>
/// Fires whenever the SettingsMenu is loaded. An instance of the <see cref="SettingsMenu"/> is provided for convenience.
/// </summary>
/// <remarks>This event won't fire when the SettingsMenu is opened or switched to, only initialized.</remarks>
public static event Action<SettingsMenu> Loaded;
/// <summary>
/// Creates a new page in settings. A <see cref="SettingsButton"/> is also made to open the page./>.
/// </summary>
/// <param name="title">The name of the page and button</param>
/// <param name="button">The button that opens this page</param>
/// <returns>The <see cref="SettingsPage"/> on the new page</returns>
/// <remarks>If you don't want the category button to be auto created, use <see cref="CreatePage(string)"/> instead.</remarks>
public static SettingsPage CreatePage(string title, out SettingsButton button)
{
var page = CreatePage(title);
button = CreateCategoryButton(title, page.Open);
return page;
}
/// <summary>
/// Creates a new page in settings, the page won't be visible unless something calls <see cref="SettingsPage.Open()"/>.
/// </summary>
/// <param name="title">The name of the page</param>
/// <returns>The <see cref="SettingsPage"/> on the new page</returns>
/// <remarks>If you want the category button to be auto created, use <see cref="CreatePage(string, out SettingsButton)"/> instead.</remarks>
public static SettingsPage CreatePage(string title)
{
var settingsPageElement = InstantiateFromTemplate<SettingsPage>(SettingsMenuTemplates.Page);
settingsPageElement.Initialize(title);
return settingsPageElement;
}
public static SettingsButton CreateCategoryButton(string label, Action onClick, Transform parent = null)
{
var categoryButton = InstantiateFromTemplate<SettingsButton>(SettingsMenuTemplates.CategoryButton, parent);
categoryButton.Initialize(label, onClick);
return categoryButton;
}
public static SettingsButton CreateButton(string label, Action onClick, Transform parent = null)
{
var button = InstantiateFromTemplate<SettingsButton>(SettingsMenuTemplates.ButtonSetting, parent);
button.Initialize(label, onClick);
return button;
}
public static SettingsToggle CreateToggle(string label, Action<bool> valueChanged = null, bool defaultValue = false, string offLabel = "Off", string onLabel = "On", Transform parent = null)
{
var toggleSetting = InstantiateFromTemplate<SettingsToggle>(SettingsMenuTemplates.ToggleSetting, parent);
toggleSetting.Initialize(label, valueChanged, defaultValue, offLabel, onLabel);
return toggleSetting;
}
public static SettingsOptions CreateOptions(string label, string[] options, Action<string, int> valueChanged = null, string defaultValue = null, bool wrap = false, Transform parent = null)
{
var optionsSetting = InstantiateFromTemplate<SettingsOptions>(SettingsMenuTemplates.OptionsSetting, parent);
optionsSetting.Initialize(label, options, valueChanged, defaultValue, wrap);
return optionsSetting;
}
public static SettingsSlider CreateSlider(string label, int min, int max, Action<int> valueChanged,
int? defaultValue = null, Func<float, string> displayFormatter = null, Transform parent = null)
=> CreateSlider(label, min, max, f => valueChanged?.Invoke((int) f), defaultValue, 0, displayFormatter, parent);
public static SettingsSlider CreateSlider(string label, float min, float max, Action<float> valueChanged, float? defaultValue = null, uint precision = 2, Func<float, string> displayFormatter = null, Transform parent = null)
{
var sliderSetting = InstantiateFromTemplate<SettingsSlider>(SettingsMenuTemplates.SliderSetting, parent);
sliderSetting.Initialize(label, min, max, valueChanged, defaultValue, precision, displayFormatter);
return sliderSetting;
}
internal static void InvokeSettingsMenuLoaded(SettingsMenu settingsMenu)
{
SettingsMenuRedirector.SetDefaults(settingsMenu); //We need this to run first
Loaded?.Invoke(settingsMenu);
}
private static T InstantiateFromTemplate<T>(GameObject template, Transform parent = null) where T : MonoBehaviour
{
ArgumentNullException.ThrowIfNull(template);
var newGameObject = Object.Instantiate(template, parent ?? template.transform.parent);
return newGameObject.GetComponent<T>() ?? newGameObject.AddComponent<T>();
}
}