Browse Source

Upgraded SliderText component

James Peret 2 years ago
parent
commit
d77871fc44
1 changed files with 34 additions and 5 deletions
  1. 34 5
      Runtime/SliderText.cs

+ 34 - 5
Runtime/SliderText.cs

@@ -3,15 +3,27 @@ using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.UI;
 using TMPro;
+using Sirenix.OdinInspector;
+using KairoEngine.Core;
 
 
 namespace KairoEngine.UI
 {
     public class SliderText : MonoBehaviour
     {
+        public enum TextType
+        {
+            Percentage,
+            CurrentMax
+        }
+        public TextType textType = TextType.Percentage;
         public TextMeshProUGUI text;
-        public string suffix = "%";
-        public float multiplier = 100;
+        [ShowIf("@textType == TextType.Percentage")] public string suffix = "%";
+        [ShowIf("@textType == TextType.Percentage")] public float multiplier = 100;
+        [ShowIf("@textType == TextType.CurrentMax")] public string separator = "/";
+
+        public bool hideEmptyFill = false; 
+        
         public Slider slider;
         
         public void OnEnable()
@@ -29,11 +41,28 @@ namespace KairoEngine.UI
             UpdateText();
         }
         
+        [OnInspectorInit]
         public void UpdateText()
         {
-            float value = slider.value * multiplier;
-            value = value < 0.6 ? 0 : Mathf.CeilToInt(value);
-            text.text = $"{(int)value}{suffix}";
+            if(textType == TextType.Percentage)
+            {
+                float value = slider.value * multiplier;
+                value = value < 0.6 ? 0 : Mathf.CeilToInt(value);
+                text.text = $"{(int)value}{suffix}";
+            }
+            else if(textType == TextType.CurrentMax)
+            {
+                float value = slider.value;
+                float max = slider.maxValue;
+                text.text = $"{(int)value}{separator}{(int)max}";
+            }
+            if(hideEmptyFill)
+            {
+                if((int)slider.value == 0 && slider.fillRect.gameObject.activeSelf) 
+                    Timer.ExecuteRealTime(20, () => slider.fillRect.gameObject.SetActive(false));
+                else if(!slider.fillRect.gameObject.activeSelf) 
+                    Timer.ExecuteRealTime(20, () => slider.fillRect.gameObject.SetActive(true));
+            }
         }
     }
 }