Skip to content

Commit

Permalink
update imgui gdx tests
Browse files Browse the repository at this point in the history
  • Loading branch information
xpenatan committed May 9, 2024
1 parent 88a3d10 commit aaf2a6e
Show file tree
Hide file tree
Showing 5 changed files with 375 additions and 68 deletions.
4 changes: 2 additions & 2 deletions buildSrc/src/main/kotlin/Dependencies.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ object LibExt {
const val gdxVersion = "1.12.1"
const val teaVMVersion = "0.10.0"

const val gdxImGuiVersion = "1.0.0-SNAPSHOT"
const val gdxMultiViewVersion = "1.0.0-SNAPSHOT"
const val gdxImGuiVersion = "-SNAPSHOT"
const val gdxMultiViewVersion = "-SNAPSHOT"

const val reflectionVersion = "0.10.2"
const val jettyVersion = "11.0.13"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,22 @@
package com.github.xpenatan.imgui.example.tests.imgui;

import com.badlogic.gdx.Application;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.glutils.HdpiUtils;
import com.badlogic.gdx.tests.AbstractTestWrapper;
import com.badlogic.gdx.tests.InputTest;
import com.badlogic.gdx.tests.utils.GdxTest;
import com.badlogic.gdx.tests.TeaVMGdxTests;
import com.badlogic.gdx.utils.ScreenUtils;
import com.github.xpenatan.gdx.multiview.EmuFrameBuffer;
import com.github.xpenatan.imgui.example.tests.frame.GameFrame;
import imgui.ImDrawData;
import imgui.ImGui;
import imgui.ImGuiConfigFlags;
import imgui.ImGuiIO;
import imgui.ImVec2;
import imgui.gdx.ImGuiGdxImpl;
Expand All @@ -22,97 +27,132 @@
*/
public class ImGuiTestsApp implements Screen {

ImGuiGdxImpl impl;
private GdxTest test;

boolean gdxTestInit = false;
private ImGuiGdxImpl impl;
private ImGuiGdxInputMultiplexer input;

int selected = -1;
private boolean gdxTestInit = false;

private GameFrame gameFrame;
private int selected = -1;

SpriteBatch batch;
private OrthographicCamera camera;
private TeaVMGdxTests.TeaVMInstancer[] testList;

TeaVMGdxTests.TeaVMInstancer[] testList;
private boolean dispose = false;

@Override
public void show() {
ImGui.CreateContext(true);

camera = new OrthographicCamera();
camera.setToOrtho(true);
batch = new SpriteBatch();
testList = TeaVMGdxTests.getTestList();
if(Gdx.app.getType() == Application.ApplicationType.WebGL) {
// Not possible to have ini filename with webgl
ImGui.CreateContext(false);
}
else {
ImGui.CreateContext(true);
}

ImGuiIO io = ImGui.GetIO();
io.ConfigFlags(ImGuiConfigFlags.ImGuiConfigFlags_DockingEnable);

// io.setIniFilename(null);
// io.SetConfigFlags(ImGuiConfigFlags.DockingEnable);
io.SetDockingFlags(false, false, false, false);

input = new ImGuiGdxInputMultiplexer();
impl = new ImGuiGdxImpl();

EmuFrameBuffer.setDefaultFramebufferHandleInitialized(false);

gameFrame = new GameFrame(20, 20, 800, 400);

testList = TeaVMGdxTests.getTestList();

gameFrame.emuWindow.setApplicationListener(new InputTest());
Gdx.input = new TeaVMInputWrapper(Gdx.input) {
@Override
public boolean keyUp (int keycode) {
if (keycode == Keys.ESCAPE) {
if (test != null) {
Gdx.app.log("GdxTest", "Exiting current test.");
dispose = true;
}
}
return false;
}

ImGuiGdxInputMultiplexer multiplexer = new ImGuiGdxInputMultiplexer();
multiplexer.addProcessor(gameFrame.emuWindow.getEmuInput());
Gdx.input.setInputProcessor(multiplexer);
@Override
public boolean touchDown (int screenX, int screenY, int pointer, int button) {
if (screenX < Gdx.graphics.getWidth() / 10.0 && screenY < Gdx.graphics.getHeight() / 10.0) {
if (test != null) {
dispose = true;
}
}
return false;
}
};
((TeaVMInputWrapper)Gdx.input).multiplexer.addProcessor(input);
}

private void drawTestListWindow() {
if(!gdxTestInit) {
gdxTestInit = true;
ImGui.SetNextWindowSize(ImVec2.TMP_1.set(200, 500));
ImGui.SetNextWindowPos(ImVec2.TMP_1.set(900, 20));
ImGui.SetNextWindowSize(ImVec2.TMP_1.set(250, 500));
ImGui.SetNextWindowPos(ImVec2.TMP_1.set(20, 20));
}
ImGui.Begin("GdxTests");
ImGui.BeginChildFrame(313, ImVec2.TMP_1.set(0f, 0f));
if(ImGui.Button("Start Test")) {
if(selected >= 0 && selected < testList.length) {
((TeaVMInputWrapper)Gdx.input).multiplexer.removeProcessor(input);
test = testList[selected].instance();
Gdx.app.log("GdxTest", "Clicked on " + test.getClass().getName());
test.create();
test.resize(Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
}
}
ImGui.Separator();
ImGui.BeginChild("List", ImVec2.TMP_1.set(0, 0));
for(int i = 0; i < testList.length; i++) {
String testName = testList[i].getSimpleName();
boolean isSelected = selected == i;
if(ImGui.Selectable(testName, isSelected)) {
if(selected != i) {
selected = i;
GdxTest newTest = testList[i].instance();
gameFrame.emuWindow.setApplicationListener(newTest);
}
}
}
ImGui.EndChildFrame();
ImGui.EndChild();
ImGui.End();
}

@Override
public void render(float delta) {
Gdx.gl.glClearColor(0.1f, 0.1f, 0.1f, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

camera.update();
gameFrame.update();

impl.update();
drawTestListWindow();
ImGui.Render();
ImDrawData drawData = ImGui.GetDrawData();
impl.render(drawData);

batch.setProjectionMatrix(camera.combined);
batch.begin();
gameFrame.draw(batch);
batch.end();
if (test == null) {
Gdx.gl.glClearColor(0.5f, 0.5f, 0.5f, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

impl.update();

drawTestListWindow();

ImGui.Render();
ImDrawData drawData = ImGui.GetDrawData();
impl.render(drawData);
}
else {
if (dispose) {
test.pause();
test.dispose();
test = null;
Gdx.graphics.setVSync(true);
TeaVMInputWrapper wrapper = ((TeaVMInputWrapper)Gdx.input);
wrapper.multiplexer.addProcessor(input);
wrapper.multiplexer.removeProcessor(wrapper.lastProcessor);
wrapper.lastProcessor = null;
dispose = false;
HdpiUtils.glViewport(0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
} else {
test.render();
}
}
}

@Override
public void resize(int width, int height) {
// gameFrame.windowX = 0;
// gameFrame.windowY = 0;
// gameFrame.windowWidth = width;
// gameFrame.windowHeight = height;
if (test != null) {
test.resize(width, height);
}
else {
HdpiUtils.glViewport(0, 0, width, height);
}
}

@Override
Expand All @@ -132,6 +172,6 @@ public void hide() {
public void dispose() {
impl.dispose();
ImGui.disposeStatic();
batch.dispose();
ImGui.DestroyContext();
}
}
Loading

0 comments on commit aaf2a6e

Please sign in to comment.