From 3e3fddb9381f0bfcb3ad158fe1420d053e6e8b3d Mon Sep 17 00:00:00 2001 From: Eric Heine <erheine@nps.edu> Date: Wed, 7 Apr 2021 15:23:59 -0700 Subject: [PATCH] Fixed an issue with Int and Float inputs to handle someone typing in negative numbers and decimals as they would in normal forms. --- .../FloatVariableInput.cs | 23 +++++++++++++++---- .../Scriptable Variables/IntVariableInput.cs | 20 ++++++++++++---- package.json | 2 +- 3 files changed, 35 insertions(+), 10 deletions(-) diff --git a/Sane Eric's UI/Scriptable Variables/FloatVariableInput.cs b/Sane Eric's UI/Scriptable Variables/FloatVariableInput.cs index cea8441..412ae44 100644 --- a/Sane Eric's UI/Scriptable Variables/FloatVariableInput.cs +++ b/Sane Eric's UI/Scriptable Variables/FloatVariableInput.cs @@ -17,6 +17,8 @@ namespace Shared.SEUI { // Temporary callback to bridge value changes in the UI and the variable private UnityAction<string> valueConverter; + private bool blockUIChanges = false; + //--------------------------------------------------------------------------- void Start() { // Make sure the input handles the right content type @@ -27,11 +29,20 @@ namespace Shared.SEUI { protected override void AddUIListener(UnityAction<float> callback) { // Define the value converting bridge callback to add to the UI listener valueConverter = delegate(string value) { - if (!String.IsNullOrEmpty(input.text)) { - callback(Convert.ToSingle(value)); + // If the user is trying to make it a negative number or a decimal, + // the string will just be "-" or end in "." at one point, + // so wait until it is a parsable number + if (value != "-" && !value.EndsWith(".")) { + blockUIChanges = false; + if (!String.IsNullOrEmpty(input.text)) { + callback(Convert.ToSingle(value)); + } + else { + callback(0); + } } else { - callback(0.0f); + blockUIChanges = true; } }; @@ -48,12 +59,14 @@ namespace Shared.SEUI { //--------------------------------------------------------------------------- protected override bool DoValuesMatch() { - return !String.IsNullOrEmpty(input.text) && Convert.ToSingle(input.text) == variable.Value; + return !String.IsNullOrEmpty(input.text) && input.text == variable.Value.ToString(); } //--------------------------------------------------------------------------- protected override void UpdateUIValue() { - input.SetTextWithoutNotify(variable.Value.ToString()); + if (!blockUIChanges) { + input.SetTextWithoutNotify(variable.Value.ToString()); + } } //--------------------------------------------------------------------------- diff --git a/Sane Eric's UI/Scriptable Variables/IntVariableInput.cs b/Sane Eric's UI/Scriptable Variables/IntVariableInput.cs index 6ba9330..fa057f3 100644 --- a/Sane Eric's UI/Scriptable Variables/IntVariableInput.cs +++ b/Sane Eric's UI/Scriptable Variables/IntVariableInput.cs @@ -17,6 +17,8 @@ namespace Shared.SEUI { // Temporary callback to bridge value changes in the UI and the variable private UnityAction<string> valueConverter; + private bool blockUIChanges = false; + //--------------------------------------------------------------------------- void Start() { // Make sure the input handles the right content type @@ -27,11 +29,19 @@ namespace Shared.SEUI { protected override void AddUIListener(UnityAction<int> callback) { // Define the value converting bridge callback to add to the UI listener valueConverter = delegate(string value) { - if (!String.IsNullOrEmpty(input.text)) { - callback(Convert.ToInt32(value)); + // If the user is trying to make it a negative number, the string will just be "-" at one point, + // so wait until it is a parsable number + if (value != "-") { + blockUIChanges = false; + if (!String.IsNullOrEmpty(input.text)) { + callback(Convert.ToInt32(value)); + } + else { + callback(0); + } } else { - callback(0); + blockUIChanges = true; } }; @@ -53,7 +63,9 @@ namespace Shared.SEUI { //--------------------------------------------------------------------------- protected override void UpdateUIValue() { - input.SetTextWithoutNotify(variable.Value.ToString()); + if (!blockUIChanges) { + input.SetTextWithoutNotify(variable.Value.ToString()); + } } //--------------------------------------------------------------------------- diff --git a/package.json b/package.json index f7e79b6..bb1cf64 100644 --- a/package.json +++ b/package.json @@ -2,7 +2,7 @@ "name": "com.futuretech.shared", "displayName": "FutureTech Shared", "description": "Contains shared items such as the Scriptable Variables.", - "version": "0.1.27", + "version": "0.1.28", "unity": "2019.3", "license": "MIT", "repository": { -- GitLab