1313import net .minecraft .network .chat .Component ;
1414import wily .factoryapi .base .config .FactoryConfig ;
1515import wily .factoryapi .base .config .FactoryConfigControl ;
16+ import wily .factoryapi .base .config .FactoryConfigDisplay ;
1617
1718
1819import java .io .StringReader ;
@@ -24,15 +25,15 @@ public class FactoryConfigWidgets {
2425 private static final List <WidgetOverride <?>> overrides = new ArrayList <>();
2526 public static final Function <Component ,Tooltip > TOOLTIP_CACHE = Util .memoize (c ->Tooltip .create (c ));
2627
27- public static Tooltip getCachedTooltip (Component component ){
28+ public static Tooltip getCachedTooltip (Component component ) {
2829 return component == null ? null : TOOLTIP_CACHE .apply (component );
2930 }
3031
3132 public interface WidgetOverride <T > {
3233 AbstractWidget createWidget (FactoryConfig <T > config , Function <T ,Tooltip > tooltip , int x , int y , int width , Consumer <T > afterSet );
3334 }
3435
35- public static <T > AbstractWidget getOverride (FactoryConfig <T > config , Function <T ,Tooltip > tooltipFunction , int x , int y , int width , Consumer <T > afterSet ){
36+ public static <T > AbstractWidget getOverride (FactoryConfig <T > config , Function <T ,Tooltip > tooltipFunction , int x , int y , int width , Consumer <T > afterSet ) {
3637 for (WidgetOverride <?> override : overrides ) {
3738 AbstractWidget widgetOverride = ((WidgetOverride <T >)override ).createWidget (config , tooltipFunction , x , y , width , afterSet );
3839 if (widgetOverride != null ) return widgetOverride ;
@@ -41,29 +42,30 @@ public static <T> AbstractWidget getOverride(FactoryConfig<T> config, Function<T
4142 }
4243
4344 public static <T > AbstractWidget createWidget (FactoryConfig <T > config , int x , int y , int width , Consumer <T > afterSet ) {
44- Function <T ,Tooltip > tooltipFunction = v -> getCachedTooltip (config .getDisplay ().tooltip ().apply (v ));
45+ FactoryConfigDisplay <T > display = config .getDisplay ();
46+ Function <T ,Tooltip > tooltipFunction = v -> getCachedTooltip (display .tooltip ().apply (v ));
4547 AbstractWidget override = getOverride (config , tooltipFunction , x , y , width , afterSet );
4648 if (override != null ) return override ;
47- if (config .control ().equals (FactoryConfigControl .TOGGLE )){
48- return CycleButton .<Boolean >builder (b -> config . getDisplay ().captionFunction (). apply (config . getDisplay (). name (), ( T ) b )).withValues (OptionInstance .BOOLEAN_VALUES .valueListSupplier ()).withTooltip (((Function <Boolean , Tooltip >) tooltipFunction )::apply ).withInitialValue ((Boolean ) config .get ()).create (x , y , width , 20 , config . getDisplay () .name (), (cycleButton , object ) -> FactoryConfig .saveOptionAndConsume (config , (T )object , afterSet ));
49- } else if (config .control () instanceof FactoryConfigControl .FromInt <T > c ){
50- return CycleButton .< T > builder (b -> config . getDisplay (). captionFunction (). apply ( config . getDisplay (). name (), b )). withValues (listSupplier (c .valueGetter (), c .valuesSize ())).withTooltip (tooltipFunction ::apply ).withInitialValue (config .get ()).create (x , y , width , 20 , config . getDisplay () .name (), (cycleButton , object ) -> FactoryConfig .saveOptionAndConsume (config , object ,afterSet ));
51- } else if (config .control () instanceof FactoryConfigControl .FromDouble <T > c ){
49+ if (config .control ().equals (FactoryConfigControl .TOGGLE )) {
50+ return CycleButton .<Boolean >builder (b -> display . valueToComponent ().apply (( T ) b )).withValues (OptionInstance .BOOLEAN_VALUES .valueListSupplier ()).withTooltip (((Function <Boolean , Tooltip >) tooltipFunction )::apply ).withInitialValue ((Boolean ) config .get ()).create (x , y , width , 20 , display .name (), (cycleButton , object ) -> FactoryConfig .saveOptionAndConsume (config , (T )object , afterSet ));
51+ } else if (config .control () instanceof FactoryConfigControl .FromInt <T > c ) {
52+ return CycleButton .builder (display . valueToComponent ()). withValues (listSupplier (c .valueGetter (), c .valuesSize ())).withTooltip (tooltipFunction ::apply ).withInitialValue (config .get ()).create (x , y , width , 20 , display .name (), (cycleButton , object ) -> FactoryConfig .saveOptionAndConsume (config , object ,afterSet ));
53+ } else if (config .control () instanceof FactoryConfigControl .FromDouble <T > c ) {
5254 return createSlider (config , x , y , width , afterSet , c .valueGetter (), c .valueSetter (), tooltipFunction );
5355 } else if (config .control () instanceof FactoryConfigControl .Int c ) {
5456 return createSlider ((FactoryConfig <Integer >)config , x , y , width , (Consumer <Integer >)afterSet , d -> (int )((c .max ().getAsInt () - c .min ()) * d ) + c .min (), i -> (double )(i - c .min ()) / (c .max ().getAsInt () - c .min ()), (Function <Integer , Tooltip >) tooltipFunction );
55- } else if (config .control () instanceof FactoryConfigControl .TextEdit <T > c ){
56- EditBox editBox = new EditBox (Minecraft .getInstance ().font , x , y , width , 20 , config . getDisplay () .name ());
57+ } else if (config .control () instanceof FactoryConfigControl .TextEdit <T > c ) {
58+ EditBox editBox = new EditBox (Minecraft .getInstance ().font , x , y , width , 20 , display .name ());
5759
5860 c .codec ().encodeStart (JsonOps .INSTANCE , config .get ()).result ().ifPresent (v -> editBox .setValue (v .toString ()));
59- editBox .setResponder (s -> {
61+ editBox .setResponder (s -> {
6062 DataResult <T > result ;
6163 try {
6264 result = c .codec ().parse (JsonOps .INSTANCE , JsonParser .parseReader (new StringReader (s )));
6365 } catch (JsonIOException | JsonSyntaxException e ) {
6466 result = DataResult .error (e ::getMessage );
6567 }
66- if (result .result ().isPresent ()){
68+ if (result .result ().isPresent ()) {
6769 editBox .setTextColor (0xE0E0E0 );
6870 config .set (result .result ().get ());
6971 config .save ();
@@ -78,11 +80,11 @@ public static <T> AbstractWidget createWidget(FactoryConfig<T> config, int x, in
7880 return null ;
7981 }
8082
81- public static <T > AbstractSliderButton createSlider (FactoryConfig <T > config , int x , int y , int width , Consumer <T > afterSet , Function <Double ,T > valueGetter , Function <T ,Double > valueSetter , Function <T ,Tooltip > tooltipFunction ){
82- return new AbstractSliderButton (x , y , width , 20 , config .getDisplay ().captionFunction (). apply ( config . getDisplay (). name (), config .get ()), valueSetter .apply (config .get ())) {
83+ public static <T > AbstractSliderButton createSlider (FactoryConfig <T > config , int x , int y , int width , Consumer <T > afterSet , Function <Double ,T > valueGetter , Function <T ,Double > valueSetter , Function <T ,Tooltip > tooltipFunction ) {
84+ return new AbstractSliderButton (x , y , width , 20 , config .getDisplay ().getMessage ( config .get ()), valueSetter .apply (config .get ())) {
8385 @ Override
8486 protected void updateMessage () {
85- setMessage (config .getDisplay ().captionFunction (). apply ( config . getDisplay (). name (), valueGetter .apply (value )));
87+ setMessage (config .getDisplay ().getMessage ( valueGetter .apply (value )));
8688 setTooltip (tooltipFunction .apply (valueGetter .apply (value )));
8789 }
8890
@@ -98,7 +100,7 @@ public static <T> AbstractWidget createWidget(FactoryConfig<T> config) {
98100 return createWidget (config , 0 , 0 , 0 , v -> {});
99101 }
100102
101- public static <T > CycleButton .ValueListSupplier <T > listSupplier (Function <Integer ,T > valueGetter , Supplier <Integer > valuesSize ){
103+ public static <T > CycleButton .ValueListSupplier <T > listSupplier (Function <Integer ,T > valueGetter , Supplier <Integer > valuesSize ) {
102104 List <T > list = new ArrayList <>();
103105 for (int i = 0 ; i < valuesSize .get (); i ++) {
104106 list .add (valueGetter .apply (i ));
0 commit comments