From e7de6f7d94a6a2168d7c0002558d5d69205d51b5 Mon Sep 17 00:00:00 2001 From: Joseph Walton-Rivers <joseph@walton-rivers.uk> Date: Tue, 7 Sep 2021 22:06:03 +0100 Subject: [PATCH] refactor imgui demos into functions --- demo/main.cpp | 213 ++++++++++++++++++++++++++------------------------ 1 file changed, 112 insertions(+), 101 deletions(-) diff --git a/demo/main.cpp b/demo/main.cpp index d865c98..f68e289 100644 --- a/demo/main.cpp +++ b/demo/main.cpp @@ -60,6 +60,114 @@ void loadShader(fggl::gfx::ShaderCache& cache, const char* name, bool hasGeom) { cache.load(config); } +void debugJoystickWindow( bool* visible, fggl::gfx::Input& input ) { + if ( ! *visible ) { + return; + } + + ImGui::Begin("Joysticks", visible); + for ( int i=0; i<16; i++ ) { + bool present = input.hasJoystick(i); + std::string title = "Joystick " + std::to_string(i); + + if ( ImGui::TreeNode(title.c_str()) ) { + ImGui::Text("present: %s", present ? "yes" : "no" ); + + if ( present ) { + const fggl::gfx::Joystick& joyData = input.joystick(i); + ImGui::Text( "%s", joyData.name ); + ImGui::Text( "gamepad: %s", joyData.gamepad ? "yes" : "no" ); + ImGui::Text( "axes: %d, buttons: %d, hats: %d", + joyData.axisCount, + joyData.buttonCount, + joyData.hatCount); + + if (ImGui::TreeNode("axis##2")) { + // dump data + for ( int axid = 0; axid < joyData.axisCount; axid++ ) { + ImGui::Text("axis %d, value: %f", + axid, joyData.axes[axid] ); + } + ImGui::TreePop(); + } + + if (ImGui::TreeNode("buttons##2")) { + // dump data + for ( int btnid = 0; btnid < joyData.buttonCount; btnid++ ) { + ImGui::Text("button %d, value: %s", btnid, + joyData.buttons[btnid] == GLFW_PRESS + ? "down" : "up" ); + } + ImGui::TreePop(); + } + + if (ImGui::TreeNode("hats##2")) { + // dump data + for ( int btnid = 0; btnid < joyData.hatCount; btnid++ ) { + ImGui::Text("button %d, value: %d", + btnid, joyData.hats[btnid] ); + } + ImGui::TreePop(); + } + } + + ImGui::TreePop(); + ImGui::Separator(); + } + + } + ImGui::End(); +} + +void debugGamepadWindow( bool* visible, fggl::gfx::Input& input ) { + if ( ! *visible ) { + return; + } + + ImGui::Begin("GamePad", visible); + for ( int i=0; i<16; i++ ) { + std::string title = "GamePad " + std::to_string(i); + + bool present = input.hasJoystick(i); + if ( ImGui::TreeNode(title.c_str()) ) { + ImGui::Text("present: %s", present ? "yes" : "no" ); + + if ( present ) { + + if ( ImGui::TreeNode("buttons##2") ) { + for ( auto& btn : fggl::gfx::PadButtons ) { + auto label = fggl::gfx::PadButtonLabels[ (int) btn ]; + ImGui::Text( "%s: %i %i %i", label.c_str(), + input.padDown(i, btn), + input.padPressed(i, btn), + input.padReleased(i, btn) + ); + } + ImGui::TreePop(); + } + + if ( ImGui::TreeNode("axes##2") ) { + for ( auto& axis : fggl::gfx::PadAxes ) { + auto label = fggl::gfx::PadAxisLabels[ (int) axis ]; + ImGui::Text("%s: %f %f", label.c_str(), + input.padAxis(i, axis), + input.padAxisDelta(i, axis) + ); + + } + ImGui::TreePop(); + } + + } + + ImGui::TreePop(); + ImGui::Separator(); + } + + } + ImGui::End(); +} + int main(int argc, char* argv[]) { fggl::gfx::Context ctx; @@ -96,6 +204,7 @@ int main(int argc, char* argv[]) { world.import<fggl::systems::OpenGL>(); world.import<fggl::systems::CameraInput>(); + // NB until input handling and frame management is fixed, this singleton is needed... world.ecs().set<fggl::components::GfxHacks>({ &win }); // make camera @@ -179,107 +288,9 @@ int main(int argc, char* argv[]) { // update step time += dt; - // imgui joystick debug - if ( joystickWindow ) { - ImGui::Begin("Joysticks", &joystickWindow); - for ( int i=0; i<16; i++ ) { - bool present = input.hasJoystick(i); - std::string title = "Joystick " + std::to_string(i); - - if ( ImGui::TreeNode(title.c_str()) ) { - ImGui::Text("present: %s", present ? "yes" : "no" ); - - if ( present ) { - const fggl::gfx::Joystick& joyData = input.joystick(i); - ImGui::Text( "%s", joyData.name ); - ImGui::Text( "gamepad: %s", joyData.gamepad ? "yes" : "no" ); - ImGui::Text( "axes: %d, buttons: %d, hats: %d", - joyData.axisCount, - joyData.buttonCount, - joyData.hatCount); - - if (ImGui::TreeNode("axis##2")) { - // dump data - for ( int axid = 0; axid < joyData.axisCount; axid++ ) { - ImGui::Text("axis %d, value: %f", - axid, joyData.axes[axid] ); - } - ImGui::TreePop(); - } - - if (ImGui::TreeNode("buttons##2")) { - // dump data - for ( int btnid = 0; btnid < joyData.buttonCount; btnid++ ) { - ImGui::Text("button %d, value: %s", btnid, - joyData.buttons[btnid] == GLFW_PRESS - ? "down" : "up" ); - } - ImGui::TreePop(); - } - - if (ImGui::TreeNode("hats##2")) { - // dump data - for ( int btnid = 0; btnid < joyData.hatCount; btnid++ ) { - ImGui::Text("button %d, value: %d", - btnid, joyData.hats[btnid] ); - } - ImGui::TreePop(); - } - } - - ImGui::TreePop(); - ImGui::Separator(); - } - - } - ImGui::End(); - } - - // imgui gamepad debug - if ( gamepadWindow ) { - ImGui::Begin("GamePad", &gamepadWindow); - for ( int i=0; i<16; i++ ) { - std::string title = "GamePad " + std::to_string(i); - - bool present = input.hasJoystick(i); - if ( ImGui::TreeNode(title.c_str()) ) { - ImGui::Text("present: %s", present ? "yes" : "no" ); - - if ( present ) { - - if ( ImGui::TreeNode("buttons##2") ) { - for ( auto& btn : fggl::gfx::PadButtons ) { - auto label = fggl::gfx::PadButtonLabels[ (int) btn ]; - ImGui::Text( "%s: %i %i %i", label.c_str(), - input.padDown(i, btn), - input.padPressed(i, btn), - input.padReleased(i, btn) - ); - } - ImGui::TreePop(); - } - - if ( ImGui::TreeNode("axes##2") ) { - for ( auto& axis : fggl::gfx::PadAxes ) { - auto label = fggl::gfx::PadAxisLabels[ (int) axis ]; - ImGui::Text("%s: %f %f", label.c_str(), - input.padAxis(i, axis), - input.padAxisDelta(i, axis) - ); - - } - ImGui::TreePop(); - } - - } - - ImGui::TreePop(); - ImGui::Separator(); - } - - } - ImGui::End(); - } + // imgui demos + debugJoystickWindow( &joystickWindow, input ); + debugGamepadWindow( &gamepadWindow, input ); // render step ogl.clear(); -- GitLab