ImGuiHelpers.h 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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. #include <functional>
  16. ////////////////////////////////////////////////////////////////////////////////
  17. // Extend ImGui by populating its namespace directly
  18. //
  19. // Code snippets taken from there:
  20. // https://eliasdaler.github.io/using-imgui-with-sfml-pt2/
  21. namespace ImGui
  22. {
  23. static auto vector_getter = [](void* vec, int idx, const char** out_text)
  24. {
  25. auto& vector = *static_cast<std::vector<std::string>*>(vec);
  26. if (idx < 0 || idx >= static_cast<int>(vector.size())) { return false; }
  27. *out_text = vector.at(idx).c_str();
  28. return true;
  29. };
  30. inline bool Combo(const char* label, int* idx, std::vector<std::string>& values)
  31. {
  32. if (values.empty()) { return false; }
  33. return Combo(label, idx, vector_getter,
  34. static_cast<void*>(&values), values.size());
  35. }
  36. inline bool Combo(const char* label, int* idx, std::function<const char *(int)> getter, int items_count)
  37. {
  38. auto func = [](void* data, int i, const char** out_text) {
  39. auto &getter = *reinterpret_cast<std::function<const char *(int)> *>(data);
  40. const char *s = getter(i);
  41. if (s) { *out_text = s; return true; }
  42. else { return false; }
  43. };
  44. return Combo(label, idx, func, reinterpret_cast<void *>(&getter), items_count);
  45. }
  46. inline bool ListBox(const char* label, int* idx, std::vector<std::string>& values)
  47. {
  48. if (values.empty()) { return false; }
  49. return ListBox(label, idx, vector_getter,
  50. static_cast<void*>(&values), values.size());
  51. }
  52. inline bool InputText(const char* label, std::string &str, ImGuiInputTextFlags flags = 0, ImGuiTextEditCallback callback = NULL, void* user_data = NULL)
  53. {
  54. char buf[1024];
  55. std::fill_n(buf, 1024, 0);
  56. std::copy_n(str.begin(), std::min(1024, (int) str.size()), buf);
  57. if (ImGui::InputText(label, buf, 1024, flags, callback, user_data))
  58. {
  59. str = std::string(buf);
  60. return true;
  61. }
  62. return false;
  63. }
  64. } // namespace ImGui
  65. #endif // IGL_OPENGL_GLFW_IMGUI_IMGUIHELPERS_H