Add support for new chat colors (#379)
* Add backwards-compatible support for new chat color system * Use default * Set LangVersion properly * Add backwards compatible ctors in TorchChatMessage * Fix not setting Font if it's not MyFontEnum * Use correct color mappings
This commit is contained in:
40
Torch.API/Utils/ColorUtils.cs
Normal file
40
Torch.API/Utils/ColorUtils.cs
Normal file
@@ -0,0 +1,40 @@
|
||||
using System.Windows.Media;
|
||||
using VRage.Game;
|
||||
using Color = VRageMath.Color;
|
||||
|
||||
namespace Torch.Utils
|
||||
{
|
||||
public static class ColorUtils
|
||||
{
|
||||
/// <summary>
|
||||
/// Convert the old "font" or a RGB hex code to a Color.
|
||||
/// </summary>
|
||||
public static Color TranslateColor(string font)
|
||||
{
|
||||
if (StringUtils.IsFontEnum(font))
|
||||
{
|
||||
// RGB values copied from Fonts.sbc
|
||||
switch (font)
|
||||
{
|
||||
case MyFontEnum.Blue:
|
||||
return new Color(220, 244, 252);
|
||||
case MyFontEnum.Red:
|
||||
return new Color(227, 65, 65);
|
||||
case MyFontEnum.Green:
|
||||
return new Color(101, 182, 193);
|
||||
case MyFontEnum.DarkBlue:
|
||||
return new Color(94, 115, 127);
|
||||
default:
|
||||
return Color.White;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
// VRage color doesn't have its own hex code parser and I don't want to write one
|
||||
var conv = (System.Windows.Media.Color)(ColorConverter.ConvertFromString(font) ??
|
||||
System.Windows.Media.Color.FromRgb(255, 255, 255));
|
||||
return new Color(conv.R, conv.G, conv.B);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
73
Torch.API/Utils/StringUtils.cs
Normal file
73
Torch.API/Utils/StringUtils.cs
Normal file
@@ -0,0 +1,73 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Torch.Utils
|
||||
{
|
||||
/// <summary>
|
||||
/// Utility methods for strings
|
||||
/// </summary>
|
||||
public static class StringUtils
|
||||
{
|
||||
/// <summary>
|
||||
/// Determines a common prefix for the given set of strings
|
||||
/// </summary>
|
||||
/// <param name="set">Set of strings</param>
|
||||
/// <returns>Common prefix</returns>
|
||||
public static string CommonPrefix(IEnumerable<string> set)
|
||||
{
|
||||
StringBuilder builder = null;
|
||||
foreach (string other in set)
|
||||
{
|
||||
if (builder == null)
|
||||
builder = new StringBuilder(other);
|
||||
if (builder.Length > other.Length)
|
||||
builder.Remove(other.Length, builder.Length - other.Length);
|
||||
for (var i = 0; i < builder.Length; i++)
|
||||
if (builder[i] != other[i])
|
||||
{
|
||||
builder.Remove(i, builder.Length - i);
|
||||
break;
|
||||
}
|
||||
}
|
||||
return builder?.ToString() ?? "";
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Determines a common suffix for the given set of strings
|
||||
/// </summary>
|
||||
/// <param name="set">Set of strings</param>
|
||||
/// <returns>Common suffix</returns>
|
||||
public static string CommonSuffix(IEnumerable<string> set)
|
||||
{
|
||||
StringBuilder builder = null;
|
||||
foreach (string other in set)
|
||||
{
|
||||
if (builder == null)
|
||||
builder = new StringBuilder(other);
|
||||
if (builder.Length > other.Length)
|
||||
builder.Remove(0, builder.Length - other.Length);
|
||||
for (var i = 0; i < builder.Length; i++)
|
||||
{
|
||||
if (builder[builder.Length - 1 - i] != other[other.Length - 1 - i])
|
||||
{
|
||||
builder.Remove(0, builder.Length - i);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
return builder?.ToString() ?? "";
|
||||
}
|
||||
|
||||
private static string[] FontEnumValues => _fontEnumValues ?? (_fontEnumValues = typeof(VRage.Game.MyFontEnum).GetFields(BindingFlags.Public | BindingFlags.Static).Where(x => x.IsLiteral && !x.IsInitOnly).Select(x => (string)x.GetValue(null)).ToArray());
|
||||
|
||||
private static string[] _fontEnumValues;
|
||||
public static bool IsFontEnum(string str)
|
||||
{
|
||||
return FontEnumValues.Contains(str);
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user