You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

467 lines
17 KiB

  1. /* ---------------------------------------
  2. * Author: Martin Pane (martintayx@gmail.com) (@tayx94)
  3. * Collaborators: Lars Aalbertsen (@Rockylars)
  4. * Project: Graphy - Ultimate Stats Monitor
  5. * Date: 28-Feb-18
  6. * Studio: Tayx
  7. *
  8. * This project is released under the MIT license.
  9. * Attribution is not required, but it is always welcomed!
  10. * -------------------------------------*/
  11. using System;
  12. using UnityEngine;
  13. using UnityEngine.UI;
  14. using System.Collections;
  15. using System.Collections.Generic;
  16. using Random = UnityEngine.Random;
  17. namespace Tayx.Graphy.CustomizationScene
  18. {
  19. public class CustomizeGraphy : MonoBehaviour
  20. {
  21. /* ----- TODO: ----------------------------
  22. * Check if we can seal this class.
  23. * Add summaries to the variables.
  24. * Add summaries to the functions.
  25. * Check if we can remove "using System.Collections;".
  26. * Check if we can remove "using Random = UnityEngine.Random;".
  27. * Check if we can remove the UnityEngine prefix on the PlayRandomSFX function
  28. * --------------------------------------*/
  29. #region Variables -> Serialized Private
  30. [Header("Customize Graphy")]
  31. [SerializeField] private G_CUIColorPicker m_colorPicker;
  32. [SerializeField] private Toggle m_backgroundToggle;
  33. [SerializeField] private Dropdown m_graphyModeDropdown;
  34. [SerializeField] private Button m_backgroundColorButton;
  35. [SerializeField] private Dropdown m_graphModulePositionDropdown;
  36. #region Section -> FPS
  37. [Header("Fps")]
  38. [SerializeField] private Dropdown m_fpsModuleStateDropdown;
  39. [SerializeField] private InputField m_goodInputField;
  40. [SerializeField] private InputField m_cautionInputField;
  41. [SerializeField] private Button m_goodColorButton;
  42. [SerializeField] private Button m_cautionColorButton;
  43. [SerializeField] private Button m_criticalColorButton;
  44. [SerializeField] private Slider m_timeToResetMinMaxSlider;
  45. [SerializeField] private Slider m_fpsGraphResolutionSlider;
  46. [SerializeField] private Slider m_fpsTextUpdateRateSlider;
  47. #endregion
  48. #region Section -> RAM
  49. [Header("Memory")]
  50. [SerializeField] private Dropdown m_ramModuleStateDropdown;
  51. [SerializeField] private Button m_reservedColorButton;
  52. [SerializeField] private Button m_allocatedColorButton;
  53. [SerializeField] private Button m_monoColorButton;
  54. [SerializeField] private Slider m_ramGraphResolutionSlider;
  55. [SerializeField] private Slider m_ramTextUpdateRateSlider;
  56. #endregion
  57. #region Section -> Audio
  58. [Header("Audio")]
  59. [SerializeField] private Dropdown m_audioModuleStateDropdown;
  60. [SerializeField] private Button m_audioGraphColorButton;
  61. [SerializeField] private Dropdown m_findAudioListenerDropdown;
  62. [SerializeField] private Dropdown m_fttWindowDropdown;
  63. [SerializeField] private Slider m_spectrumSizeSlider;
  64. [SerializeField] private Slider m_audioGraphResolutionSlider;
  65. [SerializeField] private Slider m_audioTextUpdateRateSlider;
  66. #endregion
  67. #region Section -> Advanced
  68. [Header("Advanced")]
  69. [SerializeField] private Dropdown m_advancedModulePositionDropdown;
  70. [SerializeField] private Toggle m_advancedModuleToggle;
  71. #endregion
  72. #region Section -> Other
  73. [Header("Other")]
  74. [SerializeField] private Button m_musicButton;
  75. [SerializeField] private Button m_sfxButton;
  76. [SerializeField] private Slider m_musicVolumeSlider;
  77. [SerializeField] private Slider m_sfxVolumeSlider;
  78. [SerializeField] private AudioSource m_musicAudioSource;
  79. [SerializeField] private AudioSource m_sfxAudioSource;
  80. [SerializeField] private List<AudioClip> m_sfxAudioClips = new List<AudioClip>();
  81. #endregion
  82. #endregion
  83. #region Variables -> Private
  84. private GraphyManager m_graphyManager;
  85. #endregion
  86. #region Methods -> Unity Callbacks
  87. private void OnEnable()
  88. {
  89. m_graphyManager = GraphyManager.Instance;
  90. SetupCallbacks();
  91. }
  92. #endregion
  93. #region Methods -> Private
  94. private void SetupCallbacks()
  95. {
  96. // Remove all listeners first --------------------------------
  97. m_backgroundToggle.onValueChanged.RemoveAllListeners();
  98. m_backgroundColorButton.onClick.RemoveAllListeners();
  99. m_graphyModeDropdown.onValueChanged.RemoveAllListeners();
  100. m_graphModulePositionDropdown.onValueChanged.RemoveAllListeners();
  101. #region Section -> FPS
  102. m_fpsModuleStateDropdown.onValueChanged.RemoveAllListeners();
  103. m_goodInputField.onValueChanged.RemoveAllListeners();
  104. m_cautionInputField.onValueChanged.RemoveAllListeners();
  105. m_goodColorButton.onClick.RemoveAllListeners();
  106. m_cautionColorButton.onClick.RemoveAllListeners();
  107. m_criticalColorButton.onClick.RemoveAllListeners();
  108. m_timeToResetMinMaxSlider.onValueChanged.RemoveAllListeners();
  109. m_fpsGraphResolutionSlider.onValueChanged.RemoveAllListeners();
  110. m_fpsTextUpdateRateSlider.onValueChanged.RemoveAllListeners();
  111. #endregion
  112. #region Section -> RAM
  113. m_ramModuleStateDropdown.onValueChanged.RemoveAllListeners();
  114. m_reservedColorButton.onClick.RemoveAllListeners();
  115. m_allocatedColorButton.onClick.RemoveAllListeners();
  116. m_monoColorButton.onClick.RemoveAllListeners();
  117. m_ramGraphResolutionSlider.onValueChanged.RemoveAllListeners();
  118. m_ramTextUpdateRateSlider.onValueChanged.RemoveAllListeners();
  119. #endregion
  120. #region Section -> Audio
  121. m_audioModuleStateDropdown.onValueChanged.RemoveAllListeners();
  122. m_audioGraphColorButton.onClick.RemoveAllListeners();
  123. m_findAudioListenerDropdown.onValueChanged.RemoveAllListeners();
  124. m_fttWindowDropdown.onValueChanged.RemoveAllListeners();
  125. m_spectrumSizeSlider.onValueChanged.RemoveAllListeners();
  126. m_audioGraphResolutionSlider.onValueChanged.RemoveAllListeners();
  127. m_audioTextUpdateRateSlider.onValueChanged.RemoveAllListeners();
  128. #endregion
  129. #region Section -> Advanced
  130. m_advancedModulePositionDropdown.onValueChanged.RemoveAllListeners();
  131. m_advancedModuleToggle.onValueChanged.RemoveAllListeners();
  132. #endregion
  133. #region Section -> Other
  134. m_musicButton.onClick.RemoveAllListeners();
  135. m_sfxButton.onClick.RemoveAllListeners();
  136. m_musicVolumeSlider.onValueChanged.RemoveAllListeners();
  137. m_sfxVolumeSlider.onValueChanged.RemoveAllListeners();
  138. #endregion
  139. // Add listeners --------------------------------
  140. m_backgroundToggle.onValueChanged.AddListener(
  141. value => m_graphyManager.Background = value);
  142. m_backgroundColorButton.onClick.AddListener(() =>
  143. {
  144. m_colorPicker.SetOnValueChangeCallback(null);
  145. m_colorPicker.Color = m_backgroundColorButton.GetComponent<Image>().color;
  146. m_colorPicker.SetOnValueChangeCallback(color =>
  147. {
  148. m_backgroundColorButton.GetComponent<Image>().color = color;
  149. m_graphyManager.BackgroundColor = color;
  150. });
  151. });
  152. m_graphyModeDropdown.onValueChanged.AddListener(value =>
  153. {
  154. switch ((GraphyManager.Mode)value)
  155. {
  156. case GraphyManager.Mode.FULL:
  157. m_fpsGraphResolutionSlider.maxValue = 300f;
  158. m_ramGraphResolutionSlider.maxValue = 300f;
  159. m_audioGraphResolutionSlider.maxValue = 300f;
  160. break;
  161. case GraphyManager.Mode.LIGHT:
  162. m_fpsGraphResolutionSlider.maxValue = 128f;
  163. m_ramGraphResolutionSlider.maxValue = 128f;
  164. m_audioGraphResolutionSlider.maxValue = 128f;
  165. break;
  166. }
  167. m_graphyManager.GraphyMode = (GraphyManager.Mode)value;
  168. });
  169. m_graphModulePositionDropdown.onValueChanged.AddListener(
  170. value => m_graphyManager.GraphModulePosition = (GraphyManager.ModulePosition)value);
  171. #region Section -> FPS
  172. m_fpsModuleStateDropdown.onValueChanged.AddListener(
  173. value => m_graphyManager.FpsModuleState = (GraphyManager.ModuleState)value);
  174. m_goodInputField.onValueChanged.AddListener(value =>
  175. {
  176. int threshold;
  177. if (Int32.TryParse(value, out threshold))
  178. {
  179. m_graphyManager.GoodFPSThreshold = threshold;
  180. }
  181. });
  182. m_cautionInputField.onValueChanged.AddListener(value =>
  183. {
  184. int threshold;
  185. if (Int32.TryParse(value, out threshold))
  186. {
  187. m_graphyManager.CautionFPSThreshold = threshold;
  188. }
  189. });
  190. m_goodColorButton.onClick.AddListener(() =>
  191. {
  192. m_colorPicker.SetOnValueChangeCallback(null);
  193. m_colorPicker.Color = m_goodColorButton.GetComponent<Image>().color;
  194. m_colorPicker.SetOnValueChangeCallback(color =>
  195. {
  196. m_goodColorButton.GetComponent<Image>().color = color;
  197. m_graphyManager.GoodFPSColor = color;
  198. });
  199. });
  200. m_cautionColorButton.onClick.AddListener(() =>
  201. {
  202. m_colorPicker.SetOnValueChangeCallback(null);
  203. m_colorPicker.Color = m_cautionColorButton.GetComponent<Image>().color;
  204. m_colorPicker.SetOnValueChangeCallback(color =>
  205. {
  206. m_cautionColorButton.GetComponent<Image>().color = color;
  207. m_graphyManager.CautionFPSColor = color;
  208. });
  209. });
  210. m_criticalColorButton.onClick.AddListener(() =>
  211. {
  212. m_colorPicker.SetOnValueChangeCallback(null);
  213. m_colorPicker.Color = m_criticalColorButton.GetComponent<Image>().color;
  214. m_colorPicker.SetOnValueChangeCallback(color =>
  215. {
  216. m_criticalColorButton.GetComponent<Image>().color = color;
  217. m_graphyManager.CriticalFPSColor = color;
  218. });
  219. });
  220. m_timeToResetMinMaxSlider.onValueChanged.AddListener(
  221. value => m_graphyManager.TimeToResetMinMaxFps = (int)value);
  222. m_fpsGraphResolutionSlider.onValueChanged.AddListener(
  223. value => m_graphyManager.FpsGraphResolution = (int)value);
  224. m_fpsTextUpdateRateSlider.onValueChanged.AddListener(
  225. value => m_graphyManager.FpsTextUpdateRate = (int)value);
  226. #endregion
  227. #region Section -> RAM
  228. m_ramModuleStateDropdown.onValueChanged.AddListener(
  229. value => m_graphyManager.RamModuleState = (GraphyManager.ModuleState)value);
  230. m_reservedColorButton.onClick.AddListener(() =>
  231. {
  232. m_colorPicker.SetOnValueChangeCallback(null);
  233. m_colorPicker.Color = m_reservedColorButton.GetComponent<Image>().color;
  234. m_colorPicker.SetOnValueChangeCallback(color =>
  235. {
  236. m_reservedColorButton.GetComponent<Image>().color = color;
  237. m_graphyManager.ReservedRamColor = color;
  238. });
  239. });
  240. m_allocatedColorButton.onClick.AddListener(() =>
  241. {
  242. m_colorPicker.SetOnValueChangeCallback(null);
  243. m_colorPicker.Color = m_allocatedColorButton.GetComponent<Image>().color;
  244. m_colorPicker.SetOnValueChangeCallback(color =>
  245. {
  246. m_allocatedColorButton.GetComponent<Image>().color = color;
  247. m_graphyManager.AllocatedRamColor = color;
  248. });
  249. });
  250. m_monoColorButton.onClick.AddListener(() =>
  251. {
  252. m_colorPicker.SetOnValueChangeCallback(null);
  253. m_colorPicker.Color = m_monoColorButton.GetComponent<Image>().color;
  254. m_colorPicker.SetOnValueChangeCallback(color =>
  255. {
  256. m_monoColorButton.GetComponent<Image>().color = color;
  257. m_graphyManager.MonoRamColor = color;
  258. });
  259. });
  260. m_ramGraphResolutionSlider.onValueChanged.AddListener(
  261. value => m_graphyManager.RamGraphResolution = (int)value);
  262. m_ramTextUpdateRateSlider.onValueChanged.AddListener(
  263. value => m_graphyManager.RamTextUpdateRate = (int)value);
  264. #endregion
  265. #region Section -> Audio
  266. m_audioModuleStateDropdown.onValueChanged.AddListener(
  267. value => m_graphyManager.AudioModuleState = (GraphyManager.ModuleState)value);
  268. m_audioGraphColorButton.onClick.AddListener(() =>
  269. {
  270. m_colorPicker.SetOnValueChangeCallback(null);
  271. m_colorPicker.Color = m_audioGraphColorButton.GetComponent<Image>().color;
  272. m_colorPicker.SetOnValueChangeCallback(color =>
  273. {
  274. m_audioGraphColorButton.GetComponent<Image>().color = color;
  275. m_graphyManager.AudioGraphColor = color;
  276. });
  277. });
  278. m_findAudioListenerDropdown.onValueChanged.AddListener(
  279. value => m_graphyManager.FindAudioListenerInCameraIfNull = (GraphyManager.LookForAudioListener)value);
  280. m_fttWindowDropdown.onValueChanged.AddListener(
  281. value => m_graphyManager.FftWindow = (FFTWindow)value);
  282. m_spectrumSizeSlider.onValueChanged.AddListener(
  283. value => m_graphyManager.SpectrumSize = (int)value);
  284. m_audioGraphResolutionSlider.onValueChanged.AddListener(
  285. value => m_graphyManager.AudioGraphResolution = (int)value);
  286. m_audioTextUpdateRateSlider.onValueChanged.AddListener(
  287. value => m_graphyManager.AudioTextUpdateRate = (int)value);
  288. #endregion
  289. #region Section -> Advanced
  290. m_advancedModulePositionDropdown.onValueChanged.AddListener(
  291. value => m_graphyManager.AdvancedModulePosition = (GraphyManager.ModulePosition)value);
  292. m_advancedModuleToggle.onValueChanged.AddListener(
  293. value => m_graphyManager.AdvancedModuleState = value ? GraphyManager.ModuleState.FULL : GraphyManager.ModuleState.OFF);
  294. #endregion
  295. #region Section -> Other
  296. m_musicButton.onClick.AddListener(ToggleMusic);
  297. m_sfxButton.onClick.AddListener(PlayRandomSFX);
  298. m_musicVolumeSlider.onValueChanged.AddListener(
  299. value => m_musicAudioSource.volume = value / 100f);
  300. m_sfxVolumeSlider.onValueChanged.AddListener(
  301. value => m_sfxAudioSource.volume = value / 100f);
  302. #endregion
  303. }
  304. private void ToggleMusic()
  305. {
  306. if (m_musicAudioSource.isPlaying)
  307. {
  308. m_musicAudioSource.Pause();
  309. }
  310. else
  311. {
  312. m_musicAudioSource.Play();
  313. }
  314. }
  315. private void PlayRandomSFX()
  316. {
  317. if (m_sfxAudioClips.Count > 0)
  318. {
  319. m_sfxAudioSource.clip = m_sfxAudioClips[UnityEngine.Random.Range(0, m_sfxAudioClips.Count)];
  320. m_sfxAudioSource.Play();
  321. }
  322. }
  323. #endregion
  324. }
  325. }