Skip to content

Commit 8dbf849

Browse files
committed
panda review
1 parent 6d5b7e2 commit 8dbf849

File tree

5 files changed

+52
-40
lines changed

5 files changed

+52
-40
lines changed

Framework/Intersect.Framework.Core/Color.cs

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System.Diagnostics.CodeAnalysis;
22
using System.Globalization;
3+
using System.Reflection;
34
using Intersect.Framework;
45
using Intersect.Localization;
56
using MessagePack;
@@ -9,7 +10,17 @@ namespace Intersect;
910
[MessagePackObject]
1011
public partial class Color : IEquatable<Color>
1112
{
12-
13+
private static readonly Dictionary<string, Func<Color>> KnownColors;
14+
static Color()
15+
{
16+
KnownColors = typeof(Color)
17+
.GetProperties(BindingFlags.Public | BindingFlags.Static)
18+
.Where(p => p.PropertyType == typeof(Color) && p.GetMethod != null)
19+
.ToDictionary(
20+
p => p.Name.ToLowerInvariant(),
21+
p => (Func<Color>)(() => p.GetMethod?.Invoke(null, null) as Color ?? Color.White)
22+
);
23+
}
1324
public enum ChatColor
1425
{
1526

@@ -289,7 +300,7 @@ public static Color FromRgba(int rgba)
289300
return new Color(a: a, r: r, g: g, b: b);
290301
}
291302

292-
public static Color? FromString(string val, Color? defaultColor = default)
303+
public static Color? FromCsv(string val, Color? defaultColor = default)
293304
{
294305
if (string.IsNullOrEmpty(val))
295306
{
@@ -306,7 +317,29 @@ public static Color FromRgba(int rgba)
306317
return new Color(parts[0], parts[1], parts[2], parts[3]);
307318
}
308319

309-
public static implicit operator Color(string colorString) => FromString(colorString);
320+
public static Color? FromString(string val, Color? defaultColor = default)
321+
{
322+
if (string.IsNullOrWhiteSpace(val))
323+
{
324+
return defaultColor;
325+
}
326+
327+
val = val.Trim().ToLowerInvariant();
328+
329+
if (KnownColors.TryGetValue(val, out Func<Color>? colorFunc))
330+
{
331+
return colorFunc();
332+
}
333+
334+
Color? parsedColor = Color.FromHex(val, defaultColor);
335+
336+
parsedColor ??= Color.FromCsv(val, defaultColor);
337+
338+
return parsedColor ?? defaultColor;
339+
}
340+
341+
342+
public static implicit operator Color(string colorString) => FromCsv(colorString);
310343

311344
public static Color operator *(Color left, Color right) => new Color(
312345
a: (int)(left.A * right.A / 255f),

Intersect.Client.Core/Interface/Game/EventWindow.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ private EventWindow(Canvas gameCanvas, Dialog dialog) : base(gameCanvas, nameof(
177177
_promptLabel.ClearText();
178178
_writer = new Typewriter(parsedText.ToArray(), (text, color) =>
179179
{
180-
_promptLabel.AddText(text, color, Alignments.Left, _promptTemplateLabel.Font);
180+
_promptLabel.AppendText(text, color, Alignments.Left, _promptTemplateLabel.Font);
181181
});
182182
}
183183
Defer(

Intersect.Client.Core/Interface/Game/Typewriting/Typewriter.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ internal sealed class Typewriter
1515
private static long TypingSpeed => ClientConfiguration.Instance.TypewriterPartDelay;
1616

1717
private int _offset;
18-
private int _segmentIndex = 0;
18+
private int _segmentIndex;
1919
private string? _lastText;
2020
private long _nextUpdateTime;
2121

@@ -63,6 +63,8 @@ public void Write(string? soundName)
6363
return;
6464
}
6565

66+
emitSound |= _offset % ClientConfiguration.Instance.TypewriterSoundFrequency == 0;
67+
6668
var segment = _segments[_segmentIndex];
6769

6870
if (_offset >= segment.Text.Length)
@@ -73,14 +75,12 @@ public void Write(string? soundName)
7375
if (_segmentIndex >= _segments.Length)
7476
{
7577
End();
76-
return;
78+
continue;
7779
}
7880

7981
segment = _segments[_segmentIndex];
8082
}
8183

82-
emitSound |= _offset % ClientConfiguration.Instance.TypewriterSoundFrequency == 0;
83-
8484
string nextText;
8585
if (char.IsSurrogatePair(segment.Text, _offset))
8686
{
@@ -144,7 +144,7 @@ public void End()
144144
var segment = _segments[_segmentIndex];
145145
if (_offset < segment.Text.Length)
146146
{
147-
_textWrittenHandler(segment.Text.Substring(_offset), segment.Color);
147+
_textWrittenHandler(segment.Text[_offset..], segment.Color);
148148
}
149149
_segmentIndex++;
150150
_offset = 0;

Intersect.Client.Core/Utilities/TextColorParser.cs

Lines changed: 8 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -5,45 +5,24 @@ namespace Intersect.Client.Utilities
55
{
66
public static class TextColorParser
77
{
8-
private static readonly Dictionary<string, Func<Color>> KnownColors = InitializeColorDictionary();
9-
private static readonly Regex ColorTagRegex = new(@"\\c{([^}]*)}", RegexOptions.Compiled);
8+
private static readonly Regex ColorTagRegex = new(@"(\\c{[^}]*})", RegexOptions.Compiled);
109
private static readonly string SplitPattern = @"(\\c{[^}]*})";
1110

12-
private static Dictionary<string, Func<Color>> InitializeColorDictionary()
13-
{
14-
return typeof(Color)
15-
.GetProperties(BindingFlags.Public | BindingFlags.Static)
16-
.Where(p => p.PropertyType == typeof(Color))
17-
.ToDictionary(
18-
p => p.Name.ToLowerInvariant(),
19-
p => (Func<Color>)(() => p.GetMethod?.Invoke(null, null) as Color ?? Color.White)
20-
);
21-
}
22-
2311
public static List<ColorizedText> Parse(string text, Color defaultColor)
2412
{
25-
var output = new List<ColorizedText>();
26-
var segments = Regex.Split(text, SplitPattern)
27-
.Where(s => !string.IsNullOrWhiteSpace(s));
13+
var segments = ColorTagRegex.Split(text)
14+
.Where(s => !string.IsNullOrWhiteSpace(s))
15+
.ToArray();
2816

17+
var output = new List<ColorizedText>(segments.Length);
2918
var currentColor = defaultColor;
3019

3120
foreach (var segment in segments)
3221
{
33-
var match = ColorTagRegex.Match(segment);
34-
35-
if (match.Success)
22+
if (segment.StartsWith("\\c{") && segment.EndsWith("}"))
3623
{
37-
string colorCode = match.Groups[1].Value.ToLowerInvariant();
38-
39-
if (KnownColors.TryGetValue(colorCode, out Func<Color>? colorFunc))
40-
{
41-
currentColor = colorFunc();
42-
}
43-
else
44-
{
45-
currentColor = Color.FromHex(colorCode, defaultColor) ?? defaultColor;
46-
}
24+
string colorCode = segment[3..^1].ToLowerInvariant();
25+
currentColor = Color.FromString(colorCode, defaultColor) ?? defaultColor;
4726
}
4827
else
4928
{

Intersect.Client.Framework/Gwen/Control/RichLabel.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -183,8 +183,8 @@ public void AppendText(string text, Color? color, Alignments alignment, GameFont
183183
var appendAlignment = alignment;
184184
var appendFont = font ?? lastTextBlock.Font;
185185

186-
if (appendAlignment != lastTextBlock.Alignment &&
187-
appendColor != lastTextBlock.Color &&
186+
if (appendAlignment != lastTextBlock.Alignment ||
187+
appendColor != lastTextBlock.Color ||
188188
appendFont != lastTextBlock.Font)
189189
{
190190
AddText(text, color, alignment, font);

0 commit comments

Comments
 (0)