-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
230 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,5 +7,9 @@ | |
], | ||
"injectors": { | ||
"defaultRequire": 1 | ||
} | ||
}, | ||
"client": [ | ||
"RenderSystemMixin", | ||
"WindowMixin" | ||
] | ||
} |