From 37d585ef87f98bd1c2b47dcdfa8b8bd7e7b83158 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=A9mie=20Dumas?= Date: Sun, 11 Feb 2018 11:20:58 -0500 Subject: [PATCH] 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). --- include/igl/opengl/glfw/imgui/ImGuiMenu.cpp | 12 +++++---- include/igl/opengl/glfw/imgui/ImGuiMenu.h | 8 ++++-- tutorial/106_ViewerMenu/main.cpp | 7 +++-- tutorial/107_MultipleMeshes/main.cpp | 29 +++++++++------------ 4 files changed, 29 insertions(+), 27 deletions(-) diff --git a/include/igl/opengl/glfw/imgui/ImGuiMenu.cpp b/include/igl/opengl/glfw/imgui/ImGuiMenu.cpp index e5c17012f..3b84edd73 100644 --- a/include/igl/opengl/glfw/imgui/ImGuiMenu.cpp +++ b/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); diff --git a/include/igl/opengl/glfw/imgui/ImGuiMenu.h b/include/igl/opengl/glfw/imgui/ImGuiMenu.h index 67f3bb966..4acab1cae 100644 --- a/include/igl/opengl/glfw/imgui/ImGuiMenu.h +++ b/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 draw_viewer_menu_func; + std::function draw_custom_window_func; + + IGL_INLINE void draw_labels_window(); IGL_INLINE void draw_labels(const igl::opengl::ViewerData &data); diff --git a/tutorial/106_ViewerMenu/main.cpp b/tutorial/106_ViewerMenu/main.cpp index 0b6732934..5d16a5548 100755 --- a/tutorial/106_ViewerMenu/main.cpp +++ b/tutorial/106_ViewerMenu/main.cpp @@ -6,7 +6,7 @@ #include #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 diff --git a/tutorial/107_MultipleMeshes/main.cpp b/tutorial/107_MultipleMeshes/main.cpp index e5278291e..864af23a8 100755 --- a/tutorial/107_MultipleMeshes/main.cpp +++ b/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().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().name.c_str(); }, + viewer.data_list.size()) + || last_colored_index != viewer.selected_data_index) + { + update_colors(viewer); + } + }; + // Color each mesh differently update_colors(viewer);