From 46a96af3df414dac429f00968538b76f15909011 Mon Sep 17 00:00:00 2001 From: Lendemor Date: Fri, 5 Jan 2018 18:34:23 +0100 Subject: [PATCH] added wrapper for combo and listbox : can be used with std::vector --- src/imgui.cpp | 23 +++++++++++++++++++++++ src/imgui.h | 4 ++++ 2 files changed, 27 insertions(+) diff --git a/src/imgui.cpp b/src/imgui.cpp index ff4617b..6f25662 100644 --- a/src/imgui.cpp +++ b/src/imgui.cpp @@ -1843,6 +1843,15 @@ ImGuiID ImGuiWindow::GetIDNoKeepAlive(const char* str, const char* str_end) // Internal API exposed in imgui_internal.h //----------------------------------------------------------------------------- +//used by listbox/combo wrapper for compatibility with std::vector +static auto vector_getter = [](void* vec, int idx, const char** out_text) +{ + auto& vector = *static_cast*>(vec); + if (idx < 0 || idx >= static_cast(vector.size())) { return false; } + *out_text = vector.at(idx).c_str(); + return true; +}; + static void SetCurrentWindow(ImGuiWindow* window) { ImGuiContext& g = *GImGui; @@ -8433,6 +8442,13 @@ static bool Items_SingleStringGetter(void* data, int idx, const char** out_text) return true; } +// Combo box helper allowing to pass a vector of std::string. +bool ImGui::Combo(const char* label, int* current_item, std::vector& items) +{ + if (items.empty()) { return false; } + return Combo(label, current_item, vector_getter, static_cast(&items), items.size()); +} + // Combo box helper allowing to pass an array of strings. bool ImGui::Combo(const char* label, int* current_item, const char* const* items, int items_count, int height_in_items) { @@ -8711,6 +8727,12 @@ void ImGui::ListBoxFooter() EndGroup(); } +bool ImGui::ListBox(const char* label, int* current_item, std::vector& items) +{ + if (items.empty()) { return false; } + return ListBox(label, current_item, vector_getter, static_cast(&items), items.size()); +} + bool ImGui::ListBox(const char* label, int* current_item, const char* const* items, int items_count, int height_items) { const bool value_changed = ListBox(label, current_item, Items_ArrayGetter, (void*)items, items_count, height_items); @@ -9855,3 +9877,4 @@ void ImGui::ShowMetricsWindow(bool* p_open) #endif //----------------------------------------------------------------------------- + diff --git a/src/imgui.h b/src/imgui.h index bf68717..44f7770 100644 --- a/src/imgui.h +++ b/src/imgui.h @@ -15,6 +15,8 @@ #include // va_list #include // ptrdiff_t, NULL #include // memset, memmove, memcpy, strlen, strchr, strcpy, strcmp +#include +#include #define IMGUI_VERSION "1.50 WIP" @@ -265,6 +267,7 @@ namespace ImGui IMGUI_API bool CheckboxFlags(const char* label, unsigned int* flags, unsigned int flags_value); IMGUI_API bool RadioButton(const char* label, bool active); IMGUI_API bool RadioButton(const char* label, int* v, int v_button); + IMGUI_API bool Combo(const char* label, int* current_item, std::vector& items); IMGUI_API bool Combo(const char* label, int* current_item, const char* const* items, int items_count, int height_in_items = -1); IMGUI_API bool Combo(const char* label, int* current_item, const char* items_separated_by_zeros, int height_in_items = -1); // separate items with \0, end item-list with \0\0 IMGUI_API bool Combo(const char* label, int* current_item, bool (*items_getter)(void* data, int idx, const char** out_text), void* data, int items_count, int height_in_items = -1); @@ -339,6 +342,7 @@ namespace ImGui // Widgets: Selectable / Lists IMGUI_API bool Selectable(const char* label, bool selected = false, ImGuiSelectableFlags flags = 0, const ImVec2& size = ImVec2(0,0)); // size.x==0.0: use remaining width, size.x>0.0: specify width. size.y==0.0: use label height, size.y>0.0: specify height IMGUI_API bool Selectable(const char* label, bool* p_selected, ImGuiSelectableFlags flags = 0, const ImVec2& size = ImVec2(0,0)); + IMGUI_API bool ListBox(const char* label, int* current_item, std::vector& items); IMGUI_API bool ListBox(const char* label, int* current_item, const char* const* items, int items_count, int height_in_items = -1); IMGUI_API bool ListBox(const char* label, int* current_item, bool (*items_getter)(void* data, int idx, const char** out_text), void* data, int items_count, int height_in_items = -1); IMGUI_API bool ListBoxHeader(const char* label, const ImVec2& size = ImVec2(0,0)); // use if you want to reimplement ListBox() will custom data or interactions. make sure to call ListBoxFooter() afterwards.