Skip to content

Commit 28208b8

Browse files
committed
RedM: Add description text support.
1 parent 4d56cbd commit 28208b8

3 files changed

Lines changed: 108 additions & 19 deletions

File tree

MenuAPI/Menu.cs

Lines changed: 65 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1284,8 +1284,8 @@ internal async void Draw()
12841284
}
12851285
#endif
12861286

1287-
#if FIVEM
12881287
#region draw counter + pre-counter text
1288+
#if FIVEM
12891289
string counterText = $"{CounterPreText ?? ""}{CurrentIndex + 1} / {Size}";
12901290
if (!string.IsNullOrEmpty(CounterPreText) || MaxItemsOnScreen < Size)
12911291
{
@@ -1325,9 +1325,26 @@ internal async void Draw()
13251325
{
13261326
MenuItemsYOffset += bgHeight - 1f;
13271327
}
1328+
#endif
1329+
#if REDM
1330+
if (Size > 0)
1331+
{
1332+
float textSize = (12f * 27f) / MenuController.ScreenHeight;
1333+
Call(SET_TEXT_SCALE, textSize, textSize);
1334+
Call((CitizenFX.Core.Native.Hash)0x50A41AD966910F03, 135, 135, 135, 255); // _SET_TEXT_COLOUR / 0x50A41AD966910F03
1335+
Call(SET_TEXT_CENTRE, true);
1336+
float textMinX = (headerSize.Key / 2f) / MenuController.ScreenWidth;
1337+
float textMaxX = (Width - 10f) / MenuController.ScreenWidth;
1338+
float textY = (MenuItemsYOffset + 38f * (MathUtil.Clamp(Size, 0, MaxItemsOnScreen) + 1) - 11f) / MenuController.ScreenHeight;
1339+
int font = 23;
1340+
Call((CitizenFX.Core.Native.Hash)0xADA9255D, font);
1341+
//SetTextWrap(textMinX, textMaxX);
1342+
1343+
Call(_DRAW_TEXT, Call<long>(_CREATE_VAR_STRING, 10, "LITERAL_STRING", $"{CurrentIndex + 1} of {Size}"), textMinX, textY);
1344+
}
1345+
#endif
13281346

13291347
#endregion
1330-
#endif
13311348
}
13321349
#endregion
13331350

@@ -1339,7 +1356,6 @@ internal async void Draw()
13391356
SetScriptGfxAlignParams(0f, 0f, 0f, 0f);
13401357
#endif
13411358

1342-
13431359
//DrawSprite(MenuController._texture_dict, "gradient_bgd", x, y, width, height, 0f, 255, 255, 255, 255);
13441360
#if FIVEM
13451361
float bgHeight = 38f * MathUtil.Clamp(Size, 0, MaxItemsOnScreen);
@@ -1351,20 +1367,39 @@ internal async void Draw()
13511367

13521368
ResetScriptGfxAlign();
13531369
DrawRect(x, y, width, height, 0, 0, 0, 180);
1370+
MenuItemsYOffset += bgHeight - 1f;
13541371
#endif
13551372
#if REDM
13561373
//float x = (Position.Key + ((headerSize.Key) / 2f)) / MenuController.ScreenWidth;
13571374
//float y = ((Position.Value + MenuItemsYOffset + ((bgHeight + 1f) / 2f) /) / MenuController.ScreenHeight);
13581375
//float width = (headerSize.Key + 16f) / MenuController.ScreenWidth;
13591376
//float height = (bgHeight + 17f) / MenuController.ScreenHeight;
13601377
float bgHeight = 38f * MathUtil.Clamp(Size, 0, MaxItemsOnScreen);
1378+
var currentMenuItem = GetCurrentMenuItem();
1379+
float descriptionBoxHeight = 0f;
1380+
if (currentMenuItem != null && !string.IsNullOrEmpty(currentMenuItem.Description))
1381+
{
1382+
int count = (currentMenuItem.Description.Count((a => { return a == '\n'; })) - 1);
1383+
if (count < 1)
1384+
{
1385+
descriptionBoxHeight = 42f;
1386+
}
1387+
else
1388+
{
1389+
descriptionBoxHeight = (38f * count) + 30f;
1390+
}
1391+
1392+
bgHeight += descriptionBoxHeight;
1393+
}
1394+
float actualBgYLocation = ((38f + (38f / 2f) + (bgHeight / 2f)) / MenuController.ScreenHeight);
13611395
float x = (Position.Key + (headerSize.Key / 2f)) / MenuController.ScreenWidth;
1362-
float y = ((Position.Value + MenuItemsYOffset + ((bgHeight + 1f - (headerSize.Value)) / 2f)) / MenuController.ScreenHeight);
1396+
float y = ((Position.Value + MenuItemsYOffset + ((bgHeight + 1f - (headerSize.Value)) / 2f) + 19f) / MenuController.ScreenHeight);
13631397
float width = headerSize.Key / MenuController.ScreenWidth;
1364-
float height = (headerSize.Value + bgHeight + 33f) / MenuController.ScreenHeight;
1398+
float height = (headerSize.Value + bgHeight + 33f + 38f) / MenuController.ScreenHeight;
13651399
Call(DRAW_SPRITE, MenuController._texture_dict, MenuController._header_texture, x, y, width, height, 0f, 0, 0, 0, 240);
1400+
Call(DRAW_SPRITE, MenuController._texture_dict, MenuController._header_texture, x, y + actualBgYLocation - (descriptionBoxHeight / MenuController.ScreenHeight), width, 38f / MenuController.ScreenHeight, 0f, 55, 55, 55, 255);
1401+
MenuItemsYOffset += bgHeight - descriptionBoxHeight - 1f;
13661402
#endif
1367-
MenuItemsYOffset += bgHeight - 1f;
13681403
}
13691404
#endregion
13701405

@@ -1377,9 +1412,10 @@ internal async void Draw()
13771412
}
13781413
}
13791414
#endregion
1415+
float descriptionYOffset = 0f;
1416+
13801417
#if FIVEM
13811418
#region Up Down overflow Indicator
1382-
float descriptionYOffset = 0f;
13831419
if (Size > 0)
13841420
{
13851421
if (Size > MaxItemsOnScreen)
@@ -1450,21 +1486,21 @@ internal async void Draw()
14501486
}
14511487

14521488
#endregion
1453-
1489+
#endif
14541490
#region Draw Description
14551491
if (Size > 0)
14561492
{
14571493
var currentMenuItem = GetCurrentMenuItem();
14581494
if (currentMenuItem != null && !string.IsNullOrEmpty(currentMenuItem.Description))
14591495
{
1460-
#region description text
1496+
#region description text
14611497
int font = 0;
14621498
float textSize = (14f * 27f) / MenuController.ScreenHeight;
14631499

1500+
#if FIVEM
14641501
float textMinX = 0f + (10f / MenuController.ScreenWidth);
14651502
float textMaxX = Width / MenuController.ScreenWidth - (10f / MenuController.ScreenWidth);
14661503
float textY = MenuItemsYOffset / MenuController.ScreenHeight + (16f / MenuController.ScreenHeight) + descriptionYOffset;
1467-
14681504
SetScriptGfxAlign(76, 84);
14691505
SetScriptGfxAlignParams(0f, 0f, 0f, 0f);
14701506

@@ -1517,11 +1553,25 @@ internal async void Draw()
15171553
}
15181554

15191555
ResetScriptGfxAlign();
1556+
#endif
1557+
#if REDM
15201558

1521-
#endregion
1559+
Call(SET_TEXT_SCALE, textSize, textSize);
1560+
Call(SET_TEXT_CENTRE, true);
1561+
float textMinX = (headerSize.Key / 2f) / MenuController.ScreenWidth;
1562+
float textMaxX = (Width - 10f) / MenuController.ScreenWidth;
1563+
float textY = MenuItemsYOffset / MenuController.ScreenHeight + (18f / MenuController.ScreenHeight) + (48f / MenuController.ScreenHeight);
1564+
font = 23;
1565+
Call((CitizenFX.Core.Native.Hash)0xADA9255D, font);
1566+
Call(_DRAW_TEXT, Call<long>(_CREATE_VAR_STRING, 10, "LITERAL_STRING", $"{currentMenuItem.Description}"), textMinX, textY);
15221567

1568+
#endif
15231569

1524-
#region background
1570+
#endregion
1571+
1572+
1573+
#if FIVEM
1574+
#region background
15251575
float descWidth = Width / MenuController.ScreenWidth;
15261576
float descHeight = (textHeight + 0.005f) * lineCount + (8f / MenuController.ScreenHeight) + (2.5f / MenuController.ScreenHeight);
15271577
float descX = (Position.Key + (Width / 2f)) / MenuController.ScreenWidth;
@@ -1534,19 +1584,20 @@ internal async void Draw()
15341584
DrawRect(descX, descY, descWidth, descHeight, 0, 0, 0, 180);
15351585

15361586
ResetScriptGfxAlign();
1537-
#endregion
1587+
#endregion
15381588

15391589
descriptionYOffset += descY + (descHeight / 2f) - (4f / MenuController.ScreenHeight);
1590+
#endif
15401591
}
15411592
else
15421593
{
15431594
descriptionYOffset += MenuItemsYOffset / MenuController.ScreenHeight + (2f / MenuController.ScreenHeight) + descriptionYOffset;
15441595
}
15451596

15461597
}
1547-
15481598
#endregion
15491599

1600+
#if FIVEM
15501601
#region Draw Weapon Stats
15511602
{
15521603
if (Size > 0)

MenuAPI/items/MenuItem.cs

Lines changed: 41 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using System;
1+
using System;
22
using System.Collections.Generic;
33
using System.Linq;
44
using System.Text;
@@ -206,7 +206,46 @@ public enum Icon
206206
public Icon LeftIcon { get; set; }
207207
public Icon RightIcon { get; set; }
208208
public bool Enabled { get; set; } = true;
209-
public string Description { get; set; }
209+
public string Description
210+
{
211+
get
212+
{
213+
return _description;
214+
}
215+
set
216+
{
217+
#if FIVEM
218+
_description = value;
219+
#endif
220+
#if REDM
221+
if (value != null)
222+
{
223+
string text = value;
224+
int maxLength = 50;
225+
List<string> lines = new List<string>();
226+
while (text.Length > maxLength)
227+
{
228+
var substr = text.Substring(0, Math.Min(text.Length - 1, maxLength));
229+
var lastIndex = substr.LastIndexOf(" ");
230+
if (lastIndex == -1)
231+
{
232+
lastIndex = Math.Min(text.Length - 1, maxLength);
233+
}
234+
lines.Add(text.Substring(0, lastIndex));
235+
text = text.Substring(lastIndex);
236+
}
237+
lines.Add(text);
238+
text = "";
239+
foreach (var str in lines)
240+
{
241+
text += str + "\n";
242+
}
243+
_description = text;
244+
}
245+
#endif
246+
}
247+
}
248+
private string _description;
210249
public int Index { get { if (ParentMenu != null) return ParentMenu.GetMenuItems().IndexOf(this); return -1; } } //{ get; internal set; }
211250
public bool Selected { get { if (ParentMenu != null) { return ParentMenu.CurrentIndex == Index; } return false; } }
212251
public Menu ParentMenu { get; set; }
@@ -918,7 +957,6 @@ internal virtual void Draw(int indexOffset)
918957
if (ParentMenu != null)
919958
{
920959
float yOffset = ParentMenu.MenuItemsYOffset + 1f - (RowHeight * MathUtil.Clamp(ParentMenu.Size, 0, ParentMenu.MaxItemsOnScreen));
921-
922960
#region Background Rect
923961
#if FIVEM
924962
SetScriptGfxAlign(ParentMenu.LeftAligned ? 76 : 82, 84);

TestMenu/ExampleMenu.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using System;
1+
using System;
22
using System.Collections.Generic;
33
using System.Linq;
44
using System.Text;
@@ -179,7 +179,7 @@ string ChangeCallback(MenuDynamicListItem item, bool left)
179179
// you can use AddSubmenu or AddMenu, both will work but if you want to link this menu from another menu,
180180
// you should use AddSubmenu.
181181
MenuController.AddSubmenu(menu, menu3);
182-
MenuItem thirdSubmenuBtn = new MenuItem("Another submenu", "This is just a submenu without a banner. No big deal.")
182+
MenuItem thirdSubmenuBtn = new MenuItem("Another submenu", "This is just a submenu without a banner. No big deal. This also has a very long description to test multiple lines and see if they work properly. Let's find out if it works as intended.")
183183
{
184184
#if FIVEM
185185
Label = "→→→"

0 commit comments

Comments
 (0)