Skip to content

Commit a24ab63

Browse files
committed
Merge branch 'main' of github.com:IrisShaders/imgui-mc into multiloader
# Conflicts: # src/main/java/net/irisshaders/imgui/mixin/ScreenMixin.java
2 parents f077171 + f22156e commit a24ab63

File tree

7 files changed

+108
-25
lines changed

7 files changed

+108
-25
lines changed

.github/workflows/build.yml

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,7 @@ jobs:
1212
matrix:
1313
# Use these Java versions
1414
java: [21]
15-
# and run on both Linux and Windows
16-
os: [ubuntu-22.04, windows-2022]
17-
runs-on: ${{ matrix.os }}
15+
runs-on: ubuntu-22.04
1816
steps:
1917
- name: checkout repository
2018
uses: actions/checkout@v4
@@ -26,12 +24,11 @@ jobs:
2624
java-version: ${{ matrix.java }}
2725
distribution: 'microsoft'
2826
- name: make gradle wrapper executable
29-
if: ${{ runner.os != 'Windows' }}
3027
run: chmod +x ./gradlew
3128
- name: build
3229
run: ./gradlew chiseledBuild
3330
- name: capture build artifacts
34-
if: ${{ runner.os == 'Linux' && matrix.java == '21' }} # Only upload artifacts built from latest java on one OS
31+
if: ${{ matrix.java == '21' }} # Only upload artifacts built from latest java on one OS
3532
uses: actions/upload-artifact@v4
3633
with:
3734
name: Artifacts

src/main/java/net/irisshaders/imgui/ImGuiMC.java

Lines changed: 66 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,33 @@ public class ImGuiMC {
2525
private static final ImGuiMC INSTANCE = new ImGuiMC();
2626
private static final boolean DEBUG = false;
2727
private ImGuiContext context;
28+
private ImGuiImplGl3 glAccessor;
29+
private ImGuiImplGlfw glWindow;
30+
private boolean drawing;
2831

2932
public static ImGuiMC getInstance() {
3033
return INSTANCE;
3134
}
3235

33-
private ImGuiImplGl3 glAccessor;
34-
private ImGuiImplGlfw glWindow;
36+
/**
37+
* Force sets the context. Usually, you want {@link #startDrawing()} instead.
38+
*/
39+
public static void setContext() {
40+
ImGui.setCurrentContext(getInstance().context);
41+
}
42+
43+
/**
44+
* If ready to draw, sets the current context.
45+
*
46+
* @return If drawing is allowed currently
47+
*/
48+
public static boolean startDrawing() {
49+
if (getInstance().context == null) return false;
50+
51+
ImGui.setCurrentContext(getInstance().context);
52+
53+
return getInstance().drawing;
54+
}
3555

3656
private String tryLoadFromClasspath(final String fullLibName) {
3757
if (DEBUG) {
@@ -96,14 +116,19 @@ public void onRendererInit(long window) {
96116
this.glWindow = new ImGuiImplGlfw();
97117
this.glAccessor.init();
98118
this.glWindow.init(window, false);
99-
100-
afterPollEvents(window);
101119
}
102120

103121
public void afterPollEvents(long l) {
122+
if (drawing) {
123+
// The last frame never correctly finished. The level may have changed.
124+
return;
125+
}
126+
104127
glAccessor.newFrame();
105128
glWindow.newFrame();
106129
ImGui.newFrame();
130+
131+
drawing = true;
107132
}
108133

109134
public void draw() {
@@ -113,48 +138,84 @@ public void draw() {
113138
/*Minecraft.getInstance().getMainRenderTarget().bindWrite(true);
114139
*///?}
115140
ImGui.render();
141+
drawing = false;
142+
116143
glAccessor.renderDrawData(ImGui.getDrawData());
117144

118145
ImGui.updatePlatformWindows();
119146
ImGui.renderPlatformWindowsDefault();
120147
GLFW.glfwMakeContextCurrent(Minecraft.getInstance().getWindow().getWindow());
121148
}
122149

150+
private static boolean isGone;
151+
123152
public void shutdown() {
153+
if (isGone) {
154+
throw new IllegalStateException("Cannot shutdown twice!");
155+
}
156+
isGone = true;
157+
setContext();
124158
glAccessor.shutdown();
125159
glWindow.shutdown();
126160
ImGui.destroyContext();
161+
context = null;
127162
}
128163

129164
public void onMouseMove(long window, double mouseX, double mouseY) {
165+
if (isGone) return;
166+
167+
setContext();
130168
glWindow.cursorPosCallback(window, mouseX, mouseY);
131169
}
132170

133171
public void onMouseScroll(long window, double scrollX, double scrollY) {
172+
if (isGone) return;
173+
174+
setContext();
134175
glWindow.scrollCallback(window, scrollX, scrollY);
135176
}
136177

137178
public void onMouseButton(long window, int button, int action, int mods) {
179+
if (isGone) return;
180+
181+
setContext();
138182
glWindow.mouseButtonCallback(window, button, action, mods);
139183
}
140184

141185
public void monitorCallback(long window, int event) {
186+
if (isGone) return;
187+
188+
setContext();
142189
glWindow.monitorCallback(window, event);
143190
}
144191

145192
public void cursorEnterCallback(long window, boolean entered) {
193+
if (isGone) return;
194+
195+
setContext();
146196
glWindow.cursorEnterCallback(window, entered);
147197
}
148198

149199
public void windowFocusCallback(long window, boolean focused) {
200+
if (isGone) return;
201+
202+
setContext();
150203
glWindow.windowFocusCallback(window, focused);
151204
}
152205

153206
public void onKeyPress(long window, int keycode, int scancode, int action, int mods) {
207+
if (isGone) return;
208+
209+
setContext();
154210
glWindow.keyCallback(window, keycode, scancode, action, mods);
155211
}
156212

213+
public void recreateFonts() {
214+
glAccessor.destroyFontsTexture();
215+
glAccessor.createFontsTexture();
216+
}
217+
157218
public void onCharTyped(long window, int chara, int j) {
158219
glWindow.charCallback(window, chara);
159220
}
160-
}
221+
}
Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,37 @@
11
package net.irisshaders.imgui.mixin;
22

3+
import com.mojang.blaze3d.platform.Window;
34
import imgui.ImGui;
45
import net.irisshaders.imgui.ImGuiMC;
56
import net.minecraft.client.Minecraft;
7+
import org.spongepowered.asm.mixin.Final;
68
import org.spongepowered.asm.mixin.Mixin;
9+
import org.spongepowered.asm.mixin.Shadow;
710
import org.spongepowered.asm.mixin.injection.At;
811
import org.spongepowered.asm.mixin.injection.Inject;
912
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
1013

1114
@Mixin(Minecraft.class)
1215
public class DrawMixin {
13-
@Inject(method = "runTick", at = @At(value = "INVOKE", target = /*? if >= 1.21 {*/"Lnet/minecraft/client/renderer/GameRenderer;render(Lnet/minecraft/client/DeltaTracker;Z)V"/*?} else {*//*"Lnet/minecraft/client/renderer/GameRenderer;render(FJZ)V"*//*?}*/, shift = At.Shift.AFTER))
16+
@Shadow @Final private Window window;
17+
18+
@Inject(at = @At("HEAD"), method = "runTick", remap = false)
19+
private void imgui$newFrame(
20+
boolean bl, CallbackInfo ci
21+
) {
22+
ImGuiMC.getInstance().afterPollEvents(this.window.getWindow());
23+
}
24+
25+
@Inject(method = "runTick", at = @At(value = "INVOKE", target = /*? if >= 1.21.5 {*/"Lcom/mojang/blaze3d/platform/Window;isMinimized()Z"/*?} else {*//*"Lcom/mojang/blaze3d/pipeline/RenderTarget;unbindWrite()V"*//*?}*/))
1426
private void imgui$draw(boolean bl, CallbackInfo ci) {
15-
ImGui.showDemoWindow();
16-
ImGuiMC.getInstance().draw();
27+
if (ImGuiMC.startDrawing()) {
28+
ImGui.showDemoWindow();
29+
ImGuiMC.getInstance().draw();
30+
}
31+
}
32+
33+
@Inject(method = "stop", at = @At("HEAD"))
34+
private void imgui$shutdown(CallbackInfo ci) {
35+
ImGuiMC.getInstance().shutdown();
1736
}
1837
}

src/main/java/net/irisshaders/imgui/mixin/RendererInitMixin.java

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -29,15 +29,4 @@ public class RendererInitMixin {
2929
ImGuiMC.getInstance().onRendererInit(Minecraft.getInstance().getWindow().getWindow());
3030
}
3131
*///?}
32-
33-
@Inject(at = @At("RETURN"), method = "flipFrame", remap = false)
34-
private static void imgui$newFrame(
35-
long l,
36-
//? if >= 1.21.5
37-
TracyFrameCapture tracyFrameCapture,
38-
CallbackInfo ci
39-
) {
40-
ImGuiMC.getInstance().afterPollEvents(l);
41-
}
42-
4332
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package net.irisshaders.imgui.mixin;
2+
3+
import com.mojang.blaze3d.platform.ScreenManager;
4+
import net.irisshaders.imgui.ImGuiMC;
5+
import org.spongepowered.asm.mixin.Mixin;
6+
import org.spongepowered.asm.mixin.injection.At;
7+
import org.spongepowered.asm.mixin.injection.Inject;
8+
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
9+
10+
@Mixin(ScreenManager.class)
11+
public class ScreenMixin {
12+
@Inject(method = "onMonitorChange", at = @At("HEAD"))
13+
private void imgui$onMonitorChange(long window, int event, CallbackInfo ci) {
14+
ImGuiMC.getInstance().monitorCallback(window, event);
15+
}
16+
}

src/main/java/net/irisshaders/imgui/window/ImGuiImplGlfw.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -884,7 +884,7 @@ public void shutdown() {
884884
}
885885

886886
for (int cursorN = 0; cursorN < ImGuiMouseCursor.COUNT; cursorN++) {
887-
glfwDestroyCursor(data.mouseCursors[cursorN]);
887+
if (data.mouseCursors[cursorN] != 0) glfwDestroyCursor(data.mouseCursors[cursorN]);
888888
}
889889

890890
io.setBackendPlatformName(null);

src/main/resources/imgui-for-mc.client.mixins.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
"MouseHandlingMixin",
88
"DrawMixin",
99
"KeyboardHandlingMixin",
10+
"ScreenMixin",
1011
"WindowMixin"
1112
],
1213
"injectors": {

0 commit comments

Comments
 (0)