Browse Source

Nav: Store per-window last nav id also per-layer so we can easily query them for menu navigation code. (#787)

pull/1608/head
omar 7 years ago
parent
commit
9737efb2f1
  1. 35
      imgui.cpp
  2. 3
      imgui_internal.h

35
imgui.cpp

@ -1839,7 +1839,7 @@ ImGuiWindow::ImGuiWindow(const char* name)
Appearing = false;
BeginCount = 0;
PopupId = 0;
NavLastId = 0;
NavLastIds[0] = NavLastIds[1] = 0;
AutoFitFramesX = AutoFitFramesY = -1;
AutoFitOnlyGrows = false;
AutoFitChildAxises = 0x00;
@ -1934,8 +1934,8 @@ void ImGui::SetActiveID(ImGuiID id, ImGuiWindow* window)
g.NavId = id;
if (window)
g.NavLayer = window->DC.NavLayerCurrent;
if (window && window->DC.NavLayerCurrent == 0) // (Assume that id correspond to the current NavLayer, which should be the case)
window->NavLastId = id;
if (window) // NB: We current assume that SetActiveId() is called in the context where its NavLayer is the current one, which should be the case.
window->NavLastIds[window->DC.NavLayerCurrent] = id;
}
}
@ -2450,9 +2450,9 @@ static void SetNavId(ImGuiID id)
{
ImGuiContext& g = *GImGui;
IM_ASSERT(g.NavWindow);
IM_ASSERT(g.NavLayer == 0 || g.NavLayer == 1);
g.NavId = id;
if (g.NavLayer == 0)
g.NavWindow->NavLastId = g.NavId;
g.NavWindow->NavLastIds[g.NavLayer] = g.NavId;
}
static void SetNavIdAndMoveMouse(ImGuiID id, const ImRect& rect_rel)
@ -2470,7 +2470,7 @@ static void NavInitWindow(ImGuiWindow* window, bool force_reinit)
{
ImGuiContext& g = *GImGui;
IM_ASSERT(window == g.NavWindow);
if (!(window->Flags & ImGuiWindowFlags_ChildWindow) || (window->Flags & ImGuiWindowFlags_Popup) || (window->NavLastId == 0) || force_reinit)
if (!(window->Flags & ImGuiWindowFlags_ChildWindow) || (window->Flags & ImGuiWindowFlags_Popup) || (window->NavLastIds[0] == 0) || force_reinit)
{
SetNavId(0);
g.NavInitDefaultRequest = true;
@ -2480,7 +2480,7 @@ static void NavInitWindow(ImGuiWindow* window, bool force_reinit)
}
else
{
g.NavId = window->NavLastId;
g.NavId = window->NavLastIds[0];
}
}
@ -2699,7 +2699,7 @@ static void NavUpdate()
ImGui::FocusWindow(g.NavWindowingTarget);
g.NavDisableHighlight = false;
g.NavDisableMouseHover = true;
if (g.NavWindowingTarget->NavLastId == 0)
if (g.NavWindowingTarget->NavLastIds[0] == 0)
NavInitWindow(g.NavWindowingTarget, false);
}
@ -2711,14 +2711,15 @@ static void NavUpdate()
g.NavLayer = (g.NavWindow->DC.NavLayerActiveMask & (1<<1)) ? (g.NavLayer ^ 1) : 0;
g.NavDisableHighlight = false;
g.NavDisableMouseHover = true;
if (g.NavLayer == 0 && g.NavWindow->NavLastId)
SetNavIdAndMoveMouse(g.NavWindow->NavLastId, ImRect());
if (g.NavLayer == 0 && g.NavWindow->NavLastIds[0] != 0)
SetNavIdAndMoveMouse(g.NavWindow->NavLastIds[0], ImRect());
else
NavInitWindow(g.NavWindow, true);
}
g.NavWindowingTarget = NULL;
}
}
IM_ASSERT(g.NavLayer == 0 || g.NavLayer == 1);
// Set output flags for user application
g.IO.NavUsable = g.NavWindow && !(g.NavWindow->Flags & ImGuiWindowFlags_NoNavInputs);
@ -2752,8 +2753,8 @@ static void NavUpdate()
{
// Leave the "menu" layer
g.NavLayer = 0;
if (g.NavWindow->NavLastId)
SetNavIdAndMoveMouse(g.NavWindow->NavLastId, ImRect());
if (g.NavWindow->NavLastIds[0])
SetNavIdAndMoveMouse(g.NavWindow->NavLastIds[0], ImRect());
else
NavInitWindow(g.NavWindow, true);
}
@ -2761,7 +2762,7 @@ static void NavUpdate()
{
// Clear NavLastId for popups but keep it for regular child window so we can leave one and come back where we were
if (g.NavWindow && ((g.NavWindow->Flags & ImGuiWindowFlags_Popup) || !(g.NavWindow->Flags & ImGuiWindowFlags_ChildWindow)))
g.NavWindow->NavLastId = 0;
g.NavWindow->NavLastIds[0] = 0;
g.NavId = 0;
}
}
@ -4807,7 +4808,7 @@ bool ImGui::Begin(const char* name, bool* p_open, const ImVec2& size_on_first_us
const bool window_just_appearing_after_hidden_for_resize = (window->HiddenFrames == 1);
if (window_just_appearing_after_hidden_for_resize)
window->NavLastId = 0;
window->NavLastIds[0] = 0;
window->Appearing = (window_just_activated_by_user || window_just_appearing_after_hidden_for_resize);
// Process SetNextWindow***() calls
@ -5500,7 +5501,7 @@ void ImGui::FocusWindow(ImGuiWindow* window)
if (g.NavWindow != window)
{
g.NavId = window ? window->NavLastId : 0; // Restore NavId
g.NavId = window ? window->NavLastIds[0] : 0; // Restore NavId
g.NavIdIsAlive = false;
g.NavLayer = 0;
if (window && g.NavDisableMouseHover)
@ -9588,7 +9589,7 @@ bool ImGui::BeginCombo(const char* label, const char* preview_value, ImVec2 popu
if ((pressed || g.NavActivateId == id) && !popup_open)
{
if (window->DC.NavLayerCurrent == 0)
window->NavLastId = id;
window->NavLastIds[0] = id;
OpenPopupEx(id, false);
popup_open = true;
}
@ -11617,7 +11618,7 @@ void ImGui::ShowMetricsWindow(bool* p_open)
ImGui::BulletText("Size: (%.1f,%.1f), SizeContents (%.1f,%.1f)", window->Size.x, window->Size.y, window->SizeContents.x, window->SizeContents.y);
ImGui::BulletText("Scroll: (%.2f,%.2f)", window->Scroll.x, window->Scroll.y);
ImGui::BulletText("Active: %d, Accessed: %d", window->Active, window->Accessed);
ImGui::BulletText("NavLastId: 0x%08x, NavLayerActiveMask: %02X", window->NavLastId, window->DC.NavLayerActiveMask);
ImGui::BulletText("NavLastIds: 0x%08X,0x%08X, NavLayerActiveMask: %X", window->NavLastIds[0], window->NavLastIds[1], window->DC.NavLayerActiveMask);
if (window->RootWindow != window) NodeWindow(window->RootWindow, "RootWindow");
if (window->DC.ChildWindows.Size > 0) NodeWindows(window->DC.ChildWindows, "ChildWindows");
ImGui::BulletText("Storage: %d bytes", window->StateStorage.Data.Size * (int)sizeof(ImGuiStorage::Pair));

3
imgui_internal.h

@ -764,7 +764,6 @@ struct IMGUI_API ImGuiWindow
bool Appearing; // Set during the frame where the window is appearing (or re-appearing)
int BeginCount; // Number of Begin() during the current frame (generally 0 or 1, 1+ if appending via multiple Begin/End pairs)
ImGuiID PopupId; // ID in the popup stack when this window is used as a popup/menu (because we use generic Name/ID for recycling)
ImGuiID NavLastId; // Last known NavId for this window, for nav layer 0 only.
int AutoFitFramesX, AutoFitFramesY;
bool AutoFitOnlyGrows;
int AutoFitChildAxises;
@ -776,6 +775,8 @@ struct IMGUI_API ImGuiWindow
ImVec2 SetWindowPosVal; // store window position when using a non-zero Pivot (position set needs to be processed when we know the window size)
ImVec2 SetWindowPosPivot; // store window pivot for positioning. ImVec2(0,0) when positioning from top-left corner; ImVec2(0.5f,0.5f) for centering; ImVec2(1,1) for bottom right.
ImGuiID NavLastIds[2]; // Last known NavId for this window, per layer (0/1)
ImGuiDrawContext DC; // Temporary per-window data, reset at the beginning of the frame
ImVector<ImGuiID> IDStack; // ID stack. ID are hashes seeded with the value at the top of the stack
ImRect ClipRect; // = DrawList->clip_rect_stack.back(). Scissoring / clipping rectangle. x1, y1, x2, y2.

Loading…
Cancel
Save