Skip to content

Commit

Permalink
Merge pull request #34 from blattersturm/fix/menu-visibility-performance
Browse files Browse the repository at this point in the history
Profiler hot path fix: unneeded LINQ .Any invocation
  • Loading branch information
TomGrobbe authored Oct 28, 2020
2 parents c23651e + 8503ddc commit 63eccbb
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 6 deletions.
23 changes: 22 additions & 1 deletion MenuAPI/Menu.cs
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,8 @@ protected virtual void DynamicListItemSelectEvent(Menu menu, MenuDynamicListItem

private int index = 0;

private bool visible = false;

public int ViewIndexOffset { get; private set; } = 0;

private List<MenuItem> VisibleMenuItems
Expand Down Expand Up @@ -371,7 +373,26 @@ private List<MenuItem> VisibleMenuItems

public int Size => filterActive ? FilterItems.Count : MenuItems.Count;

public bool Visible { get; set; } = false;
public bool Visible
{
get
{
return visible;
}
set
{
if (value)
{
MenuController.VisibleMenus.Add(this);
}
else
{
MenuController.VisibleMenus.Remove(this);
}

visible = value;
}
}

#if FIVEM
public bool LeftAligned => MenuController.MenuAlignment == MenuController.MenuAlignmentOption.Left;
Expand Down
11 changes: 6 additions & 5 deletions MenuAPI/MenuController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ namespace MenuAPI
public class MenuController : BaseScript
{
public static List<Menu> Menus { get; protected set; } = new List<Menu>();
internal static HashSet<Menu> VisibleMenus { get; } = new HashSet<Menu>();
#if FIVEM
public const string _texture_dict = "commonmenu";
public const string _header_texture = "interaction_bgd";
Expand Down Expand Up @@ -52,11 +53,11 @@ public class MenuController : BaseScript
public static float ScreenHeight => 1080;
public static bool DisableMenuButtons { get; set; } = false;
#if FIVEM
public static bool AreMenuButtonsEnabled => Menus.Any((m) => m.Visible) && !Game.IsPaused && CitizenFX.Core.UI.Screen.Fading.IsFadedIn && !IsPlayerSwitchInProgress() && !DisableMenuButtons && !Game.Player.IsDead;
public static bool AreMenuButtonsEnabled => IsAnyMenuOpen() && !Game.IsPaused && CitizenFX.Core.UI.Screen.Fading.IsFadedIn && !IsPlayerSwitchInProgress() && !DisableMenuButtons && !Game.Player.IsDead;
#endif
#if REDM
public static bool AreMenuButtonsEnabled =>
Menus.Any((m) => m.Visible) &&
IsAnyMenuOpen() &&
!Call<bool>(IS_PAUSE_MENU_ACTIVE) &&
Call<bool>(IS_SCREEN_FADED_IN) &&
!DisableMenuButtons &&
Expand Down Expand Up @@ -268,16 +269,16 @@ private static void UnloadAssets()
/// <returns></returns>
public static Menu GetCurrentMenu()
{
if (Menus.Any((m) => m.Visible))
return Menus.Find((m) => m.Visible);
if (IsAnyMenuOpen())
return VisibleMenus.FirstOrDefault();
return null;
}

/// <summary>
/// Returns true if any menu is currently open.
/// </summary>
/// <returns></returns>
public static bool IsAnyMenuOpen() => Menus.Any((m) => m.Visible);
public static bool IsAnyMenuOpen() => VisibleMenus.Count > 0;


#region Process Menu Buttons
Expand Down

0 comments on commit 63eccbb

Please sign in to comment.