Skip to content

Commit

Permalink
Nav: always disable highlight if nav is disabled, fix for IMGUI_DEBUG…
Browse files Browse the repository at this point in the history
…_NAV_SCORING, minor renaming.
  • Loading branch information
ocornut committed Sep 2, 2021
1 parent dff15ac commit 5ee40c8
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 17 deletions.
33 changes: 20 additions & 13 deletions imgui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3685,13 +3685,15 @@ static void ImGui::UpdateMouseInputs()

// Round mouse position to avoid spreading non-rounded position (e.g. UpdateManualResize doesn't support them well)
if (IsMousePosValid(&g.IO.MousePos))
g.IO.MousePos = g.LastValidMousePos = ImFloor(g.IO.MousePos);
g.IO.MousePos = g.MouseLastValidPos = ImFloor(g.IO.MousePos);

// If mouse just appeared or disappeared (usually denoted by -FLT_MAX components) we cancel out movement in MouseDelta
if (IsMousePosValid(&g.IO.MousePos) && IsMousePosValid(&g.IO.MousePosPrev))
g.IO.MouseDelta = g.IO.MousePos - g.IO.MousePosPrev;
else
g.IO.MouseDelta = ImVec2(0.0f, 0.0f);

// If mouse moved we re-enable mouse hovering in case it was disabled by gamepad/keyboard. In theory should use a >0.0f threshold but would need to reset in everywhere we set this to true.
if (g.IO.MouseDelta.x != 0.0f || g.IO.MouseDelta.y != 0.0f)
g.NavDisableMouseHover = false;

Expand Down Expand Up @@ -8762,7 +8764,7 @@ static bool ImGui::NavScoreItem(ImGuiNavItemData* result)
// FIXME: Those are not good variables names
ImRect cand = g.LastItemData.NavRect; // Current item nav rectangle
const ImRect curr = g.NavScoringRect; // Current modified source rect (NB: we've applied Max.x = Min.x in NavUpdate() to inhibit the effect of having varied item width)
g.NavScoringCount++;
g.NavScoringDebugCount++;

// When entering through a NavFlattened border, we consider child window items as fully clipped for scoring
if (window->ParentWindow == g.NavWindow)
Expand Down Expand Up @@ -8929,14 +8931,7 @@ static void ImGui::NavProcessItem()
if ((g.NavId != id || (g.NavMoveFlags & ImGuiNavMoveFlags_AllowCurrentNavId)) && !(item_flags & (ImGuiItemFlags_Disabled | ImGuiItemFlags_NoNav)))
{
ImGuiNavItemData* result = (window == g.NavWindow) ? &g.NavMoveResultLocal : &g.NavMoveResultOther;
bool new_best = NavScoreItem(result);

#if IMGUI_DEBUG_NAV_SCORING
// [DEBUG] Scoring all items in NavWindow at all times
if (g.NavMoveFlags & ImGuiNavMoveFlags_DebugNoResult)
new_best = false;
#endif
if (new_best)
if (NavScoreItem(result))
NavApplyItemToResult(result);

// Features like PageUp/PageDown need to maintain a separate score for the visible set of items.
Expand Down Expand Up @@ -8972,6 +8967,7 @@ void ImGui::NavMoveRequestSubmit(ImGuiDir move_dir, ImGuiDir clip_dir, ImGuiNavM
IM_ASSERT(g.NavWindow != NULL);
g.NavMoveSubmitted = g.NavMoveScoringItems = true;
g.NavMoveDir = move_dir;
g.NavMoveDirForDebug = move_dir;
g.NavMoveClipDir = clip_dir;
g.NavMoveFlags = move_flags;
g.NavMoveForwardToNextFrame = false;
Expand Down Expand Up @@ -9098,7 +9094,7 @@ static ImVec2 ImGui::NavCalcPreferredRefPos()
// Mouse (we need a fallback in case the mouse becomes invalid after being used)
if (IsMousePosValid(&g.IO.MousePos))
return g.IO.MousePos;
return g.LastValidMousePos;
return g.MouseLastValidPos;
}
else
{
Expand Down Expand Up @@ -9155,7 +9151,7 @@ static void ImGui::NavUpdate()

io.WantSetMousePos = false;
#if 0
if (g.NavScoringCount > 0) IMGUI_DEBUG_LOG("NavScoringCount %d for '%s' layer %d (Init:%d, Move:%d)\n", g.NavScoringCount, g.NavWindow ? g.NavWindow->Name : "NULL", g.NavLayer, g.NavInitRequest || g.NavInitResultId != 0, g.NavMoveRequest);
if (g.NavScoringDebugCount > 0) IMGUI_DEBUG_LOG("NavScoringDebugCount %d for '%s' layer %d (Init:%d, Move:%d)\n", g.NavScoringDebugCount, g.NavWindow ? g.NavWindow->Name : "NULL", g.NavLayer, g.NavInitRequest || g.NavInitResultId != 0, g.NavMoveRequest);
#endif

// Set input source as Gamepad when buttons are pressed (as some features differs when used with Gamepad vs Keyboard)
Expand Down Expand Up @@ -9289,8 +9285,15 @@ static void ImGui::NavUpdate()
SetScrollY(window, ImFloor(window->Scroll.y + scroll_dir.y * scroll_speed));
}

// Always prioritize mouse highlight if navigation is disabled
if (!nav_keyboard_active && !nav_gamepad_active)
{
g.NavDisableHighlight = true;
g.NavDisableMouseHover = g.NavMousePosDirty = false;
}

// [DEBUG]
g.NavScoringCount = 0;
g.NavScoringDebugCount = 0;
#if IMGUI_DEBUG_NAV_RECTS
if (g.NavWindow)
{
Expand Down Expand Up @@ -9417,6 +9420,10 @@ void ImGui::NavUpdateCreateMoveRequest()
void ImGui::NavMoveRequestApplyResult()
{
ImGuiContext& g = *GImGui;
#if IMGUI_DEBUG_NAV_SCORING
if (g.NavMoveFlags & ImGuiNavMoveFlags_DebugNoResult) // [DEBUG] Scoring all items in NavWindow at all times
return;
#endif

// No result
// In a situation when there is no results but NavId != 0, re-enable the Navigation highlight (because g.NavId is not considered as a possible result)
Expand Down
7 changes: 3 additions & 4 deletions imgui_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -1527,7 +1527,7 @@ struct ImGuiContext
ImGuiDir NavMoveDirForDebug;
ImGuiDir NavMoveClipDir; // FIXME-NAV: Describe the purpose of this better. Might want to rename?
ImRect NavScoringRect; // Rectangle used for scoring, in screen space. Based of window->NavRectRel[], modified for directional navigation scoring.
int NavScoringCount; // Metrics for debugging
int NavScoringDebugCount; // Metrics for debugging
ImGuiNavItemData NavMoveResultLocal; // Best move request candidate within NavWindow
ImGuiNavItemData NavMoveResultLocalVisible; // Best move request candidate within NavWindow that are mostly visible (when using ImGuiNavMoveFlags_AlsoScoreVisibleSet flag)
ImGuiNavItemData NavMoveResultOther; // Best move request candidate within NavWindow's flattened hierarchy (when using ImGuiWindowFlags_NavFlattened flag)
Expand Down Expand Up @@ -1587,7 +1587,7 @@ struct ImGuiContext
ImVector<ImGuiShrinkWidthItem> ShrinkWidthBuffer;

// Widget state
ImVec2 LastValidMousePos;
ImVec2 MouseLastValidPos;
ImGuiInputTextState InputTextState;
ImFont InputTextPasswordFont;
ImGuiID TempInputId; // Temporary text input when CTRL+clicking on a slider, etc.
Expand Down Expand Up @@ -1728,7 +1728,7 @@ struct ImGuiContext
NavMoveFlags = ImGuiNavMoveFlags_None;
NavMoveKeyMods = ImGuiKeyModFlags_None;
NavMoveDir = NavMoveDirForDebug = NavMoveClipDir = ImGuiDir_None;
NavScoringCount = 0;
NavScoringDebugCount = 0;

NavWindowingTarget = NavWindowingTargetAnim = NavWindowingListWindow = NULL;
NavWindowingTimer = NavWindowingHighlightAlpha = 0.0f;
Expand Down Expand Up @@ -1758,7 +1758,6 @@ struct ImGuiContext
CurrentTableStackIdx = -1;
CurrentTabBar = NULL;

LastValidMousePos = ImVec2(0.0f, 0.0f);
TempInputId = 0;
ColorEditOptions = ImGuiColorEditFlags_DefaultOptions_;
ColorEditLastHue = ColorEditLastSat = 0.0f;
Expand Down

0 comments on commit 5ee40c8

Please sign in to comment.