Start refactoring ClassicConfigGUI

This commit is contained in:
James Seibel
2025-09-21 21:30:01 -05:00
parent 21fe38da8e
commit 6db3795fa5
2 changed files with 144 additions and 82 deletions
@@ -54,6 +54,7 @@ import net.minecraft.client.gui.narration.NarratableEntry;
import net.minecraft.resources.ResourceLocation;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.jetbrains.annotations.NotNull;
import static com.seibel.distanthorizons.common.wrappers.gui.GuiHelper.*;
import static com.seibel.distanthorizons.common.wrappers.gui.GuiHelper.Translatable;
@@ -94,10 +95,10 @@ public class ClassicConfigGUI
private static class ConfigScreenConfigs
{
// This contains all the configs for the configs
public static final int SpaceFromRightScreen = 10;
public static final int ButtonWidthSpacing = 5;
public static final int ResetButtonWidth = 40;
public static final int ResetButtonHeight = 20;
public static final int SPACE_FROM_RIGHT_SCREEN = 10;
public static final int BUTTON_WIDTH_SPACING = 5;
public static final int RESET_BUTTON_WIDTH = 40;
public static final int RESET_BUTTON_HEIGHT = 20;
}
@@ -220,7 +221,7 @@ public class ClassicConfigGUI
private final String translationPrefix;
private final Screen parent;
private final String category;
private ConfigListWidget list;
private ConfigListWidget configListWidget;
private boolean reload = false;
private Button doneButton;
@@ -255,7 +256,9 @@ public class ClassicConfigGUI
}
// Changelog button
if (Config.Client.Advanced.AutoUpdater.enableAutoUpdater.get() && !ModInfo.IS_DEV_BUILD) // we only have changelogs for stable builds
if (Config.Client.Advanced.AutoUpdater.enableAutoUpdater.get()
// we only have changelogs for stable builds
&& !ModInfo.IS_DEV_BUILD)
{
this.addBtn(new TexturedButtonWidget(
// Where the button is on the screen
@@ -288,36 +291,46 @@ public class ClassicConfigGUI
this.addBtn(MakeBtn(Translatable("distanthorizons.general.cancel"),
this.width / 2 - 154, this.height - 28,
150, 20,
button ->
150, 20,
(button) ->
{
ConfigBase.INSTANCE.configFileINSTANCE.loadFromFile();
Objects.requireNonNull(this.minecraft).setScreen(this.parent);
}));
this.doneButton = this.addBtn(MakeBtn(Translatable("distanthorizons.general.done"), this.width / 2 + 4, this.height - 28, 150, 20, (button) -> {
ConfigBase.INSTANCE.configFileINSTANCE.saveToFile();
Objects.requireNonNull(this.minecraft).setScreen(this.parent);
}));
this.list = new ConfigListWidget(this.minecraft, this.width * 2, this.height, 32, 32, 25);
this.doneButton = this.addBtn(
MakeBtn(Translatable("distanthorizons.general.done"),
this.width / 2 + 4, this.height - 28,
150, 20,
(button) ->
{
ConfigBase.INSTANCE.configFileINSTANCE.saveToFile();
Objects.requireNonNull(this.minecraft).setScreen(this.parent);
}));
this.configListWidget = new ConfigListWidget(this.minecraft, this.width * 2, this.height, 32, 32, 25);
#if MC_VER < MC_1_20_6 // no background is rendered in MC 1.20.6+
if (this.minecraft != null && this.minecraft.level != null)
{
this.list.setRenderBackground(false);
}
#endif
this.addWidget(this.list);
this.addWidget(this.configListWidget);
for (AbstractConfigType info : ConfigBase.INSTANCE.entries)
{
try
{
if (info.getCategory().matches(this.category) && info.getAppearance().showInGui)
{
this.addMenuItem(info);
}
}
catch (Exception e)
{
String message = "ERROR: Failed to show [\" + info.getNameWCategory() + \"], error: ["+e.getMessage()+"]";
String message = "ERROR: Failed to show [" + info.getNameWCategory() + "], error: ["+e.getMessage()+"]";
if (info.get() != null)
{
message += " with the value [" + info.get() + "] with type [" + info.getType() + "]";
@@ -341,17 +354,20 @@ public class ClassicConfigGUI
if (ConfigEntry.class.isAssignableFrom(info.getClass()))
{
Button.OnPress btnAction = button -> {
Button.OnPress btnAction = (button) ->
{
((ConfigEntry) info).uiSetWithoutSaving(((ConfigEntry) info).getDefaultValue());
((EntryInfo) info.guiValue).index = 0;
this.reload = true;
Objects.requireNonNull(this.minecraft).setScreen(this);
};
int posX = this.width - ConfigScreenConfigs.SpaceFromRightScreen - 150 - ConfigScreenConfigs.ButtonWidthSpacing - ConfigScreenConfigs.ResetButtonWidth;
int posX = this.width - ConfigScreenConfigs.SPACE_FROM_RIGHT_SCREEN - 150 - ConfigScreenConfigs.BUTTON_WIDTH_SPACING - ConfigScreenConfigs.RESET_BUTTON_WIDTH;
int posZ = 0;
Button resetButton = MakeBtn(Translatable("distanthorizons.general.reset").withStyle(ChatFormatting.RED),
posX, posZ, ConfigScreenConfigs.ResetButtonWidth, ConfigScreenConfigs.ResetButtonHeight,
Button resetButton = MakeBtn(
Translatable("distanthorizons.general.reset").withStyle(ChatFormatting.RED),
posX, posZ,
ConfigScreenConfigs.RESET_BUTTON_WIDTH, ConfigScreenConfigs.RESET_BUTTON_HEIGHT,
btnAction);
if (((EntryInfo) info.guiValue).widget instanceof Map.Entry)
@@ -359,44 +375,67 @@ public class ClassicConfigGUI
Map.Entry<Button.OnPress, Function<Object, Component>> widget = (Map.Entry<Button.OnPress, Function<Object, Component>>) ((EntryInfo) info.guiValue).widget;
if (info.getType().isEnum())
{
widget.setValue(value -> Translatable(this.translationPrefix + "enum." + info.getType().getSimpleName() + "." + info.get().toString()));
widget.setValue((value) -> Translatable(this.translationPrefix + "enum." + info.getType().getSimpleName() + "." + info.get().toString()));
Component z = Translatable(this.translationPrefix + "enum." + info.getType().getSimpleName() + "." + info.get().toString());
}
this.list.addButton(MakeBtn(widget.getValue().apply(info.get()), this.width - 150 - ConfigScreenConfigs.SpaceFromRightScreen, 0, 150, 20, widget.getKey()), resetButton, null, name);
Component x = widget.getValue().apply(info.get());
this.configListWidget.addButton(this,
MakeBtn(
widget.getValue().apply(info.get()),
this.width - 150 - ConfigScreenConfigs.SPACE_FROM_RIGHT_SCREEN, // negative means further to the left
0, // pos Z
150, 20,
widget.getKey()),
resetButton,
null,
name);
return;
}
else if (((EntryInfo) info.guiValue).widget != null)
{
EditBox widget = new EditBox(this.font, this.width - 150 - ConfigScreenConfigs.SpaceFromRightScreen + 2, 0, 150 - 4, 20, Translatable(""));
EditBox widget = new EditBox(this.font, this.width - 150 - ConfigScreenConfigs.SPACE_FROM_RIGHT_SCREEN + 2, 0, 150 - 4, 20, Translatable(""));
widget.setMaxLength(150);
widget.insertText(String.valueOf(info.get()));
Predicate<String> processor = ((BiFunction<EditBox, Button, Predicate<String>>) ((EntryInfo) info.guiValue).widget).apply(widget, this.doneButton);
widget.setFilter(processor);
this.list.addButton(widget, resetButton, null, name);
this.configListWidget.addButton(this, widget, resetButton, null, name);
return;
}
}
if (ConfigCategory.class.isAssignableFrom(info.getClass()))
{
Button widget = MakeBtn(name, this.width / 2 - 100, this.height - 28, 100 * 2, 20, (button -> {
ConfigBase.INSTANCE.configFileINSTANCE.saveToFile();
Objects.requireNonNull(this.minecraft).setScreen(ClassicConfigGUI.getScreen(this.configBase, this, ((ConfigCategory) info).getDestination()));
}));
this.list.addButton(widget, null, null, null);
Button widget = MakeBtn(name,
this.width / 2 - 100, this.height - 28,
100 * 2, 20,
((button) ->
{
ConfigBase.INSTANCE.configFileINSTANCE.saveToFile();
Objects.requireNonNull(this.minecraft).setScreen(ClassicConfigGUI.getScreen(this.configBase, this, ((ConfigCategory) info).getDestination()));
}));
this.configListWidget.addButton(this, widget, null, null, null);
return;
}
if (ConfigUIButton.class.isAssignableFrom(info.getClass()))
{
Button widget = MakeBtn(name, this.width / 2 - 100, this.height - 28, 100 * 2, 20, (button -> {
((ConfigUIButton) info).runAction();
}));
this.list.addButton(widget, null, null, null);
Button widget = MakeBtn(name,
this.width / 2 - 100, this.height - 28,
100 * 2, 20,
(button) -> ((ConfigUIButton) info).runAction());
this.configListWidget.addButton(this, widget, null, null, null);
return;
}
if (ConfigUIComment.class.isAssignableFrom(info.getClass()))
{
this.list.addButton(null, null, null, name);
this.configListWidget.addButton(this, null, null, null, name);
return;
}
if (ConfigUiLinkedEntry.class.isAssignableFrom(info.getClass()))
{
this.addMenuItem(((ConfigUiLinkedEntry) info).get());
@@ -418,7 +457,8 @@ public class ClassicConfigGUI
#else
super.render(matrices, mouseX, mouseY, delta);
#endif
this.list.render(matrices, mouseX, mouseY, delta); // Render buttons
this.configListWidget.render(matrices, mouseX, mouseY, delta); // Render buttons
// Render title
this.DhDrawCenteredString(matrices, this.font, this.title, this.width / 2, 15,
@@ -454,12 +494,12 @@ public class ClassicConfigGUI
// Render the tooltip only if it can find a tooltip in the language file
for (AbstractConfigType info : ConfigBase.INSTANCE.entries)
{
if (info.getCategory().matches(category) && info.getAppearance().showInGui)
if (info.getCategory().matches(this.category) && info.getAppearance().showInGui)
{
if (list.getHoveredButton(mouseX, mouseY).isPresent())
if (this.configListWidget.getHoveredButton(mouseX, mouseY).isPresent())
{
AbstractWidget buttonWidget = list.getHoveredButton(mouseX, mouseY).get();
Component text = ButtonEntry.buttonsWithText.get(buttonWidget);
AbstractWidget buttonWidget = this.configListWidget.getHoveredButton(mouseX, mouseY).get();
Component text = ButtonEntry.BUTTONS_WITH_TEXT.get(buttonWidget);
if (text == null)
{
continue;
@@ -471,11 +511,11 @@ public class ClassicConfigGUI
info;
Component name = Translatable(this.translationPrefix + (info.category.isEmpty() ? "" : info.category + ".") + info.getName());
String key = translationPrefix + (newInfo.category.isEmpty() ? "" : newInfo.category + ".") + newInfo.getName() + ".@tooltip";
String key = this.translationPrefix + (newInfo.category.isEmpty() ? "" : newInfo.category + ".") + newInfo.getName() + ".@tooltip";
if (((EntryInfo) newInfo.guiValue).error != null && text.equals(name))
{
DhRenderTooltip(matrices, font, ((EntryInfo) newInfo.guiValue).error.getValue(), mouseX, mouseY);
this.DhRenderTooltip(matrices, this.font, ((EntryInfo) newInfo.guiValue).error.getValue(), mouseX, mouseY);
}
else if (I18n.exists(key) && (text != null && text.equals(name)))
{
@@ -484,7 +524,8 @@ public class ClassicConfigGUI
{
list.add(TextOrTranslatable(str));
}
DhRenderComponentTooltip(matrices, font, list, mouseX, mouseY);
this.DhRenderComponentTooltip(matrices, font, list, mouseX, mouseY);
}
}
}
@@ -592,26 +633,23 @@ public class ClassicConfigGUI
#else
super(minecraftClient, canvasWidth, canvasHeight - (topMargin + botMargin), topMargin, itemSpacing);
#endif
this.centerListVertically = false;
textRenderer = minecraftClient.font;
this.textRenderer = minecraftClient.font;
}
public void addButton(AbstractWidget button, AbstractWidget resetButton, AbstractWidget indexButton, Component text)
{
this.addEntry(ButtonEntry.create(button, text, resetButton, indexButton));
}
public void addButton(ConfigScreen gui, AbstractWidget button, AbstractWidget resetButton, AbstractWidget indexButton, Component text)
{ this.addEntry(ButtonEntry.create(gui, button, text, resetButton, indexButton)); }
@Override
public int getRowWidth()
{
return 10000;
}
public int getRowWidth() { return 10_000; }
public Optional<AbstractWidget> getHoveredButton(double mouseX, double mouseY)
{
for (ButtonEntry buttonEntry : this.children())
{
if (buttonEntry.button != null && buttonEntry.button.isMouseOver(mouseX, mouseY))
if (buttonEntry.button != null
&& buttonEntry.button.isMouseOver(mouseX, mouseY))
{
return Optional.of(buttonEntry.button);
}
@@ -625,31 +663,41 @@ public class ClassicConfigGUI
public static class ButtonEntry extends ContainerObjectSelectionList.Entry<ButtonEntry>
{
private static final Font textRenderer = Minecraft.getInstance().font;
public final AbstractWidget button;
private final ConfigScreen gui;
private final AbstractWidget resetButton;
private final AbstractWidget indexButton;
private final Component text;
private final List<AbstractWidget> children = new ArrayList<>();
public static final Map<AbstractWidget, Component> buttonsWithText = new HashMap<>();
private ButtonEntry(AbstractWidget button, Component text, AbstractWidget resetButton, AbstractWidget indexButton)
public static final Map<AbstractWidget, Component> BUTTONS_WITH_TEXT = new HashMap<>();
private ButtonEntry(ConfigScreen gui, AbstractWidget button, Component text, AbstractWidget resetButton, AbstractWidget indexButton)
{
buttonsWithText.put(button, text);
BUTTONS_WITH_TEXT.put(button, text);
this.gui = gui;
this.button = button;
this.resetButton = resetButton;
this.text = text;
this.indexButton = indexButton;
if (button != null)
children.add(button);
if (resetButton != null)
children.add(resetButton);
if (indexButton != null)
children.add(indexButton);
if (button != null) { this.children.add(button); }
if (resetButton != null) { this.children.add(resetButton); }
if (indexButton != null) { this.children.add(indexButton); }
}
public static ButtonEntry create(AbstractWidget button, Component text, AbstractWidget resetButton, AbstractWidget indexButton)
public static ButtonEntry create(ConfigScreen gui, AbstractWidget button, Component text, AbstractWidget resetButton, AbstractWidget indexButton)
{
return new ButtonEntry(button, text, resetButton, indexButton);
return new ButtonEntry(gui, button, text, resetButton, indexButton);
}
@Override
@@ -659,46 +707,60 @@ public class ClassicConfigGUI
public void render(GuiGraphics matrices, int index, int y, int x, int entryWidth, int entryHeight, int mouseX, int mouseY, boolean hovered, float tickDelta)
#endif
{
if (button != null)
if (this.button != null)
{
SetY(button, y);
button.render(matrices, mouseX, mouseY, tickDelta);
SetY(this.button, y);
this.button.render(matrices, mouseX, mouseY, tickDelta);
}
if (resetButton != null)
if (this.resetButton != null)
{
SetY(resetButton, y);
resetButton.render(matrices, mouseX, mouseY, tickDelta);
SetY(this.resetButton, y);
this.resetButton.render(matrices, mouseX, mouseY, tickDelta);
}
if (indexButton != null)
if (this.indexButton != null)
{
SetY(indexButton, y);
indexButton.render(matrices, mouseX, mouseY, tickDelta);
SetY(this.indexButton, y);
this.indexButton.render(matrices, mouseX, mouseY, tickDelta);
}
if (text != null && (!text.getString().contains("spacer") || button != null))
if (this.text != null
&&
(
!this.text.getString().contains("spacer")
|| this.button != null)
)
{
int translatedLength = textRenderer.width(this.text);
// right justify the text right next to the button
translatedLength = this.gui.width - translatedLength - 210; // TODO constant for button widths
#if MC_VER < MC_1_20_1
GuiComponent.drawString(matrices, textRenderer, text, 12, y + 5, 0xFFFFFF);
#elif MC_VER < MC_1_21_6
matrices.drawString(textRenderer, this.text, 12, y + 5, 0xFFFFFF);
#else
matrices.drawString(textRenderer, this.text, 12, y + 5, 0xFFFFFFFF);
matrices.drawString(textRenderer, this.text, translatedLength, y + 5, 0xFFFFFFFF);
#endif
}
}
@Override
public List<? extends GuiEventListener> children()
{
return children;
}
public @NotNull List<? extends GuiEventListener> children()
{ return this.children; }
// Only for 1.17 and over
// Remove in 1.16 and below
#if MC_VER >= MC_1_17_1
@Override
public List<? extends NarratableEntry> narratables()
{
return children;
}
public @NotNull List<? extends NarratableEntry> narratables()
{ return this.children; }
#endif
}