| | 1 | | // Copyright (c) 2020-2024 dotBunny Inc. |
| | 2 | | // dotBunny licenses this file to you under the BSL-1.0 license. |
| | 3 | | // See the LICENSE file in the project root for more information. |
| | 4 | |
|
| | 5 | | using System; |
| | 6 | |
|
| | 7 | | namespace GDX.Developer.ConsoleVariables |
| | 8 | | { |
| | 9 | | #if UNITY_2022_2_OR_NEWER |
| | 10 | | public class BooleanConsoleVariable : ConsoleVariableBase |
| | 11 | | { |
| | 12 | |
|
| | 13 | | public Action<bool> OnValueChanged; |
| | 14 | | readonly bool m_DefaultValue; |
| | 15 | | bool m_CurrentValue; |
| | 16 | |
|
| | 17 | | public BooleanConsoleVariable(string name, string description, bool defaultValue, |
| 0 | 18 | | ConsoleVariableFlags flags = ConsoleVariableFlags.None) : base(name, description, flags) |
| 0 | 19 | | { |
| 0 | 20 | | if (ConsoleVariableSettings.TryGetValue(name, out string settingsValue) && bool.TryParse(settingsValue, out |
| 0 | 21 | | { |
| 0 | 22 | | m_DefaultValue = newDefaultValue; |
| 0 | 23 | | } |
| | 24 | | else |
| 0 | 25 | | { |
| 0 | 26 | | m_DefaultValue = defaultValue; |
| 0 | 27 | | } |
| | 28 | |
|
| 0 | 29 | | if (CommandLineParser.Arguments.ContainsKey(name) && bool.TryParse(CommandLineParser.Arguments[name], out bo |
| 0 | 30 | | { |
| 0 | 31 | | m_CurrentValue = newCurrentValue; |
| 0 | 32 | | } |
| | 33 | | else |
| 0 | 34 | | { |
| 0 | 35 | | m_CurrentValue = defaultValue; |
| 0 | 36 | | } |
| 0 | 37 | | } |
| | 38 | |
|
| | 39 | | /// <inheritdoc /> |
| | 40 | | public override ConsoleVariableType GetConsoleVariableType() |
| 0 | 41 | | { |
| 0 | 42 | | return ConsoleVariableType.Boolean; |
| 0 | 43 | | } |
| | 44 | |
|
| | 45 | | /// <inheritdoc /> |
| | 46 | | public override object GetBoxedValue() |
| 0 | 47 | | { |
| 0 | 48 | | return m_CurrentValue; |
| 0 | 49 | | } |
| | 50 | |
|
| | 51 | | /// <inheritdoc /> |
| | 52 | | public sealed override void SetValueFromString(string newValue) |
| 0 | 53 | | { |
| 0 | 54 | | m_CurrentValue = newValue.IsBooleanPositiveValue(); |
| 0 | 55 | | OnValueChanged?.Invoke(m_CurrentValue); |
| 0 | 56 | | } |
| | 57 | |
|
| | 58 | | /// <inheritdoc /> |
| | 59 | | public override string GetCurrentValueAsString() |
| 0 | 60 | | { |
| 0 | 61 | | return m_CurrentValue.ToString(); |
| 0 | 62 | | } |
| | 63 | |
|
| | 64 | | /// <inheritdoc /> |
| | 65 | | public override string GetDefaultValueAsString() |
| 0 | 66 | | { |
| 0 | 67 | | return m_DefaultValue.ToString(); |
| 0 | 68 | | } |
| | 69 | |
|
| | 70 | | public bool GetValue() |
| 0 | 71 | | { |
| 0 | 72 | | return m_CurrentValue; |
| 0 | 73 | | } |
| | 74 | |
|
| | 75 | | public void SetValue(bool newValue) |
| 0 | 76 | | { |
| 0 | 77 | | m_CurrentValue = newValue; |
| 0 | 78 | | OnValueChanged?.Invoke(m_CurrentValue); |
| 0 | 79 | | } |
| | 80 | | } |
| | 81 | | #endif // UNITY_2022_2_OR_NEWER |
| | 82 | | } |