diff --git a/core/src/love/distributedrebirth/demo4d/Demo4DMain.java b/core/src/love/distributedrebirth/demo4d/Demo4DMain.java new file mode 100644 index 00000000..da1ad841 --- /dev/null +++ b/core/src/love/distributedrebirth/demo4d/Demo4DMain.java @@ -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 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,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 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 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; + } +} diff --git a/core/src/love/distributedrebirth/demo4d/Demo4DMainAdapter.java b/core/src/love/distributedrebirth/demo4d/Demo4DMainAdapter.java new file mode 100644 index 00000000..b3c32fdd --- /dev/null +++ b/core/src/love/distributedrebirth/demo4d/Demo4DMainAdapter.java @@ -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; + } +} diff --git a/core/src/love/distributedrebirth/demo4d/ImGuiRenderer.java b/core/src/love/distributedrebirth/demo4d/ImGuiRenderer.java new file mode 100644 index 00000000..d5508e81 --- /dev/null +++ b/core/src/love/distributedrebirth/demo4d/ImGuiRenderer.java @@ -0,0 +1,8 @@ +package love.distributedrebirth.demo4d; + +import imgui.type.ImBoolean; + +public interface ImGuiRenderer { + + void render(ImBoolean widgetOpen); +} diff --git a/core/src/love/distributedrebirth/demo4d/ImGuiRendererMain.java b/core/src/love/distributedrebirth/demo4d/ImGuiRendererMain.java new file mode 100644 index 00000000..a778ea44 --- /dev/null +++ b/core/src/love/distributedrebirth/demo4d/ImGuiRendererMain.java @@ -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; + } +} diff --git a/core/src/love/distributedrebirth/demo4d/ImGuiSetup.java b/core/src/love/distributedrebirth/demo4d/ImGuiSetup.java new file mode 100644 index 00000000..3eb2709a --- /dev/null +++ b/core/src/love/distributedrebirth/demo4d/ImGuiSetup.java @@ -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(); + } +} diff --git a/core/src/love/distributedrebirth/demo4d/MainScreen.java b/core/src/love/distributedrebirth/demo4d/MainScreen.java deleted file mode 100644 index 068c9a8d..00000000 --- a/core/src/love/distributedrebirth/demo4d/MainScreen.java +++ /dev/null @@ -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 args; - public NativeFileChooser fileChooser; - SpriteBatch batch; - BitmapFont font; - - public MainScreen(List 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(); - } -} diff --git a/desktop/src/love/distributedrebirth/demo4d/desktop/DesktopLauncher.java b/desktop/src/love/distributedrebirth/demo4d/desktop/DesktopLauncher.java index 9efbebd1..7f96bbc3 100644 --- a/desktop/src/love/distributedrebirth/demo4d/desktop/DesktopLauncher.java +++ b/desktop/src/love/distributedrebirth/demo4d/desktop/DesktopLauncher.java @@ -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() {