Browse Source

Add textfield and reset button to Slider control

Default value property has been added for the reset button to reset
the slider to. Default is 0 and bounded by default.

Default property has been introduced to SliderProp as well.

Closes #1
habeebweeb 4 years ago
parent
commit
1a6b06073c

+ 79 - 18
COM3D2.MeidoPhotoStudio.Plugin/MeidoPhotoStudio/GUI/Controls/Slider.cs

@@ -5,6 +5,7 @@ namespace COM3D2.MeidoPhotoStudio.Plugin
 {
     public class Slider : BaseControl
     {
+        private const string textFormat = "F4";
         private bool hasLabel;
         private string label;
         public string Label
@@ -16,17 +17,22 @@ namespace COM3D2.MeidoPhotoStudio.Plugin
                 hasLabel = !string.IsNullOrEmpty(label);
             }
         }
+
         private float value;
+
         public float Value
         {
             get => value;
             set
             {
                 this.value = Utility.Bound(value, Left, Right);
+                if (hasTextField) textFieldValue = Value.ToString(textFormat);
                 OnControlEvent(EventArgs.Empty);
             }
         }
+
         private float left;
+
         public float Left
         {
             get => left;
@@ -36,7 +42,9 @@ namespace COM3D2.MeidoPhotoStudio.Plugin
                 this.value = Utility.Bound(value, left, right);
             }
         }
+
         private float right;
+
         public float Right
         {
             get => right;
@@ -46,18 +54,39 @@ namespace COM3D2.MeidoPhotoStudio.Plugin
                 this.value = Utility.Bound(value, left, right);
             }
         }
+        private float defaultValue;
+        public float DefaultValue
+        {
+            get => defaultValue;
+            set => defaultValue = Utility.Bound(value, Left, Right);
+        }
+
+        private string textFieldValue;
+        private bool hasTextField;
+        public bool HasTextField
+        {
+            get => hasTextField;
+            set
+            {
+                hasTextField = value;
+                if (hasTextField) textFieldValue = Value.ToString(textFormat);
+            }
+        }
+        public bool HasReset { get; set; }
 
-        public Slider(string label, float left, float right, float value = 0)
+        public Slider(string label, float left, float right, float value = 0, float defaultValue = 0)
         {
+            textFieldValue = value.ToString(textFormat);
             Label = label;
             this.left = left;
             this.right = right;
             this.value = Utility.Bound(value, left, right);
+            DefaultValue = defaultValue;
         }
 
-        public Slider(string label, SliderProp prop) : this(label, prop.Left, prop.Right, prop.Initial) { }
+        public Slider(string label, SliderProp prop) : this(label, prop.Left, prop.Right, prop.Initial, prop.Default) { }
 
-        public Slider(SliderProp prop) : this(string.Empty, prop.Left, prop.Right, prop.Initial) { }
+        public Slider(SliderProp prop) : this(string.Empty, prop.Left, prop.Right, prop.Initial, prop.Default) { }
 
         public void SetBounds(float left, float right)
         {
@@ -68,24 +97,54 @@ namespace COM3D2.MeidoPhotoStudio.Plugin
 
         public override void Draw(params GUILayoutOption[] layoutOptions)
         {
-            GUIStyle sliderStyle = new GUIStyle(GUI.skin.horizontalSlider);
-            sliderStyle.margin.bottom = 0;
-            if (hasLabel)
+            var hasUpper = hasLabel || HasTextField || HasReset;
+
+            var tempText = string.Empty;
+
+            if (hasUpper)
             {
                 GUILayout.BeginVertical(GUILayout.ExpandWidth(false));
-                GUIStyle sliderLabelStyle = new GUIStyle(GUI.skin.label);
-                sliderLabelStyle.padding.bottom = -5;
-                sliderLabelStyle.margin = new RectOffset(0, 0, 0, 0);
-                sliderLabelStyle.alignment = TextAnchor.LowerLeft;
-                sliderLabelStyle.fontSize = 13;
-                GUILayout.Label(Label, sliderLabelStyle, GUILayout.ExpandWidth(false));
+                GUILayout.BeginHorizontal();
+
+                if (hasLabel)
+                {
+                    GUILayout.Label(Label, MpsGui.SliderLabelStyle, GUILayout.ExpandWidth(false));
+                    GUILayout.FlexibleSpace();
+                }
+
+                if (HasTextField)
+                {
+                    tempText = GUILayout.TextField(textFieldValue, MpsGui.SliderTextBoxStyle, GUILayout.Width(60f));
+                }
+
+                if (HasReset && GUILayout.Button("|", MpsGui.SliderResetButtonStyle, GUILayout.Width(15f)))
+                {
+                    Value = DefaultValue;
+                    tempText = textFieldValue = Value.ToString(textFormat);
+                }
+                GUILayout.EndHorizontal();
             }
-            else sliderStyle.margin.top = 10;
-            float value = GUILayout.HorizontalSlider(
-                Value, Left, Right, sliderStyle, GUI.skin.horizontalSliderThumb, layoutOptions
+
+            GUIStyle sliderStyle = hasUpper ? MpsGui.SliderStyle : MpsGui.SliderStyleNoLabel;
+
+            var tempValue = GUILayout.HorizontalSlider(
+                Value, Left, Right, sliderStyle, MpsGui.SliderThumbStyle, layoutOptions
             );
-            if (hasLabel) GUILayout.EndVertical();
-            if (value != Value) Value = value;
+
+            if (hasUpper) GUILayout.EndVertical();
+
+            if (HasTextField)
+            {
+                if (tempValue != Value) tempText = textFieldValue = tempValue.ToString(textFormat);
+
+                if (tempText != textFieldValue)
+                {
+                    textFieldValue = tempText;
+                    if (float.TryParse(tempText, out float newValue)) tempValue = newValue;
+                }
+            }
+
+            if (tempValue != Value) Value = tempValue;
         }
     }
 
@@ -94,12 +153,14 @@ namespace COM3D2.MeidoPhotoStudio.Plugin
         public float Left { get; }
         public float Right { get; }
         public float Initial { get; }
+        public float Default { get; }
 
-        public SliderProp(float left, float right, float initial = 0f)
+        public SliderProp(float left, float right, float initial = 0f, float @default = 0f)
         {
             Left = left;
             Right = right;
             Initial = Utility.Bound(initial, left, right);
+            Default = Utility.Bound(@default, left, right);
         }
     }
 }

+ 46 - 9
COM3D2.MeidoPhotoStudio.Plugin/MeidoPhotoStudio/MyGui.cs

@@ -7,23 +7,61 @@ namespace COM3D2.MeidoPhotoStudio.Plugin
         public static readonly GUILayoutOption HalfSlider = GUILayout.Width(98);
         public static readonly Texture2D white = Utility.MakeTex(2, 2, Color.white);
         public static readonly Texture2D transparentBlack = Utility.MakeTex(2, 2, new Color(0f, 0f, 0f, 0.8f));
+        public static readonly GUIStyle SliderLabelStyle;
+        public static readonly GUIStyle SliderStyle;
+        public static readonly GUIStyle SliderStyleNoLabel;
+        public static readonly GUIStyle SliderTextBoxStyle;
+        public static readonly GUIStyle SliderThumbStyle;
+        public static readonly GUIStyle SliderResetButtonStyle;
         private static readonly GUIStyle lineStyleWhite;
         private static readonly GUIStyle lineStyleBlack;
         private static readonly GUIStyle textureBoxStyle;
+        private static readonly GUIStyle headerLabelStyle;
 
         static MpsGui()
         {
-            lineStyleWhite = new GUIStyle(GUI.skin.box);
+            GUI.skin = null;
+
+            lineStyleWhite = new GUIStyle(GUI.skin.box)
+            {
+                margin = new RectOffset(0, 0, 8, 8),
+                normal = { background = Utility.MakeTex(2, 2, new Color(1f, 1f, 1f, 0.2f)) }
+            };
             lineStyleWhite.padding = lineStyleWhite.border = new RectOffset(0, 0, 1, 1);
-            lineStyleWhite.margin = new RectOffset(0, 0, 8, 8);
-            lineStyleWhite.normal.background = Utility.MakeTex(2, 2, new Color(1f, 1f, 1f, 0.2f));
 
-            lineStyleBlack = new GUIStyle(lineStyleWhite);
-            lineStyleBlack.normal.background = Utility.MakeTex(2, 2, new Color(0f, 0f, 0f, 0.3f));
+            lineStyleBlack = new GUIStyle(lineStyleWhite)
+            {
+                normal = { background = Utility.MakeTex(2, 2, new Color(0f, 0f, 0f, 0.3f)) }
+            };
 
-            textureBoxStyle = new GUIStyle(GUI.skin.box);
-            textureBoxStyle.normal.background = Utility.MakeTex(2, 2, new Color(0f, 0f, 0f, 0f));
+            textureBoxStyle = new GUIStyle(GUI.skin.box)
+            {
+                normal = { background = Utility.MakeTex(2, 2, new Color(0f, 0f, 0f, 0f)) }
+            };
             textureBoxStyle.padding = textureBoxStyle.margin = new RectOffset(0, 0, 0, 0);
+
+            headerLabelStyle = new GUIStyle(GUI.skin.label)
+            {
+                padding = new RectOffset(7, 0, 0, -5),
+                normal = { textColor = Color.white },
+                fontSize = 14
+            };
+
+            SliderLabelStyle = new GUIStyle(GUI.skin.label)
+            {
+                alignment = TextAnchor.LowerLeft,
+                fontSize = 13,
+                normal = { textColor = Color.white }
+            };
+            SliderStyle = new GUIStyle(GUI.skin.horizontalSlider);
+            SliderStyleNoLabel = new GUIStyle(SliderStyle) { margin = { top = 10 } };
+            SliderTextBoxStyle = new GUIStyle(GUI.skin.textField) { fontSize = 12, };
+            SliderResetButtonStyle = new GUIStyle(GUI.skin.button)
+            {
+                alignment = TextAnchor.MiddleRight,
+                fontSize = 10
+            };
+            SliderThumbStyle = new GUIStyle(GUI.skin.horizontalSliderThumb);
         }
 
         private static void Line(GUIStyle style) => GUILayout.Box(GUIContent.none, style, GUILayout.Height(1));
@@ -41,8 +79,7 @@ namespace COM3D2.MeidoPhotoStudio.Plugin
 
         public static void Header(string text, params GUILayoutOption[] layoutOptions)
         {
-            GUIStyle style = new GUIStyle(GUI.skin.label) { padding = new RectOffset(7, 0, 0, -5) };
-            GUILayout.Label(text, style, layoutOptions);
+            GUILayout.Label(text, headerLabelStyle, layoutOptions);
         }
     }
 }