Sfoglia il codice sorgente

Make Slider textfield easier to type in

Textfield will not clamp values and format them as they're being typed
out.

The formatting has also been adjusted so that decimal places are
optional.
habeebweeb 4 anni fa
parent
commit
c92d035a34

+ 9 - 7
COM3D2.MeidoPhotoStudio.Plugin/MeidoPhotoStudio/GUI/Controls/Slider.cs

@@ -1,11 +1,11 @@
 using System;
 using System;
+using System.Globalization;
 using UnityEngine;
 using UnityEngine;
 
 
 namespace COM3D2.MeidoPhotoStudio.Plugin
 namespace COM3D2.MeidoPhotoStudio.Plugin
 {
 {
     public class Slider : BaseControl
     public class Slider : BaseControl
     {
     {
-        private const string textFormat = "F4";
         private bool hasLabel;
         private bool hasLabel;
         private string label;
         private string label;
         public string Label
         public string Label
@@ -26,7 +26,7 @@ namespace COM3D2.MeidoPhotoStudio.Plugin
             set
             set
             {
             {
                 this.value = Utility.Bound(value, Left, Right);
                 this.value = Utility.Bound(value, Left, Right);
-                if (hasTextField) textFieldValue = Value.ToString(textFormat);
+                if (hasTextField) textFieldValue = FormatValue(value);
                 OnControlEvent(EventArgs.Empty);
                 OnControlEvent(EventArgs.Empty);
             }
             }
         }
         }
@@ -69,18 +69,18 @@ namespace COM3D2.MeidoPhotoStudio.Plugin
             set
             set
             {
             {
                 hasTextField = value;
                 hasTextField = value;
-                if (hasTextField) textFieldValue = Value.ToString(textFormat);
+                if (hasTextField) textFieldValue = FormatValue(Value);
             }
             }
         }
         }
         public bool HasReset { get; set; }
         public bool HasReset { get; set; }
 
 
         public Slider(string label, float left, float right, float value = 0, float defaultValue = 0)
         public Slider(string label, float left, float right, float value = 0, float defaultValue = 0)
         {
         {
-            textFieldValue = value.ToString(textFormat);
             Label = label;
             Label = label;
             this.left = left;
             this.left = left;
             this.right = right;
             this.right = right;
             this.value = Utility.Bound(value, left, right);
             this.value = Utility.Bound(value, left, right);
+            textFieldValue = FormatValue(this.value);
             DefaultValue = defaultValue;
             DefaultValue = defaultValue;
         }
         }
 
 
@@ -120,7 +120,7 @@ namespace COM3D2.MeidoPhotoStudio.Plugin
                 if (HasReset && GUILayout.Button("|", MpsGui.SliderResetButtonStyle, GUILayout.Width(15f)))
                 if (HasReset && GUILayout.Button("|", MpsGui.SliderResetButtonStyle, GUILayout.Width(15f)))
                 {
                 {
                     Value = DefaultValue;
                     Value = DefaultValue;
-                    tempText = textFieldValue = Value.ToString(textFormat);
+                    tempText = textFieldValue = FormatValue(Value);
                 }
                 }
                 GUILayout.EndHorizontal();
                 GUILayout.EndHorizontal();
             }
             }
@@ -135,17 +135,19 @@ namespace COM3D2.MeidoPhotoStudio.Plugin
 
 
             if (HasTextField)
             if (HasTextField)
             {
             {
-                if (tempValue != Value) tempText = textFieldValue = tempValue.ToString(textFormat);
+                if (tempValue != Value) tempText = textFieldValue = FormatValue(tempValue);
 
 
                 if (tempText != textFieldValue)
                 if (tempText != textFieldValue)
                 {
                 {
                     textFieldValue = tempText;
                     textFieldValue = tempText;
-                    if (float.TryParse(tempText, out float newValue)) tempValue = newValue;
+                    if (float.TryParse(tempText, out var newValue)) tempValue = newValue;
                 }
                 }
             }
             }
 
 
             if (tempValue != Value) Value = tempValue;
             if (tempValue != Value) Value = tempValue;
         }
         }
+
+        private static string FormatValue(float value) => value.ToString("0.####", CultureInfo.InvariantCulture);
     }
     }
 
 
     public readonly struct SliderProp
     public readonly struct SliderProp