Skip to content

Commit

Permalink
Add imgui, keyboard mixin
Browse files Browse the repository at this point in the history
  • Loading branch information
zeroeightysix committed Dec 19, 2019
1 parent 52da86e commit a3ed70a
Show file tree
Hide file tree
Showing 5 changed files with 107 additions and 16 deletions.
17 changes: 12 additions & 5 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
10 changes: 5 additions & 5 deletions src/main/java/net/fabricmc/example/ExampleMod.java
Original file line number Diff line number Diff line change
Expand Up @@ -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!");
}

}
62 changes: 62 additions & 0 deletions src/main/java/net/fabricmc/example/ImguiScreen.java
Original file line number Diff line number Diff line change
@@ -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());

}

}
33 changes: 28 additions & 5 deletions src/main/java/net/fabricmc/example/mixin/ExampleMixin.java
Original file line number Diff line number Diff line change
@@ -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();
}
}
}
}

}
1 change: 0 additions & 1 deletion src/main/resources/fabric.mod.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@

"depends": {
"fabricloader": ">=0.7.2",
"fabric": "*",
"minecraft": "1.15.x"
},
"suggests": {
Expand Down

0 comments on commit a3ed70a

Please sign in to comment.