Sfoglia il codice sorgente

Made ImGuiMenu plugin more flexible for customization.

Now viewer menu can be overriden with lambda function (for simple
override), or virtual methods (for more complex situations).


Former-commit-id: 37d585ef87f98bd1c2b47dcdfa8b8bd7e7b83158
Jérémie Dumas 7 anni fa
parent
commit
00961ce5f3

+ 7 - 5
include/igl/opengl/glfw/imgui/ImGuiMenu.cpp

@@ -140,7 +140,7 @@ IGL_INLINE bool ImGuiMenu::key_up(int key, int modifiers)
 IGL_INLINE void ImGuiMenu::draw_menu()
 {
   // Text labels
-  draw_labels_menu();
+  draw_labels_window();
 
   // Viewer settings
   float menu_width = 180.f * menu_scaling();
@@ -154,12 +154,14 @@ IGL_INLINE void ImGuiMenu::draw_menu()
       | ImGuiWindowFlags_AlwaysAutoResize
   );
   ImGui::PushItemWidth(ImGui::GetWindowWidth() * 0.4f);
-  draw_viewer_menu();
+  if (draw_viewer_menu_func) { draw_viewer_menu_func(); }
+  else { draw_viewer_menu(); }
   ImGui::PopItemWidth();
   ImGui::End();
 
-  // Other menus
-  draw_other_menu();
+  // Other windows
+  if (draw_custom_window_func) { draw_custom_window_func(); }
+  else { draw_custom_window(); }
 }
 
 IGL_INLINE void ImGuiMenu::draw_viewer_menu()
@@ -276,7 +278,7 @@ IGL_INLINE void ImGuiMenu::draw_viewer_menu()
   }
 }
 
-IGL_INLINE void ImGuiMenu::draw_labels_menu()
+IGL_INLINE void ImGuiMenu::draw_labels_window()
 {
   // Text labels
   ImGui::SetNextWindowPos(ImVec2(0,0), ImGuiSetCond_Always);

+ 6 - 2
include/igl/opengl/glfw/imgui/ImGuiMenu.h

@@ -73,9 +73,13 @@ public:
 
   IGL_INLINE virtual void draw_viewer_menu();
 
-  IGL_INLINE virtual void draw_other_menu() { }
+  IGL_INLINE virtual void draw_custom_window() { }
 
-  IGL_INLINE void draw_labels_menu();
+  // Easy-to-customize callbacks
+  std::function<void(void)> draw_viewer_menu_func;
+  std::function<void(void)> draw_custom_window_func;
+
+  IGL_INLINE void draw_labels_window();
 
   IGL_INLINE void draw_labels(const igl::opengl::ViewerData &data);
 

+ 3 - 4
tutorial/106_ViewerMenu/main.cpp

@@ -6,7 +6,7 @@
 #include <iostream>
 #include "tutorial_shared_path.h"
 
-class MyMenu : public igl::opengl::glfw::imgui::ImGuiMenu
+class CustomMenu : public igl::opengl::glfw::imgui::ImGuiMenu
 {
   float floatVariable = 0.1f; // Shared between two menus
 
@@ -60,7 +60,7 @@ class MyMenu : public igl::opengl::glfw::imgui::ImGuiMenu
     }
   }
 
-  virtual void draw_other_menu() override
+  virtual void draw_custom_window() override
   {
     // Define next window position + size
     ImGui::SetNextWindowPos(ImVec2(180.f * menu_scaling(), 10), ImGuiSetCond_FirstUseEver);
@@ -95,8 +95,7 @@ int main(int argc, char *argv[])
   igl::opengl::glfw::Viewer viewer;
 
   // Attach a custom menu
-  MyMenu menu;
-  // viewer.core.is_animating = true;
+  CustomMenu menu;
   viewer.plugins.push_back(&menu);
 
   // Plot the mesh

+ 13 - 16
tutorial/107_MultipleMeshes/main.cpp

@@ -25,21 +25,6 @@ void update_colors(igl::opengl::glfw::Viewer &viewer)
   last_colored_index = viewer.selected_data_index;
 }
 
-// Menu for selecting a mesh
-class MyMenu : public igl::opengl::glfw::imgui::ImGuiMenu
-{
-  virtual void draw_viewer_menu() override
-  {
-    if (ImGui::Combo("Selected Mesh", (int *) &viewer->selected_data_index,
-          [&](int i) { return viewer->data_list[i].attr<MeshData>().name.c_str(); },
-          viewer->data_list.size())
-      || last_colored_index != viewer->selected_data_index)
-    {
-      update_colors(*viewer);
-    }
-  }
-};
-
 int main(int argc, char * argv[])
 {
   igl::opengl::glfw::Viewer viewer;
@@ -52,9 +37,21 @@ int main(int argc, char * argv[])
   }
 
   // Attach a custom menu
-  MyMenu menu;
+  igl::opengl::glfw::imgui::ImGuiMenu menu;
   viewer.plugins.push_back(&menu);
 
+  // Customize default menu
+  menu.draw_viewer_menu_func = [&]()
+  {
+    if (ImGui::Combo("Selected Mesh", (int *) &viewer.selected_data_index,
+          [&](int i) { return viewer.data_list[i].attr<MeshData>().name.c_str(); },
+          viewer.data_list.size())
+      || last_colored_index != viewer.selected_data_index)
+    {
+      update_colors(viewer);
+    }
+  };
+
   // Color each mesh differently
   update_colors(viewer);