Add TruncateLabelWithTooltip helper function

* Move GetContrastColor helper to SpriteFont
* Move WidgetUtils  from OpenRA.Game.Widgets to OpenRA.Mods.Common.Widgets
This commit is contained in:
Ivaylo Draganov
2019-05-25 22:19:36 +03:00
committed by abcdefg30
parent 79d1899426
commit 1fee50be2e
6 changed files with 21 additions and 5 deletions

View File

@@ -112,7 +112,7 @@ namespace OpenRA.Graphics
public void DrawTextWithContrast(string text, float2 location, Color fg, Color bgDark, Color bgLight, int offset) public void DrawTextWithContrast(string text, float2 location, Color fg, Color bgDark, Color bgLight, int offset)
{ {
DrawTextWithContrast(text, location, fg, WidgetUtils.GetContrastColor(fg, bgDark, bgLight), offset); DrawTextWithContrast(text, location, fg, GetContrastColor(fg, bgDark, bgLight), offset);
} }
public void DrawTextWithShadow(string text, float2 location, Color fg, Color bg, int offset) public void DrawTextWithShadow(string text, float2 location, Color fg, Color bg, int offset)
@@ -125,7 +125,7 @@ namespace OpenRA.Graphics
public void DrawTextWithShadow(string text, float2 location, Color fg, Color bgDark, Color bgLight, int offset) public void DrawTextWithShadow(string text, float2 location, Color fg, Color bgDark, Color bgLight, int offset)
{ {
DrawTextWithShadow(text, location, fg, WidgetUtils.GetContrastColor(fg, bgDark, bgLight), offset); DrawTextWithShadow(text, location, fg, GetContrastColor(fg, bgDark, bgLight), offset);
} }
public int2 Measure(string text) public int2 Measure(string text)
@@ -185,6 +185,11 @@ namespace OpenRA.Graphics
return g; return g;
} }
static Color GetContrastColor(Color fgColor, Color bgDark, Color bgLight)
{
return fgColor == Color.White || fgColor.GetBrightness() > 0.33 ? bgDark : bgLight;
}
public void Dispose() public void Dispose()
{ {
font.Dispose(); font.Dispose();

View File

@@ -13,6 +13,7 @@ using System.Collections.Generic;
using System.Diagnostics; using System.Diagnostics;
using OpenRA.Graphics; using OpenRA.Graphics;
using OpenRA.Mods.Common.LoadScreens; using OpenRA.Mods.Common.LoadScreens;
using OpenRA.Mods.Common.Widgets;
using OpenRA.Primitives; using OpenRA.Primitives;
using OpenRA.Widgets; using OpenRA.Widgets;

View File

@@ -12,6 +12,7 @@
using System.Collections.Generic; using System.Collections.Generic;
using System.Diagnostics; using System.Diagnostics;
using OpenRA.Graphics; using OpenRA.Graphics;
using OpenRA.Mods.Common.Widgets;
using OpenRA.Primitives; using OpenRA.Primitives;
using OpenRA.Widgets; using OpenRA.Widgets;

View File

@@ -14,6 +14,7 @@ using System.Collections.Generic;
using System.IO; using System.IO;
using System.Linq; using System.Linq;
using OpenRA.Graphics; using OpenRA.Graphics;
using OpenRA.Mods.Common.Widgets;
using OpenRA.Primitives; using OpenRA.Primitives;
using OpenRA.Widgets; using OpenRA.Widgets;

View File

@@ -11,6 +11,7 @@
using System.Linq; using System.Linq;
using Eluant; using Eluant;
using OpenRA.Mods.Common.Widgets;
using OpenRA.Scripting; using OpenRA.Scripting;
using OpenRA.Widgets; using OpenRA.Widgets;

View File

@@ -14,7 +14,7 @@ using System.Linq;
using OpenRA.Graphics; using OpenRA.Graphics;
using OpenRA.Primitives; using OpenRA.Primitives;
namespace OpenRA.Widgets namespace OpenRA.Mods.Common.Widgets
{ {
public static class WidgetUtils public static class WidgetUtils
{ {
@@ -265,9 +265,16 @@ namespace OpenRA.Widgets
return trimmed; return trimmed;
} }
public static Color GetContrastColor(Color fgColor, Color bgDark, Color bgLight) public static void TruncateLabelToTooltip(LabelWithTooltipWidget label, string text)
{ {
return fgColor == Color.White || fgColor.GetBrightness() > 0.33 ? bgDark : bgLight; var truncatedText = TruncateText(text, label.Bounds.Width, Game.Renderer.Fonts[label.Font]);
label.GetText = () => truncatedText;
if (text != truncatedText)
label.GetTooltipText = () => text;
else
label.GetTooltipText = null;
} }
} }