Skip to content

Commit e28b107

Browse files
committed
Added IsItemEdited() to query if the last item modified its value (or was pressed). This is equivalent to the bool returned by most widgets. It is useful in some situation e.g. using InputText() with ImGuiInputTextFlags_EnterReturnsTrue. (#2034)
1 parent abaa274 commit e28b107

File tree

4 files changed

+17
-2
lines changed

4 files changed

+17
-2
lines changed

CHANGELOG.txt

+2
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@ Other Changes:
5454
- Window: Allow menu and popups windows from ignoring the style.WindowMinSize values so short menus/popups are not padded. (#1909)
5555
- Window: Added global io.OptResizeWindowsFromEdges option to enable resizing windows from their edges and from the lower-left corner. (#1495)
5656
- Window: Collapse button shows hovering highlight + clicking and dragging on it allows to drag the window as well.
57+
- Added IsItemEdited() to query if the last item modified its value (or was pressed). This is equivalent to the bool returned by most widgets.
58+
It is useful in some situation e.g. using InputText() with ImGuiInputTextFlags_EnterReturnsTrue. (#2034)
5759
- InputText: Added support for buffer size/capacity changes via the ImGuiInputTextFlags_CallbackResize flag. (#2006, #1443, #1008).
5860
- InputText: Fixed not tracking the cursor horizontally When modifying the text buffer through a callback.
5961
- InputText: Fixed minor off-by-one issue when submitting a buffer size smaller than the initial zero-terminated buffer contents.

imgui.cpp

+11-1
Original file line numberDiff line numberDiff line change
@@ -5246,6 +5246,12 @@ bool ImGui::IsItemVisible()
52465246
return window->ClipRect.Overlaps(window->DC.LastItemRect);
52475247
}
52485248

5249+
bool ImGui::IsItemEdited()
5250+
{
5251+
ImGuiWindow* window = GetCurrentWindowRead();
5252+
return (window->DC.LastItemStatusFlags & ImGuiItemStatusFlags_ValueChanged) != 0;
5253+
}
5254+
52495255
// Allow last item to be overlapped by a subsequent item. Both may be activated during the same frame before the later one takes priority.
52505256
void ImGui::SetItemAllowOverlap()
52515257
{
@@ -11954,6 +11960,7 @@ bool ImGui::ListBox(const char* label, int* current_item, bool (*items_getter)(v
1195411960
return false;
1195511961

1195611962
// Assume all items have even height (= 1 line of text). If you need items of different or variable sizes you can create a custom version of ListBox() in your code without using the clipper.
11963+
ImGuiContext& g = *GImGui;
1195711964
bool value_changed = false;
1195811965
ImGuiListClipper clipper(items_count, GetTextLineHeightWithSpacing()); // We know exactly our line height here so we pass it as a minor optimization, but generally you don't need to.
1195911966
while (clipper.Step())
@@ -11975,6 +11982,9 @@ bool ImGui::ListBox(const char* label, int* current_item, bool (*items_getter)(v
1197511982
PopID();
1197611983
}
1197711984
ListBoxFooter();
11985+
if (value_changed)
11986+
MarkItemValueChanged(g.CurrentWindow->DC.LastItemId);
11987+
1197811988
return value_changed;
1197911989
}
1198011990

@@ -13268,7 +13278,7 @@ void ImGui::EndGroup()
1326813278
}
1326913279

1327013280
// If the current ActiveId was declared within the boundary of our group, we copy it to LastItemId so IsItemActive(), IsItemDeactivated() etc. will be functional on the entire group.
13271-
// It would be be neater if we replaced window.DC.LastItemId by e.g. 'bool LastItemIsActive', but put a little more burden on individual widgets.
13281+
// It would be be neater if we replaced window.DC.LastItemId by e.g. 'bool LastItemIsActive', but would put a little more burden on individual widgets.
1327213282
// (and if you grep for LastItemId you'll notice it is only used in that context.
1327313283
if ((group_data.BackupActiveIdIsAlive != g.ActiveId) && (g.ActiveIdIsAlive == g.ActiveId) && g.ActiveId) // && g.ActiveIdWindow->RootWindow == window->RootWindow)
1327413284
window->DC.LastItemId = g.ActiveId;

imgui.h

+1
Original file line numberDiff line numberDiff line change
@@ -521,6 +521,7 @@ namespace ImGui
521521
IMGUI_API bool IsItemFocused(); // is the last item focused for keyboard/gamepad navigation?
522522
IMGUI_API bool IsItemClicked(int mouse_button = 0); // is the last item clicked? (e.g. button/node just clicked on) == IsMouseClicked(mouse_button) && IsItemHovered()
523523
IMGUI_API bool IsItemVisible(); // is the last item visible? (items may be out of sight because of clipping/scrolling)
524+
IMGUI_API bool IsItemEdited(); // did the last item modify its underlying value this frame? or was pressed? This is generally the same as the "bool" return value of many widgets.
524525
IMGUI_API bool IsItemDeactivated(); // was the last item just made inactive (item was previously active). Useful for Undo/Redo patterns with widgets that requires continuous editing.
525526
IMGUI_API bool IsItemDeactivatedAfterChange(); // was the last item just made inactive and made a value change when it was active? (e.g. Slider/Drag moved). Useful for Undo/Redo patterns with widgets that requires continuous editing. Note that you may get false positives (some widgets such as Combo()/ListBox()/Selectable() will return true even when clicking an already selected item).
526527
IMGUI_API bool IsAnyItemHovered();

imgui_demo.cpp

+3-1
Original file line numberDiff line numberDiff line change
@@ -1275,7 +1275,7 @@ void ImGui::ShowDemoWindow(bool* p_open)
12751275
ImGui::TreePop();
12761276
}
12771277

1278-
if (ImGui::TreeNode("Active, Focused, Hovered & Focused Tests"))
1278+
if (ImGui::TreeNode("Active, Focused and Hovered Tests"))
12791279
{
12801280
// Display the value of IsItemHovered() and other common item state functions. Note that the flags can be combined.
12811281
// (because BulletText is an item itself and that would affect the output of IsItemHovered() we pass all state in a single call to simplify the code).
@@ -1304,6 +1304,7 @@ void ImGui::ShowDemoWindow(bool* p_open)
13041304
"IsItemHovered(_AllowWhenOverlapped) = %d\n"
13051305
"IsItemHovered(_RectOnly) = %d\n"
13061306
"IsItemActive() = %d\n"
1307+
"IsItemEdited() = %d\n"
13071308
"IsItemDeactivated() = %d\n"
13081309
"IsItemDeactivatedAfterChange() = %d\n"
13091310
"IsItemVisible() = %d\n",
@@ -1315,6 +1316,7 @@ void ImGui::ShowDemoWindow(bool* p_open)
13151316
ImGui::IsItemHovered(ImGuiHoveredFlags_AllowWhenOverlapped),
13161317
ImGui::IsItemHovered(ImGuiHoveredFlags_RectOnly),
13171318
ImGui::IsItemActive(),
1319+
ImGui::IsItemEdited(),
13181320
ImGui::IsItemDeactivated(),
13191321
ImGui::IsItemDeactivatedAfterChange(),
13201322
ImGui::IsItemVisible()

0 commit comments

Comments
 (0)