Project: add dedicated reload_shaders method
[zinnia.git] / src / gui.cpp
blob5834911cc4c86423f27c241f738986708cfbc48d
1 #include <vector>
2 #include <iostream>
3 #include <assert.h>
4 #include <imgui.h>
5 #include "gui.h"
6 #include "project.h"
9 void Gui::render() {
10 if (imgui_demo_open) ImGui::ShowTestWindow(&imgui_demo_open);
11 if (project_overlay_open) this->render_project_overlay();
12 this->render_menubar();
15 void Gui::render_menubar() {
16 if (ImGui::BeginMainMenuBar()) {
17 if (ImGui::BeginMenu("File"))
18 render_mmb_file();
20 if (ImGui::BeginMenu("View"))
21 render_mmb_view();
23 if (ImGui::BeginMenu("Debug"))
24 render_mmb_debug();
26 ImGui::EndMainMenuBar();
30 void Gui::render_project_overlay() {
31 ImGui::SetNextWindowPos(ImVec2(20, 30));
32 if (ImGui::Begin("", &this->project_overlay_open, ImVec2(200,0), 0.3f, ImGuiWindowFlags_NoTitleBar|ImGuiWindowFlags_NoResize|ImGuiWindowFlags_NoMove|ImGuiWindowFlags_NoSavedSettings)) {
33 ImGui::Text("[project]");
34 ImGui::Separator();
35 if (state->current_project != nullptr) {
36 ImGui::Text("name: %s", state->current_project->name.c_str());
37 ImGui::Text("n_scenes: %i", state->current_project->scenes.size());
38 if (!state->current_project->scene_loaded) {
39 ImGui::Text("no scene loaded");
40 } else {
41 ImGui::Text("current scene: %s",
42 state->current_project->current_scene->name.c_str());
44 } else {
45 ImGui::Text("no project loaded.");
47 ImGui::End();
51 void Gui::render_mmb_file() {
52 if (ImGui::MenuItem("Load project", "CTRL+O")) {
53 /*nfdchar_t *out_path = nullptr;
54 nfdresult_t result = NFD_OpenDialog(NULL, NULL, &out_path);
55 if (result == NFD_OKAY) {
56 // Load project
57 auto new_project = Project::fromFile(std::string(out_path));
58 state->current_project = std::move(new_project);
60 free(out_path);
61 } else if (result == NFD_ERROR) {
62 std::cout << "Error in filepicker: " << NFD_GetError() << '\n';
63 }*/
66 ImGui::Separator();
67 if (ImGui::MenuItem("Exit", "CTRL+Q"))
68 state->running = false;
70 ImGui::EndMenu();
73 void Gui::render_mmb_view() {
74 ImGui::MenuItem("Show project meta overlay", NULL, &this->project_overlay_open);
76 ImGui::Separator();
78 if (ImGui::MenuItem("Reload ImGui styling"))
79 this->load_style(cpptoml::parse_file(state->config->stylepath));
81 ImGui::MenuItem("Show ImGui demo window", NULL, &this->imgui_demo_open);
83 ImGui::EndMenu();
86 void Gui::render_mmb_debug() {
87 ImGui::MenuItem("Profiler");
89 if (ImGui::MenuItem("Reload shaders", "CTRL+R")) {
90 if (state->current_project != nullptr) {
91 if (state->current_project->scene_loaded) {
92 std::cout << "=== reloading shaders ===\n";
93 state->current_project->reload_shaders();
98 ImGui::EndMenu();
102 static cpptoml::option<ImVec2> vec2im2(cpptoml::option<std::vector<double>> vec) {
103 if (vec) {
104 assert((*vec).size() == 2);
105 return ImVec2((*vec)[0], (*vec)[1]);
106 } else {
107 return cpptoml::option<ImVec2>();
111 static cpptoml::option<ImVec4> vec2im4(cpptoml::option<std::vector<double>> vec) {
112 if (vec) {
113 assert((*vec).size() == 4);
114 return ImVec4((*vec)[0], (*vec)[1], (*vec)[2], (*vec)[3]);
115 } else {
116 return cpptoml::option<ImVec4>();
120 #define st_lds_d(n, k) style.n = conf->get_qualified_as<double>(k).value_or(defaultstyle.n)
121 #define st_lds_v2(n, k) style.n = vec2im2(conf->get_qualified_array_of<double>(k)).value_or(defaultstyle.n)
122 #define st_lds_v4(n, k) style.n = vec2im4(conf->get_qualified_array_of<double>(k)).value_or(defaultstyle.n)
125 void Gui::load_style(std::shared_ptr<cpptoml::table> conf) {
126 ImGuiStyle &style = ImGui::GetStyle();
127 auto defaultstyle = ImGuiStyle();
129 st_lds_d(Alpha, "alpha");
130 st_lds_v2(WindowPadding, "window.padding");
131 st_lds_v2(WindowMinSize, "window.min_size");
132 st_lds_d(WindowRounding, "window.rounding");
133 st_lds_v2(WindowTitleAlign, "window.title_align");
134 st_lds_d(ChildWindowRounding, "window.child_rounding");
135 st_lds_v2(FramePadding, "frame.padding");
136 st_lds_d(FrameRounding, "frame.rounding");