From a3ed70a59f496c453968e7ac441d9ff63f7f1ae9 Mon Sep 17 00:00:00 2001 From: Ridan Vandenbergh Date: Thu, 19 Dec 2019 16:56:29 +0100 Subject: [PATCH] Add imgui, keyboard mixin https://github.com/kotlin-graphics/imgui/issues/114 --- build.gradle | 17 +++-- .../java/net/fabricmc/example/ExampleMod.java | 10 +-- .../net/fabricmc/example/ImguiScreen.java | 62 +++++++++++++++++++ .../fabricmc/example/mixin/ExampleMixin.java | 33 ++++++++-- src/main/resources/fabric.mod.json | 1 - 5 files changed, 107 insertions(+), 16 deletions(-) create mode 100644 src/main/java/net/fabricmc/example/ImguiScreen.java diff --git a/build.gradle b/build.gradle index a3c9ddc77..8100054d2 100644 --- a/build.gradle +++ b/build.gradle @@ -13,17 +13,24 @@ group = project.maven_group minecraft { } +repositories { + mavenCentral() + maven { + name = "jitpack.io" + url = "https://jitpack.io" + } +} + dependencies { //to change the versions see the gradle.properties file minecraft "com.mojang:minecraft:${project.minecraft_version}" mappings "net.fabricmc:yarn:${project.yarn_mappings}:v2" modCompile "net.fabricmc:fabric-loader:${project.loader_version}" - // Fabric API. This is technically optional, but you probably want it anyway. - modCompile "net.fabricmc.fabric-api:fabric-api:${project.fabric_version}" - - // PSA: Some older mods, compiled on Loom 0.2.1, might have outdated Maven POMs. - // You may need to force-disable transitiveness on them. + compile "com.github.kotlin-graphics:imgui:-SNAPSHOT" + compile 'com.github.kotlin-graphics:uno-sdk:f528113bf45e43406953d6881915467d85a20881' + compile 'com.github.kotlin-graphics.glm:glm:1b4ac18dd1a3c23440d3f33596688aac60bc0141' + compile group: 'org.jetbrains.kotlin', name: 'kotlin-stdlib', version: '1.3.61' } processResources { diff --git a/src/main/java/net/fabricmc/example/ExampleMod.java b/src/main/java/net/fabricmc/example/ExampleMod.java index e5ed082e5..bd96c7fb6 100644 --- a/src/main/java/net/fabricmc/example/ExampleMod.java +++ b/src/main/java/net/fabricmc/example/ExampleMod.java @@ -3,12 +3,12 @@ import net.fabricmc.api.ModInitializer; public class ExampleMod implements ModInitializer { + + public static ImguiScreen imguiScreen; + @Override public void onInitialize() { - // This code runs as soon as Minecraft is in a mod-load-ready state. - // However, some things (like resources) may still be uninitialized. - // Proceed with mild caution. - - System.out.println("Hello Fabric world!"); + System.out.println("Hello imgui!"); } + } diff --git a/src/main/java/net/fabricmc/example/ImguiScreen.java b/src/main/java/net/fabricmc/example/ImguiScreen.java new file mode 100644 index 000000000..ad6bdfccc --- /dev/null +++ b/src/main/java/net/fabricmc/example/ImguiScreen.java @@ -0,0 +1,62 @@ +package net.fabricmc.example; + +import glm_.vec2.Vec2; +import imgui.ImGui; +import imgui.ImguiKt; +import imgui.classes.Context; +import imgui.impl.gl.ImplGL3; +import imgui.impl.glfw.ImplGlfw; +import net.minecraft.client.MinecraftClient; +import net.minecraft.client.gui.screen.Screen; +import net.minecraft.text.LiteralText; +import uno.glfw.GlfwWindow; + +public class ImguiScreen extends Screen { + + private ImGui imgui = ImGui.INSTANCE; + + private ImplGL3 implGl3; + private ImplGlfw implGlfw; + + private boolean[] showDemoWindow = new boolean[] { false }; + + public ImguiScreen() { + super(new LiteralText("ImGUI Minecraft Screen")); + setup(); + } + + private void setup() { + ImguiKt.MINECRAFT_BEHAVIORS = true; + GlfwWindow window = GlfwWindow.from(MinecraftClient.getInstance().getWindow().getHandle()); + window.makeContextCurrent(); + new Context(); + implGlfw = new ImplGlfw(window, false, null); + implGl3 = new ImplGL3(); + } + + public void reload() { + implGl3 = new ImplGL3(); + } + + @Override + public void render(int x, int y, float partialTicks) { + + implGl3.newFrame(); + implGlfw.newFrame(); + imgui.newFrame(); + + imgui.text("Hello world!"); + if (imgui.button("Open demo window", new Vec2())) { + showDemoWindow[0] = true; + } + + if (showDemoWindow[0]) { + imgui.showDemoWindow(showDemoWindow); + } + + imgui.render(); + implGl3.renderDrawData(imgui.getDrawData()); + + } + +} diff --git a/src/main/java/net/fabricmc/example/mixin/ExampleMixin.java b/src/main/java/net/fabricmc/example/mixin/ExampleMixin.java index 83ee1a89d..d51d9ac06 100644 --- a/src/main/java/net/fabricmc/example/mixin/ExampleMixin.java +++ b/src/main/java/net/fabricmc/example/mixin/ExampleMixin.java @@ -1,15 +1,38 @@ package net.fabricmc.example.mixin; +import net.fabricmc.example.ExampleMod; +import net.fabricmc.example.ImguiScreen; +import net.minecraft.client.Keyboard; +import net.minecraft.client.MinecraftClient; import net.minecraft.client.gui.screen.TitleScreen; 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; -@Mixin(TitleScreen.class) + +@Mixin(Keyboard.class) public class ExampleMixin { - @Inject(at = @At("HEAD"), method = "init()V") - private void init(CallbackInfo info) { - System.out.println("This line is printed by an example mod mixin!"); + + /* + This code is injected into minecraft code during runtime. + this method will be called when any key is pressed. + + You cannot hot-reload any code from this method. + */ + @Inject(method = "onKey", at = @At(value = "RETURN", ordinal = 4), require = 1) + public void onKey(long window, int key, int scancode, int i, int j, CallbackInfo info) { + if (MinecraftClient.getInstance().player == null) return; // Don't do anything if not ingame + if (key == 89 && scancode == 29 && i != 0) { // the 'y' key. i == 0 if the key was released. + if (ExampleMod.imguiScreen == null) { + ExampleMod.imguiScreen = new ImguiScreen(); + } + MinecraftClient.getInstance().openScreen(ExampleMod.imguiScreen); + } else if (key == 82 && scancode == 27 && i != 0) { + if (ExampleMod.imguiScreen != null) { + ExampleMod.imguiScreen.reload(); + } + } } -} + +} \ No newline at end of file diff --git a/src/main/resources/fabric.mod.json b/src/main/resources/fabric.mod.json index 50ad0fe51..9a69274f5 100644 --- a/src/main/resources/fabric.mod.json +++ b/src/main/resources/fabric.mod.json @@ -28,7 +28,6 @@ "depends": { "fabricloader": ">=0.7.2", - "fabric": "*", "minecraft": "1.15.x" }, "suggests": {