mirror of
				https://github.com/Drezil/imgui.git
				synced 2025-10-31 05:01:05 +01:00 
			
		
		
		
	Nav: Toward automatically mapping keyboard input. Renamed ImGuiNavInput_PadXXX to ImGuiNavInput_XXX. Renamed ImGuiNavInput_KeyXXX to ImGuiNavInput_KeyXXX_ (internal). (#787)
This commit is contained in:
		
							
								
								
									
										42
									
								
								imgui.cpp
									
									
									
									
									
								
							
							
						
						
									
										42
									
								
								imgui.cpp
									
									
									
									
									
								
							| @@ -2739,14 +2739,14 @@ ImVec2 ImGui::GetNavInputAmount2d(ImGuiNavDirSourceFlags dir_sources, ImGuiInput | ||||
| { | ||||
|     ImVec2 delta(0.0f, 0.0f); | ||||
|     if (dir_sources & ImGuiNavDirSourceFlags_Keyboard) | ||||
|         delta += ImVec2(GetNavInputAmount(ImGuiNavInput_KeyRight, mode)       - GetNavInputAmount(ImGuiNavInput_KeyLeft,       mode), GetNavInputAmount(ImGuiNavInput_KeyDown,       mode) - GetNavInputAmount(ImGuiNavInput_KeyUp,       mode)); | ||||
|         delta += ImVec2(GetNavInputAmount(ImGuiNavInput_KeyRight_, mode)   - GetNavInputAmount(ImGuiNavInput_KeyLeft_,   mode), GetNavInputAmount(ImGuiNavInput_KeyDown_,   mode) - GetNavInputAmount(ImGuiNavInput_KeyUp_,   mode)); | ||||
|     if (dir_sources & ImGuiNavDirSourceFlags_PadDPad) | ||||
|         delta += ImVec2(GetNavInputAmount(ImGuiNavInput_PadDpadRight, mode)   - GetNavInputAmount(ImGuiNavInput_PadDpadLeft,   mode), GetNavInputAmount(ImGuiNavInput_PadDpadDown,   mode) - GetNavInputAmount(ImGuiNavInput_PadDpadUp,   mode)); | ||||
|         delta += ImVec2(GetNavInputAmount(ImGuiNavInput_DpadRight, mode)   - GetNavInputAmount(ImGuiNavInput_DpadLeft,   mode), GetNavInputAmount(ImGuiNavInput_DpadDown,   mode) - GetNavInputAmount(ImGuiNavInput_DpadUp,   mode)); | ||||
|     if (dir_sources & ImGuiNavDirSourceFlags_PadLStick) | ||||
|         delta += ImVec2(GetNavInputAmount(ImGuiNavInput_PadLStickRight, mode) - GetNavInputAmount(ImGuiNavInput_PadLStickLeft, mode), GetNavInputAmount(ImGuiNavInput_PadLStickDown, mode) - GetNavInputAmount(ImGuiNavInput_PadLStickUp, mode)); | ||||
|     if (slow_factor != 0.0f && IsNavInputDown(ImGuiNavInput_PadTweakSlow)) | ||||
|         delta += ImVec2(GetNavInputAmount(ImGuiNavInput_LStickRight, mode) - GetNavInputAmount(ImGuiNavInput_LStickLeft, mode), GetNavInputAmount(ImGuiNavInput_LStickDown, mode) - GetNavInputAmount(ImGuiNavInput_LStickUp, mode)); | ||||
|     if (slow_factor != 0.0f && IsNavInputDown(ImGuiNavInput_TweakSlow)) | ||||
|         delta *= slow_factor; | ||||
|     if (fast_factor != 0.0f && IsNavInputDown(ImGuiNavInput_PadTweakFast)) | ||||
|     if (fast_factor != 0.0f && IsNavInputDown(ImGuiNavInput_TweakFast)) | ||||
|         delta *= fast_factor; | ||||
|     return delta; | ||||
| } | ||||
| @@ -2773,7 +2773,7 @@ static void ImGui::NavUpdateWindowing() | ||||
|     ImGuiWindow* apply_focus_window = NULL; | ||||
|     bool apply_toggle_layer = false; | ||||
|  | ||||
|     bool start_windowing_with_gamepad = !g.NavWindowingTarget && IsNavInputPressed(ImGuiNavInput_PadMenu, ImGuiInputReadMode_Pressed); | ||||
|     bool start_windowing_with_gamepad = !g.NavWindowingTarget && IsNavInputPressed(ImGuiNavInput_Menu, ImGuiInputReadMode_Pressed); | ||||
|     bool start_windowing_with_keyboard = !g.NavWindowingTarget && g.IO.KeyCtrl && IsKeyPressedMap(ImGuiKey_Tab) && (g.IO.NavFlags & ImGuiNavFlags_EnableKeyboard); | ||||
|     if (start_windowing_with_gamepad || start_windowing_with_keyboard) | ||||
|         if (ImGuiWindow* window = g.NavWindow ? g.NavWindow : FindWindowNavigable(g.Windows.Size - 1, -INT_MAX, -1)) | ||||
| @@ -2792,7 +2792,7 @@ static void ImGui::NavUpdateWindowing() | ||||
|         g.NavWindowingHighlightAlpha = ImMax(g.NavWindowingHighlightAlpha, ImSaturate((g.NavWindowingHighlightTimer - 0.20f) / 0.05f)); | ||||
|  | ||||
|         // Select window to focus | ||||
|         const int focus_change_dir = (int)IsNavInputPressed(ImGuiNavInput_PadFocusPrev, ImGuiInputReadMode_RepeatSlow) - (int)IsNavInputPressed(ImGuiNavInput_PadFocusNext, ImGuiInputReadMode_RepeatSlow); | ||||
|         const int focus_change_dir = (int)IsNavInputPressed(ImGuiNavInput_FocusPrev, ImGuiInputReadMode_RepeatSlow) - (int)IsNavInputPressed(ImGuiNavInput_FocusNext, ImGuiInputReadMode_RepeatSlow); | ||||
|         if (focus_change_dir != 0) | ||||
|         { | ||||
|             NavUpdateWindowingHighlightWindow(focus_change_dir); | ||||
| @@ -2800,7 +2800,7 @@ static void ImGui::NavUpdateWindowing() | ||||
|         } | ||||
|  | ||||
|         // Single press toggles NavLayer, long press with L/R apply actual focus on release (until then the window was merely rendered front-most) | ||||
|         if (!IsNavInputDown(ImGuiNavInput_PadMenu)) | ||||
|         if (!IsNavInputDown(ImGuiNavInput_Menu)) | ||||
|         { | ||||
|             g.NavWindowingToggleLayer &= (g.NavWindowingHighlightAlpha < 1.0f); // Once button was held long enough we don't consider it a tap-to-toggle-layer press anymore. | ||||
|             if (g.NavWindowingToggleLayer && g.NavWindow) | ||||
| @@ -2824,7 +2824,7 @@ static void ImGui::NavUpdateWindowing() | ||||
|  | ||||
|     // Keyboard: Press and Release ALT to toggle menu layer | ||||
|     // FIXME: We lack an explicit IO variable for "is the imgui window focused", so compare mouse validity to detect the common case of back-end clearing releases all keys on ALT-TAB | ||||
|     if ((g.ActiveId == 0 || g.ActiveIdAllowOverlap) && IsNavInputPressed(ImGuiNavInput_KeyMenu, ImGuiInputReadMode_Released)) | ||||
|     if ((g.ActiveId == 0 || g.ActiveIdAllowOverlap) && IsNavInputPressed(ImGuiNavInput_KeyMenu_, ImGuiInputReadMode_Released)) | ||||
|         if (IsMousePosValid(&g.IO.MousePos) == IsMousePosValid(&g.IO.MousePosPrev)) | ||||
|             apply_toggle_layer = true; | ||||
|  | ||||
| @@ -3003,7 +3003,7 @@ static void ImGui::NavUpdate() | ||||
|     g.IO.NavVisible = (g.IO.NavActive && g.NavId != 0 && !g.NavDisableHighlight) || (g.NavWindowingTarget != NULL) || g.NavInitRequest; | ||||
|  | ||||
|     // Process NavCancel input (to close a popup, get back to parent, clear focus) | ||||
|     if (IsNavInputPressed(ImGuiNavInput_PadCancel, ImGuiInputReadMode_Pressed)) | ||||
|     if (IsNavInputPressed(ImGuiNavInput_Cancel, ImGuiInputReadMode_Pressed)) | ||||
|     { | ||||
|         if (g.ActiveId != 0) | ||||
|         { | ||||
| @@ -3045,15 +3045,15 @@ static void ImGui::NavUpdate() | ||||
|     g.NavActivateId = g.NavActivateDownId = g.NavActivatePressedId = g.NavInputId = 0; | ||||
|     if (g.NavId != 0 && !g.NavDisableHighlight && !g.NavWindowingTarget && g.NavWindow && !(g.NavWindow->Flags & ImGuiWindowFlags_NoNavInputs)) | ||||
|     { | ||||
|         bool activate_down = IsNavInputDown(ImGuiNavInput_PadActivate); | ||||
|         bool activate_pressed = activate_down && IsNavInputPressed(ImGuiNavInput_PadActivate, ImGuiInputReadMode_Pressed); | ||||
|         bool activate_down = IsNavInputDown(ImGuiNavInput_Activate); | ||||
|         bool activate_pressed = activate_down && IsNavInputPressed(ImGuiNavInput_Activate, ImGuiInputReadMode_Pressed); | ||||
|         if (g.ActiveId == 0 && activate_pressed) | ||||
|             g.NavActivateId = g.NavId; | ||||
|         if ((g.ActiveId == 0 || g.ActiveId == g.NavId) && activate_down) | ||||
|             g.NavActivateDownId = g.NavId; | ||||
|         if ((g.ActiveId == 0 || g.ActiveId == g.NavId) && activate_pressed) | ||||
|             g.NavActivatePressedId = g.NavId; | ||||
|         if ((g.ActiveId == 0 || g.ActiveId == g.NavId) && IsNavInputPressed(ImGuiNavInput_PadInput, ImGuiInputReadMode_Pressed)) | ||||
|         if ((g.ActiveId == 0 || g.ActiveId == g.NavId) && IsNavInputPressed(ImGuiNavInput_Input, ImGuiInputReadMode_Pressed)) | ||||
|             g.NavInputId = g.NavId; | ||||
|     } | ||||
|     if (g.NavWindow && (g.NavWindow->Flags & ImGuiWindowFlags_NoNavInputs)) | ||||
| @@ -3074,10 +3074,10 @@ static void ImGui::NavUpdate() | ||||
|         g.NavMoveDir = ImGuiDir_None; | ||||
|         if (g.NavWindow && !g.NavWindowingTarget && allowed_dir_flags && !(g.NavWindow->Flags & ImGuiWindowFlags_NoNavInputs)) | ||||
|         { | ||||
|             if ((allowed_dir_flags & (1<<ImGuiDir_Left))  && IsNavInputPressedAnyOfTwo(ImGuiNavInput_PadDpadLeft, ImGuiNavInput_KeyLeft, ImGuiInputReadMode_Repeat)) g.NavMoveDir = ImGuiDir_Left; | ||||
|             if ((allowed_dir_flags & (1<<ImGuiDir_Right)) && IsNavInputPressedAnyOfTwo(ImGuiNavInput_PadDpadRight,ImGuiNavInput_KeyRight,ImGuiInputReadMode_Repeat)) g.NavMoveDir = ImGuiDir_Right; | ||||
|             if ((allowed_dir_flags & (1<<ImGuiDir_Up))    && IsNavInputPressedAnyOfTwo(ImGuiNavInput_PadDpadUp,   ImGuiNavInput_KeyUp,   ImGuiInputReadMode_Repeat)) g.NavMoveDir = ImGuiDir_Up; | ||||
|             if ((allowed_dir_flags & (1<<ImGuiDir_Down))  && IsNavInputPressedAnyOfTwo(ImGuiNavInput_PadDpadDown, ImGuiNavInput_KeyDown, ImGuiInputReadMode_Repeat)) g.NavMoveDir = ImGuiDir_Down; | ||||
|             if ((allowed_dir_flags & (1<<ImGuiDir_Left))  && IsNavInputPressedAnyOfTwo(ImGuiNavInput_DpadLeft, ImGuiNavInput_KeyLeft_, ImGuiInputReadMode_Repeat)) g.NavMoveDir = ImGuiDir_Left; | ||||
|             if ((allowed_dir_flags & (1<<ImGuiDir_Right)) && IsNavInputPressedAnyOfTwo(ImGuiNavInput_DpadRight,ImGuiNavInput_KeyRight_,ImGuiInputReadMode_Repeat)) g.NavMoveDir = ImGuiDir_Right; | ||||
|             if ((allowed_dir_flags & (1<<ImGuiDir_Up))    && IsNavInputPressedAnyOfTwo(ImGuiNavInput_DpadUp,   ImGuiNavInput_KeyUp_,   ImGuiInputReadMode_Repeat)) g.NavMoveDir = ImGuiDir_Up; | ||||
|             if ((allowed_dir_flags & (1<<ImGuiDir_Down))  && IsNavInputPressedAnyOfTwo(ImGuiNavInput_DpadDown, ImGuiNavInput_KeyDown_, ImGuiInputReadMode_Repeat)) g.NavMoveDir = ImGuiDir_Down; | ||||
|         } | ||||
|     } | ||||
|     else | ||||
| @@ -7478,7 +7478,7 @@ bool ImGui::ButtonBehavior(const ImRect& bb, ImGuiID id, bool* out_hovered, bool | ||||
|     if (g.NavActivateDownId == id) | ||||
|     { | ||||
|         bool nav_activated_by_code = (g.NavActivateId == id); | ||||
|         bool nav_activated_by_inputs = IsNavInputPressed(ImGuiNavInput_PadActivate, (flags & ImGuiButtonFlags_Repeat) ? ImGuiInputReadMode_Repeat : ImGuiInputReadMode_Pressed); | ||||
|         bool nav_activated_by_inputs = IsNavInputPressed(ImGuiNavInput_Activate, (flags & ImGuiButtonFlags_Repeat) ? ImGuiInputReadMode_Repeat : ImGuiInputReadMode_Pressed); | ||||
|         if (nav_activated_by_code || nav_activated_by_inputs) | ||||
|             pressed = true; | ||||
|         if (nav_activated_by_code || nav_activated_by_inputs || g.ActiveId == id) | ||||
| @@ -8548,7 +8548,7 @@ bool ImGui::SliderBehavior(const ImRect& frame_bb, ImGuiID id, float* v, float v | ||||
|                 clicked_t = SliderBehaviorCalcRatioFromValue(*v, v_min, v_max, power, linear_zero_pos); | ||||
|                 if (decimal_precision == 0 && !is_non_linear) | ||||
|                 { | ||||
|                     if (fabsf(v_max - v_min) <= 100.0f || IsNavInputDown(ImGuiNavInput_PadTweakSlow)) | ||||
|                     if (fabsf(v_max - v_min) <= 100.0f || IsNavInputDown(ImGuiNavInput_TweakSlow)) | ||||
|                         delta = ((delta < 0.0f) ? -1.0f : +1.0f) / (v_max - v_min); // Gamepad/keyboard tweak speeds in integer steps | ||||
|                     else | ||||
|                         delta /= 100.0f; | ||||
| @@ -8556,10 +8556,10 @@ bool ImGui::SliderBehavior(const ImRect& frame_bb, ImGuiID id, float* v, float v | ||||
|                 else | ||||
|                 { | ||||
|                     delta /= 100.0f;    // Gamepad/keyboard tweak speeds in % of slider bounds | ||||
|                     if (IsNavInputDown(ImGuiNavInput_PadTweakSlow)) | ||||
|                     if (IsNavInputDown(ImGuiNavInput_TweakSlow)) | ||||
|                         delta /= 10.0f; | ||||
|                 } | ||||
|                 if (IsNavInputDown(ImGuiNavInput_PadTweakFast)) | ||||
|                 if (IsNavInputDown(ImGuiNavInput_TweakFast)) | ||||
|                     delta *= 10.0f; | ||||
|                 set_new_value = true; | ||||
|                 if ((clicked_t >= 1.0f && delta > 0.0f) || (clicked_t <= 0.0f && delta < 0.0f)) // This is to avoid applying the saturation when already past the limits | ||||
|   | ||||
		Reference in New Issue
	
	Block a user