Skip to content

Commit

Permalink
1.1.0: the ImGui update
Browse files Browse the repository at this point in the history
  • Loading branch information
NotNite committed Jul 12, 2023
1 parent dc4e6fe commit 7e944b1
Show file tree
Hide file tree
Showing 9 changed files with 230 additions and 7 deletions.
26 changes: 23 additions & 3 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,24 @@ plugins {
id("fabric-loom") version "1.2-SNAPSHOT"
id("org.jetbrains.kotlin.jvm") version "1.8.22"
id("maven-publish")
id("com.github.johnrengelman.shadow") version "8.1.1"
}

group = property("maven_group")!!
version = property("mod_version")!!

dependencies {
minecraft("com.mojang:minecraft:${property("minecraft_version")}")
mappings("net.fabricmc:yarn:${property("yarn_mappings")}:v2")
modImplementation("net.fabricmc:fabric-loader:${property("loader_version")}")

modImplementation("net.fabricmc.fabric-api:fabric-api:${property("fabric_version")}")
modImplementation("net.fabricmc:fabric-language-kotlin:${property("fabric_kotlin_version")}")

val imguiVersion = property("imgui_version")!!

implementation(shadow("io.github.spair:imgui-java-binding:$imguiVersion")!!)
implementation(shadow("io.github.spair:imgui-java-lwjgl3:$imguiVersion")!!)
implementation(shadow("io.github.spair:imgui-java-natives-windows:$imguiVersion")!!)
implementation(shadow("io.github.spair:imgui-java-natives-linux:$imguiVersion")!!)
implementation(shadow("io.github.spair:imgui-java-natives-macos:$imguiVersion")!!)
}

loom {
Expand All @@ -36,6 +42,20 @@ tasks {
}
}

shadowJar {
configurations = listOf(project.configurations.shadow.get())
dependencies {
exclude(dependency("org.lwjgl:lwjgl"))
exclude(dependency("org.lwjgl:lwjgl-glfw"))
exclude(dependency("org.lwjgl:lwjgl-opengl"))
}
}

remapJar {
dependsOn(shadowJar)
mustRunAfter(shadowJar)
}

compileKotlin {
kotlinOptions.jvmTarget = "17"
}
Expand Down
5 changes: 3 additions & 2 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ yarn_mappings=1.20+build.1
loader_version=0.14.21
fabric_kotlin_version=1.9.5+kotlin.1.8.22

mod_version=1.0.6
maven_group=pm.n2
version=1.1.0
group=pm.n2
archives_base_name=hajlib

fabric_version=0.83.0+1.20
imgui_version=1.86.10
19 changes: 19 additions & 0 deletions src/main/java/pm/n2/hajlib/mixin/RenderSystemMixin.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package pm.n2.hajlib.mixin;

import com.mojang.blaze3d.systems.RenderSystem;
import net.minecraft.client.MinecraftClient;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
import pm.n2.hajlib.imgui.ImGuiManager;

@Mixin(RenderSystem.class)
public class RenderSystemMixin {
@Inject(at = @At("HEAD"), method = "flipFrame")
private static void flipFrame(long window, CallbackInfo ci) {
MinecraftClient.getInstance().getProfiler().push("ImGui");
ImGuiManager.INSTANCE.onFrameRender$hajlib();
MinecraftClient.getInstance().getProfiler().pop();
}
}
25 changes: 25 additions & 0 deletions src/main/java/pm/n2/hajlib/mixin/WindowMixin.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package pm.n2.hajlib.mixin;

import net.minecraft.client.WindowEventHandler;
import net.minecraft.client.WindowSettings;
import net.minecraft.client.util.MonitorTracker;
import net.minecraft.client.util.Window;
import org.spongepowered.asm.mixin.Final;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
import pm.n2.hajlib.imgui.ImGuiManager;

@Mixin(Window.class)
public class WindowMixin {
@Final
@Shadow
private long handle;

@Inject(at = @At("TAIL"), method = "<init>")
private void init(WindowEventHandler eventHandler, MonitorTracker monitorTracker, WindowSettings settings, String videoMode, String title, CallbackInfo ci) {
ImGuiManager.INSTANCE.onGLFWInit$hajlib(handle);
}
}
2 changes: 1 addition & 1 deletion src/main/kotlin/pm/n2/hajlib/event/EventManager.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import kotlin.reflect.jvm.javaMethod
import kotlin.reflect.jvm.javaType

typealias UnregisterFunc = () -> Unit
typealias EventHandlerFunc<T> = (obj: T, unregister: UnregisterFunc) -> Any
typealias EventHandlerFunc<T> = (obj: T, unregister: UnregisterFunc) -> Any?
typealias EventTarget = Pair<Any?, Method>

/**
Expand Down
5 changes: 5 additions & 0 deletions src/main/kotlin/pm/n2/hajlib/imgui/ImGuiEvent.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package pm.n2.hajlib.imgui

sealed class ImGuiEvent {
object Draw : ImGuiEvent()
}
100 changes: 100 additions & 0 deletions src/main/kotlin/pm/n2/hajlib/imgui/ImGuiManager.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
package pm.n2.hajlib.imgui

import imgui.ImFontConfig
import imgui.ImGui
import imgui.flag.ImGuiConfigFlags
import imgui.flag.ImGuiKey
import imgui.gl3.ImGuiImplGl3
import imgui.glfw.ImGuiImplGlfw
import net.fabricmc.loader.api.FabricLoader
import org.lwjgl.glfw.GLFW
import pm.n2.hajlib.event.EventManager
import kotlin.properties.Delegates

/**
* A wrapper around ImGui, ported from [imgui-quilt](https://git.gaycatgirl.sex/evie/imgui-quilt).
* Subscribe to the Tick event via the [EventManager] to draw your UI.
* @see ImGuiScreen
*/
object ImGuiManager {
val eventManager = EventManager()

private val imguiGLFW = ImGuiImplGlfw()
private val imguiGL3 = ImGuiImplGl3()
private var windowHandle by Delegates.notNull<Long>()

internal fun onGLFWInit(handle: Long) {
ImGui.createContext()
val io = ImGui.getIO()

io.iniFilename = FabricLoader.getInstance()
.configDir
.resolve("imgui.ini")
.toString()
io.addConfigFlags(
ImGuiConfigFlags.NavEnableKeyboard
or ImGuiConfigFlags.DockingEnable
or ImGuiConfigFlags.ViewportsEnable
)

val fontAtlas = io.fonts
val fontConfig = ImFontConfig()
fontAtlas.addFontDefault()

fontConfig.mergeMode = true
fontConfig.pixelSnapH = true
fontConfig.destroy()

// APPLE MOMENT
val isTimCooksBitch = System.getProperty("os.name").lowercase().contains("mac")
imguiGL3.init(if (isTimCooksBitch) "#version 140" else "#version 130")
imguiGLFW.init(handle, true)

windowHandle = handle
}

private fun fixInputs() {
val io = ImGui.getIO()

// Something is trying to overwrite this?
io.setKeyMap(ImGuiKey.Tab, GLFW.GLFW_KEY_TAB)
io.setKeyMap(ImGuiKey.LeftArrow, GLFW.GLFW_KEY_LEFT)
io.setKeyMap(ImGuiKey.RightArrow, GLFW.GLFW_KEY_RIGHT)
io.setKeyMap(ImGuiKey.UpArrow, GLFW.GLFW_KEY_UP)
io.setKeyMap(ImGuiKey.DownArrow, GLFW.GLFW_KEY_DOWN)
io.setKeyMap(ImGuiKey.PageUp, GLFW.GLFW_KEY_PAGE_UP)
io.setKeyMap(ImGuiKey.PageDown, GLFW.GLFW_KEY_PAGE_DOWN)
io.setKeyMap(ImGuiKey.Home, GLFW.GLFW_KEY_HOME)
io.setKeyMap(ImGuiKey.End, GLFW.GLFW_KEY_END)
io.setKeyMap(ImGuiKey.Insert, GLFW.GLFW_KEY_INSERT)
io.setKeyMap(ImGuiKey.Delete, GLFW.GLFW_KEY_DELETE)
io.setKeyMap(ImGuiKey.Backspace, GLFW.GLFW_KEY_BACKSPACE)
io.setKeyMap(ImGuiKey.Space, GLFW.GLFW_KEY_SPACE)
io.setKeyMap(ImGuiKey.Enter, GLFW.GLFW_KEY_ENTER)
io.setKeyMap(ImGuiKey.Escape, GLFW.GLFW_KEY_ESCAPE)
io.setKeyMap(ImGuiKey.KeyPadEnter, GLFW.GLFW_KEY_KP_ENTER)
io.setKeyMap(ImGuiKey.A, GLFW.GLFW_KEY_A)
io.setKeyMap(ImGuiKey.C, GLFW.GLFW_KEY_C)
io.setKeyMap(ImGuiKey.V, GLFW.GLFW_KEY_V)
io.setKeyMap(ImGuiKey.X, GLFW.GLFW_KEY_X)
io.setKeyMap(ImGuiKey.Y, GLFW.GLFW_KEY_Y)
io.setKeyMap(ImGuiKey.Z, GLFW.GLFW_KEY_Z)
}

internal fun onFrameRender() {
imguiGLFW.newFrame()
ImGui.newFrame()

eventManager.dispatch(ImGuiEvent.Draw)
fixInputs()

ImGui.render()

imguiGL3.renderDrawData(ImGui.getDrawData())

val backupWindowPtr = GLFW.glfwGetCurrentContext()
ImGui.updatePlatformWindows()
ImGui.renderPlatformWindowsDefault()
GLFW.glfwMakeContextCurrent(backupWindowPtr)
}
}
49 changes: 49 additions & 0 deletions src/main/kotlin/pm/n2/hajlib/imgui/ImGuiScreen.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package pm.n2.hajlib.imgui

import imgui.ImGui
import net.minecraft.client.gui.screen.Screen
import net.minecraft.text.Text
import pm.n2.hajlib.event.UnregisterFunc

/**
* A screen that draws ImGui.
* Extend this class and insert your ImGui code into the [drawImGui] function,
* and it will be drawn on the screen.
*/
open class ImGuiScreen(title: Text) : Screen(title) {
var initialized: Boolean = false // init() gets called on resize, too

private val drawLambda = { _: ImGuiEvent.Draw, _: UnregisterFunc ->
this.drawImGui()
}

open fun drawImGui() {}

override fun init() {
if (!initialized) {
ImGuiManager.eventManager.registerFunc(ImGuiEvent.Draw::class, drawLambda)

val io = ImGui.getIO()
io.wantCaptureKeyboard = true
io.wantCaptureMouse = true
io.wantTextInput = true

initialized = true
}
}

override fun removed() {
if (initialized) {
ImGuiManager.eventManager.unregisterFunc(ImGuiEvent.Draw::class, drawLambda)

val io = ImGui.getIO()
io.wantCaptureKeyboard = false
io.wantCaptureMouse = false
io.wantTextInput = false

initialized = false
}

super.removed()
}
}
6 changes: 5 additions & 1 deletion src/main/resources/hajlib.mixins.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,9 @@
],
"injectors": {
"defaultRequire": 1
}
},
"client": [
"RenderSystemMixin",
"WindowMixin"
]
}

0 comments on commit 7e944b1

Please sign in to comment.