Skip to content

Commit 11aec67

Browse files
committed
fix(input): drain custom key bindings the open menu does not handle
1 parent 9165dfe commit 11aec67

3 files changed

Lines changed: 52 additions & 1 deletion

File tree

MenuAPI/Menu.cs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1479,6 +1479,26 @@ public void SetVehicleUpgradeStats(float topSpeed, float acceleration, float bra
14791479
#endregion
14801480

14811481
#region internal/private task functions
1482+
// Answers what MenuInput.DrainUnhandled needs to know: whether a press on this binding is going
1483+
// to reach a handler, so everything else can be thrown away instead of firing later.
1484+
internal bool Handles(MenuKeyBinding binding)
1485+
{
1486+
if (KeyBindingHandlers.Count == 0 || MenuController.DisableMenuButtons)
1487+
{
1488+
return false;
1489+
}
1490+
1491+
foreach (KeyBindingHandler entry in KeyBindingHandlers)
1492+
{
1493+
if (entry.Handler is not null && ReferenceEquals(entry.Binding, binding))
1494+
{
1495+
return true;
1496+
}
1497+
}
1498+
1499+
return false;
1500+
}
1501+
14821502
internal void ProcessKeyBindings()
14831503
{
14841504
if (KeyBindingHandlers.Count == 0 || MenuController.DisableMenuButtons)

MenuAPI/MenuController.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -462,6 +462,8 @@ private static async Task ProcessMainButtons()
462462
bool selectPressed = MenuInput.ConsumeSelect();
463463
bool backPressed = MenuInput.ConsumeBack();
464464

465+
MenuInput.DrainUnhandled(MenuReadingKeyBindings());
466+
465467
if (FrameState.IsPauseMenuActive)
466468
{
467469
return;
@@ -1118,6 +1120,18 @@ private static void RefreshTextures()
11181120
public static void RefreshNui() => MenuNui.Invalidate();
11191121

11201122
/// <summary>The game states that stop a menu being drawn, none of which announce a change.</summary>
1123+
private static Menu? MenuReadingKeyBindings()
1124+
{
1125+
if (!CanDraw() || DontOpenAnyMenu)
1126+
{
1127+
return null;
1128+
}
1129+
1130+
Menu? menu = GetCurrentMenu();
1131+
1132+
return menu is not null && menu.Visible ? menu : null;
1133+
}
1134+
11211135
private static bool CanDraw() =>
11221136
FrameState.IsScreenFadedIn &&
11231137
!FrameState.IsPauseMenuActive &&

MenuAPI/MenuInput.cs

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using CitizenFX.FiveM.Client;
1+
using CitizenFX.FiveM.Client;
22
using CitizenFX.FiveM.Shared;
33

44
namespace MenuAPI;
@@ -205,6 +205,23 @@ internal static void ClearHeld()
205205
}
206206
}
207207

208+
/// <summary>
209+
/// Drops the pending press and release on every custom binding <paramref name="menu"/> is not
210+
/// about to read this frame.
211+
/// </summary>
212+
internal static void DrainUnhandled(Menu? menu)
213+
{
214+
foreach (MenuKeyBinding binding in Custom.Values)
215+
{
216+
if (menu is not null && menu.Handles(binding))
217+
{
218+
continue;
219+
}
220+
221+
binding.ClearPending();
222+
}
223+
}
224+
208225
internal static void ClearPending()
209226
{
210227
foreach (MenuKeyBinding binding in Own)

0 commit comments

Comments
 (0)