99 * Slider widget for general use.
1010 */
1111public class SliderWidget extends net .minecraft .client .gui .widget .SliderWidget {
12- private final float round ;
12+ private final int steps ;
1313 private Consumer <Double > changedListener ;
1414 private Text text ;
1515 public double min , max ;
@@ -23,66 +23,37 @@ public class SliderWidget extends net.minecraft.client.gui.widget.SliderWidget {
2323 * @param text
2424 * @param minValue
2525 * @param maxValue
26- * @param value
27- * @param round rounding of value.
28- * For example when round==5 then value range from 0 to 256
29- * values would only be numbers that are divisible by five (0,5,15,230,255).
30- * This rule is not applied to value contructor parameter
26+ * @param initialValue
27+ * @param steps
3128 */
32- public SliderWidget (int x , int y , int width , int height , Text text , double minValue , double maxValue , double value , float round ) {
33- super (x , y , width , height , text , value );
29+ public SliderWidget (int x , int y , int width , int height , Text text , double initialValue , double minValue , double maxValue , int steps ) {
30+ super (x , y , width , height , text , MathHelper . map ( initialValue , minValue , maxValue , 0 , 1 ) );
3431 this .min =minValue ;
3532 this .max =maxValue ;
3633 this .text =text ;
37- this .round =round ;
38- setValueOwn (scaleValueTo01 (value ));
39- setMessage (text );
40- }
41- public SliderWidget (int x , int y , int width , int height , Text text , double maxValue , double value , int round ){
42- this (x , y , width , height , text ,0 ,maxValue , value ,round );
43- }
44-
45- @ Override
46- protected void updateMessage () {
47- }
48- public void setText (Text text ){
49- this .text =text ;
34+ this .steps =steps ;
5035 updateMessage ();
5136 }
52- public Text getText (){
53- return text ;
54- }
5537
5638 @ Override
57- public Text getMessage () {
58- var val = round (scaleValueFrom01 (value ));
59- var isInt = round == (int ) round ;
60- var s = isInt ? String .valueOf ((int ) val ) : String .valueOf (val );
61- return this .text .copy ().append (Text .literal (" " +s ));
62- }
63- public double round (double value ){
64- return ((float )Math .round (value /round ))*round ;
39+ protected void updateMessage () {
40+ var mappedValue = getMappedValue ();
41+ var stringValue = mappedValue == (int )mappedValue
42+ ? String .valueOf ((int )mappedValue )
43+ : String .valueOf (mappedValue );
44+ this .setMessage (this .text .copy ().append (" " ).append (stringValue ));
45+ }
46+ private double getMappedValue (){
47+ var value = this .value ;
48+ value = Math .floor (value *steps )/steps ;
49+ value = MathHelper .lerp (value , this .min , this .max );
50+ return value ;
6551 }
6652 @ Override
6753 protected void applyValue () {
6854 if (changedListener ==null )
6955 return ;
70- changedListener .accept (round (scaleValueFrom01 (value )));
71- }
72- private double scaleValueFrom01 (double value ){
73- return value *(max -min )+min ;
74- }
75- private double scaleValueTo01 (double value ){
76- return (value -min )/(max -min );
77- }
78- public void setValueOwn (double value ) {
79- double d = this .value ;
80- this .value = MathHelper .clamp (value , 0.0 , 1.0 );
81- if (d != this .value ) {
82- this .applyValue ();
83- }
84-
85- this .updateMessage ();
56+ changedListener .accept (getMappedValue ());
8657 }
8758
8859 public void setChangedListener (Consumer <Double > changedListener ) {
0 commit comments