ImGuiHelpers.h 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. // This file is part of libigl, a simple c++ geometry processing library.
  2. //
  3. // Copyright (C) 2018 Jérémie Dumas <jeremie.dumas@ens-lyon.org>
  4. //
  5. // This Source Code Form is subject to the terms of the Mozilla Public License
  6. // v. 2.0. If a copy of the MPL was not distributed with this file, You can
  7. // obtain one at http://mozilla.org/MPL/2.0/.
  8. #ifndef IGL_OPENGL_GLFW_IMGUI_IMGUIHELPERS_H
  9. #define IGL_OPENGL_GLFW_IMGUI_IMGUIHELPERS_H
  10. ////////////////////////////////////////////////////////////////////////////////
  11. #include <imgui/imgui.h>
  12. #include <vector>
  13. #include <string>
  14. #include <algorithm>
  15. ////////////////////////////////////////////////////////////////////////////////
  16. // Extend ImGui by populating its namespace directly
  17. //
  18. // Code snippets taken from there:
  19. // https://eliasdaler.github.io/using-imgui-with-sfml-pt2/
  20. namespace ImGui
  21. {
  22. static auto vector_getter = [](void* vec, int idx, const char** out_text)
  23. {
  24. auto& vector = *static_cast<std::vector<std::string>*>(vec);
  25. if (idx < 0 || idx >= static_cast<int>(vector.size())) { return false; }
  26. *out_text = vector.at(idx).c_str();
  27. return true;
  28. };
  29. inline bool Combo(const char* label, int* idx, std::vector<std::string>& values)
  30. {
  31. if (values.empty()) { return false; }
  32. return Combo(label, idx, vector_getter,
  33. static_cast<void*>(&values), values.size());
  34. }
  35. inline bool ListBox(const char* label, int* idx, std::vector<std::string>& values)
  36. {
  37. if (values.empty()) { return false; }
  38. return ListBox(label, idx, vector_getter,
  39. static_cast<void*>(&values), values.size());
  40. }
  41. inline bool InputText(const char* label, std::string &str, ImGuiInputTextFlags flags = 0, ImGuiTextEditCallback callback = NULL, void* user_data = NULL)
  42. {
  43. char buf[1024];
  44. std::fill_n(buf, 1024, 0);
  45. std::copy_n(str.begin(), std::min(1024, (int) str.size()), buf);
  46. if (ImGui::InputText(label, buf, 1024, flags, callback, user_data))
  47. {
  48. str = std::string(buf);
  49. return true;
  50. }
  51. return false;
  52. }
  53. } // namespace ImGui
  54. #endif // IGL_OPENGL_GLFW_IMGUI_IMGUIHELPERS_H