PROTO::KLUDGE  0.1
Quick prototyping library for games using SDL and OpenGL.
ImGuiUtilities.hpp
Go to the documentation of this file.
1 #include <map>
2 #include <vector>
3 #include <algorithm>
4 
5 //stl container wrappers for ImGui
6 namespace pk
7 {
8 
9 namespace ImGuiUtilities
10 {
11 
20 void removeDuplicatesSorted(std::vector<std::string> &vec)
21 {
22  vec.erase( std::unique( vec.begin(), vec.end() ), vec.end() );
23 }
24 
33 void removeDuplicates(std::vector<std::string> &vec)
34 {
35  std::sort( vec.begin(), vec.end() );
36  vec.erase( unique( vec.begin(), vec.end() ), vec.end() );
37 }
38 
44 static void HelpMarker(const char* desc)
45 {
46  ImGui::TextDisabled("(?)");
47  if (ImGui::IsItemHovered())
48  {
49  ImGui::BeginTooltip();
50  ImGui::PushTextWrapPos(ImGui::GetFontSize() * 35.0f);
51  ImGui::TextUnformatted(desc);
52  ImGui::PopTextWrapPos();
53  ImGui::EndTooltip();
54  }
55 }
56 
63 static auto vector_getter = [](void* vec, int idx, const char** out_text)
64 {
65  auto& vector = *static_cast<std::vector<std::string>*>(vec);
66  if (idx < 0 || idx >= static_cast<int>(vector.size())) { return false; }
67  *out_text = vector.at(idx).c_str();
68  return true;
69 };
70 
76 bool Combo(const char* label, int* currIndex, std::vector<std::string>& values)
77 {
78  if (values.empty()) { return false; }
79  return ImGui::Combo(label, currIndex, vector_getter, static_cast<void*>(&values), values.size());
80 }
81 
87 bool ListBox(const char* label, int* currIndex, std::vector<std::string>& values)
88 {
89  if (values.empty()) { return false; }
90  return ImGui::ListBox(label, currIndex, vector_getter, static_cast<void*>(&values), values.size());
91 }
92 
93 }
94 
95 }
Definition: Game.hpp:7
void removeDuplicates(std::vector< std::string > &vec)
Sort a vector and remove duplicates from it. Useful for Combo and ListBox when duplicate entries are ...
Definition: ImGuiUtilities.hpp:33
bool Combo(const char *label, int *currIndex, std::vector< std::string > &values)
Wrapper function that takes a std::vector<std::string> and converts it in place to build an ImGui::Co...
Definition: ImGuiUtilities.hpp:76
void removeDuplicatesSorted(std::vector< std::string > &vec)
Removes duplicate strings from a sorted vector. Useful for Combo and ListBox when duplicate entries a...
Definition: ImGuiUtilities.hpp:20
bool ListBox(const char *label, int *currIndex, std::vector< std::string > &values)
Wrapper function that takes a std::vector<std::string> and converts it in place to build an ImGui::Li...
Definition: ImGuiUtilities.hpp:87