added wrapper for combo and listbox : can be used with std::vector<std::string>

This commit is contained in:
Lendemor 2018-01-05 18:34:23 +01:00
parent b01ae22a71
commit 46a96af3df
2 changed files with 27 additions and 0 deletions

View File

@ -1843,6 +1843,15 @@ ImGuiID ImGuiWindow::GetIDNoKeepAlive(const char* str, const char* str_end)
// Internal API exposed in imgui_internal.h // 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<std::vector<std::string>*>(vec);
if (idx < 0 || idx >= static_cast<int>(vector.size())) { return false; }
*out_text = vector.at(idx).c_str();
return true;
};
static void SetCurrentWindow(ImGuiWindow* window) static void SetCurrentWindow(ImGuiWindow* window)
{ {
ImGuiContext& g = *GImGui; ImGuiContext& g = *GImGui;
@ -8433,6 +8442,13 @@ static bool Items_SingleStringGetter(void* data, int idx, const char** out_text)
return true; return true;
} }
// Combo box helper allowing to pass a vector of std::string.
bool ImGui::Combo(const char* label, int* current_item, std::vector<std::string>& items)
{
if (items.empty()) { return false; }
return Combo(label, current_item, vector_getter, static_cast<void*>(&items), items.size());
}
// Combo box helper allowing to pass an array of strings. // 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) 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(); EndGroup();
} }
bool ImGui::ListBox(const char* label, int* current_item, std::vector<std::string>& items)
{
if (items.empty()) { return false; }
return ListBox(label, current_item, vector_getter, static_cast<void*>(&items), items.size());
}
bool ImGui::ListBox(const char* label, int* current_item, const char* const* items, int items_count, int height_items) 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); 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 #endif
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------

View File

@ -15,6 +15,8 @@
#include <stdarg.h> // va_list #include <stdarg.h> // va_list
#include <stddef.h> // ptrdiff_t, NULL #include <stddef.h> // ptrdiff_t, NULL
#include <string.h> // memset, memmove, memcpy, strlen, strchr, strcpy, strcmp #include <string.h> // memset, memmove, memcpy, strlen, strchr, strcpy, strcmp
#include <vector>
#include <string>
#define IMGUI_VERSION "1.50 WIP" #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 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, bool active);
IMGUI_API bool RadioButton(const char* label, int* v, int v_button); IMGUI_API bool RadioButton(const char* label, int* v, int v_button);
IMGUI_API bool Combo(const char* label, int* current_item, std::vector<std::string>& 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* 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, 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); 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 // 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 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 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<std::string>& 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, 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 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. 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.