Skip to content

Commit

Permalink
[Bundle]: add GetID_AssertUnique_DisableInScope / use in TempInputTex…
Browse files Browse the repository at this point in the history
…t and TempInputScalar
  • Loading branch information
pthom committed Jun 14, 2024
1 parent 40ecebb commit 97622b0
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 1 deletion.
13 changes: 12 additions & 1 deletion imgui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8894,6 +8894,17 @@ ImGuiID ImGuiWindow::GetID(int n)
return id;
}

int gDisable_GetID_AssertUnique = 0;
ImGuiWindow::GetID_AssertUnique_DisableInScope::GetID_AssertUnique_DisableInScope::GetID_AssertUnique_DisableInScope()
{
gDisable_GetID_AssertUnique += 1;
}
ImGuiWindow::GetID_AssertUnique_DisableInScope::GetID_AssertUnique_DisableInScope::~GetID_AssertUnique_DisableInScope()
{
gDisable_GetID_AssertUnique -= 1;
}
static bool Disable_GetID_AssertUnique() { return gDisable_GetID_AssertUnique > 0; }

// Addition to ImGui Bundle: a version of GetID that warns if the ID was already used
IMGUI_API ImGuiID ImGuiWindow::GetID_AssertUnique(const char* str_id)
{
Expand Down Expand Up @@ -8930,7 +8941,7 @@ IMGUI_API ImGuiID ImGuiWindow::GetID_AssertUnique(const char* str_id)
sIdsThisFrame.Size = 0;
}

if (sIdsThisFrame.contains(id))
if (sIdsThisFrame.contains(id) && !Disable_GetID_AssertUnique())
IM_ASSERT(false && "Either your widgets names/ids must be distinct, or you shall call ImGui::PushID before reusing an id");

if (sIdsThisFrame.size() < sMaxCacheSize - 1)
Expand Down
5 changes: 5 additions & 0 deletions imgui_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -2886,6 +2886,11 @@ struct IMGUI_API ImGuiWindow

// Addition to ImGui Bundle: a version of GetID that warns if the ID was already used
IMGUI_API ImGuiID GetID_AssertUnique(const char* str_id); // (Specific to ImGui Bundle) Calculate unique ID (hash of whole ID stack + given parameter). Will warn if the ID was already used, and advise to call ImGui::PushID() before
// Instantiate GetID_AssertUnique_DisableInScope in a function or scope to temporarily disable the check
struct GetID_AssertUnique_DisableInScope{
GetID_AssertUnique_DisableInScope();
~GetID_AssertUnique_DisableInScope();
};

ImGuiID GetIDFromRectangle(const ImRect& r_abs);

Expand Down
7 changes: 7 additions & 0 deletions imgui_widgets.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3424,6 +3424,9 @@ int ImParseFormatPrecision(const char* fmt, int default_precision)
// FIXME: Facilitate using this in variety of other situations.
bool ImGui::TempInputText(const ImRect& bb, ImGuiID id, const char* label, char* buf, int buf_size, ImGuiInputTextFlags flags)
{
// Since this method reuses an existing ID, we allow it in its scope
ImGuiWindow::GetID_AssertUnique_DisableInScope disableAssertUnique;

// On the first frame, g.TempInputTextId == 0, then on subsequent frames it becomes == id.
// We clear ActiveID on the first frame to allow the InputText() taking it back.
ImGuiContext& g = *GImGui;
Expand All @@ -3447,6 +3450,9 @@ bool ImGui::TempInputText(const ImRect& bb, ImGuiID id, const char* label, char*
// However this may not be ideal for all uses, as some user code may break on out of bound values.
bool ImGui::TempInputScalar(const ImRect& bb, ImGuiID id, const char* label, ImGuiDataType data_type, void* p_data, const char* format, const void* p_clamp_min, const void* p_clamp_max)
{
// Since this method reuses an existing ID, we allow it in its scope
ImGuiWindow::GetID_AssertUnique_DisableInScope disableAssertUnique;

// FIXME: May need to clarify display behavior if format doesn't contain %.
// "%d" -> "%d" / "There are %d items" -> "%d" / "items" -> "%d" (fallback). Also see #6405
const ImGuiDataTypeInfo* type_info = DataTypeGetInfo(data_type);
Expand Down Expand Up @@ -3482,6 +3488,7 @@ bool ImGui::TempInputScalar(const ImRect& bb, ImGuiID id, const char* label, ImG
if (value_changed)
MarkItemEdited(id);
}

return value_changed;
}

Expand Down

0 comments on commit 97622b0

Please sign in to comment.