Skip to content

Commit

Permalink
[ui] fix ESC handling in BaseOwoScreen and BaseOwoHandledScreen
Browse files Browse the repository at this point in the history
  • Loading branch information
gliscowo committed Jan 15, 2024
1 parent 8322c21 commit 9ec6445
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 15 deletions.
21 changes: 18 additions & 3 deletions src/main/java/io/wispforest/owo/mixin/ui/HandledScreenMixin.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,29 @@
import io.wispforest.owo.ui.base.BaseOwoHandledScreen;
import io.wispforest.owo.util.pond.OwoSlotExtension;
import net.minecraft.client.gui.DrawContext;
import net.minecraft.client.gui.screen.Screen;
import net.minecraft.client.gui.screen.ingame.HandledScreen;
import net.minecraft.client.util.math.MatrixStack;
import net.minecraft.screen.slot.Slot;
import net.minecraft.text.Text;
import org.lwjgl.glfw.GLFW;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Unique;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.ModifyVariable;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;
import org.spongepowered.asm.mixin.injection.callback.LocalCapture;

@Mixin(HandledScreen.class)
public class HandledScreenMixin {
public abstract class HandledScreenMixin extends Screen {

@Unique
private static boolean owo$inOwoScreen = false;

protected HandledScreenMixin(Text title) {
super(title);
}

@SuppressWarnings("ConstantConditions")
@Inject(method = "render", at = @At("HEAD"))
private void captureOwoState(DrawContext context, int mouseX, int mouseY, float delta, CallbackInfo ci) {
Expand Down Expand Up @@ -73,4 +78,14 @@ private static void clearSlotDepth(DrawContext context, int x, int y, int z, Cal
private int doNoThrow(int slotId, @Local() Slot slot) {
return (((Object) this instanceof BaseOwoHandledScreen<?, ?>) && slot != null) ? slot.id : slotId;
}

@Inject(method = "keyPressed", at = @At(value = "INVOKE", target = "Lnet/minecraft/client/gui/screen/ingame/HandledScreen;handleHotbarKeyPressed(II)Z"), cancellable = true)
private void closeIt(int keyCode, int scanCode, int modifiers, CallbackInfoReturnable<Boolean> cir) {
if (!((Object) this instanceof BaseOwoHandledScreen<?, ?>)) return;

if (keyCode == GLFW.GLFW_KEY_ESCAPE && this.shouldCloseOnEsc()) {
this.close();
cir.setReturnValue(true);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -191,18 +191,13 @@ public void render(DrawContext vanillaContext, int mouseX, int mouseY, float del

@Override
public boolean keyPressed(int keyCode, int scanCode, int modifiers) {
if ((modifiers & GLFW.GLFW_MOD_CONTROL) == 0 && this.uiAdapter.rootComponent.focusHandler().focused() instanceof GreedyInputComponent inputComponent
? inputComponent.onKeyPress(keyCode, scanCode, modifiers)
: super.keyPressed(keyCode, scanCode, modifiers)) {
if ((modifiers & GLFW.GLFW_MOD_CONTROL) == 0
&& this.uiAdapter.rootComponent.focusHandler().focused() instanceof GreedyInputComponent inputComponent
&& inputComponent.onKeyPress(keyCode, scanCode, modifiers)) {
return true;
}

if (keyCode == GLFW.GLFW_KEY_ESCAPE && this.shouldCloseOnEsc()) {
this.close();
return true;
}

return false;
return super.keyPressed(keyCode, scanCode, modifiers);
}

@Override
Expand Down
10 changes: 7 additions & 3 deletions src/main/java/io/wispforest/owo/ui/base/BaseOwoScreen.java
Original file line number Diff line number Diff line change
Expand Up @@ -121,9 +121,13 @@ public void render(DrawContext context, int mouseX, int mouseY, float delta) {

@Override
public boolean keyPressed(int keyCode, int scanCode, int modifiers) {
if ((modifiers & GLFW.GLFW_MOD_CONTROL) == 0 && this.uiAdapter.rootComponent.focusHandler().focused() instanceof GreedyInputComponent inputComponent
? inputComponent.onKeyPress(keyCode, scanCode, modifiers)
: super.keyPressed(keyCode, scanCode, modifiers)) {
if ((modifiers & GLFW.GLFW_MOD_CONTROL) == 0
&& this.uiAdapter.rootComponent.focusHandler().focused() instanceof GreedyInputComponent inputComponent
&& inputComponent.onKeyPress(keyCode, scanCode, modifiers)) {
return true;
}

if (super.keyPressed(keyCode, scanCode, modifiers)) {
return true;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import net.minecraft.text.Text;
import net.minecraft.util.Identifier;
import org.jetbrains.annotations.NotNull;
import org.lwjgl.glfw.GLFW;

public class EpicHandledScreen extends BaseOwoHandledScreen<FlowLayout, EpicScreenHandler> {
private LabelComponent numberLabel;
Expand Down Expand Up @@ -85,6 +86,12 @@ public boolean mouseClicked(double mouseX, double mouseY, int button) {
if (Screen.hasAltDown() && this.focusedSlot != null) {
return false;
}

if (button == GLFW.GLFW_MOUSE_BUTTON_MIDDLE) {
this.uiAdapter.rootComponent.child(Containers.overlay(Components.label(Text.literal("a"))));
return true;
}

return super.mouseClicked(mouseX, mouseY, button);
}

Expand Down

0 comments on commit 9ec6445

Please sign in to comment.