diff options
Diffstat (limited to 'src/main/java/net/inyourwalls')
4 files changed, 107 insertions, 7 deletions
diff --git a/src/main/java/net/inyourwalls/customlevelcolour/ConfigScreen.java b/src/main/java/net/inyourwalls/customlevelcolour/ConfigScreen.java index a901b50..3b7ed18 100644 --- a/src/main/java/net/inyourwalls/customlevelcolour/ConfigScreen.java +++ b/src/main/java/net/inyourwalls/customlevelcolour/ConfigScreen.java @@ -2,26 +2,104 @@ package net.inyourwalls.customlevelcolour; import net.minecraft.client.gui.DrawContext; import net.minecraft.client.gui.screen.Screen; +import net.minecraft.client.gui.widget.ButtonWidget; +import net.minecraft.client.gui.widget.TextFieldWidget; import net.minecraft.text.Text; +import java.util.regex.Pattern; + +import static net.minecraft.client.gui.widget.ButtonWidget.DEFAULT_HEIGHT; +import static net.minecraft.client.gui.widget.ButtonWidget.DEFAULT_WIDTH; +import static net.minecraft.client.gui.widget.ButtonWidget.DEFAULT_WIDTH_SMALL; + public class ConfigScreen extends Screen { private static final Text TITLE = Text.translatable("gui.customlevelcolour.title"); + private static final Text OUTLINE_COLOUR_LABEL = Text.translatable("gui.customlevelcolour.outlineColour"); + private static final Text TEXT_COLOUR_LABEL = Text.translatable("gui.customlevelcolour.textColour"); + private static final Pattern HEX_COLOUR_REGEX = Pattern.compile("[0-9a-fA-F]{0,6}"); private final Screen previous; + private final CustomLevelColourConfig config; public ConfigScreen() { - super(TITLE); - this.previous = null; + this(null); } public ConfigScreen(Screen previous) { super(TITLE); this.previous = previous; + this.config = CustomLevelColour.getConfig(); + } + + @Override + public void init() { + TextFieldWidget outlineField = new TextFieldWidget( + this.textRenderer, + this.width / 2 + 4, + 25, + DEFAULT_WIDTH_SMALL, + DEFAULT_HEIGHT, + OUTLINE_COLOUR_LABEL + ); + outlineField.setTextPredicate(HEX_COLOUR_REGEX.asMatchPredicate()); + outlineField.setText(String.format("%x", config.outlineColour)); + TextFieldWidget textField = new TextFieldWidget( + this.textRenderer, + this.width / 2 + 4, + 55, + DEFAULT_WIDTH_SMALL, + DEFAULT_HEIGHT, + TEXT_COLOUR_LABEL + ); + textField.setTextPredicate(HEX_COLOUR_REGEX.asMatchPredicate()); + textField.setText(String.format("%x", config.textColour)); + + ButtonWidget backButton = ButtonWidget.builder(Text.translatable("gui.back"), thisButton -> this.close()) + .size(DEFAULT_WIDTH, DEFAULT_HEIGHT) + .position(this.width / 2 - DEFAULT_WIDTH - 4, this.height - 28) + .build(); + ButtonWidget saveButton = ButtonWidget.builder(Text.translatable("selectWorld.edit.save"), thisButton -> { + try { + config.outlineColour = Integer.parseInt(outlineField.getText(), 16); + config.textColour = Integer.parseInt(textField.getText(), 16); + } catch (NumberFormatException ex) { + CustomLevelColour.LOGGER.error("Failed to parse colour", ex); + } + CustomLevelColour.saveConfig(); + }) + .size(DEFAULT_WIDTH, DEFAULT_HEIGHT) + .position(this.width / 2 + 4, this.height - 28) + .build(); + + this.addDrawableChild(outlineField); + this.addDrawableChild(textField); + this.addDrawableChild(backButton); + this.addDrawableChild(saveButton); + } + + @Override + public void close() { + this.client.setScreen(this.previous); } @Override public void render(DrawContext context, int mouseX, int mouseY, float delta) { super.renderBackground(context); super.render(context, mouseX, mouseY, delta); - context.drawCenteredTextWithShadow(this.textRenderer, TITLE, this.width / 2, 5, 0xffffff); + context.drawCenteredTextWithShadow(this.textRenderer, TITLE, this.width / 2, 5, 0xffffff); + + int labelX = this.width / 2 - Math.max( + this.textRenderer.getWidth(TEXT_COLOUR_LABEL), + this.textRenderer.getWidth(OUTLINE_COLOUR_LABEL) + ) - 4; + context.drawTextWithShadow(this.textRenderer, OUTLINE_COLOUR_LABEL, labelX, 30, 0xffffff); + context.drawTextWithShadow(this.textRenderer, TEXT_COLOUR_LABEL, labelX, 60, 0xffffff); + + // preview text + int previewX = (this.width / 2) - (this.textRenderer.getWidth("30") / 2); + context.drawText(this.textRenderer, "30", previewX - 1, 90, config.outlineColour, false); + context.drawText(this.textRenderer, "30", previewX + 1, 90, config.outlineColour, false); + context.drawText(this.textRenderer, "30", previewX, 91, config.outlineColour, false); + context.drawText(this.textRenderer, "30", previewX, 89, config.outlineColour, false); + context.drawText(this.textRenderer, "30", previewX, 90, config.textColour, false); } } diff --git a/src/main/java/net/inyourwalls/customlevelcolour/CustomLevelColour.java b/src/main/java/net/inyourwalls/customlevelcolour/CustomLevelColour.java index f80e0f4..2c9a09d 100644 --- a/src/main/java/net/inyourwalls/customlevelcolour/CustomLevelColour.java +++ b/src/main/java/net/inyourwalls/customlevelcolour/CustomLevelColour.java @@ -27,4 +27,13 @@ public class CustomLevelColour implements ClientModInitializer { if (config == null) throw new IllegalStateException("Attempted to get the configuration before it was loaded"); return config; } + + public static void saveConfig() { + if (config == null) throw new IllegalStateException("Attempted to save the configuration before it was loaded"); + try { + CustomLevelColourConfig.save(config, CONFIG_PATH); + } catch (IOException ex) { + LOGGER.error("Failed to save configuration file", ex); + } + } } diff --git a/src/main/java/net/inyourwalls/customlevelcolour/CustomLevelColourConfig.java b/src/main/java/net/inyourwalls/customlevelcolour/CustomLevelColourConfig.java index b963f02..c8c47d7 100644 --- a/src/main/java/net/inyourwalls/customlevelcolour/CustomLevelColourConfig.java +++ b/src/main/java/net/inyourwalls/customlevelcolour/CustomLevelColourConfig.java @@ -18,9 +18,9 @@ public class CustomLevelColourConfig { return GSON.fromJson(reader, CustomLevelColourConfig.class); } - public static void save(Path path) throws IOException { + public static void save(CustomLevelColourConfig config, Path path) throws IOException { FileWriter writer = new FileWriter(path.toFile()); - GSON.toJson(writer); + GSON.toJson(config, writer); writer.close(); } } diff --git a/src/main/java/net/inyourwalls/customlevelcolour/mixin/InGameHudMixin.java b/src/main/java/net/inyourwalls/customlevelcolour/mixin/InGameHudMixin.java index 6fbc545..07e5a82 100644 --- a/src/main/java/net/inyourwalls/customlevelcolour/mixin/InGameHudMixin.java +++ b/src/main/java/net/inyourwalls/customlevelcolour/mixin/InGameHudMixin.java @@ -1,6 +1,7 @@ -// InGameHudMixin: Singular @ModifyArg to change the colour of the level text. +// InGameHudMixin: Changes the colour of the level text & outline. package net.inyourwalls.customlevelcolour.mixin; +import net.inyourwalls.customlevelcolour.CustomLevelColour; import net.minecraft.client.gui.hud.InGameHud; import org.spongepowered.asm.mixin.Mixin; import org.spongepowered.asm.mixin.injection.At; @@ -11,6 +12,18 @@ public class InGameHudMixin { @ModifyArg( at = @At( value = "INVOKE", + target = "Lnet/minecraft/client/gui/DrawContext;drawText(Lnet/minecraft/client/font/TextRenderer;Ljava/lang/String;IIIZ)I" + ), + method = "renderExperienceBar", + index = 4 + ) + private int overwriteOutlineColour(int oldColour) { + return CustomLevelColour.getConfig().outlineColour; + } + + @ModifyArg( + at = @At( + value = "INVOKE", target = "Lnet/minecraft/client/gui/DrawContext;drawText(Lnet/minecraft/client/font/TextRenderer;Ljava/lang/String;IIIZ)I", ordinal = 4 ), @@ -18,6 +31,6 @@ public class InGameHudMixin { index = 4 ) private int overwriteExperienceBarColour(int oldColour) { - return 0xffffff; + return CustomLevelColour.getConfig().textColour; } } |
