gdxapp4d/core/src/love/distributedrebirth/demo4d/ImGuiSetup.java

66 lines
1.9 KiB
Java
Raw Normal View History

2022-01-28 11:00:59 +00:00
package love.distributedrebirth.demo4d;
import com.badlogic.gdx.Gdx;
import imgui.ImFontConfig;
import imgui.ImFontGlyphRangesBuilder;
import imgui.ImGui;
import imgui.ImGuiIO;
import imgui.gl3.ImGuiImplGl3;
import imgui.glfw.ImGuiImplGlfw;
/**
2022-01-31 12:40:01 +00:00
* Create and shutdown of ImGui and font activations.
*
* @author willemtsade ©Δ 仙上主天
*/
2022-01-28 11:00:59 +00:00
public class ImGuiSetup {
public static final ImGuiImplGlfw imGuiImp = new ImGuiImplGlfw();
public static final ImGuiImplGl3 imGuiGlImp = new ImGuiImplGl3();
public static void init() {
long windowHandle = -1;
try {
Object window = Gdx.graphics.getClass().getMethod("getWindow").invoke(Gdx.graphics);
windowHandle = (Long)window.getClass().getMethod("getWindowHandle").invoke(window);
} catch (Exception e) {
throw new RuntimeException(e);
}
ImGui.createContext();
initFonts(ImGui.getIO());
imGuiImp.init(windowHandle, true);
imGuiGlImp.init("#version 150");
ImGui.init();
ImGui.styleColorsLight();
}
private static void initFonts(final ImGuiIO io) {
2022-01-31 23:26:21 +00:00
io.getFonts().addFontDefault();
ImFontConfig fontConfig = new ImFontConfig();
fontConfig.setMergeMode(true);
2022-02-01 13:53:02 +00:00
ImFontGlyphRangesBuilder fontBuilder = new ImFontGlyphRangesBuilder();
addRangeUnicodePlane0(fontBuilder);
final short[] glyphRanges = fontBuilder.buildRanges();
2022-01-28 11:00:59 +00:00
io.getFonts().addFontFromMemoryTTF(Gdx.files.internal("font/NotoSansCJKjp-Medium.otf").readBytes(), 14, fontConfig, glyphRanges);
io.getFonts().addFontFromMemoryTTF(Gdx.files.internal("font/FreeSans.ttf").readBytes(), 14, fontConfig, glyphRanges);
fontConfig.destroy();
}
2022-02-01 13:53:02 +00:00
private static void addRangeUnicodePlane0(ImFontGlyphRangesBuilder fontBuilder) {
for (int c=0x0100;c<=0xFFEF;c++) {
StringBuilder buf = new StringBuilder();
buf.append(""+(char)c);
fontBuilder.addText(buf.toString());
}
}
2022-01-28 11:00:59 +00:00
public static void dispose() {
imGuiImp.dispose();
imGuiGlImp.dispose();
ImGui.destroyContext();
}
}