Skip to content

Commit b9faad3

Browse files
authored
Merge pull request #19 from schwarper/UpdateScreenMenu
feat(ui): add color to title and disabled options To Do: - Add background support (WIP)
2 parents 61aa21c + fbeb1c1 commit b9faad3

5 files changed

Lines changed: 124 additions & 72 deletions

File tree

API/Class/Config.cs

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using CounterStrikeSharp.API;
22
using CounterStrikeSharp.API.Modules.Utils;
3+
using System.Drawing;
34
using System.Reflection;
45
using Tomlyn;
56
using Tomlyn.Model;
@@ -56,13 +57,14 @@ public class Cfg
5657
};
5758
public ScreenMenu ScreenMenu { get; set; } = new()
5859
{
59-
TextColor = "orange",
60+
TextColor = Color.FromArgb(232, 155, 27),
61+
DisabledTextColor = Color.FromArgb(231, 210, 177),
6062
PositionX = -5.5f,
6163
PositionY = 0.0f,
6264
Background = true,
6365
BackgroundHeight = 0,
6466
BackgroundWidth = 0.2f,
65-
Font = "Arial Bold",
67+
Font = "Tahoma Bold",
6668
Size = 32,
6769
FreezePlayer = false,
6870
ShowResolutionsOption = true,
@@ -128,7 +130,8 @@ public class WasdMenuSettings
128130

129131
public class ScreenMenu
130132
{
131-
public string TextColor { get; set; } = string.Empty;
133+
public Color TextColor { get; set; }
134+
public Color DisabledTextColor { get; set; }
132135
public float PositionX { get; set; }
133136
public float PositionY { get; set; }
134137
public bool Background { get; set; }
@@ -210,7 +213,6 @@ public static void LoadConfig()
210213
model.SetIfPresent("WasdMenu.ArrowColor", (string value) => Config.WasdMenu.ArrowColor = value);
211214
model.SetIfPresent("WasdMenu.FreezePlayer", (bool value) => Config.WasdMenu.FreezePlayer = value);
212215

213-
model.SetIfPresent("ScreenMenu.TextColor", (string value) => Config.ScreenMenu.TextColor = value);
214216
model.SetIfPresent("ScreenMenu.PositionX", (float value) => Config.ScreenMenu.PositionX = value);
215217
model.SetIfPresent("ScreenMenu.PositionY", (float value) => Config.ScreenMenu.PositionY = value);
216218
model.SetIfPresent("ScreenMenu.Background", (bool value) => Config.ScreenMenu.Background = value);
@@ -222,6 +224,20 @@ public static void LoadConfig()
222224
model.SetIfPresent("ScreenMenu.ShowResolutionsOption", (bool value) => Config.ScreenMenu.ShowResolutionsOption = value);
223225
model.SetIfPresent("ScreenMenu.MenuType", (string value) => Config.ScreenMenu.MenuType = value);
224226

227+
model.SetIfPresent("ScreenMenu.TextColor", (string hexOrName) =>
228+
{
229+
Config.ScreenMenu.TextColor = hexOrName.StartsWith('#') ?
230+
hexOrName.HexToColor() :
231+
Color.FromName(hexOrName);
232+
});
233+
234+
model.SetIfPresent("ScreenMenu.DisabledTextColor", (string hexOrName) =>
235+
{
236+
Config.ScreenMenu.DisabledTextColor = hexOrName.StartsWith('#') ?
237+
hexOrName.HexToColor() :
238+
Color.FromName(hexOrName);
239+
});
240+
225241
model.SetIfExist("Resolutions", (TomlTable table) =>
226242
{
227243
foreach (KeyValuePair<string, object> item in table)

API/Class/Library.cs

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ internal static partial class Library
8787
return handle.Value;
8888
}
8989

90-
public static CPointWorldText? CreateWorldText(string text, int size, string textColor, string font, bool background, float backgroundheight, float backgroundwidth)
90+
public static CPointWorldText? CreateWorldText(string text, int size, Color color, string font, bool background, float backgroundheight, float backgroundwidth)
9191
{
9292
CPointWorldText entity = Utilities.CreateEntityByName<CPointWorldText>("point_worldtext")!;
9393

@@ -98,7 +98,7 @@ internal static partial class Library
9898
entity.Enabled = true;
9999
entity.FontSize = size;
100100
entity.Fullbright = true;
101-
entity.Color = Color.FromName(textColor);
101+
entity.Color = color;
102102
entity.WorldUnitsPerPx = 0.25f / 1050 * size;
103103
entity.FontName = font;
104104
entity.JustifyHorizontal = PointWorldTextJustifyHorizontal_t.POINT_WORLD_TEXT_JUSTIFY_HORIZONTAL_LEFT;
@@ -119,7 +119,7 @@ public static void CreateFakeWorldText(this CCSPlayerController player, ScreenMe
119119
if (_isFakeCreated)
120120
return;
121121

122-
CPointWorldText? entity = CreateWorldText(" ", 35, "orange", "Arial", false, 0, 0);
122+
CPointWorldText? entity = CreateWorldText(" ", 35, Color.Orange, "Arial", false, 0, 0);
123123
if (entity == null) { instance.Close(); return; }
124124

125125
CCSGOViewModel? viewModel = player.EnsureCustomView();
@@ -248,4 +248,15 @@ public static char GetChatColor(this string colorName)
248248
{
249249
return (char)typeof(ChatColors).GetField(colorName)?.GetValue(null)!;
250250
}
251+
252+
public static Color HexToColor(this string hex)
253+
{
254+
hex = hex.TrimStart('#');
255+
256+
return Color.FromArgb(
257+
red: Convert.ToByte(hex[..2], 16),
258+
green: Convert.ToByte(hex.Substring(2, 2), 16),
259+
blue: Convert.ToByte(hex.Substring(4, 2), 16)
260+
);
261+
}
251262
}

API/Menu/ScreenMenu.cs

Lines changed: 86 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using CS2MenuManager.API.Class;
55
using CS2MenuManager.API.Enum;
66
using CS2MenuManager.API.Interface;
7+
using System.Drawing;
78
using System.Text;
89
using static CounterStrikeSharp.API.Core.Listeners;
910
using static CS2MenuManager.API.Class.Buttons;
@@ -28,7 +29,12 @@ static ScreenMenu()
2829
/// <summary>
2930
/// Gets or sets the color of the text.
3031
/// </summary>
31-
public string TextColor { get; set; } = Config.ScreenMenu.TextColor;
32+
public Color TextColor { get; set; } = Config.ScreenMenu.TextColor;
33+
34+
/// <summary>
35+
/// Gets or sets the color of the disabled text.
36+
/// </summary>
37+
public Color DisabledTextColor { get; set; } = Config.ScreenMenu.DisabledTextColor;
3238

3339
/// <summary>
3440
/// Gets or sets a value indicating whether the menu has a background.
@@ -119,17 +125,10 @@ public class ScreenMenuInstance : BaseMenuInstance
119125
/// </summary>
120126
public override int NumPerPage => ((ScreenMenu)Menu).ShowResolutionsOption ? 6 : 7;
121127

122-
/// <summary>
123-
/// Gets or sets the previous button state.
124-
/// </summary>
125-
public PlayerButtons OldButton;
126-
127-
/// <summary>
128-
/// Gets or sets the world text entity used to display the menu.
129-
/// </summary>
130-
public CPointWorldText? WorldText;
131-
132-
internal CCSGOViewModel? OldViewModel;
128+
private PlayerButtons OldButton;
129+
private CPointWorldText? WorldText;
130+
private CPointWorldText? WorldTextDisabled;
131+
private CCSGOViewModel? OldViewModel;
133132

134133
/// <summary>
135134
/// Initializes a new instance of the <see cref="ScreenMenuInstance"/> class.
@@ -165,31 +164,17 @@ public override void Display()
165164
if (Menu is not ScreenMenu screenMenu)
166165
return;
167166

168-
StringBuilder builder = new();
169-
int totalPages = (int)Math.Ceiling((double)Menu.ItemOptions.Count / MenuItemsPerPage);
170-
int currentPage = Page + 1;
167+
StringBuilder noneOptions = new();
168+
StringBuilder disabledOptions = new();
171169

172-
builder.AppendLine(" ");
173-
builder.AppendLine($"{Menu.Title} ({currentPage}/{totalPages})");
174-
builder.AppendLine(" ");
170+
disabledOptions.AppendLine(Menu.Title);
171+
noneOptions.AppendLine();
175172

176-
List<(string Text, int GlobalIndex)> visibleOptions = GetVisibleOptions();
173+
List<(string Text, int GlobalIndex, bool disabled)> visibleOptions = GetVisibleOptions();
177174

178-
int dynamicStartIndex = -1;
179175
for (int i = 0; i < visibleOptions.Count; i++)
180176
{
181-
(_, int globalIndex) = visibleOptions[i];
182-
183-
if (globalIndex < 0 && dynamicStartIndex == -1)
184-
dynamicStartIndex = i;
185-
}
186-
187-
for (int i = 0; i < visibleOptions.Count; i++)
188-
{
189-
if (i == dynamicStartIndex)
190-
builder.AppendLine(" ");
191-
192-
(string text, int _) = visibleOptions[i];
177+
(string text, int _, bool disabled) = visibleOptions[i];
193178

194179
string displayLine = screenMenu.MenuType switch
195180
{
@@ -198,23 +183,43 @@ public override void Display()
198183
_ => string.Empty
199184
};
200185

201-
builder.AppendLine(displayLine);
186+
if (disabled)
187+
{
188+
noneOptions.AppendLine();
189+
disabledOptions.AppendLine(displayLine);
190+
}
191+
else
192+
{
193+
noneOptions.AppendLine(displayLine);
194+
disabledOptions.AppendLine();
195+
}
202196
}
203197

198+
noneOptions.AppendLine();
199+
disabledOptions.AppendLine();
200+
204201
if (screenMenu.MenuType != MenuType.KeyPress)
205202
{
206-
builder.AppendLine(" ");
207-
builder.AppendLine(Player.Localizer("ScrollKey", screenMenu.ScrollUpKey, screenMenu.ScrollDownKey));
208-
builder.AppendLine(Player.Localizer("SelectKey", screenMenu.SelectKey));
209-
builder.AppendLine(" ");
203+
disabledOptions.AppendLine();
204+
disabledOptions.AppendLine();
205+
noneOptions.AppendLine(Player.Localizer("ScrollKey", screenMenu.ScrollUpKey, screenMenu.ScrollDownKey));
206+
noneOptions.AppendLine(Player.Localizer("SelectKey", screenMenu.SelectKey));
210207
}
211208

212-
if (WorldText == null || !WorldText.IsValid)
213-
WorldText = CreateWorldText(builder.ToString(), screenMenu.Size, screenMenu.TextColor, screenMenu.Font, screenMenu.Background, screenMenu.BackgroundHeight, screenMenu.BackgroundWidth);
209+
UpdateWorldText(ref WorldText, noneOptions.ToString(), screenMenu, screenMenu.TextColor);
210+
UpdateWorldText(ref WorldTextDisabled, disabledOptions.ToString(), screenMenu, screenMenu.DisabledTextColor);
211+
}
212+
213+
private static void UpdateWorldText(ref CPointWorldText? worldText, string message, ScreenMenu screenMenu, Color color)
214+
{
215+
if (worldText == null || !worldText.IsValid)
216+
{
217+
worldText = CreateWorldText(message, screenMenu.Size, color, screenMenu.Font, false, screenMenu.BackgroundHeight, screenMenu.BackgroundWidth);
218+
}
214219
else
215220
{
216-
WorldText.MessageText = builder.ToString();
217-
Utilities.SetStateChanged(WorldText, "CPointWorldText", "m_messageText");
221+
worldText.MessageText = message;
222+
Utilities.SetStateChanged(worldText, "CPointWorldText", "m_messageText");
218223
}
219224
}
220225

@@ -229,6 +234,7 @@ public override void Close()
229234
Menu.Plugin.RemoveListener<OnEntityDeleted>(OnEntityDeleted);
230235

231236
if (WorldText != null && WorldText.IsValid) WorldText.Remove();
237+
if (WorldTextDisabled != null && WorldTextDisabled.IsValid) WorldTextDisabled.Remove();
232238
if (((ScreenMenu)Menu).FreezePlayer) Player.Unfreeze();
233239

234240
if (!string.IsNullOrEmpty(Config.Sound.Exit))
@@ -256,24 +262,31 @@ private void OnTick()
256262
OldButton = button;
257263
}
258264

259-
if (WorldText != null)
260-
{
261-
CCSGOViewModel? viewModel = Player.EnsureCustomView();
262-
if (viewModel == null) { Close(); return; }
263-
if (OldViewModel == viewModel) return;
265+
CCSGOViewModel? viewModel = Player.EnsureCustomView();
266+
if (viewModel == null) { Close(); return; }
267+
if (OldViewModel == viewModel) return;
268+
269+
VectorData? vectorData = Player.FindVectorData();
270+
if (vectorData == null) { Close(); return; }
264271

265-
VectorData? vectorData = Player.FindVectorData();
266-
if (vectorData == null) { Close(); return; }
272+
OldViewModel = viewModel;
267273

268-
OldViewModel = viewModel;
274+
if (WorldText != null)
275+
{
269276
WorldText.Teleport(vectorData.Value.Position, vectorData.Value.Angle, null);
270277
WorldText.AcceptInput("SetParent", viewModel, null, "!activator");
271278
}
279+
280+
if (WorldTextDisabled != null)
281+
{
282+
WorldTextDisabled.Teleport(vectorData.Value.Position, vectorData.Value.Angle, null);
283+
WorldTextDisabled.AcceptInput("SetParent", viewModel, null, "!activator");
284+
}
272285
}
273286

274287
private void ScrollDown()
275288
{
276-
List<(string Text, int GlobalIndex)> visibleOptions = GetVisibleOptions();
289+
List<(string Text, int GlobalIndex, bool disabled)> visibleOptions = GetVisibleOptions();
277290
if (visibleOptions.Count == 0)
278291
return;
279292

@@ -286,7 +299,7 @@ private void ScrollDown()
286299

287300
private void ScrollUp()
288301
{
289-
List<(string Text, int GlobalIndex)> visibleOptions = GetVisibleOptions();
302+
List<(string Text, int GlobalIndex, bool disabled)> visibleOptions = GetVisibleOptions();
290303
if (visibleOptions.Count == 0)
291304
return;
292305

@@ -328,11 +341,11 @@ public override void OnKeyPress(CCSPlayerController player, int key)
328341

329342
private void Choose()
330343
{
331-
List<(string Text, int GlobalIndex)> visibleOptions = GetVisibleOptions();
344+
List<(string Text, int GlobalIndex, bool disabled)> visibleOptions = GetVisibleOptions();
332345
if (CurrentChoiceIndex < 0 || CurrentChoiceIndex >= visibleOptions.Count)
333346
return;
334347

335-
(string _, int globalIndex) = visibleOptions[CurrentChoiceIndex];
348+
(string _, int globalIndex, _) = visibleOptions[CurrentChoiceIndex];
336349
switch (globalIndex)
337350
{
338351
case -1: NextPage(); return;
@@ -366,9 +379,9 @@ private void HandleOption(int globalIndex)
366379
Close();
367380
}
368381

369-
private List<(string Text, int GlobalIndex)> GetVisibleOptions()
382+
private List<(string Text, int GlobalIndex, bool disabled)> GetVisibleOptions()
370383
{
371-
List<(string Text, int GlobalIndex)> visible = [];
384+
List<(string Text, int GlobalIndex, bool disabled)> visible = [];
372385
int totalItems = Menu.ItemOptions.Count;
373386
int start = CurrentOffset;
374387
int end = Math.Min(start + NumPerPage, totalItems);
@@ -377,36 +390,47 @@ private void HandleOption(int globalIndex)
377390
for (int i = start; i < end; i++)
378391
{
379392
ItemOption option = Menu.ItemOptions[i];
393+
380394
string text = option.DisableOption switch
381395
{
382396
DisableOption.None or DisableOption.DisableShowNumber =>
383397
$"{displayNumber}. {option.Text}",
384-
DisableOption.DisableHideNumber => $"{option.Text}",
398+
DisableOption.DisableHideNumber => option.Text,
385399
_ => string.Empty
386400
};
387401

402+
visible.Add((text, i, option.DisableOption != DisableOption.None));
388403
displayNumber++;
389-
visible.Add((text, i));
390404
}
391405

392-
if (((ScreenMenu)Menu).ShowResolutionsOption) visible.Add(($"{displayNumber++}. {Player.Localizer("SelectResolution")}\n", -4));
393-
if (HasPrevButton) visible.Add(($"8. {Player.Localizer("Prev")}", -2));
394-
if (HasNextButton) visible.Add(($"9. {Player.Localizer("Next")}", -1));
395-
if (HasExitButton) visible.Add(($"0. {Player.Localizer("Exit")}", -3));
406+
if (visible.Count > 0)
407+
{
408+
visible.Add(("", -99, true));
409+
}
410+
411+
if (((ScreenMenu)Menu).ShowResolutionsOption)
412+
visible.Add(($"7. {Player.Localizer("SelectResolution")}", -4, false));
413+
if (HasPrevButton)
414+
visible.Add(($"8. {Player.Localizer("Prev")}", -2, false));
415+
if (HasNextButton)
416+
visible.Add(($"9. {Player.Localizer("Next")}", -1, false));
417+
if (HasExitButton)
418+
visible.Add(($"0. {Player.Localizer("Exit")}", -3, false));
396419

397420
return visible;
398421
}
399422

400423
private void OnCheckTransmit(CCheckTransmitInfoList infoList)
401424
{
402-
if (WorldText == null) return;
425+
if (WorldText == null || WorldTextDisabled == null) return;
403426

404427
foreach ((CCheckTransmitInfo info, CCSPlayerController? player) in infoList)
405428
{
406429
if (player == null || player == Player)
407430
continue;
408431

409432
info.TransmitEntities.Remove(WorldText);
433+
info.TransmitEntities.Remove(WorldTextDisabled);
410434
}
411435
}
412436

0 commit comments

Comments
 (0)