forked from FabricMC/fabric-example-mod
-
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
1 parent
52da86e
commit a3ed70a
Showing
5 changed files
with
107 additions
and
16 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,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
33
src/main/java/net/fabricmc/example/mixin/ExampleMixin.java
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 |
---|---|---|
@@ -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(); | ||
} | ||
} | ||
} | ||
} | ||
|
||
} |
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 |
---|---|---|
|
@@ -28,7 +28,6 @@ | |
|
||
"depends": { | ||
"fabricloader": ">=0.7.2", | ||
"fabric": "*", | ||
"minecraft": "1.15.x" | ||
}, | ||
"suggests": { | ||
|