mirror of
https://github.com/ocornut/imgui.git
synced 2025-12-28 00:54:34 +00:00
Fixed IsItemDeactivated(), IsItemDeactivatedAfterEdit() to work when interrupted before/after the active id is submitted. (#5184, #5904, #6766, #8303, #8004)
This commit is contained in:
40
imgui.cpp
40
imgui.cpp
@@ -3930,9 +3930,7 @@ ImGuiContext::ImGuiContext(ImFontAtlas* shared_font_atlas)
|
||||
ActiveIdSource = ImGuiInputSource_None;
|
||||
ActiveIdMouseButton = -1;
|
||||
ActiveIdPreviousFrame = 0;
|
||||
ActiveIdPreviousFrameIsAlive = false;
|
||||
ActiveIdPreviousFrameHasBeenEditedBefore = false;
|
||||
ActiveIdPreviousFrameWindow = NULL;
|
||||
memset(&DeactivatedItemData, 0, sizeof(DeactivatedItemData));
|
||||
memset(&ActiveIdValueOnActivation, 0, sizeof(ActiveIdValueOnActivation));
|
||||
LastActiveId = 0;
|
||||
LastActiveIdTimer = 0.0f;
|
||||
@@ -4175,7 +4173,7 @@ void ImGui::Shutdown()
|
||||
g.WindowsById.Clear();
|
||||
g.NavWindow = NULL;
|
||||
g.HoveredWindow = g.HoveredWindowUnderMovingWindow = NULL;
|
||||
g.ActiveIdWindow = g.ActiveIdPreviousFrameWindow = NULL;
|
||||
g.ActiveIdWindow = NULL;
|
||||
g.MovingWindow = NULL;
|
||||
|
||||
g.KeysRoutingTable.Clear();
|
||||
@@ -4359,6 +4357,13 @@ void ImGui::SetActiveID(ImGuiID id, ImGuiWindow* window)
|
||||
g.MovingWindow = NULL;
|
||||
}
|
||||
|
||||
// Store deactivate data
|
||||
ImGuiDeactivatedItemData* deactivated_data = &g.DeactivatedItemData;
|
||||
deactivated_data->ID = g.ActiveId;
|
||||
deactivated_data->ElapseFrame = (g.LastItemData.ID == g.ActiveId) ? g.FrameCount : g.FrameCount + 1; // FIXME: OK to use LastItemData?
|
||||
deactivated_data->HasBeenEditedBefore = g.ActiveIdHasBeenEditedBefore;
|
||||
deactivated_data->IsAlive = (g.ActiveIdIsAlive == g.ActiveId);
|
||||
|
||||
// This could be written in a more general way (e.g associate a hook to ActiveId),
|
||||
// but since this is currently quite an exception we'll leave it as is.
|
||||
// One common scenario leading to this is: pressing Key ->NavMoveRequestApplyResult() -> ClearActiveID()
|
||||
@@ -5189,11 +5194,8 @@ void ImGui::NewFrame()
|
||||
g.ActiveIdTimer += g.IO.DeltaTime;
|
||||
g.LastActiveIdTimer += g.IO.DeltaTime;
|
||||
g.ActiveIdPreviousFrame = g.ActiveId;
|
||||
g.ActiveIdPreviousFrameWindow = g.ActiveIdWindow;
|
||||
g.ActiveIdPreviousFrameHasBeenEditedBefore = g.ActiveIdHasBeenEditedBefore;
|
||||
g.ActiveIdIsAlive = 0;
|
||||
g.ActiveIdHasBeenEditedThisFrame = false;
|
||||
g.ActiveIdPreviousFrameIsAlive = false;
|
||||
g.ActiveIdIsJustActivated = false;
|
||||
if (g.TempInputId != 0 && g.ActiveId != g.TempInputId)
|
||||
g.TempInputId = 0;
|
||||
@@ -5202,6 +5204,9 @@ void ImGui::NewFrame()
|
||||
g.ActiveIdUsingNavDirMask = 0x00;
|
||||
g.ActiveIdUsingAllKeyboardKeys = false;
|
||||
}
|
||||
if (g.DeactivatedItemData.ElapseFrame < g.FrameCount)
|
||||
g.DeactivatedItemData.ID = 0;
|
||||
g.DeactivatedItemData.IsAlive = false;
|
||||
|
||||
// Record when we have been stationary as this state is preserved while over same item.
|
||||
// FIXME: The way this is expressed means user cannot alter HoverStationaryDelay during the frame to use varying values.
|
||||
@@ -5833,13 +5838,13 @@ bool ImGui::IsItemDeactivated()
|
||||
ImGuiContext& g = *GImGui;
|
||||
if (g.LastItemData.StatusFlags & ImGuiItemStatusFlags_HasDeactivated)
|
||||
return (g.LastItemData.StatusFlags & ImGuiItemStatusFlags_Deactivated) != 0;
|
||||
return (g.ActiveIdPreviousFrame == g.LastItemData.ID && g.ActiveIdPreviousFrame != 0 && g.ActiveId != g.LastItemData.ID);
|
||||
return (g.DeactivatedItemData.ID == g.LastItemData.ID && g.LastItemData.ID != 0 && g.DeactivatedItemData.ElapseFrame >= g.FrameCount);
|
||||
}
|
||||
|
||||
bool ImGui::IsItemDeactivatedAfterEdit()
|
||||
{
|
||||
ImGuiContext& g = *GImGui;
|
||||
return IsItemDeactivated() && (g.ActiveIdPreviousFrameHasBeenEditedBefore || (g.ActiveId == 0 && g.ActiveIdHasBeenEditedBefore));
|
||||
return IsItemDeactivated() && g.DeactivatedItemData.HasBeenEditedBefore;
|
||||
}
|
||||
|
||||
// == (GetItemID() == GetFocusID() && GetFocusID() != 0)
|
||||
@@ -10443,8 +10448,8 @@ void ImGui::KeepAliveID(ImGuiID id)
|
||||
ImGuiContext& g = *GImGui;
|
||||
if (g.ActiveId == id)
|
||||
g.ActiveIdIsAlive = id;
|
||||
if (g.ActiveIdPreviousFrame == id)
|
||||
g.ActiveIdPreviousFrameIsAlive = true;
|
||||
if (g.DeactivatedItemData.ID == id)
|
||||
g.DeactivatedItemData.IsAlive = true;
|
||||
}
|
||||
|
||||
// Declare item bounding box for clipping and interaction.
|
||||
@@ -10529,6 +10534,9 @@ bool ImGui::ItemAdd(const ImRect& bb, ImGuiID id, const ImRect* nav_bb_arg, ImGu
|
||||
// window->DrawList->AddRect(g.LastItemData.NavRect.Min, g.LastItemData.NavRect.Max, IM_COL32(255,255,0,255)); // [DEBUG]
|
||||
#endif
|
||||
|
||||
if (id != 0 && g.DeactivatedItemData.ID == id)
|
||||
g.DeactivatedItemData.ElapseFrame = g.FrameCount;
|
||||
|
||||
// We need to calculate this now to take account of the current clipping rectangle (as items like Selectable may change them)
|
||||
if (is_rect_visible)
|
||||
g.LastItemData.StatusFlags |= ImGuiItemStatusFlags_Visible;
|
||||
@@ -10888,7 +10896,7 @@ void ImGui::BeginGroup()
|
||||
group_data.BackupActiveIdIsAlive = g.ActiveIdIsAlive;
|
||||
group_data.BackupHoveredIdIsAlive = g.HoveredId != 0;
|
||||
group_data.BackupIsSameLine = window->DC.IsSameLine;
|
||||
group_data.BackupActiveIdPreviousFrameIsAlive = g.ActiveIdPreviousFrameIsAlive;
|
||||
group_data.BackupDeactivatedIdIsAlive = g.DeactivatedItemData.IsAlive;
|
||||
group_data.EmitItem = true;
|
||||
|
||||
window->DC.GroupOffset.x = window->DC.CursorPos.x - window->Pos.x - window->DC.ColumnsOffset.x;
|
||||
@@ -10939,11 +10947,11 @@ void ImGui::EndGroup()
|
||||
// Also if you grep for LastItemId you'll notice it is only used in that context.
|
||||
// (The two tests not the same because ActiveIdIsAlive is an ID itself, in order to be able to handle ActiveId being overwritten during the frame.)
|
||||
const bool group_contains_curr_active_id = (group_data.BackupActiveIdIsAlive != g.ActiveId) && (g.ActiveIdIsAlive == g.ActiveId) && g.ActiveId;
|
||||
const bool group_contains_prev_active_id = (group_data.BackupActiveIdPreviousFrameIsAlive == false) && (g.ActiveIdPreviousFrameIsAlive == true);
|
||||
const bool group_contains_deactivated_id = (group_data.BackupDeactivatedIdIsAlive == false) && (g.DeactivatedItemData.IsAlive == true);
|
||||
if (group_contains_curr_active_id)
|
||||
g.LastItemData.ID = g.ActiveId;
|
||||
else if (group_contains_prev_active_id)
|
||||
g.LastItemData.ID = g.ActiveIdPreviousFrame;
|
||||
else if (group_contains_deactivated_id)
|
||||
g.LastItemData.ID = g.DeactivatedItemData.ID;
|
||||
g.LastItemData.Rect = group_bb;
|
||||
|
||||
// Forward Hovered flag
|
||||
@@ -10957,7 +10965,7 @@ void ImGui::EndGroup()
|
||||
|
||||
// Forward Deactivated flag
|
||||
g.LastItemData.StatusFlags |= ImGuiItemStatusFlags_HasDeactivated;
|
||||
if (group_contains_prev_active_id && g.ActiveId != g.ActiveIdPreviousFrame)
|
||||
if (group_contains_deactivated_id)
|
||||
g.LastItemData.StatusFlags |= ImGuiItemStatusFlags_Deactivated;
|
||||
|
||||
g.GroupStack.pop_back();
|
||||
|
||||
Reference in New Issue
Block a user