Merge pull request #11693 from obrakmann/player-color-contrast
Add contrast to text drawn in player colors
This commit is contained in:
@@ -12,6 +12,7 @@
|
||||
using System;
|
||||
using System.Drawing;
|
||||
using OpenRA.Graphics;
|
||||
using OpenRA.Widgets;
|
||||
|
||||
namespace OpenRA.Mods.Common.Graphics
|
||||
{
|
||||
@@ -21,17 +22,27 @@ namespace OpenRA.Mods.Common.Graphics
|
||||
readonly WPos pos;
|
||||
readonly int zOffset;
|
||||
readonly Color color;
|
||||
readonly Color bgDark;
|
||||
readonly Color bgLight;
|
||||
readonly string text;
|
||||
|
||||
public TextRenderable(SpriteFont font, WPos pos, int zOffset, Color color, string text)
|
||||
public TextRenderable(SpriteFont font, WPos pos, int zOffset, Color color, Color bgDark, Color bgLight, string text)
|
||||
{
|
||||
this.font = font;
|
||||
this.pos = pos;
|
||||
this.zOffset = zOffset;
|
||||
this.color = color;
|
||||
this.bgDark = bgDark;
|
||||
this.bgLight = bgLight;
|
||||
this.text = text;
|
||||
}
|
||||
|
||||
public TextRenderable(SpriteFont font, WPos pos, int zOffset, Color color, string text)
|
||||
: this(font, pos, zOffset, color,
|
||||
ChromeMetrics.Get<Color>("TextContrastColorDark"),
|
||||
ChromeMetrics.Get<Color>("TextContrastColorLight"),
|
||||
text) { }
|
||||
|
||||
public WPos Pos { get { return pos; } }
|
||||
public PaletteReference Palette { get { return null; } }
|
||||
public int ZOffset { get { return zOffset; } }
|
||||
@@ -47,7 +58,7 @@ namespace OpenRA.Mods.Common.Graphics
|
||||
{
|
||||
var screenPos = wr.Viewport.Zoom * (wr.ScreenPosition(pos) - wr.Viewport.TopLeft.ToFloat2()) - 0.5f * font.Measure(text).ToFloat2();
|
||||
var screenPxPos = new float2((float)Math.Round(screenPos.X), (float)Math.Round(screenPos.Y));
|
||||
font.DrawTextWithContrast(text, screenPxPos, color, Color.Black, 1);
|
||||
font.DrawTextWithContrast(text, screenPxPos, color, bgDark, bgLight, 1);
|
||||
}
|
||||
|
||||
public void RenderDebugGeometry(WorldRenderer wr)
|
||||
|
||||
@@ -36,13 +36,16 @@ namespace OpenRA.Mods.Common.Widgets
|
||||
public Color TextColor = ChromeMetrics.Get<Color>("ButtonTextColor");
|
||||
public Color TextColorDisabled = ChromeMetrics.Get<Color>("ButtonTextColorDisabled");
|
||||
public bool Contrast = ChromeMetrics.Get<bool>("ButtonTextContrast");
|
||||
public Color ContrastColor = ChromeMetrics.Get<Color>("ButtonTextContrastColor");
|
||||
public bool Shadow = ChromeMetrics.Get<bool>("ButtonTextShadow");
|
||||
public Color ContrastColorDark = ChromeMetrics.Get<Color>("ButtonTextContrastColorDark");
|
||||
public Color ContrastColorLight = ChromeMetrics.Get<Color>("ButtonTextContrastColorLight");
|
||||
public bool Disabled = false;
|
||||
public bool Highlighted = false;
|
||||
public Func<string> GetText;
|
||||
public Func<Color> GetColor;
|
||||
public Func<Color> GetColorDisabled;
|
||||
public Func<Color> GetContrastColor;
|
||||
public Func<Color> GetContrastColorDark;
|
||||
public Func<Color> GetContrastColorLight;
|
||||
public Func<bool> IsDisabled;
|
||||
public Func<bool> IsHighlighted;
|
||||
public Action<MouseInput> OnMouseDown = _ => { };
|
||||
@@ -67,7 +70,8 @@ namespace OpenRA.Mods.Common.Widgets
|
||||
GetText = () => Text;
|
||||
GetColor = () => TextColor;
|
||||
GetColorDisabled = () => TextColorDisabled;
|
||||
GetContrastColor = () => ContrastColor;
|
||||
GetContrastColorDark = () => ContrastColorDark;
|
||||
GetContrastColorLight = () => ContrastColorLight;
|
||||
OnMouseUp = _ => OnClick();
|
||||
OnKeyPress = _ => OnClick();
|
||||
IsDisabled = () => Disabled;
|
||||
@@ -88,14 +92,17 @@ namespace OpenRA.Mods.Common.Widgets
|
||||
TextColor = other.TextColor;
|
||||
TextColorDisabled = other.TextColorDisabled;
|
||||
Contrast = other.Contrast;
|
||||
ContrastColor = other.ContrastColor;
|
||||
Shadow = other.Shadow;
|
||||
Depressed = other.Depressed;
|
||||
Background = other.Background;
|
||||
VisualHeight = other.VisualHeight;
|
||||
GetText = other.GetText;
|
||||
GetColor = other.GetColor;
|
||||
GetColorDisabled = other.GetColorDisabled;
|
||||
GetContrastColor = other.GetContrastColor;
|
||||
ContrastColorDark = other.ContrastColorDark;
|
||||
ContrastColorLight = other.ContrastColorLight;
|
||||
GetContrastColorDark = other.GetContrastColorDark;
|
||||
GetContrastColorLight = other.GetContrastColorLight;
|
||||
OnMouseDown = other.OnMouseDown;
|
||||
Disabled = other.Disabled;
|
||||
IsDisabled = other.IsDisabled;
|
||||
@@ -213,12 +220,12 @@ namespace OpenRA.Mods.Common.Widgets
|
||||
var rb = RenderBounds;
|
||||
var disabled = IsDisabled();
|
||||
var highlighted = IsHighlighted();
|
||||
|
||||
var font = Game.Renderer.Fonts[Font];
|
||||
var text = GetText();
|
||||
var color = GetColor();
|
||||
var colordisabled = GetColorDisabled();
|
||||
var contrast = GetContrastColor();
|
||||
var bgDark = GetContrastColorDark();
|
||||
var bgLight = GetContrastColorLight();
|
||||
var s = font.Measure(text);
|
||||
var stateOffset = Depressed ? new int2(VisualHeight, VisualHeight) : new int2(0, 0);
|
||||
var position = new int2(rb.X + (UsableWidth - s.X) / 2, rb.Y - BaseLine + (Bounds.Height - s.Y) / 2);
|
||||
@@ -226,7 +233,9 @@ namespace OpenRA.Mods.Common.Widgets
|
||||
DrawBackground(rb, disabled, Depressed, Ui.MouseOverWidget == this, highlighted);
|
||||
if (Contrast)
|
||||
font.DrawTextWithContrast(text, position + stateOffset,
|
||||
disabled ? colordisabled : color, contrast, 2);
|
||||
disabled ? colordisabled : color, bgDark, bgLight, 2);
|
||||
else if (Shadow)
|
||||
font.DrawTextWithShadow(text, position, color, bgDark, bgLight, 1);
|
||||
else
|
||||
font.DrawText(text, position + stateOffset,
|
||||
disabled ? colordisabled : color);
|
||||
|
||||
@@ -21,6 +21,9 @@ namespace OpenRA.Mods.Common.Widgets
|
||||
{
|
||||
public readonly int RemoveTime = 0;
|
||||
public readonly bool UseContrast = false;
|
||||
public readonly bool UseShadow = false;
|
||||
public readonly Color BackgroundColorDark = ChromeMetrics.Get<Color>("TextContrastColorDark");
|
||||
public readonly Color BackgroundColorLight = ChromeMetrics.Get<Color>("TextContrastColorLight");
|
||||
public string Notification = "";
|
||||
|
||||
const int LogLength = 9;
|
||||
@@ -56,12 +59,24 @@ namespace OpenRA.Mods.Common.Widgets
|
||||
|
||||
if (owner != null)
|
||||
{
|
||||
font.DrawTextWithContrast(owner, chatpos,
|
||||
line.Color, Color.Black, UseContrast ? 1 : 0);
|
||||
if (UseContrast)
|
||||
font.DrawTextWithContrast(owner, chatpos,
|
||||
line.Color, BackgroundColorDark, BackgroundColorLight, 1);
|
||||
else if (UseShadow)
|
||||
font.DrawTextWithShadow(owner, chatpos,
|
||||
line.Color, BackgroundColorDark, BackgroundColorLight, 1);
|
||||
else
|
||||
font.DrawText(owner, chatpos, line.Color);
|
||||
}
|
||||
|
||||
font.DrawTextWithContrast(text, chatpos + new int2(inset, 0),
|
||||
Color.White, Color.Black, UseContrast ? 1 : 0);
|
||||
if (UseContrast)
|
||||
font.DrawTextWithContrast(text, chatpos + new int2(inset, 0),
|
||||
Color.White, Color.Black, 1);
|
||||
else if (UseShadow)
|
||||
font.DrawTextWithShadow(text, chatpos + new int2(inset, 0),
|
||||
Color.White, Color.Black, 1);
|
||||
else
|
||||
font.DrawText(text, chatpos + new int2(inset, 0), Color.White);
|
||||
}
|
||||
|
||||
Game.Renderer.DisableScissor();
|
||||
|
||||
@@ -48,7 +48,8 @@ namespace OpenRA.Mods.Common.Widgets
|
||||
var font = Game.Renderer.Fonts[Font];
|
||||
var color = GetColor();
|
||||
var colordisabled = GetColorDisabled();
|
||||
var contrast = GetContrastColor();
|
||||
var bgDark = GetContrastColorDark();
|
||||
var bgLight = GetContrastColorLight();
|
||||
var rect = RenderBounds;
|
||||
var text = GetText();
|
||||
var textSize = font.Measure(text);
|
||||
@@ -64,7 +65,7 @@ namespace OpenRA.Mods.Common.Widgets
|
||||
|
||||
if (Contrast)
|
||||
font.DrawTextWithContrast(text, position,
|
||||
disabled ? colordisabled : color, contrast, 2);
|
||||
disabled ? colordisabled : color, bgDark, bgLight, 2);
|
||||
else
|
||||
font.DrawText(text, position,
|
||||
disabled ? colordisabled : color);
|
||||
|
||||
@@ -27,17 +27,21 @@ namespace OpenRA.Mods.Common.Widgets
|
||||
public string Font = ChromeMetrics.Get<string>("TextFont");
|
||||
public Color TextColor = ChromeMetrics.Get<Color>("TextColor");
|
||||
public bool Contrast = ChromeMetrics.Get<bool>("TextContrast");
|
||||
public Color ContrastColor = ChromeMetrics.Get<Color>("TextContrastColor");
|
||||
public bool Shadow = ChromeMetrics.Get<bool>("TextShadow");
|
||||
public Color ContrastColorDark = ChromeMetrics.Get<Color>("TextContrastColorDark");
|
||||
public Color ContrastColorLight = ChromeMetrics.Get<Color>("TextContrastColorLight");
|
||||
public bool WordWrap = false;
|
||||
public Func<string> GetText;
|
||||
public Func<Color> GetColor;
|
||||
public Func<Color> GetContrastColor;
|
||||
public Func<Color> GetContrastColorDark;
|
||||
public Func<Color> GetContrastColorLight;
|
||||
|
||||
public LabelWidget()
|
||||
{
|
||||
GetText = () => Text;
|
||||
GetColor = () => TextColor;
|
||||
GetContrastColor = () => ContrastColor;
|
||||
GetContrastColorDark = () => ContrastColorDark;
|
||||
GetContrastColorLight = () => ContrastColorLight;
|
||||
}
|
||||
|
||||
protected LabelWidget(LabelWidget other)
|
||||
@@ -48,11 +52,14 @@ namespace OpenRA.Mods.Common.Widgets
|
||||
Font = other.Font;
|
||||
TextColor = other.TextColor;
|
||||
Contrast = other.Contrast;
|
||||
ContrastColor = other.ContrastColor;
|
||||
ContrastColorDark = other.ContrastColorDark;
|
||||
ContrastColorLight = other.ContrastColorLight;
|
||||
Shadow = other.Shadow;
|
||||
WordWrap = other.WordWrap;
|
||||
GetText = other.GetText;
|
||||
GetColor = other.GetColor;
|
||||
GetContrastColor = other.GetContrastColor;
|
||||
GetContrastColorDark = other.GetContrastColorDark;
|
||||
GetContrastColorLight = other.GetContrastColorLight;
|
||||
}
|
||||
|
||||
public override void Draw()
|
||||
@@ -84,9 +91,12 @@ namespace OpenRA.Mods.Common.Widgets
|
||||
text = WidgetUtils.WrapText(text, Bounds.Width, font);
|
||||
|
||||
var color = GetColor();
|
||||
var contrast = GetContrastColor();
|
||||
var bgDark = GetContrastColorDark();
|
||||
var bgLight = GetContrastColorLight();
|
||||
if (Contrast)
|
||||
font.DrawTextWithContrast(text, position, color, contrast, 2);
|
||||
font.DrawTextWithContrast(text, position, color, bgDark, bgLight, 2);
|
||||
else if (Shadow)
|
||||
font.DrawTextWithShadow(text, position, color, bgDark, bgLight, 1);
|
||||
else
|
||||
font.DrawText(text, position, color);
|
||||
}
|
||||
|
||||
@@ -40,6 +40,8 @@ namespace OpenRA.Mods.Common.Widgets
|
||||
public bool DisplayFirstYAxisValue = false;
|
||||
public string LabelFont;
|
||||
public string AxisFont;
|
||||
public Color BackgroundColorDark = ChromeMetrics.Get<Color>("TextContrastColorDark");
|
||||
public Color BackgroundColorLight = ChromeMetrics.Get<Color>("TextContrastColorLight");
|
||||
|
||||
public LineGraphWidget()
|
||||
{
|
||||
@@ -79,6 +81,8 @@ namespace OpenRA.Mods.Common.Widgets
|
||||
DisplayFirstYAxisValue = other.DisplayFirstYAxisValue;
|
||||
LabelFont = other.LabelFont;
|
||||
AxisFont = other.AxisFont;
|
||||
BackgroundColorDark = other.BackgroundColorDark;
|
||||
BackgroundColorLight = other.BackgroundColorLight;
|
||||
}
|
||||
|
||||
public override void Draw()
|
||||
@@ -131,10 +135,12 @@ namespace OpenRA.Mods.Common.Widgets
|
||||
}), 1, color);
|
||||
|
||||
if (lastPoint != 0f)
|
||||
tiny.DrawText(GetValueFormat().F(lastPoint), origin + new float2(lastX * xStep, -lastPoint * scale - 2), color);
|
||||
tiny.DrawTextWithShadow(GetValueFormat().F(lastPoint), origin + new float2(lastX * xStep, -lastPoint * scale - 2),
|
||||
color, BackgroundColorDark, BackgroundColorLight, 1);
|
||||
}
|
||||
|
||||
tiny.DrawText(key, new float2(rect.Left, rect.Top) + new float2(5, 10 * keyOffset + 3), color);
|
||||
tiny.DrawTextWithShadow(key, new float2(rect.Left, rect.Top) + new float2(5, 10 * keyOffset + 3),
|
||||
color, BackgroundColorDark, BackgroundColorLight, 1);
|
||||
keyOffset++;
|
||||
}
|
||||
|
||||
|
||||
@@ -29,7 +29,8 @@ namespace OpenRA.Mods.Common.Widgets
|
||||
|
||||
readonly int timestep;
|
||||
readonly IEnumerable<SupportPowerInstance> powers;
|
||||
Tuple<string, Color, Color>[] texts;
|
||||
readonly Color bgDark, bgLight;
|
||||
Pair<string, Color>[] texts;
|
||||
|
||||
[ObjectCreator.UseCtor]
|
||||
public SupportPowerTimerWidget(World world)
|
||||
@@ -43,6 +44,9 @@ namespace OpenRA.Mods.Common.Widgets
|
||||
timestep = world.Timestep;
|
||||
if (world.IsReplay)
|
||||
timestep = world.WorldActor.Trait<MapOptions>().GameSpeed.Timestep;
|
||||
|
||||
bgDark = ChromeMetrics.Get<Color>("TextContrastColorDark");
|
||||
bgLight = ChromeMetrics.Get<Color>("TextContrastColorLight");
|
||||
}
|
||||
|
||||
public override void Tick()
|
||||
@@ -66,11 +70,7 @@ namespace OpenRA.Mods.Common.Widgets
|
||||
|
||||
var color = !p.Ready || Game.LocalTick % 50 < 25 ? playerColor : Color.White;
|
||||
|
||||
var inversedColor = self.Owner.Color;
|
||||
var inversedL = color == Color.White || inversedColor.L > 128 ? (byte)0 : (byte)255;
|
||||
inversedColor = new HSLColor(inversedColor.H, 0, inversedL);
|
||||
|
||||
return Tuple.Create(text, color, inversedColor.RGB);
|
||||
return Pair.New(text, color);
|
||||
}).ToArray();
|
||||
}
|
||||
|
||||
@@ -83,8 +83,8 @@ namespace OpenRA.Mods.Common.Widgets
|
||||
foreach (var t in texts)
|
||||
{
|
||||
var font = Game.Renderer.Fonts[Font];
|
||||
font.DrawTextWithContrast(t.Item1, new float2(Bounds.Location) + new float2(0, y), t.Item2, t.Item3, 1);
|
||||
y += (font.Measure(t.Item1).Y + 5) * (int)Order;
|
||||
font.DrawTextWithShadow(t.First, new float2(Bounds.Location) + new float2(0, y), t.Second, bgDark, bgLight, 1);
|
||||
y += (font.Measure(t.First).Y + 5) * (int)Order;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user