-
Notifications
You must be signed in to change notification settings - Fork 99
Expand file tree
/
Copy pathUIGameSettings.cs
More file actions
141 lines (118 loc) · 4.21 KB
/
UIGameSettings.cs
File metadata and controls
141 lines (118 loc) · 4.21 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
138
139
140
141
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UIElements;
namespace Unity.Megacity.UI
{
/// <summary>
/// Allows game settings to navigate through game setting views.
/// Shows and hides view according to the game settings menu.
/// </summary>
public class UIGameSettings : MonoBehaviour
{
[SerializeField] private UISettingsTab[] m_TabViews;
private int m_CurrentView;
private Button m_GraphicsButton;
private Button m_AudioButton;
private Button m_CloseButton;
private Button m_ApplyButton;
private Button m_ControlsButton;
private VisualElement m_GameSettings;
private VisualElement m_TriggerSettings;
private List<VisualElement> m_ViewSettingsList = new();
private List<Button> m_TabButtons = new();
private const string SELECTED_CLASS = "button-menu-active";
public bool IsVisible => m_GameSettings.style.display == DisplayStyle.Flex;
private void OnEnable()
{
var root = GetComponent<UIDocument>().rootVisualElement;
m_GameSettings = root.Q<VisualElement>("game-settings");
foreach (var view in m_TabViews)
{
view.GameSettingsView = root.Q<VisualElement>(view.TabName);
m_ViewSettingsList.Add(view.GameSettingsView);
}
root.Q<VisualElement>("settings-background");
m_GraphicsButton = root.Q<Button>("graphics-button");
m_AudioButton = root.Q<Button>("audio-button");
m_ControlsButton = root.Q<Button>("controls-button");
m_CloseButton = root.Q<Button>("close-button");
m_ApplyButton = root.Q<Button>("apply-button");
m_TabButtons.Add(m_GraphicsButton);
m_TabButtons.Add(m_AudioButton);
m_TabButtons.Add(m_ControlsButton);
m_GraphicsButton.clicked += () =>
{
m_CurrentView = 0;
ShowSettings();
};
m_AudioButton.clicked += () =>
{
m_CurrentView = 1;
ShowSettings();
};
m_ControlsButton.clicked += () =>
{
m_CurrentView = 2;
ShowSettings();
};
m_CloseButton.clicked += () =>
{
Reset();
Hide();
};
m_ApplyButton.clicked += () =>
{
Apply();
Hide();
};
Hide();
#if UNITY_ANDROID || UNITY_IPHONE
var hideInMobileElements = root.Query(className: "hide-in-mobile").ToList();
foreach (var mobileElement in hideInMobileElements)
{
mobileElement.style.display = DisplayStyle.None;
}
#endif
}
public void Show(VisualElement caller = null)
{
if (caller != null)
{
caller.style.display = DisplayStyle.None;
m_TriggerSettings = caller;
}
m_GameSettings.style.display = DisplayStyle.Flex;
ShowSettings();
}
public void Hide()
{
if (m_TriggerSettings != null)
{
m_TriggerSettings.style.display = DisplayStyle.Flex;
m_TriggerSettings = null;
}
m_GameSettings.style.display = DisplayStyle.None;
m_CurrentView = 0;
}
private void ShowSettings()
{
m_ViewSettingsList.ForEach(element => element.style.display = DisplayStyle.None);
m_ViewSettingsList[m_CurrentView].style.display = DisplayStyle.Flex;
foreach (var tab in m_TabViews)
{
tab.Hide();
}
m_TabViews[m_CurrentView].Show();
m_TabButtons.ForEach(b => b.RemoveFromClassList(SELECTED_CLASS));
m_TabButtons[m_CurrentView].AddToClassList(SELECTED_CLASS);
}
private void Reset()
{
m_TabViews[m_CurrentView].Reset();
}
private void Apply()
{
m_TabViews[m_CurrentView].Apply();
}
}
}