Skip to content

Commit cd15404

Browse files
committed
feat(SliderInputField): Add new component JCS_SliderInputField
1 parent a8ed63f commit cd15404

File tree

5 files changed

+487
-3
lines changed

5 files changed

+487
-3
lines changed
Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
/**
2+
* $File: JCS_SliderInputField.cs $
3+
* $Date: 2022-09-12 01:45:45 $
4+
* $Revision: $
5+
* $Creator: Jen-Chieh Shen $
6+
* $Notice: See LICENSE.txt for modification and distribution information
7+
* Copyright © 2022 by Shen, Jen-Chieh $
8+
*/
9+
using System;
10+
using System.Collections.Generic;
11+
using UnityEngine;
12+
using UnityEngine.UI;
13+
using UnityEngine.Windows;
14+
15+
namespace JCSUnity
16+
{
17+
/// <summary>
18+
/// Input field that controls sliders.
19+
/// </summary>
20+
[RequireComponent(typeof(InputField))]
21+
public class JCS_SliderInputField : MonoBehaviour
22+
{
23+
/* Variables */
24+
25+
private InputField mInputField = null;
26+
27+
[Header("** Initialize Variables (JCS_SliderInputField) **")]
28+
29+
[Tooltip("Update the content type once on start.")]
30+
[SerializeField]
31+
private bool mAutoContentType = true;
32+
33+
[Header("** Runtime Variables (JCS_SliderInputField) **")]
34+
35+
[Tooltip("List of sliders you want this input field to control.")]
36+
public List<Slider> sliders = null;
37+
38+
[Tooltip("Place you want to round the decimal.")]
39+
[SerializeField]
40+
[Range(0, 15)]
41+
private int mRoundPlace = 2;
42+
43+
/* Setter & Getter */
44+
45+
public bool AutoContentType { get { return mAutoContentType; } set { this.mAutoContentType = value; } }
46+
public int RoundPlace { get { return this.mRoundPlace; } set { this.mRoundPlace = value; } }
47+
48+
/* Functions */
49+
50+
private void Awake()
51+
{
52+
mInputField = GetComponent<InputField>();
53+
54+
mInputField.onValueChanged.AddListener(OnValueChanged);
55+
}
56+
57+
private void Start()
58+
{
59+
if (mAutoContentType)
60+
UpdateContentType();
61+
}
62+
63+
private void Update()
64+
{
65+
if (mInputField.isFocused)
66+
return;
67+
68+
Slider slider = MasterSlider();
69+
70+
if (slider == null)
71+
return;
72+
73+
double val = Math.Round(slider.value, mRoundPlace);
74+
75+
mInputField.text = val.ToString();
76+
}
77+
78+
private void OnValueChanged(string text)
79+
{
80+
float val;
81+
82+
try
83+
{
84+
val = float.Parse(text.ToString());
85+
}
86+
catch (FormatException)
87+
{
88+
return;
89+
}
90+
91+
UpdateSliders(val);
92+
}
93+
94+
private void UpdateSliders(float val)
95+
{
96+
foreach (Slider slider in sliders)
97+
{
98+
slider.value = val;
99+
}
100+
}
101+
102+
/// <summary>
103+
/// Update content type according to the master slider.
104+
/// </summary>
105+
public void UpdateContentType()
106+
{
107+
Slider slider = MasterSlider();
108+
109+
if (slider == null)
110+
return;
111+
112+
InputField.ContentType type = InputField.ContentType.DecimalNumber;
113+
114+
if (slider.wholeNumbers)
115+
type = InputField.ContentType.IntegerNumber;
116+
117+
mInputField.contentType = type;
118+
}
119+
120+
/// <summary>
121+
/// Return a slider.
122+
/// </summary>
123+
private Slider MasterSlider()
124+
{
125+
if (sliders.Count == 0)
126+
return null;
127+
128+
return sliders[0];
129+
}
130+
}
131+
}

Assets/JCSUnity/Scripts/UI/Slider/JCS_SliderInputField.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public class JCS_SliderTextDisplay : JCS_TextObject
1919
{
2020
/* Variables */
2121

22-
[Header("** Initialize Variables (JCS_TextSliderDisplay) **")]
22+
[Header("** Runtime Variables (JCS_TextSliderDisplay) **")]
2323

2424
[Tooltip("To update the text along with this slider's value.")]
2525
[SerializeField]

0 commit comments

Comments
 (0)