Skip to content

Commit

Permalink
Add menu.FilterMenuItems and menu.ResetFilter functions
Browse files Browse the repository at this point in the history
  • Loading branch information
TomGrobbe committed Feb 9, 2019
1 parent 86477aa commit e0d72ea
Showing 1 changed file with 89 additions and 19 deletions.
108 changes: 89 additions & 19 deletions MenuAPI/Menu.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
Expand Down Expand Up @@ -298,11 +298,21 @@ private List<MenuItem> VisibleMenuItems
get
{
// Create a duplicate list, just in case the original list is modified while we're looping through it.
var items = GetMenuItems().ToList().GetRange(ViewIndexOffset, Math.Min(MaxItemsOnScreen, Size - ViewIndexOffset));
return items;
if (filterActive)
{
var items = _FilterItems.ToList().GetRange(ViewIndexOffset, Math.Min(MaxItemsOnScreen, Size - ViewIndexOffset));
return items;
}
else
{
var items = GetMenuItems().ToList().GetRange(ViewIndexOffset, Math.Min(MaxItemsOnScreen, Size - ViewIndexOffset));
return items;
}

}
}

private List<MenuItem> _FilterItems { get; set; } = new List<MenuItem>();
private List<MenuItem> _MenuItems { get; set; } = new List<MenuItem>();

private readonly int ColorPanelScaleform = RequestScaleformMovie("COLOUR_SWITCHER_02"); // Could probably be improved, but was getting some glitchy results if it wasn't pre-loaded.
Expand All @@ -320,7 +330,7 @@ private List<MenuItem> VisibleMenuItems

public int MaxItemsOnScreen { get; internal set; } = 10;

public int Size => _MenuItems.Count;
public int Size => filterActive ? _FilterItems.Count : _MenuItems.Count;

public bool Visible { get; set; } = false;

Expand All @@ -338,6 +348,8 @@ private List<MenuItem> VisibleMenuItems

public bool EnableInstructionalButtons { get; set; } = true;

private bool filterActive = false;

public Dictionary<Control, string> InstructionalButtons = new Dictionary<Control, string>() { { Control.FrontendAccept, GetLabelText("HUD_INPUT28") }, { Control.FrontendCancel, GetLabelText("HUD_INPUT53") } };

public enum ControlPressCheckType
Expand Down Expand Up @@ -417,7 +429,7 @@ public void SetMaxItemsOnScreen(int max)
/// <returns></returns>
public List<MenuItem> GetMenuItems()
{
return _MenuItems.ToList();
return filterActive ? _FilterItems.ToList() : _MenuItems.ToList();
}

/// <summary>
Expand All @@ -428,6 +440,7 @@ public void ClearMenuItems()
CurrentIndex = 0;
ViewIndexOffset = 0;
_MenuItems.Clear();
_FilterItems.Clear();
}
/// <summary>
/// Removes all menu items.
Expand All @@ -440,6 +453,7 @@ public void ClearMenuItems(bool dontResetIndex)
ViewIndexOffset = 0;
}
_MenuItems.Clear();
_FilterItems.Clear();
}

/// <summary>
Expand Down Expand Up @@ -506,9 +520,19 @@ public void RemoveMenuItem(MenuItem item)
/// <param name="index"></param>
public void SelectItem(int index)
{
if (index > -1 && _MenuItems.Count - 1 >= index)
if (!filterActive)
{
if (index > -1 && _MenuItems.Count - 1 >= index)
{
SelectItem(_MenuItems[index]);
}
}
else
{
SelectItem(_MenuItems[index]);
if (index > -1 && _FilterItems.Count - 1 >= index)
{
SelectItem(_FilterItems[index]);
}
}
}

Expand Down Expand Up @@ -589,12 +613,24 @@ public void GoUp()
{
if (Visible && Size > 1)
{
var oldItem = _MenuItems[CurrentIndex];
MenuItem oldItem = null;

if (filterActive)
{
oldItem = _FilterItems[CurrentIndex];
}
else
{
oldItem = _MenuItems[CurrentIndex];
}

CurrentIndex--; if (CurrentIndex < 0)
{
CurrentIndex = Size - 1;
}
if (!VisibleMenuItems.Contains(_MenuItems[CurrentIndex]))


if (!VisibleMenuItems.Contains(filterActive ? _FilterItems[CurrentIndex] : _MenuItems[CurrentIndex]))
{
ViewIndexOffset--;
if (ViewIndexOffset < 0)
Expand All @@ -603,7 +639,7 @@ public void GoUp()
}
}

IndexChangeEvent(this, oldItem, _MenuItems[CurrentIndex], oldItem.Index, CurrentIndex);
IndexChangeEvent(this, oldItem, filterActive ? _FilterItems[CurrentIndex] : _MenuItems[CurrentIndex], oldItem.Index, CurrentIndex);
PlaySoundFrontend(-1, "NAV_UP_DOWN", "HUD_FRONTEND_DEFAULT_SOUNDSET", false);
}
}
Expand All @@ -615,21 +651,31 @@ public void GoDown()
{
if (Visible && Size > 1)
{
var oldItem = _MenuItems[CurrentIndex];
MenuItem oldItem = null;

if (filterActive)
{
oldItem = _FilterItems[CurrentIndex];
}
else
{
oldItem = _MenuItems[CurrentIndex];
}

CurrentIndex++;
if (CurrentIndex >= Size)
{
CurrentIndex = 0;
}
if (!VisibleMenuItems.Contains(_MenuItems[CurrentIndex]))
if (!VisibleMenuItems.Contains(filterActive ? _FilterItems[CurrentIndex] : _MenuItems[CurrentIndex]))
{
ViewIndexOffset++;
if (CurrentIndex == 0)
{
ViewIndexOffset = 0;
}
}
IndexChangeEvent(this, oldItem, _MenuItems[CurrentIndex], oldItem.Index, CurrentIndex);
IndexChangeEvent(this, oldItem, filterActive ? _FilterItems[CurrentIndex] : _MenuItems[CurrentIndex], oldItem.Index, CurrentIndex);
PlaySoundFrontend(-1, "NAV_UP_DOWN", "HUD_FRONTEND_DEFAULT_SOUNDSET", false);
}
}
Expand All @@ -641,7 +687,7 @@ public void GoLeft()
{
if (MenuController.AreMenuButtonsEnabled)
{
var item = _MenuItems.ElementAt(CurrentIndex);
var item = filterActive ? _FilterItems[CurrentIndex] : _MenuItems[CurrentIndex];
if (item.Enabled && item is MenuListItem listItem)
{
if (listItem.ItemsCount > 0)
Expand Down Expand Up @@ -692,7 +738,7 @@ public void GoRight()
{
if (MenuController.AreMenuButtonsEnabled)
{
var item = _MenuItems.ElementAt(CurrentIndex);
var item = filterActive ? _FilterItems[CurrentIndex] : _MenuItems[CurrentIndex];
if (item.Enabled && item is MenuListItem listItem)
{
if (listItem.ItemsCount > 0)
Expand Down Expand Up @@ -742,9 +788,33 @@ public void GoRight()
/// <param name="compare"></param>
public void SortMenuItems(Comparison<MenuItem> compare)
{
if (filterActive)
{
filterActive = false;
_FilterItems.Clear();
}
_MenuItems.Sort(compare);
}

public void FilterMenuItems(Func<MenuItem, bool> predicate)
{
if (filterActive)
{
ResetFilter();
}
RefreshIndex(0, 0);
ViewIndexOffset = 0;
_FilterItems = _MenuItems.Where(i => predicate.Invoke(i)).ToList();
filterActive = true;
}

public void ResetFilter()
{
RefreshIndex(0, 0);
filterActive = false;
_FilterItems.Clear();
}

#endregion
#region internal task functions
/// <summary>
Expand Down Expand Up @@ -945,7 +1015,7 @@ internal async void Draw()
SetScriptGfxAlign(LeftAligned ? 76 : 82, 84);
SetScriptGfxAlignParams(0f, 0f, 0f, 0f);

float bgHeight = 38f * MathUtil.Clamp(_MenuItems.Count, 0, MaxItemsOnScreen);
float bgHeight = 38f * MathUtil.Clamp(Size, 0, MaxItemsOnScreen);


float x = (Position.X + (headerSize.Width / 2f)) / MenuController.ScreenWidth;
Expand Down Expand Up @@ -1048,7 +1118,7 @@ internal async void Draw()
#region Draw Description
if (Size > 0)
{
if (!string.IsNullOrEmpty(_MenuItems[CurrentIndex].Description))
if (!string.IsNullOrEmpty(filterActive ? _FilterItems[CurrentIndex].Description : _MenuItems[CurrentIndex].Description))
{
#region description text
int font = 0;
Expand All @@ -1066,7 +1136,7 @@ internal async void Draw()
SetTextFont(font);
SetTextScale(textSize, textSize);
SetTextJustification(1);
string text = _MenuItems[CurrentIndex].Description;
string text = filterActive ? _FilterItems[CurrentIndex].Description : _MenuItems[CurrentIndex].Description;
foreach (string s in CitizenFX.Core.UI.Screen.StringToArray(text))
{
AddTextComponentSubstringPlayerName(s);
Expand Down Expand Up @@ -1142,7 +1212,7 @@ internal async void Draw()
#region Draw Color and opacity palletes
if (Size > 0)
{
var currentItem = _MenuItems[CurrentIndex];
var currentItem = filterActive ? _FilterItems[CurrentIndex] : _MenuItems[CurrentIndex];
if (currentItem is MenuListItem listItem)
{
/// OPACITY PANEL
Expand Down

0 comments on commit e0d72ea

Please sign in to comment.