| | | 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 | | using System.Globalization; |
| | | 7 | | |
| | | 8 | | namespace GDX.Developer.ConsoleVariables |
| | | 9 | | { |
| | | 10 | | #if UNITY_2022_2_OR_NEWER |
| | | 11 | | public class FloatConsoleVariable : ConsoleVariableBase |
| | | 12 | | { |
| | | 13 | | public Action<float> OnValueChanged; |
| | | 14 | | readonly float m_DefaultValue; |
| | | 15 | | float m_CurrentValue; |
| | | 16 | | |
| | | 17 | | public FloatConsoleVariable(string name, string description, float defaultValue, |
| | 0 | 18 | | ConsoleVariableFlags flags = ConsoleVariableFlags.None) : base(name, description, flags) |
| | 0 | 19 | | { |
| | 0 | 20 | | if (ConsoleVariableSettings.TryGetValue(name, out string settingsValue) && float.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) && float.TryParse(CommandLineParser.Arguments[name], out f |
| | 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.Float; |
| | 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 | | if(float.TryParse(newValue, out m_CurrentValue)) |
| | 0 | 55 | | { |
| | 0 | 56 | | OnValueChanged?.Invoke(m_CurrentValue); |
| | 0 | 57 | | } |
| | 0 | 58 | | } |
| | | 59 | | |
| | | 60 | | /// <inheritdoc /> |
| | | 61 | | public override string GetCurrentValueAsString() |
| | 0 | 62 | | { |
| | 0 | 63 | | return m_CurrentValue.ToString(CultureInfo.InvariantCulture); |
| | 0 | 64 | | } |
| | | 65 | | |
| | | 66 | | /// <inheritdoc /> |
| | | 67 | | public override string GetDefaultValueAsString() |
| | 0 | 68 | | { |
| | 0 | 69 | | return m_DefaultValue.ToString(CultureInfo.InvariantCulture); |
| | 0 | 70 | | } |
| | | 71 | | |
| | | 72 | | public float GetValue() |
| | 0 | 73 | | { |
| | 0 | 74 | | return m_CurrentValue; |
| | 0 | 75 | | } |
| | | 76 | | |
| | | 77 | | public void SetValue(float newValue) |
| | 0 | 78 | | { |
| | 0 | 79 | | m_CurrentValue = newValue; |
| | 0 | 80 | | OnValueChanged?.Invoke(m_CurrentValue); |
| | 0 | 81 | | } |
| | | 82 | | } |
| | | 83 | | #endif // UNITY_2022_2_OR_NEWER |
| | | 84 | | } |