ImGuiHelpers.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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 "ImGuiTraits.h"
  12. #include <imgui/imgui.h>
  13. #include <vector>
  14. #include <string>
  15. #include <algorithm>
  16. #include <functional>
  17. #include <cstddef>
  18. ////////////////////////////////////////////////////////////////////////////////
  19. // Extend ImGui by populating its namespace directly
  20. //
  21. // Code snippets taken from there:
  22. // https://eliasdaler.github.io/using-imgui-with-sfml-pt2/
  23. namespace ImGui
  24. {
  25. static auto vector_getter = [](void* vec, int idx, const char** out_text)
  26. {
  27. auto& vector = *static_cast<std::vector<std::string>*>(vec);
  28. if (idx < 0 || idx >= static_cast<int>(vector.size())) { return false; }
  29. *out_text = vector.at(idx).c_str();
  30. return true;
  31. };
  32. inline bool Combo(const char* label, int* idx, std::vector<std::string>& values)
  33. {
  34. if (values.empty()) { return false; }
  35. return Combo(label, idx, vector_getter,
  36. static_cast<void*>(&values), values.size());
  37. }
  38. inline bool Combo(const char* label, int* idx, std::function<const char *(int)> getter, int items_count)
  39. {
  40. auto func = [](void* data, int i, const char** out_text) {
  41. auto &getter = *reinterpret_cast<std::function<const char *(int)> *>(data);
  42. const char *s = getter(i);
  43. if (s) { *out_text = s; return true; }
  44. else { return false; }
  45. };
  46. return Combo(label, idx, func, reinterpret_cast<void *>(&getter), items_count);
  47. }
  48. inline bool ListBox(const char* label, int* idx, std::vector<std::string>& values)
  49. {
  50. if (values.empty()) { return false; }
  51. return ListBox(label, idx, vector_getter,
  52. static_cast<void*>(&values), values.size());
  53. }
  54. inline bool InputText(const char* label, std::string &str, ImGuiInputTextFlags flags = 0, ImGuiTextEditCallback callback = NULL, void* user_data = NULL)
  55. {
  56. char buf[1024];
  57. std::fill_n(buf, 1024, 0);
  58. std::copy_n(str.begin(), std::min(1024, (int) str.size()), buf);
  59. if (ImGui::InputText(label, buf, 1024, flags, callback, user_data))
  60. {
  61. str = std::string(buf);
  62. return true;
  63. }
  64. return false;
  65. }
  66. // template<typename T>
  67. // inline bool DragScalar(const char *label, T* value, void* v, float v_speed, const void* v_min = NULL, const void* v_max = NULL, const char* format = NULL, float power = 1.0f)
  68. // {
  69. // const char *fmt = format;
  70. // if (format == nullptr) {
  71. // fmt = ImGuiDataTypeTraits<T>::format;
  72. // }
  73. // return DragScalar(label, ImGuiDataTypeTraits<T>::value, value, &min, &max, fmt);
  74. // }
  75. // template<typename T>
  76. // inline bool InputScalar(const char *label, T* value, T min = 0, T max = 0, const char* format = nulltptr)
  77. // {
  78. // const char *fmt = format;
  79. // if (format == nullptr) {
  80. // fmt = ImGuiDataTypeTraits<T>::format;
  81. // }
  82. // return InputScalar(label, ImGuiDataTypeTraits<T>::value, value, &min, &max, fmt);
  83. // }
  84. template<typename T>
  85. inline bool SliderScalar(const char *label, T* value, T min = 0, T max = 0, const char* format = "")
  86. {
  87. const char *fmt = format;
  88. if (format == nullptr) {
  89. fmt = ImGuiDataTypeTraits<T>::format;
  90. }
  91. return SliderScalar(label, ImGuiDataTypeTraits<T>::value, value, &min, &max, fmt);
  92. }
  93. template<typename Getter, typename Setter>
  94. inline bool Checkbox(const char* label, Getter get, Setter set)
  95. {
  96. bool value = get();
  97. bool ret = ImGui::Checkbox(label, &value);
  98. set(value);
  99. return ret;
  100. }
  101. } // namespace ImGui
  102. #endif // IGL_OPENGL_GLFW_IMGUI_IMGUIHELPERS_H