Skip to content

Commit

Permalink
feature: Pass keyboard events to dialogs (#12)
Browse files Browse the repository at this point in the history
  • Loading branch information
lucyydotp committed Oct 21, 2023
1 parent eebf567 commit bcf685d
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 6 deletions.
26 changes: 21 additions & 5 deletions api/src/main/java/com/noxcrew/sheeplib/mixin/ChatScreenMixin.java
Original file line number Diff line number Diff line change
Expand Up @@ -99,20 +99,36 @@ public boolean mouseDragged(double d, double e, int i, double f, double g) {
}

/**
* Fall back on the input element if the focused child does not handle charTyped.
* This is necesary as {@link ChatScreen} assumes that the input element is the only child.
* Attempt to pass char type events to {@link DialogContainer} first.
* This is necessary as {@link ChatScreen} assumes that the input element is the only child.
*/
@Override
public boolean charTyped(char c, int i) {
return super.charTyped(c, i) || input.charTyped(c, i);
return DialogContainer.INSTANCE.charTyped(c, i) || input.charTyped(c, i);
}

/**
* Pass key press events to {@link DialogContainer} first.
*/
@Inject(
method = "keyPressed",
at = @At("RETURN"),
at = @At("HEAD"),
cancellable = true
)
public void keyPressed(int i, int j, int k, CallbackInfoReturnable<Boolean> cir) {
cir.setReturnValue(cir.getReturnValueZ() || input.keyPressed(i, j, k));
if (DialogContainer.INSTANCE.keyPressed(i, j, k)) {
cir.setReturnValue(true);
}
}

/**
* Clear the dialog container's focus when the screen is closed.
*/
@Inject(
method = "removed",
at = @At("TAIL")
)
public void removed(CallbackInfo ci) {
DialogContainer.INSTANCE.setFocused(null);
}
}
7 changes: 7 additions & 0 deletions api/src/main/kotlin/com/noxcrew/sheeplib/DialogContainer.kt
Original file line number Diff line number Diff line change
Expand Up @@ -96,11 +96,18 @@ public object DialogContainer : Renderable, ContainerEventHandler, NarratableEnt
* @throws IllegalArgumentException if [dialog] is both not null and not in this container
*/
override fun setFocused(dialog: GuiEventListener?) {

if (dialog == null) {
focused?.focused = null
}

if (dialog !is Dialog) return
require(dialog in children.value) {
"New focused element is not in the container"
}
focused = dialog
logger.info("Focused $dialog")
minecraft.screen?.focused = this
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
@file:Suppress("MagicNumber")

package com.noxcrew.sheeplib.theme

import com.noxcrew.sheeplib.dialog.title.TextTitleWidget
Expand Down Expand Up @@ -26,7 +28,6 @@ private val colors = ColorsImpl(
)

/** The default theme. */
@Suppress("MagicNumber")
public val DefaultTheme: ThemeImpl = ThemeImpl(
Theme.Dimensions(
buttonWidth = 70,
Expand Down

0 comments on commit bcf685d

Please sign in to comment.