From 8025c27ed9f279bdf0ed83f2915f4c3aacf58ff7 Mon Sep 17 00:00:00 2001 From: gradient Date: Wed, 18 Oct 2017 14:22:47 -0500 Subject: [PATCH] move gui stuff to src/gui/ --- src/gui.cpp | 137 --------------------------------------------------- src/gui.h | 35 -------------- src/gui/gui.cpp | 148 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/gui/gui.h | 38 +++++++++++++++ src/main.cpp | 2 +- 5 files changed, 187 insertions(+), 173 deletions(-) delete mode 100644 src/gui.cpp delete mode 100644 src/gui.h create mode 100644 src/gui/gui.cpp create mode 100644 src/gui/gui.h diff --git a/src/gui.cpp b/src/gui.cpp deleted file mode 100644 index 872e38c..0000000 --- a/src/gui.cpp +++ /dev/null @@ -1,137 +0,0 @@ -#include -#include -#include -#include -#include "gui.h" -#include "project.h" - - -void Gui::render() { - if (imgui_demo_open) ImGui::ShowTestWindow(&imgui_demo_open); - if (project_overlay_open) this->render_project_overlay(); - this->render_menubar(); -} - -void Gui::render_project_overlay() { - ImGui::SetNextWindowPos(ImVec2(20, 30)); - if (ImGui::Begin("", &this->project_overlay_open, ImVec2(200,0), 0.3f, ImGuiWindowFlags_NoTitleBar|ImGuiWindowFlags_NoResize|ImGuiWindowFlags_NoMove|ImGuiWindowFlags_NoSavedSettings)) { - ImGui::Text("[project]"); - ImGui::Separator(); - if (state->current_project != nullptr) { - ImGui::Text("name: %s", state->current_project->name.c_str()); - ImGui::Text("n_scenes: %i", state->current_project->scenes.size()); - if (!state->current_project->scene_loaded) { - ImGui::Text("no scene loaded"); - } else { - ImGui::Text("current scene: %s", - state->current_project->current_scene->name.c_str()); - } - } else { - ImGui::Text("no project loaded."); - } - ImGui::End(); - } -} - -void Gui::render_menubar() { - if (ImGui::BeginMainMenuBar()) { - if (ImGui::BeginMenu("File")) - render_mmb_file(); - - if (ImGui::BeginMenu("View")) - render_mmb_view(); - - if (ImGui::BeginMenu("Debug")) - render_mmb_debug(); - - ImGui::EndMainMenuBar(); - } -} - -void Gui::render_mmb_file() { - if (ImGui::MenuItem("Load project", "CTRL+O")) { - /*nfdchar_t *out_path = nullptr; - nfdresult_t result = NFD_OpenDialog(NULL, NULL, &out_path); - if (result == NFD_OKAY) { - // Load project - auto new_project = Project::fromFile(std::string(out_path)); - state->current_project = std::move(new_project); - - free(out_path); - } else if (result == NFD_ERROR) { - std::cout << "Error in filepicker: " << NFD_GetError() << '\n'; - }*/ - } - - ImGui::Separator(); - if (ImGui::MenuItem("Exit", "CTRL+Q")) - state->running = false; - - ImGui::EndMenu(); -} - -void Gui::render_mmb_view() { - ImGui::MenuItem("Show project meta overlay", NULL, &this->project_overlay_open); - - ImGui::Separator(); - - if (ImGui::MenuItem("Reload ImGui styling")) - this->load_style(cpptoml::parse_file(state->config->stylepath)); - - ImGui::MenuItem("Show ImGui demo window", NULL, &this->imgui_demo_open); - - ImGui::EndMenu(); -} - -void Gui::render_mmb_debug() { - ImGui::MenuItem("Profiler"); - - if (ImGui::MenuItem("Reload shaders", "CTRL+R")) { - if (state->current_project != nullptr) { - if (state->current_project->scene_loaded) { - std::cout << "=== reloading shaders ===\n"; - state->current_project->reload_shaders(); - } - } - } - - ImGui::EndMenu(); -} - - -static cpptoml::option vec2im2(cpptoml::option> vec) { - if (vec) { - assert((*vec).size() == 2); - return ImVec2((*vec)[0], (*vec)[1]); - } else { - return cpptoml::option(); - } -} - -static cpptoml::option vec2im4(cpptoml::option> vec) { - if (vec) { - assert((*vec).size() == 4); - return ImVec4((*vec)[0], (*vec)[1], (*vec)[2], (*vec)[3]); - } else { - return cpptoml::option(); - } -} - -#define st_lds_d(n, k) style.n = conf->get_qualified_as(k).value_or(defaultstyle.n) -#define st_lds_v2(n, k) style.n = vec2im2(conf->get_qualified_array_of(k)).value_or(defaultstyle.n) -#define st_lds_v4(n, k) style.n = vec2im4(conf->get_qualified_array_of(k)).value_or(defaultstyle.n) - - -void Gui::load_style(std::shared_ptr conf) { - ImGuiStyle &style = ImGui::GetStyle(); - auto defaultstyle = ImGuiStyle(); - - st_lds_d(Alpha, "alpha"); - st_lds_v2(WindowPadding, "window.padding"); - st_lds_v2(WindowMinSize, "window.min_size"); - st_lds_d(WindowRounding, "window.rounding"); - st_lds_v2(WindowTitleAlign, "window.title_align"); - st_lds_d(ChildWindowRounding, "window.child_rounding"); - st_lds_v2(FramePadding, "frame.padding"); - st_lds_d(FrameRounding, "frame.rounding"); -} diff --git a/src/gui.h b/src/gui.h deleted file mode 100644 index aeb9f8f..0000000 --- a/src/gui.h +++ /dev/null @@ -1,35 +0,0 @@ -#pragma once - -#include "state.h" -#include -#include - -class Gui { -public: - Gui(DemoState *state) : - perf_open(false), - state(state) - { - this->load_style(cpptoml::parse_file(state->config->stylepath)); - } - - void render(); - - bool perf_open = false; - bool project_overlay_open = true; - - bool imgui_demo_open = false; - -private: - DemoState *state; - - void render_project_overlay(); - - void render_menubar(); - void render_mmb_file(); - void render_mmb_view(); - void render_mmb_debug(); - - void load_style(std::shared_ptr style); -}; - diff --git a/src/gui/gui.cpp b/src/gui/gui.cpp new file mode 100644 index 0000000..6226b66 --- /dev/null +++ b/src/gui/gui.cpp @@ -0,0 +1,148 @@ +#include +#include +#include +#include +#include "gui.h" +#include "../project.h" + + +void Gui::render() { + if (present_mode) + return; + + if (imgui_demo_open) + ImGui::ShowTestWindow(&imgui_demo_open); + + if (project_overlay_open) + this->render_project_overlay(); + + if (node_editor_open) + this->node_editor.draw_window("Node Editor", &node_editor_open); + + this->render_menubar(); +} + +void Gui::render_project_overlay() { + ImGui::SetNextWindowPos(ImVec2(20, 30)); + if (ImGui::Begin("", &this->project_overlay_open, ImVec2(200,0), 0.3f, ImGuiWindowFlags_NoTitleBar|ImGuiWindowFlags_NoResize|ImGuiWindowFlags_NoMove|ImGuiWindowFlags_NoSavedSettings)) { + ImGui::Text("[project]"); + ImGui::Separator(); + if (state->current_project != nullptr) { + ImGui::Text("name: %s", state->current_project->name.c_str()); + ImGui::Text("n_scenes: %d", static_cast(state->current_project->scenes.size())); + if (!state->current_project->scene_loaded) { + ImGui::Text("no scene loaded"); + } else { + ImGui::Text("current scene: %s", + state->current_project->current_scene->name.c_str()); + } + } else { + ImGui::Text("no project loaded."); + } + ImGui::End(); + } +} + +void Gui::render_menubar() { + if (ImGui::BeginMainMenuBar()) { + if (ImGui::BeginMenu("File")) + render_mmb_file(); + + if (ImGui::BeginMenu("View")) + render_mmb_view(); + + if (ImGui::BeginMenu("Debug")) + render_mmb_debug(); + + ImGui::EndMainMenuBar(); + } +} + +void Gui::render_mmb_file() { + if (ImGui::MenuItem("Load project", "CTRL+O")) { + /*nfdchar_t *out_path = nullptr; + nfdresult_t result = NFD_OpenDialog(NULL, NULL, &out_path); + if (result == NFD_OKAY) { + // Load project + auto new_project = Project::from_file(std::string(out_path)); + state->current_project = std::move(new_project); + + free(out_path); + } else if (result == NFD_ERROR) { + std::cout << "Error in filepicker: " << NFD_GetError() << '\n'; + }*/ + } + + ImGui::Separator(); + if (ImGui::MenuItem("Exit", "CTRL+Q")) + state->running = false; + + ImGui::EndMenu(); +} + +void Gui::render_mmb_view() { + ImGui::MenuItem("Node Editor", NULL, &this->node_editor_open); + ImGui::MenuItem("Meta overlay", NULL, &this->project_overlay_open); + + ImGui::Separator(); + + if (ImGui::MenuItem("Reload ImGui styling")) + this->load_style(cpptoml::parse_file(state->config->stylepath)); + + ImGui::MenuItem("Show ImGui demo window", NULL, &this->imgui_demo_open); + + ImGui::EndMenu(); +} + +void Gui::render_mmb_debug() { + ImGui::MenuItem("Profiler"); + + if (ImGui::MenuItem("Reload shaders", "CTRL+R")) { + if (state->current_project != nullptr) { + if (state->current_project->scene_loaded) { + std::cout << "=== reloading shaders ===\n"; + state->current_project->reload_shaders(); + } + } + } + + ImGui::EndMenu(); +} + + +static cpptoml::option vec2im2(cpptoml::option> vec) { + if (vec) { + assert((*vec).size() == 2); + return ImVec2((*vec)[0], (*vec)[1]); + } else { + return cpptoml::option(); + } +} + +static cpptoml::option vec2im4(cpptoml::option> vec) { + if (vec) { + assert((*vec).size() == 4); + return ImVec4((*vec)[0], (*vec)[1], (*vec)[2], (*vec)[3]); + } else { + return cpptoml::option(); + } +} + +#define st_lds_d(n, k) style.n = conf->get_qualified_as(k).value_or(defaultstyle.n) +#define st_lds_v2(n, k) style.n = vec2im2(conf->get_qualified_array_of(k)).value_or(defaultstyle.n) +#define st_lds_v4(n, k) style.n = vec2im4(conf->get_qualified_array_of(k)).value_or(defaultstyle.n) + + +void Gui::load_style(std::shared_ptr conf) { + ImGuiStyle &style = ImGui::GetStyle(); + auto defaultstyle = ImGuiStyle(); + + st_lds_d(Alpha, "imgui.alpha"); + st_lds_v2(WindowPadding, "imgui.window.padding"); + st_lds_v2(WindowMinSize, "imgui.window.min_size"); + st_lds_d(WindowRounding, "imgui.window.rounding"); + st_lds_v2(WindowTitleAlign, "imgui.window.title_align"); + st_lds_d(ChildWindowRounding, "imgui.window.child_rounding"); + st_lds_v2(FramePadding, "imgui.frame.padding"); + st_lds_d(FrameRounding, "imgui.frame.rounding"); +} diff --git a/src/gui/gui.h b/src/gui/gui.h new file mode 100644 index 0000000..7b34a22 --- /dev/null +++ b/src/gui/gui.h @@ -0,0 +1,38 @@ +#pragma once + +#include +#include +#include "../state.h" +#include "node_editor.h" + +class Gui { +public: + Gui(DemoState *state) : state(state), node_editor{} + { + this->load_style(cpptoml::parse_file(state->config->stylepath)); + } + + void render(); + + bool perf_open = false; + bool project_overlay_open = true; + bool imgui_demo_open = false; + bool node_editor_open = true; + + /// Skip rendering the GUI, for presenting stuff. + bool present_mode = false; + + NodeEditor node_editor; + +private: + DemoState *state; + + void render_project_overlay(); + + void render_menubar(); + void render_mmb_file(); + void render_mmb_view(); + void render_mmb_debug(); + + void load_style(std::shared_ptr style); +}; diff --git a/src/main.cpp b/src/main.cpp index 4b2bb2e..da3d1d1 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -7,8 +7,8 @@ #include #include -#include "gui.h" #include "geom.h" +#include "gui/gui.h" #include "state.h" #include "notify.h"