From aa608c5155254f532a6f81ba411c9cc48402bae8 Mon Sep 17 00:00:00 2001 From: Asoji Date: Mon, 1 Jul 2024 10:22:39 -0700 Subject: [PATCH] =?UTF-8?q?=E2=9A=97=EF=B8=8F=20testing=20jank=20dearimgui?= =?UTF-8?q?=20lib=20port=20setup?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- build.gradle.kts | 14 +- gradle/libs.versions.toml | 6 + .../innerpastels/client/InnerPastelsClient.kt | 15 +- .../client/imgui/InnerPastelsImGuiImpl.kt | 148 ++++++++++-------- 4 files changed, 101 insertions(+), 82 deletions(-) diff --git a/build.gradle.kts b/build.gradle.kts index ac2bf66a..ceca8a20 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -46,19 +46,7 @@ dependencies { modImplementation(libs.fabric.loader) modImplementation(libs.fabric.api) - implementation("kotlin.graphics:imgui-core:1.89.7-1") { - exclude(group = "org.lwjgl") - } - implementation("kotlin.graphics:imgui-gl:1.89.7-1") { - exclude(group = "org.lwjgl") - } - implementation("kotlin.graphics:imgui-glfw:1.89.7-1") { - exclude(group = "org.lwjgl") - } - implementation("kotlin.graphics:uno-core:0.7.21") { - exclude(group = "org.lwjgl") - } - implementation("kotlin.graphics:glm:0.9.9.1-build-11") { + implementation(libs.bundles.imgui) { exclude(group = "org.lwjgl") } diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index 4d7cf898..ac16089a 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -4,6 +4,7 @@ minecraft = "1.21" fabric-loader = "0.15.11" fabric-api = "0.100.4+1.21" fabric_language_kotlin = "1.11.0+kotlin.2.0.0" +imgui-java = "1.86.11" dokka = "1.9.10" @@ -12,6 +13,11 @@ minecraft = { module = "com.mojang:minecraft", version.ref = "minecraft" } fabric_loader = { module = "net.fabricmc:fabric-loader", version.ref = "fabric-loader" } fabric-api = { module = "net.fabricmc.fabric-api:fabric-api", version.ref = "fabric-api" } fabric_language_kotlin = { module = "net.fabricmc:fabric-language-kotlin", version.ref = "fabric_language_kotlin" } +imgui-binding = { module = "io.github.spair:imgui-java-binding", version.ref = "imgui-java" } +imgui-lwjgl3 = { module = "io.github.spair:imgui-java-lwjgl3", version.ref = "imgui-java" } + +[bundles] +imgui = [ "imgui-binding", "imgui-lwjgl3" ] [plugins] grgit = { id = "org.ajoberstar.grgit", version = "5.2.1"} diff --git a/src/main/kotlin/gay/asoji/innerpastels/client/InnerPastelsClient.kt b/src/main/kotlin/gay/asoji/innerpastels/client/InnerPastelsClient.kt index a1ea6536..dd7aebdd 100644 --- a/src/main/kotlin/gay/asoji/innerpastels/client/InnerPastelsClient.kt +++ b/src/main/kotlin/gay/asoji/innerpastels/client/InnerPastelsClient.kt @@ -6,12 +6,14 @@ import gay.asoji.innerpastels.client.imgui.InnerPastelsImGuiImpl import gay.asoji.innerpastels.client.imgui.InnerPastelsImGuiImpl.implGl3 import gay.asoji.innerpastels.client.imgui.InnerPastelsImGuiImpl.implGlfw import gay.asoji.innerpastels.client.imgui.ImGuiPanel +import gay.asoji.innerpastels.client.imgui.InnerPastelsImGuiImpl.endFrame +import gay.asoji.innerpastels.client.imgui.InnerPastelsImGuiImpl.imGui +import gay.asoji.innerpastels.client.imgui.InnerPastelsImGuiImpl.windowHandle import gay.asoji.innerpastels.events.InputAction import gay.asoji.innerpastels.events.KeyInputEvent import gay.asoji.innerpastels.events.MouseInputEvent import gay.asoji.innerpastels.events.MouseScrollInputEvent import imgui.ImGui -import imgui.internal.DrawData import net.fabricmc.api.ClientModInitializer import net.fabricmc.fabric.api.client.event.lifecycle.v1.ClientTickEvents import net.fabricmc.fabric.api.client.keybinding.v1.KeyBindingHelper @@ -42,8 +44,8 @@ class InnerPastelsClient : ClientModInitializer { if (client.player != null && client.screen == null) { isImGuiRenderEnabled = !isImGuiRenderEnabled - implGl3.shutdown() - implGlfw.shutdown() + implGl3.dispose() + implGlfw.dispose() } } } @@ -57,16 +59,15 @@ class InnerPastelsClient : ClientModInitializer { if (!isImGuiRenderEnabled) { return@register } - implGl3.newFrame() + implGl3.renderDrawData(ImGui.getDrawData()) implGlfw.newFrame() - InnerPastelsImGuiImpl.imgui.newFrame() + ImGui.newFrame() panels.forEach { it.render(booleanArrayOf(true)) } - InnerPastelsImGuiImpl.imgui.render() - implGl3.renderDrawData(Objects.requireNonNull(ImGui.drawData)) + endFrame(windowHandle) } KeyInputEvent.EVENT.register { key, scancode, action, mods -> diff --git a/src/main/kotlin/gay/asoji/innerpastels/client/imgui/InnerPastelsImGuiImpl.kt b/src/main/kotlin/gay/asoji/innerpastels/client/imgui/InnerPastelsImGuiImpl.kt index 938fa3e7..99546afc 100644 --- a/src/main/kotlin/gay/asoji/innerpastels/client/imgui/InnerPastelsImGuiImpl.kt +++ b/src/main/kotlin/gay/asoji/innerpastels/client/imgui/InnerPastelsImGuiImpl.kt @@ -1,110 +1,134 @@ package gay.asoji.innerpastels.client.imgui -import gln.cap.Caps +import imgui.ImFontAtlas +import imgui.ImFontConfig import imgui.ImGui -import imgui.MINECRAFT_BEHAVIORS -import imgui.MouseButton -import imgui.classes.Context -import imgui.impl.gl.ImplGL3 -import imgui.impl.glfw.ImplGlfw -import imgui.impl.glfw.ImplGlfw.Companion.imguiKey +import imgui.flag.ImGuiConfigFlags +import imgui.gl3.ImGuiImplGl3 +import imgui.glfw.ImGuiImplGlfw import net.fabricmc.api.EnvType import net.fabricmc.api.Environment import net.minecraft.client.Minecraft import org.lwjgl.glfw.GLFW -import uno.gl.GlWindow -import uno.glfw.GlfwWindow +import org.lwjgl.glfw.GLFW.glfwGetCurrentContext +import org.lwjgl.glfw.GLFW.glfwMakeContextCurrent @Environment(EnvType.CLIENT) object InnerPastelsImGuiImpl { - val imgui = ImGui - var implGl3: ImplGL3 - var implGlfw: ImplGlfw + var implGl3: ImGuiImplGl3 = ImGuiImplGl3() + var implGlfw: ImGuiImplGlfw = ImGuiImplGlfw() + var imGui: ImGui = ImGui() + var imGuiIO = ImGui.getIO() + var windowHandle: Long = Minecraft.getInstance().window.window + + // GLFW Initialization for imgui. + fun glfwInit(handle: Long) { + implGlfw.init(handle, false) + implGl3.init() + ImGui.createContext() + + windowHandle = handle + } + + fun imGuiInit() { + imGuiIO.iniFilename = null + imGuiIO.addConfigFlags(ImGuiConfigFlags.NavEnableKeyboard) + imGuiIO.addConfigFlags(ImGuiConfigFlags.DockingEnable) + imGuiIO.addConfigFlags(ImGuiConfigFlags.ViewportsEnable) + + val fontAtlas: ImFontAtlas = imGuiIO.getFonts() + val fontConfig = ImFontConfig() + + fontConfig.glyphRanges = fontAtlas.glyphRangesCyrillic + + fontAtlas.addFontDefault() + + fontConfig.mergeMode = true + fontConfig.pixelSnapH = true - // Initialization for imgui. - init { - MINECRAFT_BEHAVIORS = true + fontConfig.destroy() - val glfwWindow = GlfwWindow(Minecraft.getInstance().window.window) - val window = GlWindow(glfwWindow, Caps.Profile.CORE, true) + } - window.makeCurrent(true) - Context().setCurrent() + fun endFrame(windowPtr: Long) { + implGl3.renderDrawData(ImGui.getDrawData()) - implGlfw = ImplGlfw(window, false, null) - implGl3 = ImplGL3() + if (ImGui.getIO().hasConfigFlags(ImGuiConfigFlags.ViewportsEnable)) { + val backupWindowPtr = glfwGetCurrentContext() + ImGui.updatePlatformWindows() + ImGui.renderPlatformWindowsDefault() + glfwMakeContextCurrent(backupWindowPtr) + } } fun mouseReleased(mouseX: Double, mouseY: Double, button: Int): Boolean { - if (imgui.io.wantCaptureMouse) { - imgui.io.addMouseButtonEvent(MouseButton.of(button), false) + if (imGuiIO.getMouseDown(button)) { + imGuiIO.setMouseDown(booleanArrayOf(true)) } - return imgui.io.wantCaptureMouse + return imGuiIO.wantCaptureMouse } fun mouseScrolled(mouseX: Double, mouseY: Double, scrollX: Double, scrollY: Double): Boolean { - if (imgui.io.wantCaptureMouse) { - imgui.io.mouseWheelH = scrollX.toFloat() - imgui.io.mouseWheel = scrollY.toFloat() + if (imGuiIO.wantCaptureMouse) { + imGuiIO.mouseWheelH = scrollX.toFloat() + imGuiIO.mouseWheel = scrollY.toFloat() } - return imgui.io.wantCaptureMouse + return imGuiIO.wantCaptureMouse } fun mouseClicked(mouseX: Double, mouseY: Double, button: Int): Boolean { - if (imgui.io.wantCaptureMouse) { - imgui.io.addMouseButtonEvent(MouseButton.of(button), true) + if (imGuiIO.wantCaptureMouse) { + if (imGuiIO.getMouseDown(button)) { + imGuiIO.setMouseDown(booleanArrayOf(false)) + } } - return imgui.io.wantCaptureMouse + return imGuiIO.wantCaptureMouse } fun keyPressed(keyCode: Int, scanCode: Int, modifiers: Int): Boolean { - if (imgui.io.wantCaptureKeyboard) { - val key = uno.glfw.Key.of(keyCode).imguiKey - - imgui.io.keyCtrl = modifiers and GLFW.GLFW_MOD_CONTROL != 0 - imgui.io.keyShift = modifiers and GLFW.GLFW_MOD_SHIFT != 0 - imgui.io.keyAlt = modifiers and GLFW.GLFW_MOD_ALT != 0 - imgui.io.keySuper = modifiers and GLFW.GLFW_MOD_SUPER != 0 - - imgui.io.addKeyEvent(key, true) - imgui.io.keysData[key.index].down = true - imgui.io.setKeyEventNativeData(key, key.i, scanCode) + if (imGuiIO.wantCaptureKeyboard) { + imGuiIO.keyCtrl = modifiers and GLFW.GLFW_MOD_CONTROL != 0 + imGuiIO.keyShift = modifiers and GLFW.GLFW_MOD_SHIFT != 0 + imGuiIO.keyAlt = modifiers and GLFW.GLFW_MOD_ALT != 0 + imGuiIO.keySuper = modifiers and GLFW.GLFW_MOD_SUPER != 0 + + if (imGuiIO.getKeysDown(keyCode)) { + imGuiIO.setKeysDown(booleanArrayOf(false)) + } } - return imgui.io.wantCaptureKeyboard + return imGuiIO.wantCaptureKeyboard } fun keyReleased(keyCode: Int, scanCode: Int, modifiers: Int): Boolean { - if (imgui.io.wantCaptureKeyboard) { - val key = uno.glfw.Key.of(keyCode).imguiKey - - imgui.io.keyCtrl = modifiers and GLFW.GLFW_MOD_CONTROL != 0 - imgui.io.keyShift = modifiers and GLFW.GLFW_MOD_SHIFT != 0 - imgui.io.keyAlt = modifiers and GLFW.GLFW_MOD_ALT != 0 - imgui.io.keySuper = modifiers and GLFW.GLFW_MOD_SUPER != 0 - - imgui.io.addKeyEvent(key, false) - imgui.io.keysData[key.index].down = false - imgui.io.setKeyEventNativeData(key, key.i, scanCode) + if (imGuiIO.wantCaptureKeyboard) { + imGuiIO.keyCtrl = modifiers and GLFW.GLFW_MOD_CONTROL != 0 + imGuiIO.keyShift = modifiers and GLFW.GLFW_MOD_SHIFT != 0 + imGuiIO.keyAlt = modifiers and GLFW.GLFW_MOD_ALT != 0 + imGuiIO.keySuper = modifiers and GLFW.GLFW_MOD_SUPER != 0 + + if (imGuiIO.getKeysDown(keyCode)) { + imGuiIO.setKeysDown(booleanArrayOf(false)) + } } - return imgui.io.wantCaptureKeyboard + return imGuiIO.wantCaptureKeyboard } fun charTyped(codePoint: Char, modifiers: Int): Boolean { - if (imgui.io.wantCaptureKeyboard) { - imgui.io.keyCtrl = modifiers and GLFW.GLFW_MOD_CONTROL != 0 - imgui.io.keyShift = modifiers and GLFW.GLFW_MOD_SHIFT != 0 - imgui.io.keyAlt = modifiers and GLFW.GLFW_MOD_ALT != 0 - imgui.io.keySuper = modifiers and GLFW.GLFW_MOD_SUPER != 0 + if (imGuiIO.wantCaptureKeyboard) { + imGuiIO.keyCtrl = modifiers and GLFW.GLFW_MOD_CONTROL != 0 + imGuiIO.keyShift = modifiers and GLFW.GLFW_MOD_SHIFT != 0 + imGuiIO.keyAlt = modifiers and GLFW.GLFW_MOD_ALT != 0 + imGuiIO.keySuper = modifiers and GLFW.GLFW_MOD_SUPER != 0 - imgui.io.addInputCharacter(codePoint) + imGuiIO.addInputCharacter(codePoint.code) } - return imgui.io.wantCaptureKeyboard + return imGuiIO.wantCaptureKeyboard } } \ No newline at end of file