-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathXPXNumberMenu.cs
More file actions
171 lines (144 loc) · 5.61 KB
/
XPXNumberMenu.cs
File metadata and controls
171 lines (144 loc) · 5.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
using System.Text;
using CounterStrikeSharp.API;
using CounterStrikeSharp.API.Core;
using CounterStrikeSharp.API.Modules.Menu;
namespace XPXLevels;
public sealed class XPXNumberMenu : BaseMenu
{
private readonly BasePlugin _plugin;
public int ItemsPerPage { get; set; } = 4;
public List<string> BodyLines { get; } = [];
public int BodyLinesPerPage { get; set; } = 3;
public bool PaginateBodyWithMenu { get; set; }
public string? SlotSevenLabel { get; set; }
public Action<CCSPlayerController>? SlotSevenAction { get; set; }
public int MaxOptionTextLength { get; set; } = 24;
public string TitleColor { get; set; } = "gold";
public string BodyColor { get; set; } = "silver";
public string EnabledColor { get; set; } = "white";
public string DisabledColor { get; set; } = "gray";
public string PrevPageColor { get; set; } = "deepskyblue";
public string NextPageColor { get; set; } = "deepskyblue";
public string CloseColor { get; set; } = "tomato";
public XPXNumberMenu(string title, BasePlugin plugin) : base(title)
{
_plugin = plugin;
}
public override void Open(CCSPlayerController player)
{
if (MenuManager.GetActiveMenu(player) is XPXNumberMenuInstance XPXMenu)
{
XPXMenu.Close();
}
else
{
MenuManager.CloseActiveMenu(player);
}
MenuManager.GetActiveMenus()[player.Handle] = new XPXNumberMenuInstance(_plugin, player, this);
MenuManager.GetActiveMenus()[player.Handle].Display();
}
}
public sealed class XPXNumberMenuInstance : BaseMenuInstance
{
private readonly BasePlugin _plugin;
public override int NumPerPage => Menu is XPXNumberMenu xpxMenu ? xpxMenu.ItemsPerPage : 6;
protected override int MenuItemsPerPage => NumPerPage;
public bool IsBodyPagedMenu => Menu is XPXNumberMenu { PaginateBodyWithMenu: true };
public bool CanGoPrevPage => HasPrevButton;
public bool CanGoNextPage => HasNextButton;
public int BodyPageIndex => MenuItemsPerPage <= 0 ? 0 : CurrentOffset / MenuItemsPerPage;
public XPXNumberMenuInstance(BasePlugin plugin, CCSPlayerController player, IMenu menu) : base(player, menu)
{
_plugin = plugin;
RemoveOnTickListener();
plugin.RegisterListener<CounterStrikeSharp.API.Core.Listeners.OnTick>(Display);
}
public override void Display()
{
if (MenuManager.GetActiveMenu(Player) != this)
{
RemoveOnTickListener();
return;
}
if (Menu is XPXNumberMenu menu)
{
var builder = new StringBuilder();
builder.Append($"<b><font color='{menu.TitleColor}'>{menu.Title}</font></b>");
builder.AppendLine("<br>");
if (menu.BodyLines.Count > 0)
{
var bodyLines = menu.PaginateBodyWithMenu
? menu.BodyLines
.Skip((CurrentOffset / MenuItemsPerPage) * menu.BodyLinesPerPage)
.Take(menu.BodyLinesPerPage)
: menu.BodyLines;
foreach (var bodyLine in bodyLines)
{
builder.Append($"<font color='{menu.BodyColor}'>{bodyLine}</font>");
builder.AppendLine("<br>");
}
}
var keyOffset = 1;
for (var index = CurrentOffset; index < Math.Min(CurrentOffset + MenuItemsPerPage, menu.MenuOptions.Count); index++)
{
var option = menu.MenuOptions[index];
if (string.IsNullOrWhiteSpace(option.Text))
{
continue;
}
var color = option.Disabled ? menu.DisabledColor : menu.EnabledColor;
builder.Append($"<font color='{color}'>{keyOffset++}.</font> {TrimOptionLabel(option.Text, menu.MaxOptionTextLength)}");
builder.AppendLine("<br>");
}
if (HasPrevButton)
{
builder.Append($"<font color='{menu.PrevPageColor}'>7.</font> Prev");
builder.AppendLine("<br>");
}
else if (menu.SlotSevenAction is not null && !string.IsNullOrWhiteSpace(menu.SlotSevenLabel))
{
builder.Append($"<font color='{menu.PrevPageColor}'>7.</font> {menu.SlotSevenLabel}");
builder.AppendLine("<br>");
}
if (HasNextButton)
{
builder.Append($"<font color='{menu.NextPageColor}'>8.</font> Next");
builder.AppendLine("<br>");
}
if (menu.ExitButton)
{
builder.Append($"<font color='{menu.CloseColor}'>9.</font> Close");
builder.AppendLine("<br>");
}
Player.PrintToCenterHtml(builder.ToString());
}
}
public override void Close()
{
base.Close();
RemoveOnTickListener();
Player.PrintToCenterHtml(" ");
}
public void GoToBodyPage(int pageIndex)
{
if (!IsBodyPagedMenu)
{
return;
}
CurrentOffset = Math.Max(0, pageIndex * MenuItemsPerPage);
Display();
}
private void RemoveOnTickListener()
{
CounterStrikeSharp.API.Core.Listeners.OnTick handler = Display;
_plugin.RemoveListener("OnTick", handler);
}
private static string TrimOptionLabel(string text, int maxLength)
{
if (string.IsNullOrWhiteSpace(text) || maxLength <= 0 || text.Length <= maxLength)
{
return text;
}
return text[..Math.Max(1, maxLength - 3)].TrimEnd() + "...";
}
}