Add helpers for truncating long labels.

This commit is contained in:
Paul Chote
2015-11-07 17:16:42 +00:00
parent c7ebfd608e
commit 6673c882ee

View File

@@ -227,6 +227,22 @@ namespace OpenRA.Widgets
return text; return text;
} }
public static string TruncateText(string text, int width, SpriteFont font)
{
var trimmedWidth = font.Measure(text).X;
if (trimmedWidth <= width)
return text;
var trimmed = text;
while (trimmedWidth > width && trimmed.Length > 3)
{
trimmed = text.Substring(0, trimmed.Length - 4) + "...";
trimmedWidth = font.Measure(trimmed).X;
}
return trimmed;
}
public static Action Once(Action a) { return () => { if (a != null) { a(); a = null; } }; } public static Action Once(Action a) { return () => { if (a != null) { a(); a = null; } }; }
public static string ChooseInitialMap(string initialUid) public static string ChooseInitialMap(string initialUid)
@@ -242,6 +258,32 @@ namespace OpenRA.Widgets
} }
} }
public class CachedTransform<T, U>
{
readonly Func<T, U> transform;
bool initialized;
T lastInput;
U lastOutput;
public CachedTransform(Func<T, U> transform)
{
this.transform = transform;
}
public U Update(T input)
{
if (initialized && ((input == null && lastInput == null) || input.Equals(lastInput)))
return lastOutput;
lastInput = input;
lastOutput = transform(input);
initialized = true;
return lastOutput;
}
}
[Flags] [Flags]
public enum PanelSides public enum PanelSides
{ {