@@ -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+ }
0 commit comments