From 3ff6fb0b1458734da147488aa290a5c8d27d0c93 Mon Sep 17 00:00:00 2001 From: MitchellHansen Date: Wed, 27 Sep 2017 23:36:20 -0700 Subject: [PATCH] Adding a method of rendering GUI's that avoid the whole throwing raw data around everywhere thing I had going on before --- include/CLCaster.h | 8 +- include/Camera.h | 9 ++- include/Gui.h | 66 ++++++++++++++++ include/Input.h | 10 ++- include/LightHandle.h | 7 +- src/Application.cpp | 57 ++------------ src/CLCaster.cpp | 19 +++++ src/Camera.cpp | 33 ++++++++ src/Gui.cpp | 4 + src/Input.cpp | 172 +++++++++++++++++++++++++++++++++++++++++- src/LightHandle.cpp | 17 +++++ 11 files changed, 343 insertions(+), 59 deletions(-) create mode 100644 include/Gui.h create mode 100644 src/Gui.cpp diff --git a/include/CLCaster.h b/include/CLCaster.h index 848a5ab..52403bd 100644 --- a/include/CLCaster.h +++ b/include/CLCaster.h @@ -11,6 +11,7 @@ #include #include "Logger.h" #include "map/Map.h" +#include "Gui.h" #ifdef linux #include @@ -86,7 +87,7 @@ struct device_info { struct PackedData; -class CLCaster { +class CLCaster : private Gui { public: @@ -152,6 +153,11 @@ public: void test_edit_viewport(int width, int height, float v_fov, float h_fov); + // ============= GUI ============== + virtual void render_gui() override; + virtual void update_gui() override; + // ================================ + private: /** diff --git a/include/Camera.h b/include/Camera.h index 571c534..04c00aa 100644 --- a/include/Camera.h +++ b/include/Camera.h @@ -1,11 +1,12 @@ #pragma once #include -#include +#include #include "util.hpp" #include "Pub_Sub.h" #include +#include "Gui.h" -class Camera : public VrEventSubscriber{ +class Camera : public VrEventSubscriber, private Gui{ public: enum DIRECTION { FORWARD, REARWARD, LEFT, RIGHT, UP, DOWN }; @@ -40,6 +41,10 @@ public: void recieve_event(VrEventPublisher* publisher, std::unique_ptr event) override; + + virtual void render_gui() override; + virtual void update_gui() override; + private: float friction_coefficient = 0.1f; diff --git a/include/Gui.h b/include/Gui.h new file mode 100644 index 0000000..1c266de --- /dev/null +++ b/include/Gui.h @@ -0,0 +1,66 @@ +#pragma once +#include +#include +#include + +class Gui { + +public: + + Gui() { + container_lock.lock(); + renderable_container.push_back(this); + container_lock.unlock(); + }; + virtual ~Gui() { + container_lock.lock(); + renderable_container.remove(this); + container_lock.unlock(); + }; + + virtual void render_gui() = 0; + virtual void update_gui() = 0; + + // Instead of rendering nil, we can pass our render call if we would like + bool renderable() { return rendering; }; + +private: + + // Whatever class that wants to call this must be a friend!!! + friend class Application; + static void do_render() { + for (auto i : renderable_container) { + i->update_gui(); + if (i->renderable()) + i->render_gui(); + } + }; + + + + static std::mutex container_lock; + static std::list renderable_container; + +protected: + bool rendering = false; + // Derived class will handle imgui calls + +}; + + + + + + + + + + + + + + + + + + diff --git a/include/Input.h b/include/Input.h index 4db76d9..883a96b 100644 --- a/include/Input.h +++ b/include/Input.h @@ -4,9 +4,11 @@ #include "Event.hpp" #include #include "Pub_Sub.h" +#include "Gui.h" +#include -class Input : public VrEventPublisher { +class Input : public VrEventPublisher, private Gui{ public: Input(); @@ -21,6 +23,10 @@ public: void handle_held_keys(); void dispatch_events(); + + virtual void render_gui() override; + virtual void update_gui() override; + private: void transpose_sf_events(std::list event_queue); @@ -33,6 +39,8 @@ private: private: + static const std::vector key_strings; + std::list> event_queue; }; diff --git a/include/LightHandle.h b/include/LightHandle.h index 5d26f88..cd33ba1 100644 --- a/include/LightHandle.h +++ b/include/LightHandle.h @@ -4,13 +4,14 @@ #include #include "Pub_Sub.h" #include "Vector4.hpp" +#include "Gui.h" struct LightPrototype; class LightController; struct PackedData; -class LightHandle : public VrEventSubscriber{ +class LightHandle : public VrEventSubscriber, private Gui{ public: @@ -35,6 +36,10 @@ public: void update(double delta_time); + + virtual void render_gui() override; + virtual void update_gui() override; + private: LightHandle(LightController *const light_controller, unsigned int light_id, LightPrototype light_prototype, PackedData *const data_reference); diff --git a/src/Application.cpp b/src/Application.cpp index a3f6100..b1d8418 100644 --- a/src/Application.cpp +++ b/src/Application.cpp @@ -145,40 +145,23 @@ bool Application::game_loop() { fps.frame(delta_time); fps.draw(); + Gui::do_render(); + ImGuiWindowFlags window_flags = ImGuiWindowFlags_MenuBar; bool window_show = true; - ImGui::Begin("Camera", &window_show, window_flags); + if (ImGui::BeginMenuBar()) { if (ImGui::BeginMenu("Menu")) { + ImGui::Button("asdoifjasodif"); ImGui::EndMenu(); } ImGui::EndMenuBar(); } - ImGui::Columns(2); - - ImGui::Text("Camera Inclination"); - ImGui::Text("Camera Azimuth"); - ImGui::Text("Camera Pos_X"); - ImGui::Text("Camera Poz_Y"); - ImGui::Text("Camera Poz_Z"); - - ImGui::NextColumn(); - - sf::Vector2f dir = camera->get_direction(); - sf::Vector3f pos = camera->get_position(); - - ImGui::Text("%f", dir.x); - ImGui::Text("%f", dir.y); - ImGui::Text("%f", pos.x); - ImGui::Text("%f", pos.y); - ImGui::Text("%f", pos.z); - - ImGui::NextColumn(); - + ImGui::Begin("Window"); ImGui::InputText("filename", screenshot_buf, 128); if (ImGui::Button("Take Screen shot")) { @@ -197,9 +180,6 @@ bool Application::game_loop() { ImGui::NextColumn(); - if (ImGui::Button("Recompile kernel")) { - while (!raycaster->debug_quick_recompile()); - } if (ImGui::Button("Pause")) { paused = !paused; @@ -210,35 +190,8 @@ bool Application::game_loop() { Logger::log("Unpausing", Logger::LogLevel::INFO); } - ImGui::End(); - ImGui::Begin("Lights"); - - if (ImGui::SliderFloat4("Color", light_color, 0, 1)) { - sf::Vector4f light(light_color[0], light_color[1], light_color[2], light_color[3]); - light_handle->set_rgbi(light); - } - - if (ImGui::SliderFloat("Camera Speed", &camera_speed, 0, 4)) { - camera->setSpeed(camera_speed); - } - - if (ImGui::SliderFloat3("Position", light_pos, 0, MAP_X)) { - sf::Vector3f light(light_pos[0], light_pos[1], light_pos[2]); - light_handle->set_position(light); - } - - if (ImGui::CollapsingHeader("Window options")) - { - if (ImGui::TreeNode("Style")) - { - ImGui::ShowStyleEditor(); - ImGui::TreePop(); - } - } - - ImGui::End(); ImGui::Begin("Controller debugger"); diff --git a/src/CLCaster.cpp b/src/CLCaster.cpp index 2583e28..c03de5a 100644 --- a/src/CLCaster.cpp +++ b/src/CLCaster.cpp @@ -335,6 +335,25 @@ bool CLCaster::debug_quick_recompile() { } + +void CLCaster::render_gui() { + + ImGui::Begin("CLCaster"); + + if (ImGui::Button("Recompile Kernel")) { + while (!debug_quick_recompile()) { + std::cin.get(); + }; + } + + ImGui::End(); +} + + +void CLCaster::update_gui() { + rendering = true; +} + bool CLCaster::aquire_hardware() { Logger::log("Acquiring OpenCL Hardware", Logger::LogLevel::INFO); diff --git a/src/Camera.cpp b/src/Camera.cpp index d8f6198..1aa3e94 100644 --- a/src/Camera.cpp +++ b/src/Camera.cpp @@ -197,6 +197,39 @@ void Camera::recieve_event(VrEventPublisher* publisher, std::unique_ptr Gui::renderable_container; \ No newline at end of file diff --git a/src/Input.cpp b/src/Input.cpp index 400e535..c2032ee 100644 --- a/src/Input.cpp +++ b/src/Input.cpp @@ -1,8 +1,10 @@ -#include "Input.h" +#pragma once +#include "Input.h" #include #include -#include "imgui/imgui-SFML.h" #include "Logger.h" +#include "LightHandle.h" +#include "imgui/imgui-SFML.h" Input::Input() : @@ -119,6 +121,67 @@ void Input::dispatch_events() { } + +void Input::render_gui() { + + ImGui::Begin("Input Debugger"); + + ImDrawList* draw_list = ImGui::GetWindowDrawList(); + static ImVec4 col = ImVec4(1.0f, 0.0f, 1.0f, 1.0f); + const ImVec2 p = ImGui::GetCursorScreenPos(); + const ImU32 col32 = ImColor(col); + + std::vector axis_values = { + sf::Joystick::getAxisPosition(0, sf::Joystick::Axis::X) / 2, + sf::Joystick::getAxisPosition(0, sf::Joystick::Axis::Y) / 2, + sf::Joystick::getAxisPosition(0, sf::Joystick::Axis::U) / 2, + sf::Joystick::getAxisPosition(0, sf::Joystick::Axis::R) / 2, + sf::Joystick::getAxisPosition(0, sf::Joystick::Axis::Z) / 2, + sf::Joystick::getAxisPosition(0, sf::Joystick::Axis::V) / 2 + }; + + ImGui::Columns(3, "Axis's"); // 4-ways, with border + ImGui::Separator(); + ImGui::Text("X Y"); ImGui::NextColumn(); + ImGui::Text("U R"); ImGui::NextColumn(); + ImGui::Text("Z V"); ImGui::NextColumn(); + ImGui::Separator(); + + for (int i = 0; i < 3; i++) { + + + float offset = ImGui::GetColumnWidth(i); + + draw_list->AddLine(ImVec2(p.x + 0 + offset * i, p.y + 50), ImVec2(p.x + 100 + offset * i, p.y + 50), col32, 1.0); + draw_list->AddLine(ImVec2(p.x + 50 + offset * i, p.y + 0), ImVec2(p.x + 50 + offset * i, p.y + 100), col32, 1.0); + draw_list->AddCircleFilled(ImVec2(p.x + axis_values[2 * i] + 50 + offset * i, p.y + axis_values[2 * i + 1] + 50), 6, col32, 32); + + ImGui::Dummy(ImVec2(100, 100)); + ImGui::NextColumn(); + } + + ImGui::Text("Pressed Keyboard Keys"); + + ImGui::Separator(); + + ImGui::Columns(6); + + for (auto i : held_keys) { + ImGui::Text(key_strings.at(i).c_str()); + ImGui::NextColumn(); + } + + ImGui::Separator(); + + ImGui::End(); + +} + + +void Input::update_gui() { + rendering = true; +} + void Input::transpose_sf_events(std::list sf_event_queue) { @@ -229,3 +292,108 @@ void Input::transpose_sf_events(std::list sf_event_queue) { } } +const std::vector Input::key_strings = { + "A", + "B", + "C", + "D", + "E", + "F", + "G", + "H", + "I", + "J", + "K", + "L", + "M", + "N", + "O", + "P", + "Q", + "R", + "S", + "T", + "U", + "V", + "W", + "X", + "Y", + "Z", + "Num0", + "Num1", + "Num2", + "Num3", + "Num4", + "Num5", + "Num6", + "Num7", + "Num8", + "Num9", + "Escape", + "LControl", + "LShift", + "LAlt", + "LSystem", + "RControl", + "RShift", + "RAlt", + "RSystem", + "Menu", + "LBracket", + "RBracket", + "SemiColon", + "Comma", + "Period", + "Quote", + "Slash", + "BackSlash", + "Tilde", + "Equal", + "Dash", + "Space", + "Return", + "BackSpace", + "Tab", + "PageUp", + "PageDown", + "End", + "Home", + "Insert", + "Delete", + "Add", + "Subtract", + "Multiply", + "Divide", + "Left", + "Right", + "Up", + "Down", + "Numpad0", + "Numpad1", + "Numpad2", + "Numpad3", + "Numpad4", + "Numpad5", + "Numpad6", + "Numpad7", + "Numpad8", + "Numpad9", + "F1" , + "F2" , + "F3" , + "F4" , + "F5" , + "F6" , + "F7" , + "F8" , + "F9" , + "F10", + "F11", + "F12", + "F13", + "F14", + "F15", + "Pause" +}; + + diff --git a/src/LightHandle.cpp b/src/LightHandle.cpp index 930dd26..3c08924 100644 --- a/src/LightHandle.cpp +++ b/src/LightHandle.cpp @@ -95,3 +95,20 @@ void LightHandle::update(double delta_time) { } +void LightHandle::render_gui() { + ImGui::Begin("Lights"); + + // Well I'm noooot reaallly supposed to do this. But I've been doing with the caster for + // a wile with no problems.... + ImGui::SliderFloat4("Color", reinterpret_cast(&data_reference->rgbi), 0, 1); + ImGui::SliderFloat3("Position", reinterpret_cast(&data_reference->position), 0, 256); + + ImGui::Separator(); + + ImGui::End(); +} + +void LightHandle::update_gui() { + rendering = true; +} +