Skip to content

Commit

Permalink
C#/Lua UIMenu: Added itemless menu with long description (like GTA:O)
Browse files Browse the repository at this point in the history
  • Loading branch information
manups4e committed Nov 5, 2023
1 parent 3824227 commit 784978e
Show file tree
Hide file tree
Showing 2 changed files with 144 additions and 16 deletions.
111 changes: 99 additions & 12 deletions ScaleformUI_Csharp/Menus/UIMenu/UIMenu.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1033,7 +1033,7 @@ public ItemFont DescriptionFont
private bool canBuild = true;
private bool isFading;
private float fadingTime = 0.1f;

internal bool itemless = false;
public PointF Offset { get; internal set; }

public List<UIMenuWindow> Windows = new List<UIMenuWindow>();
Expand Down Expand Up @@ -1155,6 +1155,7 @@ public UIMenu(string title, string subtitle, PointF offset, KeyValuePair<string,
/// <param name="spriteName">Sprite name for the banner.</param>
/// <param name="glare">Add menu Glare scaleform?.</param>
/// <param name="alternativeTitle">Set the alternative type to the title?.</param>
/// <param name="fadingTime">Set fading time for the menu and the items, set it to 0.0 to disable it.</param>
public UIMenu(string title, string subtitle, PointF offset, string spriteLibrary, string spriteName, bool glare = false, bool alternativeTitle = false, float fadingTime = 0.1f)
{
_customTexture = new KeyValuePair<string, string>(spriteLibrary, spriteName);
Expand Down Expand Up @@ -1186,6 +1187,50 @@ public UIMenu(string title, string subtitle, PointF offset, string spriteLibrary
new InstructionalButton(Control.PhoneCancel, _backTextLocalized)
};
}
/// <summary>
/// This is an itemless menu, it meas you cannot add items to this menu but you can add a description like on GTA:O
/// </summary>
/// <param name="title">Title that appears on the big banner. Set to "" if you are using a custom banner.</param>
/// <param name="subtitle">Subtitle that appears in capital letters in a small black bar.</param>
/// <param name="offset">PointF object with X and Y data for offsets. Applied to all menu elements.</param>
/// <param name="spriteLibrary">Sprite library name for the banner.</param>
/// <param name="spriteName">Sprite name for the banner.</param>
/// <param name="glare">Add menu Glare scaleform?.</param>
/// <param name="alternativeTitle">Set the alternative type to the title?.</param>
/// <param name="fadingTime">Set fading time for the menu and the items, set it to 0.0 to disable it.</param>
public UIMenu(string title, string subtitle, string description, PointF offset, string spriteLibrary, string spriteName, bool glare = false, bool alternativeTitle = false, float fadingTime = 0.1f)
{
_customTexture = new KeyValuePair<string, string>(spriteLibrary, spriteName);
Offset = offset;
WidthOffset = 0;
Glare = glare;
_menuGlare = new ScaleformWideScreen("mp_menu_glare");
Title = title;
Subtitle = subtitle;
AlternativeTitle = alternativeTitle;
MouseWheelControlEnabled = true;
Pagination = new PaginationHandler();
Pagination.ItemsPerPage = 7;
this.fadingTime = fadingTime;

SetKey(MenuControls.Up, Control.PhoneUp);
SetKey(MenuControls.Down, Control.PhoneDown);

SetKey(MenuControls.Left, Control.PhoneLeft);
SetKey(MenuControls.Right, Control.PhoneRight);
SetKey(MenuControls.Select, Control.FrontendAccept);

SetKey(MenuControls.Back, Control.PhoneCancel);
SetKey(MenuControls.Back, Control.FrontendPause);

InstructionalButtons = new List<InstructionalButton>()
{
new InstructionalButton(Control.PhoneSelect, _selectTextLocalized),
new InstructionalButton(Control.PhoneCancel, _backTextLocalized)
};
itemless = true;
AddTextEntry("ScaleformUILongDesc", description);
}

#endregion

Expand Down Expand Up @@ -1273,12 +1318,16 @@ public void SetBannerType(KeyValuePair<string, string> pathToCustomSprite)
/// <param name="item">Item object to be added. Can be normal item, checkbox or list item.</param>
public void AddItem(UIMenuItem item)
{
int selectedItem = CurrentSelection;
item.Parent = this;
MenuItems.Add(item);
if (Visible)
CurrentSelection = selectedItem;
Pagination.TotalItems = MenuItems.Count;
if (!itemless)
{
int selectedItem = CurrentSelection;
item.Parent = this;
MenuItems.Add(item);
if (Visible)
CurrentSelection = selectedItem;
Pagination.TotalItems = MenuItems.Count;
}
else throw new Exception("ScaleformUI - You cannot add items to an itemless menu, only a long description");
}

/// <summary>
Expand All @@ -1287,8 +1336,12 @@ public void AddItem(UIMenuItem item)
/// <param name="window"></param>
public void AddWindow(UIMenuWindow window)
{
window.ParentMenu = this;
Windows.Add(window);
if (!itemless)
{
window.ParentMenu = this;
Windows.Add(window);
}
else throw new Exception("ScaleformUI - You cannot add windows to an itemless menu, only a long description");
}

/// <summary>
Expand All @@ -1301,7 +1354,7 @@ public void RemoveWindowAt(int index)
}

/// <summary>
/// If a Description is changed during some events after the menu as been opened this updates the description live
/// If an item's description is changed during some events after the menu as been opened this updates the description live
/// </summary>
public void UpdateDescription()
{
Expand Down Expand Up @@ -2198,7 +2251,7 @@ public override bool Visible
_itemsDirty = value;
if (value)
{
if (this.MenuItems.Count == 0)
if (!itemless && this.MenuItems.Count == 0)
{
MenuHandler.CloseAndClearHistory();
throw new Exception($"UIMenu {this.Title} menu is empty... Closing and clearing history.");
Expand Down Expand Up @@ -2235,11 +2288,41 @@ internal async void BuildUpMenuAsync(bool itemsOnly = false)
{
isBuilding = true;
bool _animEnabled = EnableAnimation;
if (itemless)
{
EnableAnimation = false;
while (!Main.scaleformUI.IsLoaded) await BaseScript.Delay(0);
//Main.scaleformUI.CallFunction("CREATE_MENU", Title, Subtitle, Offset.X, Offset.Y, AlternativeTitle, _customTexture.Key, _customTexture.Value, MaxItemsOnScreen, MenuItems.Count, EnableAnimation, (int)AnimationType, (int)buildingAnimation, counterColor, descriptionFont.FontName, descriptionFont.FontID, fadingTime, true);
BeginScaleformMovieMethod(Main.scaleformUI.Handle, "CREATE_MENU");
PushScaleformMovieMethodParameterString(Title);
PushScaleformMovieMethodParameterString(Subtitle);
PushScaleformMovieMethodParameterFloat(Offset.X);
PushScaleformMovieMethodParameterFloat(Offset.Y);
PushScaleformMovieMethodParameterBool(AlternativeTitle);
PushScaleformMovieMethodParameterString(_customTexture.Key);
PushScaleformMovieMethodParameterString(_customTexture.Value);
PushScaleformMovieFunctionParameterInt(MaxItemsOnScreen);
PushScaleformMovieFunctionParameterInt(MenuItems.Count);
PushScaleformMovieFunctionParameterBool(EnableAnimation);
PushScaleformMovieFunctionParameterInt((int)AnimationType);
PushScaleformMovieFunctionParameterInt((int)buildingAnimation);
PushScaleformMovieFunctionParameterInt(counterColor.ArgbValue);
PushScaleformMovieMethodParameterString(descriptionFont.FontName);
PushScaleformMovieFunctionParameterInt(descriptionFont.FontID);
PushScaleformMovieMethodParameterFloat(fadingTime);
PushScaleformMovieFunctionParameterBool(true);
BeginTextCommandScaleformString("ScaleformUILongDesc");
EndTextCommandScaleformString_2();
EndScaleformMovieMethod();
FadeInMenu();
isBuilding = false;
return;
}
if (!itemsOnly)
{
EnableAnimation = false;
while (!Main.scaleformUI.IsLoaded) await BaseScript.Delay(0);
Main.scaleformUI.CallFunction("CREATE_MENU", Title, Subtitle, Offset.X, Offset.Y, AlternativeTitle, _customTexture.Key, _customTexture.Value, MaxItemsOnScreen, MenuItems.Count, EnableAnimation, (int)AnimationType, (int)buildingAnimation, counterColor, descriptionFont.FontName, descriptionFont.FontID, fadingTime);
Main.scaleformUI.CallFunction("CREATE_MENU", Title, Subtitle, Offset.X, Offset.Y, AlternativeTitle, _customTexture.Key, _customTexture.Value, MaxItemsOnScreen, MenuItems.Count, EnableAnimation, (int)AnimationType, (int)buildingAnimation, counterColor, descriptionFont.FontName, descriptionFont.FontID, fadingTime, false);
if (Windows.Count > 0)
{
foreach (UIMenuWindow wind in Windows)
Expand Down Expand Up @@ -2327,6 +2410,7 @@ internal async void BuildUpMenuAsync(bool itemsOnly = false)

public void SortMenuItems(Comparison<UIMenuItem> compare)
{
if (itemless) throw new("ScaleformUI - You can't compare or sort an itemless menu");
MenuItems[CurrentSelection].Selected = false;
_unfilteredMenuItems = MenuItems.ToList();
Clear();
Expand All @@ -2339,6 +2423,7 @@ public void SortMenuItems(Comparison<UIMenuItem> compare)

public void FilterMenuItems(Func<UIMenuItem, bool> predicate)
{
if (itemless) throw new("ScaleformUI - You can't compare or sort an itemless menu");
MenuItems[CurrentSelection].Selected = false;
_unfilteredMenuItems = MenuItems.ToList();
Clear();
Expand All @@ -2349,6 +2434,7 @@ public void FilterMenuItems(Func<UIMenuItem, bool> predicate)

public void ResetFilter()
{
if (itemless) throw new("ScaleformUI - You can't compare or sort an itemless menu");
MenuItems[CurrentSelection].Selected = false;
Clear();
MenuItems = _unfilteredMenuItems.ToList();
Expand All @@ -2359,6 +2445,7 @@ public void ResetFilter()

private void _itemCreation(int page, int pageIndex, bool before, bool isOverflow = false)
{
if (itemless) throw new("ScaleformUI - You can't add items to an itemless menu");
int menuIndex = Pagination.GetMenuIndexFromPageIndex(page, pageIndex);
if (!before)
{
Expand Down
49 changes: 45 additions & 4 deletions ScaleformUI_Lua/src/Menus/UIMenu/UIMenu.lua
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ end
---@param txtDictionary string? -- Custom texture dictionary for the menu banner background (default: commonmenu)
---@param txtName string? -- Custom texture name for the menu banner background (default: interaction_bgd)
---@param alternativeTitleStyle boolean? -- Use alternative title style (default: false)
function UIMenu.New(title, subTitle, x, y, glare, txtDictionary, txtName, alternativeTitleStyle, fadeTime)
function UIMenu.New(title, subTitle, x, y, glare, txtDictionary, txtName, alternativeTitleStyle, fadeTime, longdesc)
local X, Y = tonumber(x) or 0, tonumber(y) or 0
if title ~= nil then
title = tostring(title) or ""
Expand All @@ -79,6 +79,9 @@ function UIMenu.New(title, subTitle, x, y, glare, txtDictionary, txtName, altern
if alternativeTitleStyle == nil then
alternativeTitleStyle = false
end
if longdesc ~= nil then
AddTextEntry("ScaleformUILongDesc", longdesc)
end
local _UIMenu = {
_Title = title,
_Subtitle = subTitle,
Expand All @@ -100,6 +103,7 @@ function UIMenu.New(title, subTitle, x, y, glare, txtDictionary, txtName, altern
TxtName = txtName,
Glare = glare or false,
Logo = nil,
_itemless = longdesc ~= nil,
_keyboard = false,
_changed = false,
_maxItem = 7,
Expand Down Expand Up @@ -491,6 +495,8 @@ end
---AddWindow
---@param window table
function UIMenu:AddWindow(window)
assert(not self._itemless, "ScaleformUI - You cannot add windows to an itemless menu, only a long description")

if window() == "UIMenuWindow" then
window:SetParentMenu(self)
self.Windows[#self.Windows + 1] = window
Expand All @@ -511,6 +517,7 @@ end
---@param item UIMenuItem
---@see UIMenuItem
function UIMenu:AddItem(item)
assert(not self._itemless, "ScaleformUI - You cannot add items to an itemless menu, only a long description")
if item() ~= "UIMenuItem" then
return
end
Expand Down Expand Up @@ -581,6 +588,10 @@ function UIMenu:Visible(bool)
self.Dirty = ToBool(bool)

if bool then
if not self._itemless and #self.Items == 0 then
MenuHandler:CloseAndClearHistory()
assert(self._itemless or #self.Items == 0, "UIMenu ".. self:Title() .. " menu is empty... Closing and clearing history.")
end
ScaleformUI.Scaleforms.InstructionalButtons:SetInstructionalButtons(self.InstructionalButtons)
MenuHandler._currentMenu = self
MenuHandler.ableToDraw = true
Expand All @@ -606,14 +617,41 @@ end
function UIMenu:BuildUpMenuAsync(itemsOnly)
if itemsOnly == nil then itemsOnly = false end
self._isBuilding = true

if self._itemless then
BeginScaleformMovieMethod(ScaleformUI.Scaleforms._ui.handle, "CREATE_MENU");
PushScaleformMovieMethodParameterString(self._Title)
PushScaleformMovieMethodParameterString(self._Subtitle)
PushScaleformMovieMethodParameterFloat(self.Position.x)
PushScaleformMovieMethodParameterFloat(self.Position.y)
PushScaleformMovieMethodParameterBool(self.AlternativeTitle)
PushScaleformMovieMethodParameterString(self.TxtDictionary)
PushScaleformMovieMethodParameterString(self.TxtName)
PushScaleformMovieFunctionParameterInt(self:MaxItemsOnScreen())
PushScaleformMovieFunctionParameterInt(#self.Items)
PushScaleformMovieFunctionParameterBool(self:AnimationEnabled())
PushScaleformMovieFunctionParameterInt(self:AnimationType())
PushScaleformMovieFunctionParameterInt(self:BuildingAnimation())
PushScaleformMovieFunctionParameterInt(self.counterColor:ToArgb())
PushScaleformMovieMethodParameterString(self.descFont.FontName)
PushScaleformMovieFunctionParameterInt(self.descFont.FontID)
PushScaleformMovieMethodParameterFloat(self.fadingTime)
PushScaleformMovieFunctionParameterBool(true)
BeginTextCommandScaleformString("ScaleformUILongDesc");
EndTextCommandScaleformString_2()
EndScaleformMovieMethod()
self:FadeInMenu()
self._isBuilding = false;
return;

end

if not itemsOnly then
Citizen.CreateThread(function()
local enab = self:AnimationEnabled()
self:AnimationEnabled(false)
while not ScaleformUI.Scaleforms._ui:IsLoaded() do Citizen.Wait(0) end
ScaleformUI.Scaleforms._ui:CallFunction("CREATE_MENU", false, self._Title, self._Subtitle, self.Position.x,
self.Position.y,
self.AlternativeTitle, self.TxtDictionary, self.TxtName, self:MaxItemsOnScreen(), #self.Items, true,
self.AlternativeTitle, self.TxtDictionary, self.TxtName, self:MaxItemsOnScreen(), #self.Items, self:AnimationEnabled(),
self:AnimationType(), self:BuildingAnimation(), self.counterColor, self.descFont.FontName,
self.descFont.FontID, self.fadingTime)
if #self.Windows > 0 then
Expand Down Expand Up @@ -846,6 +884,7 @@ function UIMenu:_itemCreation(page, pageIndex, before, overflow)
end

function UIMenu:FilterMenuItems(predicate)
assert(not self._itemless, "ScaleformUI - You can't compare or sort an itemless menu")
self.Items[self:CurrentSelection()]:Selected(false)
self._unfilteredMenuItems = self.Items
self:Clear()
Expand All @@ -859,6 +898,7 @@ function UIMenu:FilterMenuItems(predicate)
end

function UIMenu:SortMenuItems(compare)
assert(not self._itemless, "ScaleformUI - You can't compare or sort an itemless menu")
self.Items[self:CurrentSelection()]:Selected(false)
self._unfilteredMenuItems = self.Items
self:Clear()
Expand All @@ -873,6 +913,7 @@ function UIMenu:SortMenuItems(compare)
end

function UIMenu:ResetFilter()
assert(not self._itemless, "ScaleformUI - You can't compare or sort an itemless menu")
self.Items[self:CurrentSelection()]:Selected(false)
self:Clear()
self.Items = self._unfilteredMenuItems
Expand Down

0 comments on commit 784978e

Please sign in to comment.