Added ImGui setup and renamed main.

This commit is contained in:
Willem Cazander 2022-01-28 12:00:59 +01:00
parent 85f0a34e20
commit 8b1ac0a7d1
7 changed files with 283 additions and 44 deletions

View file

@ -0,0 +1,183 @@
package love.distributedrebirth.demo4d;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.badlogic.gdx.Game;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.Input.Keys;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.utils.ScreenUtils;
import imgui.ImGui;
import imgui.type.ImBoolean;
import love.distributedrebirth.demo4d.music.MusicManager;
import net.spookygames.gdx.nativefilechooser.NativeFileChooser;
public class Demo4DMain extends Game {
private List<String> args;
public NativeFileChooser fileChooser;
public SpriteBatch batch;
public BitmapFont font;
public OrthographicCamera camera;
public final int viewWidth = 800;
public final int viewHeight = 600;
public MusicManager music;
private Map<Class<? extends Screen>,Screen> screens;
private ImBoolean showImGuiDemo = new ImBoolean(false);
private ImBoolean showMusicPlayer = new ImBoolean(false);
private ImBoolean showHebrewWallet = new ImBoolean(false);
private MainScreenRender render;
public Demo4DMain(List<String> args, NativeFileChooser fileChooser) {
this.args = args;
this.fileChooser = fileChooser;
}
public void create() {
batch = new SpriteBatch();
font = new BitmapFont();
camera = new OrthographicCamera();
camera.setToOrtho(false, viewWidth, viewHeight);
camera.update();
batch.setProjectionMatrix(camera.combined);
music = new MusicManager();
music.init();
render = new MainScreenRender(this);
ImGuiSetup.init();
screens = new HashMap<>();
putScreen(new ScreenIntro(this));
putScreen(new ScreenIntroMission(this));
putScreen(new ScreenDefault(this));
putScreen(new ScreenCredits(this));
putScreen(new ScreenHelp(this));
putScreen(new ScreenMatrix4D(this));
putScreen(new ScreenUnicode4D(this));
if (args.contains("default")) {
selectScreen(ScreenDefault.class);
} else {
selectScreen(ScreenIntro.class);
}
}
@Override
public void dispose() {
ImGuiSetup.dispose();
for (Screen screen:screens.values()) {
screen.dispose();
}
music.dispose();
batch.dispose();
font.dispose();
}
private void putScreen(Screen screen) {
screens.put(screen.getClass(), screen);
}
public void selectScreen(Class<? extends Screen> screenClass) {
Screen screen = screens.get(screenClass);
if (screen == null) {
throw new NullPointerException("Unknow screen: "+screenClass);
}
setScreen(screen);
}
@Override
public void render() {
ScreenUtils.clear(0f, 0f, 0f, 1f, true);
ImGuiSetup.imGuiImp.newFrame();
ImGui.newFrame();
if (hasMainMenu()) {
renderMenu();
}
if (showImGuiDemo.get()) {
ImGui.showDemoWindow(showImGuiDemo);
}
if (showMusicPlayer.get()) {
//render.renderMusicPlayer(showMusicPlayer);
}
if (showHebrewWallet.get()) {
render.renderHebrewWallet(showHebrewWallet);
}
if (screen != null) {
screen.render(Gdx.graphics.getDeltaTime());
}
if (Gdx.input.isKeyPressed(Keys.ESCAPE)) {
selectScreen(ScreenDefault.class);
}
ImGui.render();
ImGuiSetup.imGuiGlImp.renderDrawData(ImGui.getDrawData());
}
private void renderMenu() {
ImGui.beginMainMenuBar();
if (ImGui.beginMenu("Demo")) {
if (ImGui.menuItem("Matrix4D")) {
selectScreen(ScreenMatrix4D.class);
}
if (ImGui.menuItem("Unicode4D")) {
selectScreen(ScreenUnicode4D.class);
}
if (ImGui.menuItem("Hebrew Wallet")) {
showHebrewWallet.set(true);
}
ImGui.separator();
if (ImGui.menuItem("ImGui")) {
showImGuiDemo.set(true);
}
ImGui.separator();
if (ImGui.menuItem("Stop Music")) {
music.stop();
}
if (ImGui.menuItem("Music Player")) {
showMusicPlayer.set(true);
}
ImGui.separator();
if (ImGui.menuItem("Exit")) {
dispose();
System.exit(0);
}
ImGui.endMenu();
}
if (ImGui.beginMenu("Help")) {
if (ImGui.menuItem("Credits")) {
selectScreen(ScreenCredits.class);
}
ImGui.separator();
if (ImGui.menuItem("Help")) {
selectScreen(ScreenHelp.class);
}
ImGui.endMenu();
}
ImGui.endMainMenuBar();
}
private boolean hasMainMenu() {
Screen screen = getScreen();
if (screen == null) {
return false;
}
if (ScreenIntro.class.equals(screen.getClass())) {
return false;
}
if (ScreenIntroMission.class.equals(screen.getClass())) {
return false;
}
if (ScreenCredits.class.equals(screen.getClass())) {
return false;
}
return true;
}
}

View file

@ -0,0 +1,11 @@
package love.distributedrebirth.demo4d;
import com.badlogic.gdx.ScreenAdapter;
public class Demo4DMainAdapter extends ScreenAdapter {
protected final Demo4DMain main;
public Demo4DMainAdapter(Demo4DMain main) {
this.main = main;
}
}

View file

@ -0,0 +1,8 @@
package love.distributedrebirth.demo4d;
import imgui.type.ImBoolean;
public interface ImGuiRenderer {
void render(ImBoolean widgetOpen);
}

View file

@ -0,0 +1,10 @@
package love.distributedrebirth.demo4d;
abstract public class ImGuiRendererMain implements ImGuiRenderer {
protected final Demo4DMain main;
public ImGuiRendererMain(Demo4DMain main) {
this.main = main;
}
}

View file

@ -0,0 +1,69 @@
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;
public class ImGuiSetup {
public static final ImGuiImplGlfw imGuiImp = new ImGuiImplGlfw();
public static final ImGuiImplGl3 imGuiGlImp = new ImGuiImplGl3();
private static final short[] glyphRangesHebrew = new short[]{0x0590, 0x05F4};
private static final short[] glyphRangesArabic = new short[]{0x0600, 0x06FF};
private static final short[] glyphRangesSubSuper0 = new short[]{0x00B2, 0x00B9};
private static final short[] glyphRangesSubSuper1 = new short[]{0x2070, 0x209F};
private static final short[] glyphRangesToneLetters0 = new short[]{0x02E5, 0x02E9};
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) {
io.getFonts().addFontDefault(); // Add default font for latin glyphs
final ImFontGlyphRangesBuilder rangesBuilder = new ImFontGlyphRangesBuilder(); // Glyphs ranges provide
rangesBuilder.addRanges(io.getFonts().getGlyphRangesDefault());
rangesBuilder.addRanges(io.getFonts().getGlyphRangesCyrillic());
rangesBuilder.addRanges(io.getFonts().getGlyphRangesJapanese());
rangesBuilder.addRanges(io.getFonts().getGlyphRangesChineseFull());
rangesBuilder.addRanges(glyphRangesHebrew);
rangesBuilder.addRanges(glyphRangesArabic);
rangesBuilder.addRanges(glyphRangesSubSuper0);
rangesBuilder.addRanges(glyphRangesSubSuper1);
rangesBuilder.addRanges(glyphRangesToneLetters0);
rangesBuilder.addText("\u4ed9⁴ ˧꜏⋇꜊꜔ ⁴ﷲΩ@");
rangesBuilder.addText("©Δ∞ \u4ed9\u4e0a\u4e3b\u5929");
final ImFontConfig fontConfig = new ImFontConfig();
fontConfig.setMergeMode(true); // Enable merge mode to merge cyrillic, japanese and icons with default font
final short[] glyphRanges = rangesBuilder.buildRanges();
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();
}
public static void dispose() {
imGuiImp.dispose();
imGuiGlImp.dispose();
ImGui.destroyContext();
}
}

View file

@ -1,42 +0,0 @@
package love.distributedrebirth.demo4d;
import java.util.List;
import com.badlogic.gdx.ApplicationAdapter;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.utils.ScreenUtils;
import net.spookygames.gdx.nativefilechooser.NativeFileChooser;
public class MainScreen extends ApplicationAdapter {
private List<String> args;
public NativeFileChooser fileChooser;
SpriteBatch batch;
BitmapFont font;
public MainScreen(List<String> args, NativeFileChooser fileChooser) {
this.args = args;
this.fileChooser = fileChooser;
}
@Override
public void create () {
batch = new SpriteBatch();
font = new BitmapFont();
}
@Override
public void render () {
ScreenUtils.clear(1, 0, 0, 1);
batch.begin();
font.draw(batch, "Hello World.", 33, 33);
batch.end();
}
@Override
public void dispose () {
batch.dispose();
font.dispose();
}
}

View file

@ -7,7 +7,7 @@ import com.badlogic.gdx.Files.FileType;
import com.badlogic.gdx.backends.lwjgl3.Lwjgl3Application;
import com.badlogic.gdx.backends.lwjgl3.Lwjgl3ApplicationConfiguration;
import love.distributedrebirth.demo4d.MainScreen;
import love.distributedrebirth.demo4d.Demo4DMain;
import net.spookygames.gdx.nativefilechooser.desktop.DesktopFileChooser;
public class DesktopLauncher {
@ -27,7 +27,7 @@ public class DesktopLauncher {
config.setTitle(WINDOW_TITLE);
config.setWindowedMode(800, 600);
config.setWindowIcon(FileType.Internal, WINDOW_ICONS);
new Lwjgl3Application(new MainScreen(args, new DesktopFileChooser()), config);
new Lwjgl3Application(new Demo4DMain(args, new DesktopFileChooser()), config);
}
private static void printMessage() {