Skip to content

Commit

Permalink
⚗️ testing jank dearimgui lib port setup
Browse files Browse the repository at this point in the history
  • Loading branch information
asoji committed Jul 1, 2024
1 parent a32f35d commit aa608c5
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 82 deletions.
14 changes: 1 addition & 13 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}

Expand Down
6 changes: 6 additions & 0 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand All @@ -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"}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -42,8 +44,8 @@ class InnerPastelsClient : ClientModInitializer {
if (client.player != null && client.screen == null) {
isImGuiRenderEnabled = !isImGuiRenderEnabled

implGl3.shutdown()
implGlfw.shutdown()
implGl3.dispose()
implGlfw.dispose()
}
}
}
Expand All @@ -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<DrawData>(ImGui.drawData))
endFrame(windowHandle)
}

KeyInputEvent.EVENT.register { key, scancode, action, mods ->
Expand Down
Original file line number Diff line number Diff line change
@@ -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
}
}

0 comments on commit aa608c5

Please sign in to comment.