Advertisement
eliasdaler

imgui-stl

Jun 10th, 2016
344
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.48 KB | None | 0 0
  1. #include "imgui-STL.h"
  2. #include <imgui.h>
  3.  
  4. namespace ImGui
  5. {
  6.  
  7. // well, at least it is possible..
  8. static auto vector_getter = [](void* vec, int idx, const char** out_text)
  9. {
  10.     auto& vector = *reinterpret_cast<std::vector<std::string>*>(vec);
  11.     if (idx < 0 || idx >= static_cast<int>(vector.size())) { return false; }
  12.     *out_text = vector.at(idx).c_str();
  13.     return true;
  14. };
  15.  
  16. bool Combo(const char* label, int* currIndex, std::vector<std::string>& values)
  17. {
  18.     if (values.empty()) { return false; }
  19.     return Combo(label, currIndex, vector_getter, reinterpret_cast<void*>(&values), values.size());
  20. }
  21.  
  22. bool ListBox(const char* label, int* currIndex, std::vector<std::string>& values)
  23. {
  24.     if (values.empty()) { return false; }
  25.     return ListBox(label, currIndex, vector_getter, reinterpret_cast<void*>(&values), values.size());
  26. }
  27.  
  28. bool InputText(const char* label, std::string& str, size_t maxInputSize,
  29.     ImGuiInputTextFlags flags, ImGuiTextEditCallback callback, void* user_data)
  30. {
  31.     if (str.size() > maxInputSize) { // too large for editing
  32.         ImGui::Text(str.c_str());
  33.         return false;
  34.     }
  35.  
  36.     std::string buffer(str);
  37.     buffer.resize(maxInputSize);
  38.     bool changed = ImGui::InputText(label, &buffer[0], maxInputSize, flags, callback, user_data);
  39.     // using string as char array
  40.     if (changed) {
  41.         auto i = buffer.find_first_of('\0');
  42.         str = buffer.substr(0u, i);
  43.     }
  44.     return changed;
  45. }
  46.  
  47. } // end of namespace ImGui
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement