Skip to content

Commit f4c914f

Browse files
committed
fix(UI): simplify usage of slider text display
1 parent 777745f commit f4c914f

File tree

2 files changed

+12
-64
lines changed

2 files changed

+12
-64
lines changed

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

Lines changed: 7 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,6 @@
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-
2716
/// <summary>
2817
/// Text that display slider's value.
2918
/// </summary>
@@ -37,25 +26,20 @@ public class JCS_SliderTextDisplay : JCS_TextObject
3726
[SerializeField]
3827
private Slider mSlider = null;
3928

40-
[Tooltip("Text display type.")]
29+
[Tooltip("Format text to display.")]
4130
[SerializeField]
42-
private SliderTextDisplay mTextDisplay = SliderTextDisplay.VALUE;
31+
private string mFormat = "{0} / {1}";
4332

4433
[Tooltip("Place you want to round the decimal.")]
4534
[SerializeField]
4635
[Range(0, 15)]
4736
private int mRoundPlace = 2;
4837

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

5540
public Slider slider { get { return this.mSlider; } set { this.mSlider = value; } }
56-
public SliderTextDisplay TextDisplay { get { return this.mTextDisplay; } set { this.mTextDisplay = value; } }
41+
public string Format { get { return this.mFormat; } set { this.mFormat = value; } }
5742
public int RoundPlace { get { return this.mRoundPlace; } set { this.mRoundPlace = value; } }
58-
public string CustomFormat { get { return this.mCustomFormat; } set { this.mCustomFormat = value; } }
5943

6044
/* Functions */
6145

@@ -82,45 +66,10 @@ private void AddListener()
8266

8367
private void OnValueChanged()
8468
{
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);
96-
97-
this.text = val.ToString();
98-
}
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;
69+
this.text = string.Format(mFormat,
70+
Math.Round(mSlider.value, mRoundPlace),
71+
Math.Round(mSlider.maxValue, mRoundPlace),
72+
Math.Round(mSlider.minValue, mRoundPlace));
12473
}
12574
}
12675
}

docs/ScriptReference/UI/Slider/JCS_SliderTextDisplay.md

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,8 @@ Text that display slider's value.
44

55
## Variables
66

7-
| Name | Description |
8-
|:--------------|:---------------------------------------------------|
9-
| mSlider | To update the text along with this slider's value. |
10-
| mTextDisplay | Text display type. |
11-
| mRoundPlace | Place you want to round the decimal. |
12-
| mCustomFormat | Display with the custom text. |
7+
| Name | Description |
8+
|:------------|:---------------------------------------------------|
9+
| mSlider | To update the text along with this slider's value. |
10+
| mFormat | Format text to display. |
11+
| mRoundPlace | Place you want to round the decimal. |

0 commit comments

Comments
 (0)