Skip to content

Commit b77f84e

Browse files
committed
feat(UI): Improve slider text display
1 parent 6b59f14 commit b77f84e

File tree

1 file changed

+58
-1
lines changed

1 file changed

+58
-1
lines changed

Assets/JCSUnity/Scripts/UI/Slider/JCS_SliderTextDisplay.cs

Lines changed: 58 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,17 @@
1313

1414
namespace JCSUnity
1515
{
16+
/// <summary>
17+
/// A list of slider text display type.
18+
/// </summary>
19+
public enum SliderTextDisplay
20+
{
21+
CUSTOM,
22+
VALUE,
23+
MIN_VALUE,
24+
MAX_VALUE,
25+
}
26+
1627
/// <summary>
1728
/// Text that display slider's value.
1829
/// </summary>
@@ -26,15 +37,25 @@ public class JCS_SliderTextDisplay : JCS_TextObject
2637
[SerializeField]
2738
private Slider mSlider = null;
2839

40+
[Tooltip("Text display type.")]
41+
[SerializeField]
42+
private SliderTextDisplay mTextDisplay = SliderTextDisplay.VALUE;
43+
2944
[Tooltip("Place you want to round the decimal.")]
3045
[SerializeField]
3146
[Range(0, 15)]
3247
private int mRoundPlace = 2;
3348

49+
[Tooltip("Display with custom text.")]
50+
[SerializeField]
51+
private string mCustomFormat = "{0} / {1}";
52+
3453
/* Setter & Getter */
3554

3655
public Slider slider { get { return this.mSlider; } set { this.mSlider = value; } }
56+
public SliderTextDisplay TextDisplay { get { return this.mTextDisplay; } set { this.mTextDisplay = value; } }
3757
public int RoundPlace { get { return this.mRoundPlace; } set { this.mRoundPlace = value; } }
58+
public string CustomFormat { get { return this.mCustomFormat; } set { this.mCustomFormat = value; } }
3859

3960
/* Functions */
4061

@@ -61,9 +82,45 @@ private void AddListener()
6182

6283
private void OnValueChanged()
6384
{
64-
double val = Math.Round(mSlider.value, mRoundPlace);
85+
if (mTextDisplay == SliderTextDisplay.CUSTOM)
86+
{
87+
this.text = string.Format(mCustomFormat,
88+
GetRoundValue(SliderTextDisplay.VALUE),
89+
GetRoundValue(SliderTextDisplay.MAX_VALUE),
90+
GetRoundValue(SliderTextDisplay.MIN_VALUE));
91+
92+
return;
93+
}
94+
95+
double val = GetRoundValue(mTextDisplay);
6596

6697
this.text = val.ToString();
6798
}
99+
100+
/// <summary>
101+
/// Like the function `GetValue` but round the result.
102+
/// </summary>
103+
private double GetRoundValue(SliderTextDisplay displayType)
104+
{
105+
return Math.Round(GetValue(displayType), mRoundPlace);
106+
}
107+
108+
/// <summary>
109+
/// Return the slider text display type.
110+
/// </summary>
111+
private double GetValue(SliderTextDisplay displayType)
112+
{
113+
switch (displayType)
114+
{
115+
case SliderTextDisplay.VALUE:
116+
return mSlider.value;
117+
case SliderTextDisplay.MIN_VALUE:
118+
return mSlider.minValue;
119+
case SliderTextDisplay.MAX_VALUE:
120+
return mSlider.maxValue;
121+
}
122+
123+
return 0.0f;
124+
}
68125
}
69126
}

0 commit comments

Comments
 (0)