Makes ButtonWidget and Widgets based on it more customizable, sets defaults in metrics.yaml.

This commit is contained in:
reaperrr
2013-11-19 01:39:57 +01:00
parent 915bf2cff0
commit 394a2b5166
7 changed files with 58 additions and 7 deletions

View File

@@ -1,6 +1,6 @@
#region Copyright & License Information #region Copyright & License Information
/* /*
* Copyright 2007-2011 The OpenRA Developers (see AUTHORS) * Copyright 2007-2013 The OpenRA Developers (see AUTHORS)
* This file is part of OpenRA, which is free software. It is made * This file is part of OpenRA, which is free software. It is made
* available to you under the terms of the GNU General Public License * available to you under the terms of the GNU General Public License
* as published by the Free Software Foundation. For more information, * as published by the Free Software Foundation. For more information,
@@ -27,9 +27,16 @@ namespace OpenRA.Widgets
public bool Depressed = false; public bool Depressed = false;
public int VisualHeight = ChromeMetrics.Get<int>("ButtonDepth"); public int VisualHeight = ChromeMetrics.Get<int>("ButtonDepth");
public string Font = ChromeMetrics.Get<string>("ButtonFont"); public string Font = ChromeMetrics.Get<string>("ButtonFont");
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 Disabled = false; public bool Disabled = false;
public bool Highlighted = false; public bool Highlighted = false;
public Func<string> GetText; public Func<string> GetText;
public Func<Color> GetColor;
public Func<Color> GetColorDisabled;
public Func<Color> GetContrastColor;
public Func<bool> IsDisabled; public Func<bool> IsDisabled;
public Func<bool> IsHighlighted; public Func<bool> IsHighlighted;
public Action<MouseInput> OnMouseDown = _ => {}; public Action<MouseInput> OnMouseDown = _ => {};
@@ -48,6 +55,9 @@ namespace OpenRA.Widgets
public ButtonWidget() public ButtonWidget()
{ {
GetText = () => { return Text; }; GetText = () => { return Text; };
GetColor = () => TextColor;
GetColorDisabled = () => TextColorDisabled;
GetContrastColor = () => ContrastColor;
OnMouseUp = _ => OnClick(); OnMouseUp = _ => OnClick();
OnKeyPress = _ => OnClick(); OnKeyPress = _ => OnClick();
IsDisabled = () => Disabled; IsDisabled = () => Disabled;
@@ -61,9 +71,16 @@ namespace OpenRA.Widgets
{ {
Text = other.Text; Text = other.Text;
Font = other.Font; Font = other.Font;
TextColor = other.TextColor;
TextColorDisabled = other.TextColorDisabled;
Contrast = other.Contrast;
ContrastColor = other.ContrastColor;
Depressed = other.Depressed; Depressed = other.Depressed;
VisualHeight = other.VisualHeight; VisualHeight = other.VisualHeight;
GetText = other.GetText; GetText = other.GetText;
GetColor = other.GetColor;
GetColorDisabled = other.GetColorDisabled;
GetContrastColor = other.GetContrastColor;
OnMouseDown = other.OnMouseDown; OnMouseDown = other.OnMouseDown;
Disabled = other.Disabled; Disabled = other.Disabled;
IsDisabled = other.IsDisabled; IsDisabled = other.IsDisabled;
@@ -172,12 +189,19 @@ namespace OpenRA.Widgets
var font = Game.Renderer.Fonts[Font]; var font = Game.Renderer.Fonts[Font];
var text = GetText(); var text = GetText();
var color = GetColor();
var colordisabled = GetColorDisabled();
var contrast = GetContrastColor();
var s = font.Measure(text); var s = font.Measure(text);
var stateOffset = (Depressed) ? new int2(VisualHeight, VisualHeight) : new int2(0, 0); var stateOffset = (Depressed) ? new int2(VisualHeight, VisualHeight) : new int2(0, 0);
DrawBackground(rb, disabled, Depressed, Ui.MouseOverWidget == this, highlighted); DrawBackground(rb, disabled, Depressed, Ui.MouseOverWidget == this, highlighted);
font.DrawText(text, new int2(rb.X + (UsableWidth - s.X)/ 2, rb.Y + (Bounds.Height - s.Y) / 2) + stateOffset, if (Contrast)
disabled ? Color.Gray : Color.White); font.DrawTextWithContrast(text, new int2(rb.X + (UsableWidth - s.X) / 2, rb.Y + (Bounds.Height - s.Y) / 2) + stateOffset,
disabled ? colordisabled : color, contrast, 2);
else
font.DrawText(text, new int2(rb.X + (UsableWidth - s.X)/ 2, rb.Y + (Bounds.Height - s.Y) / 2) + stateOffset,
disabled ? colordisabled : color);
} }
public override Widget Clone() { return new ButtonWidget(this); } public override Widget Clone() { return new ButtonWidget(this); }

View File

@@ -43,6 +43,9 @@ namespace OpenRA.Widgets
{ {
var disabled = IsDisabled(); var disabled = IsDisabled();
var font = Game.Renderer.Fonts[Font]; var font = Game.Renderer.Fonts[Font];
var color = GetColor();
var colordisabled = GetColorDisabled();
var contrast = GetContrastColor();
var rect = RenderBounds; var rect = RenderBounds;
var check = new Rectangle(rect.Location, new Size(Bounds.Height, Bounds.Height)); var check = new Rectangle(rect.Location, new Size(Bounds.Height, Bounds.Height));
var state = disabled ? "checkbox-disabled" : var state = disabled ? "checkbox-disabled" :
@@ -53,9 +56,15 @@ namespace OpenRA.Widgets
WidgetUtils.DrawPanel(state, check); WidgetUtils.DrawPanel(state, check);
var textSize = font.Measure(Text); var textSize = font.Measure(Text);
font.DrawText(Text,
new float2(rect.Left + rect.Height * 1.5f, RenderOrigin.Y - BaseLine + (Bounds.Height - textSize.Y)/2), if (Contrast)
disabled ? Color.Gray : Color.White); font.DrawTextWithContrast(Text,
new float2(rect.Left + rect.Height * 1.5f, RenderOrigin.Y - BaseLine + (Bounds.Height - textSize.Y)/2),
disabled ? colordisabled : color, contrast, 2);
else
font.DrawText(Text,
new float2(rect.Left + rect.Height * 1.5f, RenderOrigin.Y - BaseLine + (Bounds.Height - textSize.Y)/2),
disabled ? colordisabled : color);
if (IsChecked() || (Depressed && HasPressedState && !disabled)) if (IsChecked() || (Depressed && HasPressedState && !disabled))
{ {

View File

@@ -31,6 +31,8 @@ namespace OpenRA.Widgets
var image = ChromeProvider.GetImage("scrollbar", IsDisabled() ? "down_pressed" : "down_arrow"); var image = ChromeProvider.GetImage("scrollbar", IsDisabled() ? "down_pressed" : "down_arrow");
var rb = RenderBounds; var rb = RenderBounds;
var color = GetColor();
var colordisabled = GetColorDisabled();
WidgetUtils.DrawRGBA( image, WidgetUtils.DrawRGBA( image,
stateOffset + new float2( rb.Right - rb.Height + 4, stateOffset + new float2( rb.Right - rb.Height + 4,
@@ -38,7 +40,7 @@ namespace OpenRA.Widgets
WidgetUtils.FillRectWithColor(new Rectangle(stateOffset.X + rb.Right - rb.Height, WidgetUtils.FillRectWithColor(new Rectangle(stateOffset.X + rb.Right - rb.Height,
stateOffset.Y + rb.Top + 3, 1, rb.Height - 6), stateOffset.Y + rb.Top + 3, 1, rb.Height - 6),
Color.White); IsDisabled() ? colordisabled : color);
} }
public override Widget Clone() { return new DropDownButtonWidget(this); } public override Widget Clone() { return new DropDownButtonWidget(this); }

View File

@@ -3,6 +3,10 @@
Metrics: Metrics:
ButtonDepth: 0 ButtonDepth: 0
ButtonFont: Bold ButtonFont: Bold
ButtonTextColor: 255,255,255
ButtonTextColorDisabled: 128,128,128
ButtonTextContrast: false
ButtonTextContrastColor: 0,0,0
CheckboxPressedState: true CheckboxPressedState: true
TextFont: Regular TextFont: Regular
TextColor: 255,255,255 TextColor: 255,255,255

View File

@@ -3,6 +3,10 @@
Metrics: Metrics:
ButtonDepth: 1 ButtonDepth: 1
ButtonFont: Regular ButtonFont: Regular
ButtonTextColor: 255,255,255
ButtonTextColorDisabled: 128,128,128
ButtonTextContrast: false
ButtonTextContrastColor: 0,0,0
CheckboxPressedState: false CheckboxPressedState: false
TextFont: Regular TextFont: Regular
TextColor: 255,255,255 TextColor: 255,255,255

View File

@@ -3,6 +3,10 @@
Metrics: Metrics:
ButtonDepth: 1 ButtonDepth: 1
ButtonFont: Regular ButtonFont: Regular
ButtonTextColor: 255,255,255
ButtonTextColorDisabled: 128,128,128
ButtonTextContrast: true
ButtonTextContrastColor: 0,0,0
CheckboxPressedState: false CheckboxPressedState: false
TextFont: Regular TextFont: Regular
TextColor: 255,255,255 TextColor: 255,255,255

View File

@@ -3,6 +3,10 @@
Metrics: Metrics:
ButtonDepth: 1 ButtonDepth: 1
ButtonFont: Regular ButtonFont: Regular
ButtonTextColor: 255,255,255
ButtonTextColorDisabled: 128,128,128
ButtonTextContrast: false
ButtonTextContrastColor: 0,0,0
CheckboxPressedState: false CheckboxPressedState: false
TextFont: Regular TextFont: Regular
TextColor: 255,255,255 TextColor: 255,255,255