-
-
Notifications
You must be signed in to change notification settings - Fork 213
Expand file tree
/
Copy pathCenterHtmlMenu.cs
More file actions
228 lines (189 loc) · 7.57 KB
/
Copy pathCenterHtmlMenu.cs
File metadata and controls
228 lines (189 loc) · 7.57 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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
/*
* This file is part of CounterStrikeSharp.
* CounterStrikeSharp is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* CounterStrikeSharp is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with CounterStrikeSharp. If not, see <https://www.gnu.org/licenses/>. *
*/
using System.Text;
using Microsoft.Extensions.Logging;
namespace CounterStrikeSharp.API.Modules.Menu;
public class CenterHtmlMenu : BaseMenu
{
private readonly BasePlugin? _plugin;
public string TitleColor { get; set; } = "yellow";
public string EnabledColor { get; set; } = "green";
public string DisabledColor { get; set; } = "grey";
public string PrevPageColor { get; set; } = "yellow";
public string NextPageColor { get; set; } = "yellow";
public string CloseColor { get; set; } = "red";
public bool InlinePageOptions { get; set; } = true;
public int MaxTitleLength { get; set; } = 0; // defaults to 0 = no limit, if enabled, recommended value is 32
public int MaxOptionLength { get; set; } = 0; // defaults to 0 = no limit, if enabled, recommended value is 26
public CenterHtmlMenu(string title, BasePlugin plugin, bool inlinePageOptions = true, int maxTitleLength = 0, int maxOptionLength = 0): base(title)
{
Title = title.TruncateHtml(MaxTitleLength);
_plugin = plugin;
InlinePageOptions = inlinePageOptions;
MaxTitleLength = maxTitleLength;
MaxOptionLength = maxOptionLength;
}
[Obsolete("Use the constructor that takes a BasePlugin")]
public CenterHtmlMenu(string title) : base(title)
{
Title = title.TruncateHtml(MaxTitleLength);
}
public override void Open(CCSPlayerController player)
{
if (_plugin == null)
{
throw new InvalidOperationException("This method is unsupported with the CenterHtmlMenu constructor used." +
"Please provide a BasePlugin in the constructor.");
};
MenuManager.OpenCenterHtmlMenu(_plugin, player, this);
}
public override ChatMenuOption AddMenuOption(string display, Action<CCSPlayerController, ChatMenuOption> onSelect,
bool disabled = false)
{
var option = new ChatMenuOption(display.TruncateHtml(MaxOptionLength), disabled, onSelect);
MenuOptions.Add(option);
return option;
}
}
public class CenterHtmlMenuInstance : BaseMenuInstance
{
private readonly BasePlugin _plugin;
public override int NumPerPage => 5; // one less than the actual number of items per page to avoid truncated options
protected override bool HasNextButton => Menu.MenuOptions.Count > NumPerPage + 1 && CurrentOffset + NumPerPage < Menu.MenuOptions.Count;
public bool InlinePageOptions { get; set; } = true;
protected override int MenuItemsPerPage
{
get
{
int count = NumPerPage;
if (InlinePageOptions == false)
{
if (!HasPrevButton)
count++;
if (!HasNextButton)
count++;
}
else
{
count++;
if (!HasExitButton && !HasPrevButton && !HasNextButton)
count++;
}
return count;
}
}
public CenterHtmlMenuInstance(BasePlugin plugin, CCSPlayerController player, IMenu menu) : base(player, menu)
{
_plugin = plugin;
RemoveOnTickListener();
plugin.RegisterListener<Core.Listeners.OnTick>(Display);
if (menu is CenterHtmlMenu centerHtmlMenu)
InlinePageOptions = centerHtmlMenu.InlinePageOptions;
}
public override void Display()
{
if (MenuManager.GetActiveMenu(Player) != this)
{
Reset();
return;
}
if (Menu is not CenterHtmlMenu centerHtmlMenu)
{
return;
}
var builder = new StringBuilder();
builder.Append($"<b><font color='{centerHtmlMenu.TitleColor}'>{centerHtmlMenu.Title}</font></b>");
builder.AppendLine("<br>");
var keyOffset = 1;
for (var i = CurrentOffset; i < Math.Min(CurrentOffset + MenuItemsPerPage, centerHtmlMenu.MenuOptions.Count); i++)
{
var option = centerHtmlMenu.MenuOptions[i];
string color = option.Disabled ? centerHtmlMenu.DisabledColor : centerHtmlMenu.EnabledColor;
builder.Append($"<font color='{color}'>!{keyOffset++}</font> {option.Text}");
builder.AppendLine("<br>");
}
AddPageOptions(centerHtmlMenu, builder);
var currentPageText = builder.ToString();
Player.PrintToCenterHtml(currentPageText);
}
private void AddPageOptions(CenterHtmlMenu centerHtmlMenu, StringBuilder builder)
{
string prevText = $"<font color='{centerHtmlMenu.PrevPageColor}'>!7 <</font> Prev";
string closeText = $"<font color='{centerHtmlMenu.CloseColor}'>!9 X</font> Close";
string nextText = $"<font color='{centerHtmlMenu.NextPageColor}'>!8 ></font> Next";
if (InlinePageOptions)
AddInlinePageOptions(prevText, closeText, nextText, centerHtmlMenu.ExitButton, builder);
else
AddMultilinePageOptions(prevText, closeText, nextText, centerHtmlMenu.ExitButton, builder);
}
private void AddInlinePageOptions(string prevText, string closeText, string nextText, bool hasExitButton, StringBuilder builder)
{
if (HasPrevButton && HasExitButton && HasNextButton)
{
builder.Append($"{prevText} | {closeText} | {nextText}");
return;
}
string doubleOptionSplitString = " \u200e \u200e \u200e \u200e | \u200e \u200e \u200e \u200e "; // empty characters that are not trimmed
int optionsCount = 0;
if (HasPrevButton)
{
builder.AppendFormat(prevText);
optionsCount++;
}
if (hasExitButton)
{
if (optionsCount++ > 0)
builder.Append(doubleOptionSplitString);
builder.AppendFormat(closeText);
}
if (HasNextButton)
{
if (optionsCount > 0)
builder.Append(doubleOptionSplitString);
builder.AppendFormat(nextText);
}
}
private void AddMultilinePageOptions(string prevText, string closeText, string nextText, bool hasExitButton, StringBuilder builder)
{
if (HasPrevButton)
{
builder.AppendFormat(prevText);
builder.AppendLine("<br>");
}
if (HasNextButton)
{
builder.AppendFormat(nextText);
builder.AppendLine("<br>");
}
if (hasExitButton)
{
builder.AppendFormat(closeText);
builder.AppendLine("<br>");
}
}
public override void Close()
{
base.Close();
RemoveOnTickListener();
// Send a blank message to clear the menu
Player.PrintToCenterHtml(" ");
}
private void RemoveOnTickListener()
{
var onTick = new Core.Listeners.OnTick(Display);
_plugin.RemoveListener("OnTick", onTick);
}
}