better abstraction

This commit is contained in:
2025-11-04 13:52:26 +01:00
parent e421b1a5a0
commit bd74bce522
7 changed files with 52 additions and 40 deletions

View File

@@ -8,18 +8,14 @@ import net.minecraft.client.gui.widget.ClickableWidget;
import net.minecraft.client.gui.widget.ElementListWidget;
import net.minecraft.client.gui.widget.TextWidget;
import java.util.ArrayList;
import java.util.List;
import static com.kasetoatz.fastghast.configscreen.DumbassConfig.*;
public class ConfigEntry extends ElementListWidget.Entry<ConfigEntry>
{
private final List<ClickableWidget> widgets;
public ConfigEntry(ClickableWidget... widgets)
{
this.widgets = List.of(widgets);
}
private final ArrayList<ClickableWidget> widgets = new ArrayList<>();
@Override
public List<? extends Selectable> selectableChildren()
@@ -58,4 +54,9 @@ public class ConfigEntry extends ElementListWidget.Entry<ConfigEntry>
widget.render(context, mouseX, mouseY, deltaTicks);
}
}
public void add(ClickableWidget widget)
{
widgets.add(widget);
}
}

View File

@@ -1,5 +1,8 @@
package com.kasetoatz.fastghast.configscreen;
import com.kasetoatz.fastghast.configscreen.options.BoolOption;
import com.kasetoatz.fastghast.configscreen.options.ButtonOption;
import com.kasetoatz.fastghast.configscreen.options.InputOption;
import com.kasetoatz.fastghast.configscreen.options.Option;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.gui.DrawContext;
@@ -51,6 +54,20 @@ public class ConfigEntryList extends ElementListWidget<ConfigEntry>
public void add(Option<?> option)
{
this.addEntry(option.toEntry(this.client.textRenderer, this.getWidth()));
ConfigEntry entry = new ConfigEntry();
entry.add(new TextWidget(option.getText(), this.client.textRenderer));
if (option instanceof ButtonOption<?> buttonOption)
{
entry.add(ButtonWidget.builder(buttonOption.getButtonText(), buttonOption::onClick).build());
}
else if (option instanceof InputOption<?> inputOption)
{
inputOption.textField = new InputField(this.client.textRenderer, inputOption.getText());
inputOption.textField.setText(inputOption.getValue().toString());
inputOption.textField.setTextPredicate(inputOption::predicate);
inputOption.textField.setChangedListener(inputOption::changeListener);
entry.add(inputOption.textField);
}
this.addEntry(entry);
}
}

View File

@@ -1,38 +1,28 @@
package com.kasetoatz.fastghast.configscreen.options;
import com.kasetoatz.fastghast.configscreen.ConfigEntry;
import net.minecraft.client.font.TextRenderer;
import net.minecraft.client.gui.widget.ButtonWidget;
import net.minecraft.client.gui.widget.TextWidget;
import net.minecraft.text.Text;
import static com.kasetoatz.fastghast.configscreen.DumbassConfig.BOOLEAN_FALSE_COLOR;
import static com.kasetoatz.fastghast.configscreen.DumbassConfig.BOOLEAN_TRUE_COLOR;
public class BoolOption extends Option<Boolean>
public class BoolOption extends ButtonOption<Boolean>
{
public BoolOption(String text, String key, boolean defaultValue)
{
super(text, key, defaultValue);
}
private Text getButtonText()
@Override
public Text getButtonText()
{
return this.getValue() ? Text.literal("True").withColor(BOOLEAN_TRUE_COLOR) : Text.literal("False").withColor(BOOLEAN_FALSE_COLOR);
}
private void onClick(ButtonWidget button)
@Override
public void onClick(ButtonWidget button)
{
this.setValue(!this.getValue());
button.setMessage(this.getButtonText());
}
@Override
public ConfigEntry toEntry(TextRenderer textRenderer, int maxWidth)
{
return new ConfigEntry(
new TextWidget(this.getText(), textRenderer),
ButtonWidget.builder(getButtonText(), this::onClick).build()
);
}
}

View File

@@ -0,0 +1,15 @@
package com.kasetoatz.fastghast.configscreen.options;
import net.minecraft.client.gui.widget.ButtonWidget;
import net.minecraft.text.Text;
public abstract class ButtonOption<T> extends Option<T>
{
public ButtonOption(String text, String key, T defaultValue)
{
super(text, key, defaultValue);
}
public abstract Text getButtonText();
public abstract void onClick(ButtonWidget button);
}

View File

@@ -1,10 +1,6 @@
package com.kasetoatz.fastghast.configscreen.options;
import com.kasetoatz.fastghast.configscreen.ConfigEntry;
import com.kasetoatz.fastghast.configscreen.InputField;
import com.kasetoatz.fastghast.configscreen.options.validators.Validator;
import net.minecraft.client.font.TextRenderer;
import net.minecraft.client.gui.widget.TextWidget;
public class FloatOption extends InputOption<Float>
{
@@ -18,7 +14,8 @@ public class FloatOption extends InputOption<Float>
super(text, key, defaultValue);
}
private boolean predicate(String text)
@Override
public boolean predicate(String text)
{
if (text.isEmpty() || text.equals(".") || text.equals("-"))
{
@@ -36,15 +33,8 @@ public class FloatOption extends InputOption<Float>
}
@Override
public ConfigEntry toEntry(TextRenderer textRenderer, int maxWidth)
public void changeListener(String text)
{
this.textField = new InputField(textRenderer, this.getText());
textField.setText(this.getValue().toString());
textField.setTextPredicate(this::predicate);
textField.setChangedListener(text -> this.onChange((text.isEmpty() || text.equals(".") || text.equals("-")) ? 0.F : Float.parseFloat(text)));
return new ConfigEntry(
new TextWidget(this.getText(), textRenderer),
textField
);
this.onChange((text.isEmpty() || text.equals(".") || text.equals("-")) ? 0.F : Float.parseFloat(text));
}
}

View File

@@ -43,4 +43,7 @@ public abstract class InputOption<T> extends Option<T>
this.textField.setEditableColor(INVALID_INPUT_COLOR);
this.textField.setTooltip(Tooltip.of(Text.literal(message).withColor(INVALID_INPUT_COLOR)));
}
public abstract boolean predicate(String value);
public abstract void changeListener(String value);
}

View File

@@ -1,7 +1,5 @@
package com.kasetoatz.fastghast.configscreen.options;
import com.kasetoatz.fastghast.configscreen.ConfigEntry;
import net.minecraft.client.font.TextRenderer;
import net.minecraft.text.Text;
public abstract class Option<T>
@@ -43,6 +41,4 @@ public abstract class Option<T>
{
this.value = value;
}
public abstract ConfigEntry toEntry(TextRenderer textRenderer, int maxWidth);
}