diff --git a/imgui.cpp b/imgui.cpp index a056870f..8aed1508 100644 --- a/imgui.cpp +++ b/imgui.cpp @@ -1163,6 +1163,14 @@ float* ImGuiStorage::GetFloatPtr(ImGuiID key, float default_val) return &it->val_f; } +void* ImGuiStorage::GetVoidPtr(ImGuiID key) +{ + ImVector::iterator it = LowerBound(Data, key); + if (it == Data.end() || it->key != key) + it = Data.insert(it, Pair(key, (void*)0)); + return it->val_p; +} + // FIXME-OPT: Wasting CPU because all SetInt() are preceeded by GetInt() calls so we should have the result from lower_bound already in place. // However we only use SetInt() on explicit user action (so that's maximum once a frame) so the optimisation isn't much needed. void ImGuiStorage::SetInt(ImU32 key, int val) @@ -1187,6 +1195,17 @@ void ImGuiStorage::SetFloat(ImU32 key, float val) it->val_f = val; } +void ImGuiStorage::SetVoidPtr(ImU32 key, void* val) +{ + ImVector::iterator it = LowerBound(Data, key); + if (it == Data.end() || it->key != key) + { + Data.insert(it, Pair(key, val)); + return; + } + it->val_p = val; +} + void ImGuiStorage::SetAllInt(int v) { for (size_t i = 0; i < Data.size(); i++) diff --git a/imgui.h b/imgui.h index a1a31b2c..ef643e2f 100644 --- a/imgui.h +++ b/imgui.h @@ -641,9 +641,10 @@ struct ImGuiStorage struct Pair { ImGuiID key; - union { int val_i; float val_f; }; + union { int val_i; float val_f; void* val_p; }; Pair(ImGuiID _key, int _val_i) { key = _key; val_i = _val_i; } Pair(ImGuiID _key, float _val_f) { key = _key; val_f = _val_f; } + Pair(ImGuiID _key, void* _val_p) { key = _key; val_p = _val_p; } }; ImVector Data; @@ -660,6 +661,8 @@ struct ImGuiStorage IMGUI_API float GetFloat(ImGuiID key, float default_val = 0.0f) const; IMGUI_API void SetFloat(ImGuiID key, float val); IMGUI_API float* GetFloatPtr(ImGuiID key, float default_val = 0); + IMGUI_API void SetVoidPtr(ImGuiID key, void* val); + IMGUI_API void* GetVoidPtr(ImGuiID key); IMGUI_API void SetAllInt(int val); // Use on your own storage if you know only integer are being stored. };