Skip to content

Commit

Permalink
Fix screen prompts not being keyboard-navigable (#2297)
Browse files Browse the repository at this point in the history
  • Loading branch information
haykam821 authored Feb 1, 2024
1 parent 6463f77 commit af09e34
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -139,6 +140,10 @@ protected void init() {
super.init();

this.rebuildGUI();

if (this.prompt != null) {
this.prompt.init();
}
}

private void rebuildGUI() {
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -411,6 +416,11 @@ public void close() {
this.client.setScreen(this.prevScreen);
}

@Override
public List<? extends Element> children() {
return this.prompt == null ? super.children() : this.prompt.getWidgets();
}

@Override
public void setPrompt(@Nullable ScreenPrompt prompt) {
this.prompt = prompt;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,19 @@ public ScreenPrompt(ScreenPromptable parent, List<StringVisitable> 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();
Expand Down Expand Up @@ -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);
}
Expand All @@ -100,8 +107,8 @@ private static FlatButtonWidget.Style createButtonStyle() {
}

@NotNull
private List<AbstractWidget> getWidgets() {
return List.of(this.closeButton, this.actionButton);
public List<AbstractWidget> getWidgets() {
return List.of(this.actionButton, this.closeButton);
}

@Override
Expand All @@ -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);
Expand Down

0 comments on commit af09e34

Please sign in to comment.