Skip to content

Commit

Permalink
added example usage for custom Fonts
Browse files Browse the repository at this point in the history
  • Loading branch information
FlorianMichael committed May 8, 2023
1 parent 5220a1b commit 55f4312
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 9 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ ImGuiImpl.draw(io -> {
ImGui.showDemoWindow();
});
```
Keep in mind that ImGui needs to be initalized, so you must not remove the MinecraftClient mixin in the ImGui package
Keep in mind that ImGui needs to be initialized, so you must not remove the MinecraftClient mixin in the ImGui package
**The ImGuiImpl also contains a basic font loading base**

## License

Expand Down
39 changes: 31 additions & 8 deletions src/main/java/de/florianmichael/imguiexample/imgui/ImGuiImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@
*/
package de.florianmichael.imguiexample.imgui;

import imgui.ImGui;
import imgui.ImGuiIO;
import imgui.*;
import imgui.extension.implot.ImPlot;
import imgui.flag.ImGuiConfigFlags;
import imgui.gl3.ImGuiImplGl3;
Expand All @@ -23,17 +22,41 @@ public static void create(final long handle) {
data.setIniFilename("modid.ini");
data.setFontGlobalScale(1F);

imGuiImplGlfw.init(handle, true);
// If you want to have custom fonts, you can use the following code here

// {
// final ImFontAtlas fonts = data.getFonts();
// final ImFontGlyphRangesBuilder rangesBuilder = new ImFontGlyphRangesBuilder();
//
// rangesBuilder.addRanges(data.getFonts().getGlyphRangesDefault());
// rangesBuilder.addRanges(data.getFonts().getGlyphRangesCyrillic());
// rangesBuilder.addRanges(data.getFonts().getGlyphRangesJapanese());
//
// final short[] glyphRanges = rangesBuilder.buildRanges();
//
// final ImFontConfig basicConfig = new ImFontConfig();
// basicConfig.setGlyphRanges(data.getFonts().getGlyphRangesCyrillic());
//
// final List<ImFont> generatedFonts = new ArrayList<>();
// for (int i = 5 /* MINIMUM_FONT_SIZE */; i < 50 /* MAXIMUM_FONT_SIZE */; i++) {
// basicConfig.setName("<Font Name> " + i + "px");
// generatedFonts.add(fonts.addFontFromMemoryTTF(IOUtils.toByteArray(Objects.requireNonNull(ImGuiImpl.class.getResourceAsStream("<File Path>"))), i, basicConfig, glyphRanges));
// }
// fonts.build();
// basicConfig.destroy();
// }

data.setNavActive(false);
data.setNavVisible(false);
data.setKeyCtrl(false);
// The "generatedFonts" list now contains an ImFont for each scale from 5 to 50, you should save the font scales you want as global fields here to use them later:
// For example:
// defaultFont = generatedFonts.get(30); // Font scale is 30
// How you can apply the font then, you can see in ExampleMixin

data.setConfigFlags(ImGuiConfigFlags.NavNoCaptureKeyboard | ImGuiConfigFlags.DockingEnable);
imGuiImplGlfw.init(handle, true);

data.setConfigFlags(ImGuiConfigFlags.DockingEnable);
imGuiImplGl3.init();
}


public static void draw(final RenderInterface runnable) {
imGuiImplGlfw.newFrame(); // Handle keyboard and mouse interactions
ImGui.newFrame();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,14 @@ private void init(CallbackInfo info) {
@Inject(method = "render", at = @At("RETURN"))
private void render(MatrixStack matrices, int mouseX, int mouseY, float delta, CallbackInfo ci) {
ImGuiImpl.draw(io -> {
// Example on how to use a custom Font
// ImGui.pushFont(ImGuiImpl.defaultFont);
if (ImGui.begin("Hello World")) {
ImGui.end();
}

ImGui.showDemoWindow();
// ImGui.popFont();
});
}
}

0 comments on commit 55f4312

Please sign in to comment.