A little toolkit for single-quad fragment shader demos
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

137 lines
3.8 KiB

  1. #include <vector>
  2. #include <iostream>
  3. #include <assert.h>
  4. #include <imgui.h>
  5. #include "gui.h"
  6. #include "project.h"
  7. void Gui::render() {
  8. if (imgui_demo_open) ImGui::ShowTestWindow(&imgui_demo_open);
  9. if (project_overlay_open) this->render_project_overlay();
  10. this->render_menubar();
  11. }
  12. void Gui::render_project_overlay() {
  13. ImGui::SetNextWindowPos(ImVec2(20, 30));
  14. if (ImGui::Begin("", &this->project_overlay_open, ImVec2(200,0), 0.3f, ImGuiWindowFlags_NoTitleBar|ImGuiWindowFlags_NoResize|ImGuiWindowFlags_NoMove|ImGuiWindowFlags_NoSavedSettings)) {
  15. ImGui::Text("[project]");
  16. ImGui::Separator();
  17. if (state->current_project != nullptr) {
  18. ImGui::Text("name: %s", state->current_project->name.c_str());
  19. ImGui::Text("n_scenes: %i", state->current_project->scenes.size());
  20. if (!state->current_project->scene_loaded) {
  21. ImGui::Text("no scene loaded");
  22. } else {
  23. ImGui::Text("current scene: %s",
  24. state->current_project->current_scene->name.c_str());
  25. }
  26. } else {
  27. ImGui::Text("no project loaded.");
  28. }
  29. ImGui::End();
  30. }
  31. }
  32. void Gui::render_menubar() {
  33. if (ImGui::BeginMainMenuBar()) {
  34. if (ImGui::BeginMenu("File"))
  35. render_mmb_file();
  36. if (ImGui::BeginMenu("View"))
  37. render_mmb_view();
  38. if (ImGui::BeginMenu("Debug"))
  39. render_mmb_debug();
  40. ImGui::EndMainMenuBar();
  41. }
  42. }
  43. void Gui::render_mmb_file() {
  44. if (ImGui::MenuItem("Load project", "CTRL+O")) {
  45. /*nfdchar_t *out_path = nullptr;
  46. nfdresult_t result = NFD_OpenDialog(NULL, NULL, &out_path);
  47. if (result == NFD_OKAY) {
  48. // Load project
  49. auto new_project = Project::fromFile(std::string(out_path));
  50. state->current_project = std::move(new_project);
  51. free(out_path);
  52. } else if (result == NFD_ERROR) {
  53. std::cout << "Error in filepicker: " << NFD_GetError() << '\n';
  54. }*/
  55. }
  56. ImGui::Separator();
  57. if (ImGui::MenuItem("Exit", "CTRL+Q"))
  58. state->running = false;
  59. ImGui::EndMenu();
  60. }
  61. void Gui::render_mmb_view() {
  62. ImGui::MenuItem("Show project meta overlay", NULL, &this->project_overlay_open);
  63. ImGui::Separator();
  64. if (ImGui::MenuItem("Reload ImGui styling"))
  65. this->load_style(cpptoml::parse_file(state->config->stylepath));
  66. ImGui::MenuItem("Show ImGui demo window", NULL, &this->imgui_demo_open);
  67. ImGui::EndMenu();
  68. }
  69. void Gui::render_mmb_debug() {
  70. ImGui::MenuItem("Profiler");
  71. if (ImGui::MenuItem("Reload shaders", "CTRL+R")) {
  72. if (state->current_project != nullptr) {
  73. if (state->current_project->scene_loaded) {
  74. std::cout << "=== reloading shaders ===\n";
  75. state->current_project->reload_shaders();
  76. }
  77. }
  78. }
  79. ImGui::EndMenu();
  80. }
  81. static cpptoml::option<ImVec2> vec2im2(cpptoml::option<std::vector<double>> vec) {
  82. if (vec) {
  83. assert((*vec).size() == 2);
  84. return ImVec2((*vec)[0], (*vec)[1]);
  85. } else {
  86. return cpptoml::option<ImVec2>();
  87. }
  88. }
  89. static cpptoml::option<ImVec4> vec2im4(cpptoml::option<std::vector<double>> vec) {
  90. if (vec) {
  91. assert((*vec).size() == 4);
  92. return ImVec4((*vec)[0], (*vec)[1], (*vec)[2], (*vec)[3]);
  93. } else {
  94. return cpptoml::option<ImVec4>();
  95. }
  96. }
  97. #define st_lds_d(n, k) style.n = conf->get_qualified_as<double>(k).value_or(defaultstyle.n)
  98. #define st_lds_v2(n, k) style.n = vec2im2(conf->get_qualified_array_of<double>(k)).value_or(defaultstyle.n)
  99. #define st_lds_v4(n, k) style.n = vec2im4(conf->get_qualified_array_of<double>(k)).value_or(defaultstyle.n)
  100. void Gui::load_style(std::shared_ptr<cpptoml::table> conf) {
  101. ImGuiStyle &style = ImGui::GetStyle();
  102. auto defaultstyle = ImGuiStyle();
  103. st_lds_d(Alpha, "alpha");
  104. st_lds_v2(WindowPadding, "window.padding");
  105. st_lds_v2(WindowMinSize, "window.min_size");
  106. st_lds_d(WindowRounding, "window.rounding");
  107. st_lds_v2(WindowTitleAlign, "window.title_align");
  108. st_lds_d(ChildWindowRounding, "window.child_rounding");
  109. st_lds_v2(FramePadding, "frame.padding");
  110. st_lds_d(FrameRounding, "frame.rounding");
  111. }