mirror of
				https://github.com/Drezil/imgui.git
				synced 2025-10-31 21:21:06 +01:00 
			
		
		
		
	Nav: Tidying up activation mechanism so that setting NavActivateId can trigger buttons.
This commit is contained in:
		
							
								
								
									
										44
									
								
								imgui.cpp
									
									
									
									
									
								
							
							
						
						
									
										44
									
								
								imgui.cpp
									
									
									
									
									
								
							| @@ -2818,13 +2818,23 @@ static void NavUpdate() | |||||||
|         } |         } | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     g.NavActivateId = (g.NavId && !g.NavDisableHighlight && !g.NavWindowingTarget && g.ActiveId == 0 && IsNavInputPressed(ImGuiNavInput_PadActivate, ImGuiNavReadMode_Pressed)) ? g.NavId : 0; |     g.NavActivateId = g.NavActivateDownId = g.NavInputId = 0; | ||||||
|     g.NavInputId = (g.NavId && !g.NavDisableHighlight && !g.NavWindowingTarget && g.ActiveId == 0 && IsNavInputPressed(ImGuiNavInput_PadInput, ImGuiNavReadMode_Pressed)) ? g.NavId : 0; |     if (g.NavId != 0 && !g.NavDisableHighlight && !g.NavWindowingTarget) | ||||||
|  |     { | ||||||
|  |         if (g.ActiveId == 0 && IsNavInputPressed(ImGuiNavInput_PadActivate, ImGuiNavReadMode_Pressed)) | ||||||
|  |             g.NavActivateId = g.NavId; | ||||||
|  |         if ((g.ActiveId == 0 || g.ActiveId == g.NavId) && IsNavInputDown(ImGuiNavInput_PadActivate)) | ||||||
|  |             g.NavActivateDownId = g.NavId; | ||||||
|  |         if (g.ActiveId == 0 && IsNavInputPressed(ImGuiNavInput_PadInput, ImGuiNavReadMode_Pressed)) | ||||||
|  |             g.NavInputId = g.NavId; | ||||||
|  |     } | ||||||
|     if (g.NavWindow && (g.NavWindow->Flags & ImGuiWindowFlags_NoNavInputs)) |     if (g.NavWindow && (g.NavWindow->Flags & ImGuiWindowFlags_NoNavInputs)) | ||||||
|     { |     { | ||||||
|         g.NavActivateId = g.NavInputId = 0; |         g.NavActivateId = g.NavActivateDownId = g.NavInputId = 0; | ||||||
|         g.NavDisableHighlight = true; |         g.NavDisableHighlight = true; | ||||||
|     } |     } | ||||||
|  |     if (g.NavActivateId != 0) | ||||||
|  |         IM_ASSERT(g.NavActivateDownId == g.NavActivateId); | ||||||
|     g.NavMoveRequest = false; |     g.NavMoveRequest = false; | ||||||
|  |  | ||||||
|     // Initiate directional inputs request |     // Initiate directional inputs request | ||||||
| @@ -6681,18 +6691,18 @@ bool ImGui::ButtonBehavior(const ImRect& bb, ImGuiID id, bool* out_hovered, bool | |||||||
|     { |     { | ||||||
|         // We report navigated item as hovered but we don't set g.HoveredId to not interfere with mouse |         // We report navigated item as hovered but we don't set g.HoveredId to not interfere with mouse | ||||||
|         hovered = true; |         hovered = true; | ||||||
|         if (!g.NavWindowingTarget && IsNavInputDown(ImGuiNavInput_PadActivate)) |     } | ||||||
|  |     if (g.NavActivateDownId == id) | ||||||
|  |     { | ||||||
|  |         bool nav_pressed = (g.NavActivateId == id) || IsNavInputPressed(ImGuiNavInput_PadActivate, (flags & ImGuiButtonFlags_Repeat) ? ImGuiNavReadMode_Repeat : ImGuiNavReadMode_Pressed); | ||||||
|  |         if (nav_pressed) | ||||||
|  |             pressed = true; | ||||||
|  |         if (nav_pressed || g.ActiveId == id) | ||||||
|         { |         { | ||||||
|             bool nav_pressed = IsNavInputPressed(ImGuiNavInput_PadActivate, (flags & ImGuiButtonFlags_Repeat) ? ImGuiNavReadMode_Repeat : ImGuiNavReadMode_Pressed); |             // Set active id so it can be queried by user via IsItemActive(), equivalent of holding the mouse button. | ||||||
|             if (nav_pressed) |             g.NavActivateId = id; // This is so SetActiveId assign a Nav source | ||||||
|                 pressed = true; |             SetActiveID(id, window); | ||||||
|             if (nav_pressed || g.ActiveId == id) |             g.ActiveIdAllowNavDirFlags = (1 << ImGuiDir_Left) | (1 << ImGuiDir_Right) | (1 << ImGuiDir_Up) | (1 << ImGuiDir_Down); | ||||||
|             { |  | ||||||
|                 // Set active id so it can be queried by user via IsItemActive(), equivalent of holding the mouse button. |  | ||||||
|                 g.NavActivateId = id; // This is so SetActiveId assign a Nav source |  | ||||||
|                 SetActiveID(id, window); |  | ||||||
|                 g.ActiveIdAllowNavDirFlags = (1 << ImGuiDir_Left) | (1 << ImGuiDir_Right) | (1 << ImGuiDir_Up) | (1 << ImGuiDir_Down); |  | ||||||
|             } |  | ||||||
|         } |         } | ||||||
|     } |     } | ||||||
|  |  | ||||||
| @@ -6717,7 +6727,7 @@ bool ImGui::ButtonBehavior(const ImRect& bb, ImGuiID id, bool* out_hovered, bool | |||||||
|         } |         } | ||||||
|         else if (g.ActiveIdSource == ImGuiInputSource_Nav) |         else if (g.ActiveIdSource == ImGuiInputSource_Nav) | ||||||
|         { |         { | ||||||
|             if (!IsNavInputDown(ImGuiNavInput_PadActivate)) |             if (g.NavActivateDownId != id) | ||||||
|                 ClearActiveID(); |                 ClearActiveID(); | ||||||
|         } |         } | ||||||
|     } |     } | ||||||
| @@ -7677,7 +7687,7 @@ bool ImGui::SliderBehavior(const ImRect& frame_bb, ImGuiID id, float* v, float v | |||||||
|                 clicked_t = 1.0f - clicked_t; |                 clicked_t = 1.0f - clicked_t; | ||||||
|             set_new_value = true; |             set_new_value = true; | ||||||
|         } |         } | ||||||
|         else if (g.ActiveIdSource == ImGuiInputSource_Nav && IsNavInputDown(ImGuiNavInput_PadActivate)) |         else if (g.ActiveIdSource == ImGuiInputSource_Nav && g.NavActivateDownId == id) | ||||||
|         { |         { | ||||||
|             const ImVec2 delta2 = GetNavInputAmount2d(0, ImGuiNavReadMode_RepeatFast, 0.0f, 0.0f); |             const ImVec2 delta2 = GetNavInputAmount2d(0, ImGuiNavReadMode_RepeatFast, 0.0f, 0.0f); | ||||||
|             if (float delta = is_horizontal ? delta2.x : -delta2.y) |             if (float delta = is_horizontal ? delta2.x : -delta2.y) | ||||||
| @@ -7997,7 +8007,7 @@ bool ImGui::DragBehavior(const ImRect& frame_bb, ImGuiID id, float* v, float v_s | |||||||
|     // Process clicking on the drag |     // Process clicking on the drag | ||||||
|     if (g.ActiveId == id) |     if (g.ActiveId == id) | ||||||
|     { |     { | ||||||
|         if (g.IO.MouseDown[0] || IsNavInputDown(ImGuiNavInput_PadActivate)) |         if (g.IO.MouseDown[0] || g.NavActivateDownId == id) | ||||||
|         { |         { | ||||||
|             if (g.ActiveIdIsJustActivated) |             if (g.ActiveIdIsJustActivated) | ||||||
|             { |             { | ||||||
|   | |||||||
| @@ -465,7 +465,8 @@ struct ImGuiContext | |||||||
|     // Navigation data (for gamepad/keyboard) |     // Navigation data (for gamepad/keyboard) | ||||||
|     ImGuiWindow*            NavWindow;                          // Nav/focused window for navigation |     ImGuiWindow*            NavWindow;                          // Nav/focused window for navigation | ||||||
|     ImGuiID                 NavId;                              // Nav/focused item for navigation |     ImGuiID                 NavId;                              // Nav/focused item for navigation | ||||||
|     ImGuiID                 NavActivateId, NavInputId;          // ~~ IsKeyPressedMap(ImGuiKey_NavActive) ? NavId : 0, etc. (to make widget code terser) |     ImGuiID                 NavActivateId, NavActivateDownId;   // ~~ IsNavInputPressed(ImGuiNavInput_PadActivate) ? NavId : 0, etc. | ||||||
|  |     ImGuiID                 NavInputId;                         // ~~ IsNavInputPressed(ImGuiNavInput_PadInput) ? NavId : 0, etc. | ||||||
|     ImGuiID                 NavJustTabbedId;                    // Just tabbed to this id. |     ImGuiID                 NavJustTabbedId;                    // Just tabbed to this id. | ||||||
|     ImGuiID                 NavJustNavigatedId;                 // Just navigated to this id (result of a successfully MoveRequest) |     ImGuiID                 NavJustNavigatedId;                 // Just navigated to this id (result of a successfully MoveRequest) | ||||||
|     ImRect                  NavScoringRectScreen;               // Rectangle used for scoring, in screen space. Based of window->DC.NavRefRectRel[], modified for directional navigation scoring. |     ImRect                  NavScoringRectScreen;               // Rectangle used for scoring, in screen space. Based of window->DC.NavRefRectRel[], modified for directional navigation scoring. | ||||||
| @@ -582,7 +583,7 @@ struct ImGuiContext | |||||||
|         SettingsDirtyTimer = 0.0f; |         SettingsDirtyTimer = 0.0f; | ||||||
|  |  | ||||||
|         NavWindow = NULL; |         NavWindow = NULL; | ||||||
|         NavId = NavActivateId = NavInputId = 0; |         NavId = NavActivateId = NavActivateDownId = NavInputId = 0; | ||||||
|         NavJustTabbedId = NavJustNavigatedId = 0; |         NavJustTabbedId = NavJustNavigatedId = 0; | ||||||
|         NavScoringRectScreen = ImRect(); |         NavScoringRectScreen = ImRect(); | ||||||
|         NavWindowingTarget = NULL; |         NavWindowingTarget = NULL; | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user