Skip to content

Commit

Permalink
Added Mouse Click Mouse Release and Keyboard Click to UIHandler
Browse files Browse the repository at this point in the history
  • Loading branch information
odtheking committed Nov 9, 2024
1 parent 5145f89 commit 3057ee9
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 37 deletions.
8 changes: 8 additions & 0 deletions odin/src/main/java/me/odin/mixin/mixins/MixinGuiScreen.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,14 @@ private void onMouseInput(CallbackInfo ci){
}
}

@Inject(method = "handleMouseInput", at = @At(value = "INVOKE", target = "net/minecraft/client/gui/GuiScreen.mouseReleased(III)V"), cancellable = true)
private void onReleaseMouseInput(CallbackInfo ci){
if (!Mouse.getEventButtonState()) {
if (postAndCatch(new GuiEvent.GuiMouseReleaseEvent(odin$gui, Mouse.getEventButton(), Mouse.getEventX(), Mouse.getEventY())))
ci.cancel();
}
}

@Inject(method = "handleKeyboardInput", at = @At(value = "INVOKE", target = "Lnet/minecraft/client/gui/GuiScreen;keyTyped(CI)V"), cancellable = true)
private void onHandleKeyboardInput(CallbackInfo ci) {
if (Keyboard.getEventKeyState()) {
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,14 @@ private void onMouseInput(CallbackInfo ci){
}
}

@Inject(method = "handleMouseInput", at = @At(value = "INVOKE", target = "net/minecraft/client/gui/GuiScreen.mouseReleased(III)V"), cancellable = true)
private void onReleaseMouseInput(CallbackInfo ci){
if (!Mouse.getEventButtonState()) {
if (postAndCatch(new GuiEvent.GuiMouseReleaseEvent(odin$gui, Mouse.getEventButton(), Mouse.getEventX(), Mouse.getEventY())))
ci.cancel();
}
}

@Inject(method = "handleKeyboardInput", at = @At(value = "INVOKE", target = "Lnet/minecraft/client/gui/GuiScreen;keyTyped(CI)V"), cancellable = true)
private void onHandleKeyboardInput(CallbackInfo ci) {
if (postAndCatch(new GuiEvent.GuiKeyPressEvent(odin$gui, Keyboard.getEventKey(), Keyboard.getEventCharacter())))
Expand Down
3 changes: 3 additions & 0 deletions src/main/kotlin/me/odinmain/events/impl/GuiEvents.kt
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ abstract class GuiEvent : Event() {
@Cancelable
data class GuiMouseClickEvent(val gui: GuiScreen, val button: Int, val x: Int, val y: Int) : GuiEvent()

@Cancelable
data class GuiMouseReleaseEvent(val gui: GuiScreen, val button: Int, val x: Int, val y: Int) : GuiEvent()

@Cancelable
data class GuiKeyPressEvent(val gui: GuiScreen, val keyCode: Int, val char: Char) : Event()

Expand Down
24 changes: 0 additions & 24 deletions src/main/kotlin/me/odinmain/features/Module.kt
Original file line number Diff line number Diff line change
Expand Up @@ -148,30 +148,6 @@ abstract class Module(
ModuleManager.messageFunctions.add(ModuleManager.MessageFunction(filter, shouldRun, func))
}

/**
* Runs the given function when a Chat Packet is sent with the same message as the given text (or contains the given text) (Case Sensitive!)
*
* @param text The text to look for.
* @param contains If the function should run when the message only contains the text but does not necessarily equal it.
* @param shouldRun Boolean getter to decide if the function should run at any given time, could check if the option is enabled for instance.
* @param func The function to run if the message matches or contains the given text and shouldRun returns true.
*
* @author Bonsai
*/
fun onMessage(text: String, contains: Boolean, shouldRun: () -> Boolean = { alwaysActive || enabled }, func: (String) -> Unit) {
val regex =
if (contains)
".*${Regex.escape(text)}.*".toRegex()
else
Regex.escape(text).toRegex()

ModuleManager.messageFunctions.add(ModuleManager.MessageFunction(regex, shouldRun, func))
}

// fun onMessageCancellable(filter: Regex, shouldRun: () -> Boolean = { alwaysActive || enabled }, func: (ChatPacketEvent) -> Unit) {
// ModuleManager.cancellableMessageFunctions.add(ModuleManager.MessageFunctionCancellable(filter, shouldRun, func))
// }

fun onWorldLoad(func: () -> Unit) {
ModuleManager.worldLoadFunctions.add(func)
}
Expand Down
16 changes: 15 additions & 1 deletion src/main/kotlin/me/odinmain/utils/ui/UIHandler.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package me.odinmain.utils.ui
import com.github.stivais.ui.UI
import com.github.stivais.ui.Window
import me.odinmain.OdinMain.mc
import me.odinmain.events.impl.GuiEvent
import net.minecraftforge.client.event.RenderWorldLastEvent
import net.minecraftforge.common.MinecraftForge
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent
Expand Down Expand Up @@ -51,5 +52,18 @@ class UIHandler(private val ui: UI, private val onlyRender: Boolean = false) : W
ui.render()
}

// todo: add mouse click and other stuff
@SubscribeEvent
fun onMouseClick(event: GuiEvent.GuiMouseClickEvent) {
ui.eventManager.onMouseClick(event.button)
}

@SubscribeEvent
fun onMouseReleased(event: GuiEvent.GuiMouseReleaseEvent) {
ui.eventManager.onMouseRelease(event.button)
}

@SubscribeEvent
fun onKeyboardClick(event: GuiEvent.GuiKeyPressEvent) {
ui.eventManager.onKeyType(event.char)
}
}

0 comments on commit 3057ee9

Please sign in to comment.