Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for multiple menu toggle keys #41

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions MenuAPI/Menu.cs
Original file line number Diff line number Diff line change
Expand Up @@ -592,6 +592,8 @@ public ButtonPressHandler(Control control, ControlPressCheckType pressType, Acti
}
public List<ButtonPressHandler> ButtonPressHandlers = new List<ButtonPressHandler>();

public Control MenuToggleKey;

#endregion

#region Constructors
Expand Down
36 changes: 12 additions & 24 deletions MenuAPI/MenuController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,13 +67,6 @@ public class MenuController : BaseScript
public static bool PreventExitingMenu { get; set; } = false;
public static bool DisableBackButton { get; set; } = false;
public static bool SetDrawOrder { get; set; } = true;
public static Control MenuToggleKey { get; set; }
#if FIVEM
= Control.InteractionMenu;
#endif
#if REDM
= Control.PlayerMenu;
#endif

public static bool EnableMenuToggleKeyOnController { get; set; } = true;

Expand Down Expand Up @@ -235,6 +228,11 @@ public static Menu GetCurrentMenu()
return null;
}

public static HashSet<Menu> GetCurrentOpenMenus()
{
return VisibleMenus;
}

/// <summary>
/// Returns true if any menu is currently open.
/// </summary>
Expand Down Expand Up @@ -705,25 +703,15 @@ private async Task HandleDownNavigation(Menu currentMenu)
private void HandleMenuToggleKeyForKeyboard()
{
if (
(Game.IsControlJustPressed(0, MenuToggleKey) || Game.IsDisabledControlJustPressed(0, MenuToggleKey)) &&
!Game.IsPaused &&
!Game.Player.IsDead &&
!IsPlayerSwitchInProgress() &&
!DontOpenAnyMenu &&
IsScreenFadedIn()
)
{
if (!Menus.Any())
{
return;
}
if (MainMenu != null)
{
MainMenu.OpenMenu();
}
else
{
Menus.First().OpenMenu();
foreach (Menu menu in Menus.Where(menu => !menu.Visible && (Game.IsControlJustPressed(0, menu.MenuToggleKey) || Game.IsDisabledControlJustPressed(0, menu.MenuToggleKey)))) {
menu.OpenMenu();
}
}
}
Expand Down Expand Up @@ -754,15 +742,15 @@ private async Task HandleMenuToggleKeyForController()

private void DisableMenuKeyThisFrame()
{
Game.DisableControlThisFrame(0, MenuToggleKey);
if (Game.CurrentInputMode == InputMode.MouseAndKeyboard)
{
if ((Game.IsControlJustPressed(0, MenuToggleKey) || Game.IsDisabledControlJustPressed(0, MenuToggleKey)) && !PreventExitingMenu)
var menus = new HashSet<Menu>(GetCurrentOpenMenus());
foreach (Menu menu in menus)
{
var menu = GetCurrentMenu();
if (menu != null)
Game.DisableControlThisFrame(0, menu.MenuToggleKey);
if ((Game.IsControlJustPressed(0, menu.MenuToggleKey) || Game.IsDisabledControlJustPressed(0, menu.MenuToggleKey)) && !PreventExitingMenu)
{
menu.CloseMenu();
menu?.CloseMenu();
}
}
}
Expand Down