From c57f6732bbf120d771502a04f3420fc3cf2d9f84 Mon Sep 17 00:00:00 2001 From: yurisuika Date: Sat, 24 Feb 2024 05:09:18 -0800 Subject: [PATCH] text options screen --- gradle.properties | 10 +- .../java/dev/yurisuika/raised/Raised.java | 2 +- .../client/gui/screen/RaisedScreen.java | 160 ++++++++++++++++-- .../mixin/client/gui/hud/InGameHudMixin.java | 43 ++--- .../resources/assets/raised/lang/en_us.json | 7 +- 5 files changed, 171 insertions(+), 51 deletions(-) diff --git a/gradle.properties b/gradle.properties index 740f24f4..8c42546f 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1,14 +1,14 @@ org.gradle.jvmargs = -Xmx3G loom.platform = fabric -minecraft_version = 24w06a -yarn_mappings = 24w06a+build.6 -loader_version = 0.15.6 +minecraft_version = 24w07a +yarn_mappings = 24w07a+build.4 +loader_version = 0.15.7 mod_id = raised mod_author = yurisuika -mod_version = 3.1.1 +mod_version = 3.1.2 maven_group = dev.yurisuika.raised archives_base_name = raised -fabric_version = 0.95.6+1.20.5 \ No newline at end of file +fabric_version = 0.96.3+1.20.5 \ No newline at end of file diff --git a/src/main/java/dev/yurisuika/raised/Raised.java b/src/main/java/dev/yurisuika/raised/Raised.java index 3f88d5e8..0a5311d7 100644 --- a/src/main/java/dev/yurisuika/raised/Raised.java +++ b/src/main/java/dev/yurisuika/raised/Raised.java @@ -18,7 +18,7 @@ public class Raised implements ClientModInitializer { public static void registerClientTickEvents() { ClientTickEvents.END_CLIENT_TICK.register(client -> { while (options.wasPressed()) { - client.setScreen(new RaisedScreen(Text.translatable("options.raised.title"))); + client.setScreen(new RaisedScreen.SliderScreen(Text.translatable("options.raised.title"))); } }); } diff --git a/src/main/java/dev/yurisuika/raised/client/gui/screen/RaisedScreen.java b/src/main/java/dev/yurisuika/raised/client/gui/screen/RaisedScreen.java index 6be9a62b..0c88e7f1 100644 --- a/src/main/java/dev/yurisuika/raised/client/gui/screen/RaisedScreen.java +++ b/src/main/java/dev/yurisuika/raised/client/gui/screen/RaisedScreen.java @@ -5,45 +5,65 @@ import net.fabricmc.api.Environment; import net.minecraft.client.gui.DrawContext; import net.minecraft.client.gui.screen.Screen; -import net.minecraft.client.gui.widget.GridWidget; -import net.minecraft.client.gui.widget.SimplePositioningWidget; +import net.minecraft.client.gui.tooltip.Tooltip; +import net.minecraft.client.gui.widget.*; import net.minecraft.client.option.GameOptions; import net.minecraft.client.option.SimpleOption; import net.minecraft.screen.ScreenTexts; import net.minecraft.text.Text; +import net.minecraft.util.Formatting; import static dev.yurisuika.raised.client.option.RaisedConfig.*; +import static dev.yurisuika.raised.client.option.RaisedKeyBinding.*; @Environment(EnvType.CLIENT) -public class RaisedScreen extends Screen { +public abstract class RaisedScreen extends Screen { + + public GridWidget gridWidget; + public GridWidget.Adder adder; + public CheckboxWidget checkbox; + public ClickableWidget support; + public ClickableWidget sync; + public ClickableWidget share; public RaisedScreen(Text title) { super(title); } + public void setScreenType() { + if (client.currentScreen instanceof SliderScreen) { + client.setScreen(new TextScreen(Text.translatable("options.raised.title"))); + } else if (client.currentScreen instanceof TextScreen) { + client.setScreen(new SliderScreen(Text.translatable("options.raised.title"))); + } + } + @Override public void init() { - GridWidget gridWidget = new GridWidget(); - gridWidget.getMainPositioner().margin(0, 0, 4, 4); - GridWidget.Adder adder = gridWidget.createAdder(2); - adder.add(new SimpleOption<>("options.raised.hud", SimpleOption.constantTooltip(Text.translatable("options.raised.hud.tooltip")), (prefix, value) -> value == 0 ? GameOptions.getGenericValueText(prefix, ScreenTexts.OFF) : GameOptions.getGenericValueText(prefix, Text.literal(String.valueOf(value))), new SimpleOption.ValidatingIntSliderCallbacks(0, client.getWindow().getScaledHeight() / 4), getHud(), RaisedConfig::setHud).createWidget(client.options, 0, 0, 200), 2); - adder.add(new SimpleOption<>("options.raised.chat", SimpleOption.constantTooltip(Text.translatable("options.raised.chat.tooltip")), (prefix, value) -> value == 0 ? GameOptions.getGenericValueText(prefix, ScreenTexts.OFF) : GameOptions.getGenericValueText(prefix, Text.literal(String.valueOf(value))), new SimpleOption.ValidatingIntSliderCallbacks(0, client.getWindow().getScaledHeight() / 4), getChat(), RaisedConfig::setChat).createWidget(client.options, 0, 0, 200), 2); - adder.add(SimpleOption.ofBoolean("options.raised.support", SimpleOption.constantTooltip(Text.translatable("options.raised.support.tooltip")), getSupport(), RaisedConfig::setSupport).createWidget(client.options, 0, 0, 98)); - adder.add(SimpleOption.ofBoolean("options.raised.sync", SimpleOption.constantTooltip(Text.translatable("options.raised.sync.tooltip")), getSync(), RaisedConfig::setSync).createWidget(client.options, 0, 0, 98)); - adder.add(SimpleOption.ofBoolean("options.raised.share", SimpleOption.constantTooltip(Text.translatable("options.raised.share.tooltip")), getShare(), RaisedConfig::setShare).createWidget(client.options, 0, 0, 200), 2); - gridWidget.refreshPositions(); - SimplePositioningWidget.setPos(gridWidget, 16, 32, this.width, this.height, 0.0f, 0.0f); - gridWidget.forEachChild(this::addDrawableChild); + gridWidget = new GridWidget(); + gridWidget.getMainPositioner().alignHorizontalCenter().margin(2, 0, 2, 4); + adder = gridWidget.createAdder(2); + + checkbox = CheckboxWidget.builder(Text.translatable("options.raised.checkbox").withColor(-1).formatted(Formatting.WHITE), textRenderer).option(SimpleOption.ofBoolean("options.raised.checkbox", SimpleOption.emptyTooltip(), client.currentScreen instanceof TextScreen, value -> setScreenType())).tooltip(Tooltip.of(Text.translatable("options.raised.checkbox.tooltip"))).build(); + checkbox.setWidth(200); + support = SimpleOption.ofBoolean("options.raised.support", SimpleOption.constantTooltip(Text.translatable("options.raised.support.tooltip")), getSupport(), RaisedConfig::setSupport).createWidget(client.options, 0, 0, 98); + sync = SimpleOption.ofBoolean("options.raised.sync", SimpleOption.constantTooltip(Text.translatable("options.raised.sync.tooltip")), getSync(), RaisedConfig::setSync).createWidget(client.options, 0, 0, 98); + share = SimpleOption.ofBoolean("options.raised.share", SimpleOption.constantTooltip(Text.translatable("options.raised.share.tooltip")), getShare(), RaisedConfig::setShare).createWidget(client.options, 0, 0, 200); } @Override public void render(DrawContext context, int mouseX, int mouseY, float delta) { super.render(context, mouseX, mouseY, delta); - context.drawTextWithShadow(this.textRenderer, Text.translatable("options.raised.title"), 16, 16, -1); } @Override - public void renderBackground(DrawContext context, int mouseX, int mouseY, float delta) { + public boolean keyPressed(int keyCode, int scanCode, int modifiers) { + super.keyPressed(keyCode, scanCode, modifiers); + if (options.matchesKey(keyCode, scanCode)) { + close(); + return true; + } + return true; } @Override @@ -51,4 +71,112 @@ public boolean shouldPause() { return false; } + public static class SliderScreen extends RaisedScreen { + + public ClickableWidget hud; + public ClickableWidget chat; + + public SliderScreen(Text title) { + super(title); + } + + @Override + public void init() { + super.init(); + + hud = new SimpleOption<>("options.raised.hud", SimpleOption.constantTooltip(Text.translatable("options.raised.hud.tooltip")), (prefix, value) -> value == 0 ? GameOptions.getGenericValueText(prefix, ScreenTexts.OFF) : GameOptions.getGenericValueText(prefix, Text.literal(String.valueOf(value))), new SimpleOption.ValidatingIntSliderCallbacks(0, client.getWindow().getScaledHeight() / 4), getHud(), RaisedConfig::setHud).createWidget(client.options, 0, 0, 200); + chat = new SimpleOption<>("options.raised.chat", SimpleOption.constantTooltip(Text.translatable("options.raised.chat.tooltip")), (prefix, value) -> value == 0 ? GameOptions.getGenericValueText(prefix, ScreenTexts.OFF) : GameOptions.getGenericValueText(prefix, Text.literal(String.valueOf(value))), new SimpleOption.ValidatingIntSliderCallbacks(0, client.getWindow().getScaledHeight() / 4), getChat(), RaisedConfig::setChat).createWidget(client.options, 0, 0, 200); + + adder.add(checkbox, 2); + adder.add(hud, 2); + adder.add(chat, 2); + adder.add(support); + adder.add(sync); + adder.add(share, 2); + + gridWidget.refreshPositions(); + SimplePositioningWidget.setPos(gridWidget, 0, 32, width, height, 0.5F, 0.0F); + gridWidget.forEachChild(this::addDrawableChild); + } + + @Override + public void render(DrawContext context, int mouseX, int mouseY, float delta) { + int max = client.getWindow().getScaledHeight() / 4; + boolean changed = false; + + if (getHud() > max) { + setHud(max); + changed = true; + } + if (getChat() > max) { + setChat(max); + changed = true; + } + if (changed) { + client.setScreen(new SliderScreen(Text.translatable("options.raised.title"))); + } + + super.render(context, mouseX, mouseY, delta); + } + + @Override + public void renderBackground(DrawContext context, int mouseX, int mouseY, float delta) { + context.fillGradient((width / 2) - (((gridWidget.getWidth() - 4) / 2) + 8), 24, (width / 2) + (((gridWidget.getWidth() - 4) / 2) + 8), gridWidget.getHeight() + 24 + 8 + 4, -1072689136, -804253680); + } + + } + + public static class TextScreen extends RaisedScreen { + + public TextFieldWidget hud; + public TextFieldWidget chat; + + public TextScreen(Text title) { + super(title); + } + + @Override + public void init() { + super.init(); + + hud = new TextFieldWidget(textRenderer, 50, 20, Text.translatable("options.raised.hud")); + hud.setTooltip(Tooltip.of(Text.translatable("options.raised.hud.tooltip"))); + hud.setPlaceholder(Text.of(String.valueOf(0))); + hud.setText(String.valueOf(getHud())); + hud.setMaxLength(7); + hud.setChangedListener(value -> { + if (value.matches("[0-9]+") || value.isEmpty()) { + setHud(Integer.parseInt(value.isEmpty() ? "0" : value)); + } + }); + chat = new TextFieldWidget(textRenderer, 50, 20, Text.translatable("options.raised.chat")); + chat.setTooltip(Tooltip.of(Text.translatable("options.raised.chat.tooltip"))); + chat.setPlaceholder(Text.of(String.valueOf(0))); + chat.setText(String.valueOf(getChat())); + chat.setMaxLength(7); + chat.setChangedListener(value -> { + if (value.matches("[0-9]+") || value.isEmpty()) { + setChat(Integer.parseInt(value.isEmpty() ? "0" : value)); + } + }); + + adder.add(checkbox, 2); + adder.add(hud, 2); + adder.add(chat, 2); + adder.add(support); + adder.add(sync); + adder.add(share, 2); + + gridWidget.refreshPositions(); + SimplePositioningWidget.setPos(gridWidget, 0, 32, width, height, 0.5F, 0.0F); + gridWidget.forEachChild(this::addDrawableChild); + } + + @Override + public void renderBackground(DrawContext context, int mouseX, int mouseY, float delta) { + context.fillGradient((width / 2) - (((gridWidget.getWidth() - 4) / 2) + 8), 24, (width / 2) + (((gridWidget.getWidth() - 4) / 2) + 8), gridWidget.getHeight() + 24 + 8 + 4, -1072689136, -804253680); + } + + } + } \ No newline at end of file diff --git a/src/main/java/dev/yurisuika/raised/mixin/client/gui/hud/InGameHudMixin.java b/src/main/java/dev/yurisuika/raised/mixin/client/gui/hud/InGameHudMixin.java index fa16a7d8..593dd1e8 100644 --- a/src/main/java/dev/yurisuika/raised/mixin/client/gui/hud/InGameHudMixin.java +++ b/src/main/java/dev/yurisuika/raised/mixin/client/gui/hud/InGameHudMixin.java @@ -18,9 +18,17 @@ public abstract class InGameHudMixin { @Mixin(value = InGameHud.class, priority = -999999999) public abstract static class Pre { - // HEAD + // RENDER (HEAD) @Inject(method = "render", at = @At("HEAD")) - private void startHeadTranslate(DrawContext context, float tickDelta, CallbackInfo ci) { + private void startRenderHeadTranslate(DrawContext context, float tickDelta, CallbackInfo ci) { + if (getSupport()) { + start(context, 0, getHud(), 0); + } + } + + // RENDER (TAIL) + @Inject(method = "render", at = @At("TAIL")) + private void startRenderTailTranslate(DrawContext context, float tickDelta, CallbackInfo ci) { if (getSupport()) { start(context, 0, getHud(), 0); } @@ -49,27 +57,16 @@ private void endExperienceLevelTranslate(DrawContext context, float tickDelta, C } // OVERLAY MESSAGE - @Inject(method = "renderOverlayMessage", at = @At(value = "HEAD")) + @Inject(method = "renderOverlayMessage", at = @At(value = "INVOKE", target = "Lnet/minecraft/util/profiler/Profiler;push(Ljava/lang/String;)V")) private void startOverlayMessageTranslate(DrawContext context, float tickDelta, CallbackInfo ci) { start(context, 0, getHud(), 0); } - @Inject(method = "renderOverlayMessage", at = @At(value = "TAIL")) + @Inject(method = "renderOverlayMessage", at = @At(value = "INVOKE", target = "Lnet/minecraft/util/profiler/Profiler;pop()V", shift = At.Shift.AFTER)) private void endOverlayMessageTranslate(DrawContext context, float tickDelta, CallbackInfo ci) { end(context, 0, getHud(), 0); } - // TITLE AND SUBTITLES - @Inject(method = "renderOverlayMessage", at = @At(value = "HEAD")) - private void startTitleAndSubtitlesTranslate(DrawContext context, float tickDelta, CallbackInfo ci) { - end(context, 0, getHud(), 0); - } - - @Inject(method = "renderOverlayMessage", at = @At(value = "TAIL")) - private void endTitleAndSubtitlesTranslate(DrawContext context, float tickDelta, CallbackInfo ci) { - start(context, 0, getHud(), 0); - } - // CHAT @Inject(method = "renderChat", at = @At(value = "HEAD")) private void startChatTranslate(DrawContext context, float tickDelta, CallbackInfo ci) { @@ -81,14 +78,6 @@ private void endChatTranslate(DrawContext context, float tickDelta, CallbackInfo end(context, 0, getSync() ? getHud() : getChat(), 0); } - // TAIL - @Inject(method = "render", at = @At("TAIL")) - private void startTailTranslate(DrawContext context, float tickDelta, CallbackInfo ci) { - if (getSupport()) { - start(context, 0, getHud(), 0); - } - } - // HOTBAR SELECTOR @ModifyArgs(method = "renderHotbar", at = @At(value = "INVOKE", target = "Lnet/minecraft/client/gui/DrawContext;drawGuiTexture(Lnet/minecraft/util/Identifier;IIII)V", ordinal = 1)) private void resizeHotbarSelector(Args args) { @@ -101,17 +90,17 @@ private void resizeHotbarSelector(Args args) { @Mixin(value = InGameHud.class, priority = 999999999) public abstract static class Post { - // HEAD + // RENDER (HEAD) @Inject(method = "render", at = @At("HEAD")) - private void endHeadTranslate(DrawContext context, float tickDelta, CallbackInfo ci) { + private void endRenderHeadTranslate(DrawContext context, float tickDelta, CallbackInfo ci) { if (getSupport()) { end(context, 0, getHud(), 0); } } - // TAIL + // RENDER (TAIL) @Inject(method = "render", at = @At("TAIL")) - private void endTailTranslate(DrawContext context, float tickDelta, CallbackInfo ci) { + private void endRenderTailTranslate(DrawContext context, float tickDelta, CallbackInfo ci) { if (getSupport()) { end(context, 0, getHud(), 0); } diff --git a/src/main/resources/assets/raised/lang/en_us.json b/src/main/resources/assets/raised/lang/en_us.json index 331f9c0c..cf2b810d 100644 --- a/src/main/resources/assets/raised/lang/en_us.json +++ b/src/main/resources/assets/raised/lang/en_us.json @@ -23,11 +23,14 @@ "options.raised.title": "Raised Options", + "options.raised.checkbox": "Raised Options", + "options.raised.checkbox.tooltip": "Choose between control types.", + "options.raised.hud": "HUD", - "options.raised.hud.tooltip": "Controls the HUD offset.\nMaximum value is a quarter of current scaled screen height.", + "options.raised.hud.tooltip": "Controls the HUD offset.\nMaximum slider value is a quarter of scaled screen height.", "options.raised.chat": "Chat", - "options.raised.chat.tooltip": "Controls the Chat offset.\nMaximum value is a quarter of current scaled screen height.", + "options.raised.chat.tooltip": "Controls the Chat offset.\nMaximum slider value is a quarter of scaled screen height.", "options.raised.share": "Share", "options.raised.share.tooltip": "Enables Fabric mod support.\nAdds support for mod elements using Object Share on Fabric.",