From af09e34e0721378496b119a3625ef79b4d1a3ccb Mon Sep 17 00:00:00 2001 From: haykam821 <24855774+haykam821@users.noreply.github.com> Date: Thu, 1 Feb 2024 05:01:07 -0500 Subject: [PATCH] Fix screen prompts not being keyboard-navigable (#2297) --- .../sodium/client/gui/SodiumOptionsGUI.java | 16 ++++++++++--- .../client/gui/prompt/ScreenPrompt.java | 24 ++++++++++++------- 2 files changed, 29 insertions(+), 11 deletions(-) diff --git a/src/main/java/me/jellysquid/mods/sodium/client/gui/SodiumOptionsGUI.java b/src/main/java/me/jellysquid/mods/sodium/client/gui/SodiumOptionsGUI.java index cfc288cfd..2f441b160 100644 --- a/src/main/java/me/jellysquid/mods/sodium/client/gui/SodiumOptionsGUI.java +++ b/src/main/java/me/jellysquid/mods/sodium/client/gui/SodiumOptionsGUI.java @@ -16,6 +16,7 @@ import net.fabricmc.loader.api.FabricLoader; import net.minecraft.client.MinecraftClient; import net.minecraft.client.gui.DrawContext; +import net.minecraft.client.gui.Element; import net.minecraft.client.gui.screen.Screen; import net.minecraft.client.gui.screen.option.VideoOptionsScreen; import net.minecraft.text.OrderedText; @@ -139,6 +140,10 @@ protected void init() { super.init(); this.rebuildGUI(); + + if (this.prompt != null) { + this.prompt.init(); + } } private void rebuildGUI() { @@ -372,11 +377,11 @@ private void openDonationPage() { @Override public boolean keyPressed(int keyCode, int scanCode, int modifiers) { - if (this.prompt != null) { - return this.prompt.keyPressed(keyCode, scanCode, modifiers); + if (this.prompt != null && this.prompt.keyPressed(keyCode, scanCode, modifiers)) { + return true; } - if (keyCode == GLFW.GLFW_KEY_P && (modifiers & GLFW.GLFW_MOD_SHIFT) != 0) { + if (this.prompt == null && keyCode == GLFW.GLFW_KEY_P && (modifiers & GLFW.GLFW_MOD_SHIFT) != 0) { MinecraftClient.getInstance().setScreen(new VideoOptionsScreen(this.prevScreen, MinecraftClient.getInstance().options)); return true; @@ -411,6 +416,11 @@ public void close() { this.client.setScreen(this.prevScreen); } + @Override + public List children() { + return this.prompt == null ? super.children() : this.prompt.getWidgets(); + } + @Override public void setPrompt(@Nullable ScreenPrompt prompt) { this.prompt = prompt; diff --git a/src/main/java/me/jellysquid/mods/sodium/client/gui/prompt/ScreenPrompt.java b/src/main/java/me/jellysquid/mods/sodium/client/gui/prompt/ScreenPrompt.java index e00e8a073..c60a1aae7 100644 --- a/src/main/java/me/jellysquid/mods/sodium/client/gui/prompt/ScreenPrompt.java +++ b/src/main/java/me/jellysquid/mods/sodium/client/gui/prompt/ScreenPrompt.java @@ -34,6 +34,19 @@ public ScreenPrompt(ScreenPromptable parent, List text, int wid this.action = action; } + public void init() { + var parentDimensions = this.parent.getDimensions(); + + int boxX = (parentDimensions.width() / 2) - (width / 2); + int boxY = (parentDimensions.height() / 2) - (height / 2); + + this.closeButton = new FlatButtonWidget(new Dim2i((boxX + width) - 84, (boxY + height) - 24, 80, 20), Text.literal("Close"), this::close); + this.closeButton.setStyle(createButtonStyle()); + + this.actionButton = new FlatButtonWidget(new Dim2i((boxX + width) - 198, (boxY + height) - 24, 110, 20), this.action.label, this::runAction); + this.actionButton.setStyle(createButtonStyle()); + } + public void render(DrawContext drawContext, int mouseX, int mouseY, float delta) { var matrices = drawContext.getMatrices(); matrices.push(); @@ -74,12 +87,6 @@ public void render(DrawContext drawContext, int mouseX, int mouseY, float delta) textY += 8; } - this.closeButton = new FlatButtonWidget(new Dim2i((boxX + width) - 84, (boxY + height) - 24, 80, 20), Text.literal("Close"), this::close); - this.closeButton.setStyle(createButtonStyle()); - - this.actionButton = new FlatButtonWidget(new Dim2i((boxX + width) - 198, (boxY + height) - 24, 110, 20), this.action.label, this::runAction); - this.actionButton.setStyle(createButtonStyle()); - for (var button : getWidgets()) { button.render(drawContext, mouseX, mouseY, delta); } @@ -100,8 +107,8 @@ private static FlatButtonWidget.Style createButtonStyle() { } @NotNull - private List getWidgets() { - return List.of(this.closeButton, this.actionButton); + public List getWidgets() { + return List.of(this.actionButton, this.closeButton); } @Override @@ -128,6 +135,7 @@ public boolean mouseClicked(double mouseX, double mouseY, int button) { public boolean keyPressed(int keyCode, int scanCode, int modifiers) { if (keyCode == GLFW.GLFW_KEY_ESCAPE) { this.close(); + return true; } return Element.super.keyPressed(keyCode, scanCode, modifiers);