Skip to content

Commit

Permalink
Fixed crash when opening in-game text editor
Browse files Browse the repository at this point in the history
  • Loading branch information
Ocelot5836 committed Nov 12, 2024
1 parent 8147db3 commit c990f38
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 6 deletions.
11 changes: 11 additions & 0 deletions common/src/main/java/foundry/veil/Veil.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,17 @@ public static void init() {
VeilMolang.set(MolangCompiler.create(MolangCompiler.DEFAULT_FLAGS, Veil.class.getClassLoader()));
}

/**
* Runs the specified code with the correct ImGui context.
*
* @param task The ImGui task to run
*/
public static void withImGui(Runnable task) {
beginImGui();
task.run();
endImGui();
}

/**
* <p>Enables writing ImGui to the screen. This useful for debugging during the normal render loop.</p>
* <p>Be sure to call {@link #endImGui()} when done.</p>
Expand Down
17 changes: 11 additions & 6 deletions common/src/main/java/foundry/veil/api/client/imgui/CodeEditor.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package foundry.veil.api.client.imgui;

import foundry.veil.Veil;
import imgui.ImGui;
import imgui.ImVec2;
import imgui.extension.texteditor.TextEditor;
Expand Down Expand Up @@ -59,7 +60,7 @@ public void save() {
if (errors.isEmpty()) {
this.oldSource = this.editor.getText();
}
this.editor.setErrorMarkers(errors);
Veil.withImGui(() -> this.editor.setErrorMarkers(errors));
}

/**
Expand All @@ -73,8 +74,10 @@ public void show(@Nullable String fileName, String source) {
this.oldSource = this.editor.getText();
this.editor.setErrorMarkers(Collections.emptyMap());
this.open.set(true);
ImGui.setWindowFocus("###editor");
ImGui.setWindowCollapsed("###editor", false);
Veil.withImGui(() -> {
ImGui.setWindowFocus("###editor");
ImGui.setWindowCollapsed("###editor", false);
});
}

/**
Expand All @@ -83,9 +86,11 @@ public void show(@Nullable String fileName, String source) {
public void hide() {
if (this.hasTextChanged()) {
this.open.set(true);
ImGui.pushID(this.hashCode());
ImGui.openPopup("###save_confirm");
ImGui.popID();
Veil.withImGui(() -> {
ImGui.pushID(this.hashCode());
ImGui.openPopup("###save_confirm");
ImGui.popID();
});
} else {
this.oldSource = null;
this.open.set(false);
Expand Down

0 comments on commit c990f38

Please sign in to comment.