Nav: ActiveIdAllowNavMove -> ActiveIdAllowNavDirFlags for more flexibility (nav up/down typically allowed on a single-line text input) (#323)

This commit is contained in:
ocornut 2016-07-30 14:31:34 +02:00
parent d9d6b0e629
commit d6ce800a20
2 changed files with 13 additions and 11 deletions

View File

@ -1824,7 +1824,7 @@ void ImGui::SetActiveID(ImGuiID id, ImGuiWindow* window = NULL)
ImGuiContext& g = *GImGui; ImGuiContext& g = *GImGui;
g.ActiveIdIsJustActivated = (g.ActiveId != id); g.ActiveIdIsJustActivated = (g.ActiveId != id);
g.ActiveId = id; g.ActiveId = id;
g.ActiveIdAllowNavMove = false; g.ActiveIdAllowNavDirFlags = 0;
g.ActiveIdAllowOverlap = false; g.ActiveIdAllowOverlap = false;
g.ActiveIdWindow = window; g.ActiveIdWindow = window;
if (id) if (id)
@ -1847,7 +1847,7 @@ void ImGui::SetActiveIDNoNav(ImGuiID id, ImGuiWindow* window)
ImGuiContext& g = *GImGui; ImGuiContext& g = *GImGui;
g.ActiveIdIsJustActivated = (g.ActiveId != id); g.ActiveIdIsJustActivated = (g.ActiveId != id);
g.ActiveId = id; g.ActiveId = id;
g.ActiveIdAllowNavMove = false; g.ActiveIdAllowNavDirFlags = 0;
g.ActiveIdAllowOverlap = false; g.ActiveIdAllowOverlap = false;
g.ActiveIdWindow = window; g.ActiveIdWindow = window;
g.ActiveIdSource = (g.NavActivateId == id || g.NavInputId == id) ? ImGuiInputSource_Nav : ImGuiInputSource_Mouse; g.ActiveIdSource = (g.NavActivateId == id || g.NavInputId == id) ? ImGuiInputSource_Nav : ImGuiInputSource_Mouse;
@ -2511,13 +2511,14 @@ static void NavUpdate()
g.NavMoveRequest = false; g.NavMoveRequest = false;
// Initiate directional inputs request // Initiate directional inputs request
const int allowed_dir_flags = (g.ActiveId == 0) ? ~0 : g.ActiveIdAllowNavDirFlags;
g.NavMoveDir = ImGuiNavDir_None; g.NavMoveDir = ImGuiNavDir_None;
if (g.FocusedWindow && !g.NavWindowingTarget && (g.ActiveId == 0 || g.ActiveIdAllowNavMove) && !(g.FocusedWindow->Flags & ImGuiWindowFlags_NoNav)) if (g.FocusedWindow && !g.NavWindowingTarget && allowed_dir_flags && !(g.FocusedWindow->Flags & ImGuiWindowFlags_NoNav))
{ {
if (IsKeyPressedMap(ImGuiKey_NavLeft, true)) g.NavMoveDir = ImGuiNavDir_Left; if ((allowed_dir_flags & (1<<ImGuiNavDir_Left)) && IsKeyPressedMap(ImGuiKey_NavLeft, true)) g.NavMoveDir = ImGuiNavDir_Left;
if (IsKeyPressedMap(ImGuiKey_NavRight, true)) g.NavMoveDir = ImGuiNavDir_Right; if ((allowed_dir_flags & (1<<ImGuiNavDir_Right)) && IsKeyPressedMap(ImGuiKey_NavRight, true)) g.NavMoveDir = ImGuiNavDir_Right;
if (IsKeyPressedMap(ImGuiKey_NavUp, true)) g.NavMoveDir = ImGuiNavDir_Up; if ((allowed_dir_flags & (1<<ImGuiNavDir_Up)) && IsKeyPressedMap(ImGuiKey_NavUp, true)) g.NavMoveDir = ImGuiNavDir_Up;
if (IsKeyPressedMap(ImGuiKey_NavDown, true)) g.NavMoveDir = ImGuiNavDir_Down; if ((allowed_dir_flags & (1<<ImGuiNavDir_Down)) && IsKeyPressedMap(ImGuiKey_NavDown, true)) g.NavMoveDir = ImGuiNavDir_Down;
} }
if (g.NavMoveDir != ImGuiNavDir_None) if (g.NavMoveDir != ImGuiNavDir_None)
{ {
@ -6167,7 +6168,7 @@ bool ImGui::ButtonBehavior(const ImRect& bb, ImGuiID id, bool* out_hovered, bool
// Set active id so it can be queried by user via IsItemActive(), etc. but don't react to it ourselves // Set active id so it can be queried by user via IsItemActive(), etc. but don't react to it ourselves
g.NavActivateId = g.NavId; g.NavActivateId = g.NavId;
SetActiveID(g.NavId, window); SetActiveID(g.NavId, window);
g.ActiveIdAllowNavMove = true; g.ActiveIdAllowNavDirFlags = (1<<ImGuiNavDir_Left) | (1<<ImGuiNavDir_Right) | (1<<ImGuiNavDir_Up) | (1<<ImGuiNavDir_Down);
if (IsKeyPressedMap(ImGuiKey_NavActivate, (flags & ImGuiButtonFlags_Repeat) != 0)) if (IsKeyPressedMap(ImGuiKey_NavActivate, (flags & ImGuiButtonFlags_Repeat) != 0))
pressed = true; pressed = true;
} }
@ -6986,7 +6987,7 @@ bool ImGui::InputScalarAsWidgetReplacement(const ImRect& aabb, const char* label
// Our replacement widget will override the focus ID (registered previously to allow for a TAB focus to happen) // Our replacement widget will override the focus ID (registered previously to allow for a TAB focus to happen)
SetActiveIDNoNav(g.ScalarAsInputTextId, window); SetActiveIDNoNav(g.ScalarAsInputTextId, window);
g.ActiveIdAllowNavMove = true; g.ActiveIdAllowNavDirFlags = (1 << ImGuiNavDir_Up) | (1 << ImGuiNavDir_Down);
SetHoveredID(0); SetHoveredID(0);
FocusableItemUnregister(window); FocusableItemUnregister(window);
@ -8445,7 +8446,8 @@ bool ImGui::InputTextEx(const char* label, char* buf, int buf_size, const ImVec2
select_all = true; select_all = true;
} }
SetActiveID(id, window); SetActiveID(id, window);
g.ActiveIdAllowNavMove = true; if (!is_multiline)
g.ActiveIdAllowNavDirFlags = ((1 << ImGuiNavDir_Up) | (1 << ImGuiNavDir_Down));
FocusWindow(window); FocusWindow(window);
} }
else if (io.MouseClicked[0]) else if (io.MouseClicked[0])

View File

@ -388,8 +388,8 @@ struct ImGuiContext
ImGuiID ActiveIdPreviousFrame; ImGuiID ActiveIdPreviousFrame;
bool ActiveIdIsAlive; // Active widget has been seen this frame bool ActiveIdIsAlive; // Active widget has been seen this frame
bool ActiveIdIsJustActivated; // Set at the time of activation for one frame bool ActiveIdIsJustActivated; // Set at the time of activation for one frame
bool ActiveIdAllowNavMove; // Active widget allows using directional navigation (e.g. can activate a button and move away from it)
bool ActiveIdAllowOverlap; // Active widget allows another widget to steal active id (generally for overlapping widgets, but not always) bool ActiveIdAllowOverlap; // Active widget allows another widget to steal active id (generally for overlapping widgets, but not always)
int ActiveIdAllowNavDirFlags; // Active widget allows using directional navigation (e.g. can activate a button and move away from it)
ImVec2 ActiveIdClickOffset; // Clicked offset from upper-left corner, if applicable (currently only set by ButtonBehavior) ImVec2 ActiveIdClickOffset; // Clicked offset from upper-left corner, if applicable (currently only set by ButtonBehavior)
ImGuiWindow* ActiveIdWindow; ImGuiWindow* ActiveIdWindow;
ImGuiInputSource ActiveIdSource; // Activating with mouse or nav (gamepad/keyboard) ImGuiInputSource ActiveIdSource; // Activating with mouse or nav (gamepad/keyboard)