Skip to content

Commit

Permalink
Fixed several ImGui stuff, inputs are broken though
Browse files Browse the repository at this point in the history
  • Loading branch information
BluSpring committed Aug 20, 2024
1 parent 3e8b998 commit a8c2471
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 40 deletions.
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ dependencies {
"natives-linux" to false,
"natives-macos" to false
).forEach { (module) ->
val version = "1.87.0"
val version = "1.87.1"
api("io.github.spair:imgui-java-$module:$version") {
exclude(group = "org.lwjgl")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@

@FunctionalInterface
public interface KeyInputEvent {
Event<KeyInputEvent> EVENT = EventFactory.createArrayBacked(KeyInputEvent.class, (listeners) -> (key, action, mods) -> {
Event<KeyInputEvent> EVENT = EventFactory.createArrayBacked(KeyInputEvent.class, (listeners) -> (key, action, mods, scanCode) -> {
for (KeyInputEvent listener : listeners) {
listener.onKeyInput(key, action, mods);
listener.onKeyInput(key, action, mods, scanCode);
}
});

void onKeyInput(int key, InputAction action, int mods);
void onKeyInput(int key, InputAction action, int mods, int scanCode);
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ private void onKeyPress(long handle, int key, int scancode, int action, int mods
return;

InputAction inputAction = InputAction.from(action);
KeyInputEvent.EVENT.invoker().onKeyInput(key, inputAction, mods);
KeyInputEvent.EVENT.invoker().onKeyInput(key, inputAction, mods, scancode);
}


Expand Down
10 changes: 10 additions & 0 deletions src/main/java/gay/asoji/innerpastels/mixins/MouseHandlerMixin.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package gay.asoji.innerpastels.mixins;

import com.moulberry.mixinconstraints.annotations.IfDevEnvironment;
import gay.asoji.innerpastels.client.ImGuiClient;
import gay.asoji.innerpastels.client.imgui.InnerPastelsImGuiImpl;
import gay.asoji.innerpastels.events.InputAction;
import gay.asoji.innerpastels.events.MouseInputEvent;
Expand All @@ -22,6 +23,15 @@ public class MouseHandlerMixin {
@Final
private Minecraft minecraft;

@Inject(method = "onMove", at = @At("HEAD"))
private void onMove(long handle, double xpos, double ypos, CallbackInfo ci) {
if (handle != minecraft.getWindow().getWindow())
return;

// TODO: move to event
InnerPastelsImGuiImpl.INSTANCE.mouseMove(xpos, ypos);
}

@Inject(method = "onPress", at = @At("HEAD"))
private void onPress(long handle, int button, int action, int mods, CallbackInfo ci) {
if (handle != minecraft.getWindow().getWindow())
Expand Down
10 changes: 5 additions & 5 deletions src/main/kotlin/gay/asoji/innerpastels/client/ImGuiClient.kt
Original file line number Diff line number Diff line change
Expand Up @@ -76,19 +76,19 @@ object ImGuiClient {
endFrame()
}

KeyInputEvent.EVENT.register { key, action, mods ->
KeyInputEvent.EVENT.register { key, action, mods, scanCode ->
when (action ?: return@register) {
InputAction.PRESS -> InnerPastelsImGuiImpl.keyPress(key, mods)
InputAction.RELEASE -> InnerPastelsImGuiImpl.keyRelease(key, mods)
InputAction.PRESS -> InnerPastelsImGuiImpl.setKeyState(key, mods, scanCode, true)
InputAction.RELEASE -> InnerPastelsImGuiImpl.setKeyState(key, mods, scanCode, false)
}
}

MouseInputEvent.EVENT.register { button, action, mods ->
val mouseX = Minecraft.getInstance().mouseHandler.xpos()
val mouseY = Minecraft.getInstance().mouseHandler.ypos()
when (action ?: return@register) {
InputAction.PRESS -> InnerPastelsImGuiImpl.mouseClick(mouseX, mouseY, button)
InputAction.RELEASE -> InnerPastelsImGuiImpl.mouseRelease(mouseX, mouseY, button)
InputAction.PRESS -> InnerPastelsImGuiImpl.mouseClick(mouseX, mouseY, button, mods)
InputAction.RELEASE -> InnerPastelsImGuiImpl.mouseRelease(mouseX, mouseY, button, mods)
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,22 @@
package gay.asoji.innerpastels.client.imgui

import com.mojang.blaze3d.platform.GlDebug
import com.mojang.blaze3d.systems.RenderSystem
import imgui.ImFontConfig
import imgui.ImFontGlyphRangesBuilder
import imgui.ImGui
import imgui.ImGuiIO
import imgui.flag.ImGuiCol
import imgui.flag.ImGuiConfigFlags
import imgui.flag.ImGuiDockNodeFlags
import imgui.flag.ImGuiWindowFlags
import imgui.flag.*
import imgui.gl3.ImGuiImplGl3
import imgui.glfw.ImGuiImplGlfw
import imgui.internal.ImGuiContext
import net.fabricmc.api.EnvType
import net.fabricmc.api.Environment
import net.fabricmc.loader.api.FabricLoader
import net.minecraft.client.Minecraft
import org.lwjgl.glfw.GLFW
import org.lwjgl.opengl.GL11C
import org.lwjgl.opengl.GL32C


@Environment(EnvType.CLIENT)
Expand Down Expand Up @@ -56,23 +59,31 @@ object InnerPastelsImGuiImpl {
ImGui.setWindowFocus(null)
}

fun mouseClick(mouseX: Double, mouseY: Double, button: Int): Boolean {
if (!isInitialized || !ImGui.getIO().wantCaptureMouse) {
fun mouseMove(mouseX: Double, mouseY: Double) {
if (!isInitialized) {
return
}

imguiGlfw.cursorPosCallback(Minecraft.getInstance().window.window, mouseX, mouseY)
}

fun mouseClick(mouseX: Double, mouseY: Double, button: Int, mods: Int): Boolean {
if (!isInitialized) {
return false
}

ImGui.getIO().setMousePos(mouseX.toFloat(), mouseY.toFloat())
ImGui.getIO().setMouseDown(button, true)
imguiGlfw.cursorPosCallback(Minecraft.getInstance().window.window, mouseX, mouseY)
//imguiGlfw.mouseButtonCallback(Minecraft.getInstance().window.window, button, GLFW.GLFW_PRESS, mods)
return true
}

fun mouseRelease(mouseX: Double, mouseY: Double, button: Int): Boolean {
if (!isInitialized || !ImGui.getIO().wantCaptureMouse) {
fun mouseRelease(mouseX: Double, mouseY: Double, button: Int, mods: Int): Boolean {
if (!isInitialized) {
return false
}

ImGui.getIO().setMousePos(mouseX.toFloat(), mouseY.toFloat())
ImGui.getIO().setMouseDown(button, false)
imguiGlfw.cursorPosCallback(Minecraft.getInstance().window.window, mouseX, mouseY)
//imguiGlfw.mouseButtonCallback(Minecraft.getInstance().window.window, button, GLFW.GLFW_RELEASE, mods)
return true
}

Expand All @@ -86,29 +97,13 @@ object InnerPastelsImGuiImpl {
return true
}

fun keyPress(keyCode: Int, mods: Int): Boolean {
fun setKeyState(keyCode: Int, mods: Int, scanCode: Int, isDown: Boolean): Boolean {
if (!isInitialized || !ImGui.getIO().wantCaptureKeyboard) {
return false
}

ImGui.getIO().keyCtrl = mods and GLFW.GLFW_MOD_CONTROL != 0
ImGui.getIO().keyAlt = mods and GLFW.GLFW_MOD_ALT != 0
ImGui.getIO().keyShift = mods and GLFW.GLFW_MOD_SHIFT != 0
ImGui.getIO().keySuper = mods and GLFW.GLFW_MOD_SUPER != 0
ImGui.getIO().setKeysDown(keyCode, true)
return true
}

fun keyRelease(keyCode: Int, mods: Int): Boolean {
if (!isInitialized || !ImGui.getIO().wantCaptureKeyboard) {
return false
}
imguiGlfw.keyCallback(Minecraft.getInstance().window.window, keyCode, scanCode, if (isDown) GLFW.GLFW_PRESS else GLFW.GLFW_RELEASE, mods)

ImGui.getIO().keyCtrl = mods and GLFW.GLFW_MOD_CONTROL != 0
ImGui.getIO().keyAlt = mods and GLFW.GLFW_MOD_ALT != 0
ImGui.getIO().keyShift = mods and GLFW.GLFW_MOD_SHIFT != 0
ImGui.getIO().keySuper = mods and GLFW.GLFW_MOD_SUPER != 0
ImGui.getIO().setKeysDown(keyCode, false)
return true
}

Expand All @@ -124,6 +119,7 @@ object InnerPastelsImGuiImpl {

fun startFrame() {
imguiGlfw.newFrame()
imguiGl3.newFrame() // the best line
ImGui.newFrame()
}

Expand Down

0 comments on commit a8c2471

Please sign in to comment.