demo_imgui_bundle.cpp 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. // Part of ImGui Bundle - MIT License - Copyright (c) 2022-2026 Pascal Thomet - https://github.com/pthom/imgui_bundle
  2. #include "immapp/immapp.h"
  3. #include "hello_imgui/hello_imgui.h"
  4. #include "immapp/snippets.h"
  5. #include "demo_utils/api_demos.h"
  6. #include "imgui_md_wrapper/imgui_md_wrapper.h"
  7. #include <functional>
  8. #include <map>
  9. #include <cstdio>
  10. void demo_immapp_launcher();
  11. void demo_nanovg_launcher();
  12. void demo_text_edit();
  13. void demo_imgui_bundle_intro();
  14. void demo_imgui_show_demo_window();
  15. void demo_widgets();
  16. void demo_implot();
  17. void demo_imgui_md();
  18. void demo_immvision_launcher();
  19. void demo_imguizmo_launcher();
  20. void demo_tex_inspect_launcher();
  21. void demo_node_editor_launcher();
  22. void demo_themes();
  23. void demo_logger();
  24. void demo_utils();
  25. void demo_im_anim();
  26. using VoidFunction = std::function<void(void)>;
  27. static std::map<std::string, bool> gShowCodeStates;
  28. void ShowModuleDemo(const std::string& demoFilename, VoidFunction demoFunction, bool showCode)
  29. {
  30. if (ImGui::GetFrameCount() < 2) // cf https://github.com/pthom/imgui_bundle/issues/293
  31. return;
  32. if (showCode)
  33. {
  34. bool current = gShowCodeStates[demoFilename];
  35. if (ImGui::Checkbox(("Show code##" + demoFilename).c_str(), &current))
  36. gShowCodeStates[demoFilename] = current;
  37. if (current)
  38. ShowPythonVsCppFile(demoFilename.c_str(), 40);
  39. }
  40. demoFunction();
  41. }
  42. struct DemoDetails
  43. {
  44. std::string Label;
  45. std::string DemoFilename;
  46. VoidFunction DemoFunction;
  47. bool ShowCode = false;
  48. };
  49. struct DemoGroup
  50. {
  51. std::string Label;
  52. std::vector<DemoDetails> Demos;
  53. };
  54. void ShowGroupGui(const DemoGroup& group)
  55. {
  56. if (ImGui::GetFrameCount() < 2)
  57. return;
  58. for (const auto& demo : group.Demos)
  59. {
  60. if (ImGui::CollapsingHeader(demo.Label.c_str()))
  61. {
  62. ImGui::Indent();
  63. ShowModuleDemo(demo.DemoFilename, demo.DemoFunction, demo.ShowCode);
  64. ImGui::Unindent();
  65. }
  66. }
  67. }
  68. int main(int, char **)
  69. {
  70. printf("Dear ImGui Bundle Explorer - v%s build %s, compiled on %s at %s\n",
  71. IMGUI_BUNDLE_VERSION, IMGUI_BUNDLE_BUILD_NUMBER, __DATE__, __TIME__);
  72. ChdirBesideAssetsFolder();
  73. //###############################################################################################
  74. // Part 1: Define the runner params
  75. //###############################################################################################
  76. // Hello ImGui params (they hold the settings as well as the Gui callbacks)
  77. HelloImGui::RunnerParams runnerParams;
  78. // Window size and title
  79. runnerParams.appWindowParams.windowTitle = "Dear ImGui Bundle Explorer";
  80. runnerParams.appWindowParams.windowGeometry.size = {1400, 950};
  81. // Menu bar
  82. runnerParams.imGuiWindowParams.showMenuBar = true;
  83. runnerParams.imGuiWindowParams.showStatusBar = true;
  84. //###############################################################################################
  85. // Part 2: Define the application layout and windows
  86. //###############################################################################################
  87. // First, tell HelloImGui that we want full screen dock space (this will create "MainDockSpace")
  88. runnerParams.imGuiWindowParams.defaultImGuiWindowType = HelloImGui::DefaultImGuiWindowType::ProvideFullScreenDockSpace;
  89. // In this demo, we also demonstrate multiple viewports.
  90. // you can drag windows outside out the main window in order to put their content into new native windows
  91. runnerParams.imGuiWindowParams.enableViewports = true;
  92. //
  93. // Define our dockable windows : each window provide a Gui callback, and will be displayed
  94. // in a docking split.
  95. //
  96. std::vector<HelloImGui::DockableWindow> dockableWindows;
  97. #define DEMO_DETAILS(label, function_name) DemoDetails{ label, #function_name, function_name, false }
  98. #define DEMO_DETAILS_WITH_CODE(label, function_name) DemoDetails{ label, #function_name, function_name, true }
  99. // --- Standalone tabs (no grouping) ---
  100. std::vector<DemoDetails> standaloneDemos {
  101. DEMO_DETAILS("Intro", demo_imgui_bundle_intro),
  102. DEMO_DETAILS("Dear ImGui", demo_imgui_show_demo_window),
  103. DEMO_DETAILS("Demo Apps", demo_immapp_launcher),
  104. };
  105. for (const auto& demo : standaloneDemos)
  106. {
  107. HelloImGui::DockableWindow window;
  108. window.label = demo.Label;
  109. window.dockSpaceName = "MainDockSpace";
  110. window.GuiFunction = [demo]()
  111. {
  112. ShowModuleDemo(demo.DemoFilename, demo.DemoFunction, demo.ShowCode);
  113. };
  114. dockableWindows.push_back(window);
  115. }
  116. // --- Grouped tabs (sub-demos shown as collapsing headers) ---
  117. std::vector<DemoGroup> groups {
  118. { "Visualization", {
  119. DEMO_DETAILS( "Plots with ImPlot and ImPlot3D", demo_implot),
  120. DEMO_DETAILS( "ImmVision - Image analyzer", demo_immvision_launcher),
  121. DEMO_DETAILS( "Tex Inspect - Texture Inspector", demo_tex_inspect_launcher),
  122. }},
  123. { "Widgets", {
  124. DEMO_DETAILS_WITH_CODE("Misc Widgets - Knobs, Toggles, ...", demo_widgets),
  125. DEMO_DETAILS_WITH_CODE("Logger - Log Window Widget", demo_logger),
  126. DEMO_DETAILS( "ImGuizmo - Immediate Mode 3D Gizmo", demo_imguizmo_launcher),
  127. DEMO_DETAILS_WITH_CODE("Markdown - Rich Text Rendering", demo_imgui_md),
  128. DEMO_DETAILS ("Text Editor - Code Editing Widget", demo_text_edit),
  129. }},
  130. { "Tools", {
  131. DEMO_DETAILS( "Node Editor - Visual Node Graphs", demo_node_editor_launcher),
  132. DEMO_DETAILS_WITH_CODE("Themes - Style & Color Customization", demo_themes),
  133. DEMO_DETAILS( "ImAnim - Animation Library", demo_im_anim),
  134. #ifdef IMGUI_BUNDLE_WITH_NANOVG
  135. DEMO_DETAILS( "NanoVG - 2D Vector Drawing", demo_nanovg_launcher),
  136. #endif
  137. }},
  138. };
  139. for (const auto& group : groups)
  140. {
  141. HelloImGui::DockableWindow window;
  142. window.label = group.Label;
  143. window.dockSpaceName = "MainDockSpace";
  144. window.GuiFunction = [group]()
  145. {
  146. ShowGroupGui(group);
  147. };
  148. dockableWindows.push_back(window);
  149. }
  150. runnerParams.dockingParams.dockableWindows = dockableWindows;
  151. // the main gui is only responsible to give focus to ImGui Bundle dockable window
  152. auto showGui = [&runnerParams]
  153. {
  154. static int nbFrames = 0;
  155. if (nbFrames == 1)
  156. {
  157. // Focus cannot be given at frame 0, since some additional windows will
  158. // be created after (and will steal the focus)
  159. runnerParams.dockingParams.focusDockableWindow("Dear ImGui Bundle");
  160. }
  161. nbFrames += 1;
  162. };
  163. auto showStatusBar = []()
  164. {
  165. ImGui::SetNextItemWidth(ImGui::GetContentRegionAvail().x / 10.f);
  166. ImGui::SliderFloat("Font scale", & ImGui::GetStyle().FontScaleMain, 0.5f, 5.f);
  167. ImGui::SameLine(0.f, HelloImGui::EmSize(4.f));
  168. ImGui::TextDisabled("Dear ImGui Bundle Explorer - v" IMGUI_BUNDLE_VERSION " build " IMGUI_BUNDLE_BUILD_NUMBER);
  169. };
  170. runnerParams.callbacks.ShowStatus = showStatusBar;
  171. runnerParams.callbacks.ShowGui = showGui;
  172. runnerParams.useImGuiTestEngine = true;
  173. runnerParams.callbacks.SetupImGuiConfig = [] {
  174. ImGui::GetIO().ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard;
  175. };
  176. // ################################################################################################
  177. // Part 3: Run the app
  178. // ################################################################################################
  179. auto addons = ImmApp::AddOnsParams();
  180. addons.withMarkdown = true;
  181. addons.withLatex = true;
  182. addons.withNodeEditor = true;
  183. addons.withImplot = true;
  184. addons.withImplot3d = true;
  185. addons.withTexInspect = true;
  186. addons.withImAnim = true;
  187. runnerParams.iniClearPreviousSettings = true;
  188. ImmApp::Run(runnerParams, addons);
  189. return 0;
  190. }