Refactor checkbox

This commit is contained in:
Gustas
2022-06-20 17:07:45 +03:00
committed by teinarss
parent 804bff1b0e
commit bf5bd63635
13 changed files with 138 additions and 65 deletions

View File

@@ -18,73 +18,74 @@ namespace OpenRA.Mods.Common.Widgets
{ {
public class CheckboxWidget : ButtonWidget public class CheckboxWidget : ButtonWidget
{ {
public string CheckType = "checked"; public new string Background = "checkbox";
public Func<string> GetCheckType; public string Checkmark = "tick";
public Func<string> GetCheckmark;
public Func<bool> IsChecked = () => false; public Func<bool> IsChecked = () => false;
public int CheckOffset = 2;
public bool HasPressedState = ChromeMetrics.Get<bool>("CheckboxPressedState"); CachedTransform<(string, bool, bool), CachedTransform<(bool, bool, bool, bool), Sprite>> getCheckmarkImageCache;
[ObjectCreator.UseCtor] [ObjectCreator.UseCtor]
public CheckboxWidget(ModData modData) public CheckboxWidget(ModData modData)
: base(modData) : base(modData)
{ {
GetCheckType = () => CheckType; GetCheckmark = () => Checkmark;
TextColor = ChromeMetrics.Get<Color>("TextColor"); TextColor = ChromeMetrics.Get<Color>("TextColor");
TextColorDisabled = ChromeMetrics.Get<Color>("TextDisabledColor"); TextColorDisabled = ChromeMetrics.Get<Color>("TextDisabledColor");
GetColor = () => TextColor; GetColor = () => TextColor;
GetColorDisabled = () => TextColorDisabled; GetColorDisabled = () => TextColorDisabled;
CreateCheckmarkImageCache();
} }
protected CheckboxWidget(CheckboxWidget other) protected CheckboxWidget(CheckboxWidget other)
: base(other) : base(other)
{ {
CheckType = other.CheckType; Background = other.Background;
GetCheckType = other.GetCheckType; Checkmark = other.Checkmark;
GetCheckmark = other.GetCheckmark;
IsChecked = other.IsChecked; IsChecked = other.IsChecked;
CheckOffset = other.CheckOffset;
HasPressedState = other.HasPressedState;
TextColor = other.TextColor; TextColor = other.TextColor;
TextColorDisabled = other.TextColorDisabled; TextColorDisabled = other.TextColorDisabled;
GetColor = other.GetColor; GetColor = other.GetColor;
GetColorDisabled = other.GetColorDisabled; GetColorDisabled = other.GetColorDisabled;
CreateCheckmarkImageCache();
}
void CreateCheckmarkImageCache()
{
getCheckmarkImageCache = new CachedTransform<(string, bool, bool), CachedTransform<(bool, bool, bool, bool), Sprite>>(
((string CheckType, bool Highlighted, bool Checked) args) =>
{
var variantImageCollection = args.Highlighted ? "checkmark-highlighted-" : "checkmark-" + args.CheckType;
var variantBaseName = args.Checked ? "checked" : "unchecked";
return WidgetUtils.GetCachedStatefulImage(variantImageCollection, variantBaseName);
});
} }
public override void Draw() public override void Draw()
{ {
var disabled = IsDisabled(); var disabled = IsDisabled();
var font = Game.Renderer.Fonts[Font]; var font = Game.Renderer.Fonts[Font];
var hover = Ui.MouseOverWidget == this;
var color = GetColor(); var color = GetColor();
var colordisabled = GetColorDisabled(); var colordisabled = GetColorDisabled();
var bgDark = GetContrastColorDark();
var bgLight = GetContrastColorLight();
var rect = RenderBounds;
var text = GetText(); var text = GetText();
var textSize = font.Measure(text); var rect = new Rectangle(RenderBounds.Location, new Size(Bounds.Height, Bounds.Height));
var check = new Rectangle(rect.Location, new Size(Bounds.Height, Bounds.Height));
var baseName = IsHighlighted() ? "checkbox-highlighted" : "checkbox";
var state = WidgetUtils.GetStatefulImageName(baseName, disabled, Depressed && HasPressedState, Ui.MouseOverWidget == this);
WidgetUtils.DrawPanel(state, check); DrawBackground(Background, rect, disabled, Depressed, hover, IsHighlighted());
var topOffset = font.TopOffset;
var position = new float2(rect.Left + rect.Height * 1.5f, RenderOrigin.Y + (Bounds.Height - textSize.Y - topOffset) / 2);
var textPosition = new float2(RenderBounds.Left + RenderBounds.Height * 1.5f, RenderOrigin.Y + (Bounds.Height - font.Measure(text).Y - font.TopOffset) / 2);
if (Contrast) if (Contrast)
font.DrawTextWithContrast(text, position, font.DrawTextWithContrast(text, textPosition,
disabled ? colordisabled : color, bgDark, bgLight, 2); disabled ? colordisabled : color, GetContrastColorDark(), GetContrastColorLight(), 2);
else else
font.DrawText(text, position, font.DrawText(text, textPosition,
disabled ? colordisabled : color); disabled ? colordisabled : color);
if (IsChecked() || (Depressed && HasPressedState && !disabled)) var checkmarkImage = getCheckmarkImageCache
{ .Update((GetCheckmark(), IsHighlighted(), IsChecked()))
var checkType = GetCheckType(); .Update((disabled, Depressed, hover, false));
if (HasPressedState && (Depressed || disabled)) WidgetUtils.DrawSprite(checkmarkImage, new float2(rect.Right - (int)((rect.Height + checkmarkImage.Size.X) / 2), rect.Top + (int)((rect.Height - checkmarkImage.Size.Y) / 2)));
checkType += "-disabled";
var offset = new float2(rect.Left + CheckOffset, rect.Top + CheckOffset);
WidgetUtils.DrawSprite(ChromeProvider.GetImage("checkbox-bits", checkType), offset);
}
} }
public override Widget Clone() { return new CheckboxWidget(this); } public override Widget Clone() { return new CheckboxWidget(this); }

View File

@@ -70,7 +70,7 @@ namespace OpenRA.Mods.Common.Widgets.Logic
var checkbox = widget.Get<CheckboxWidget>("OBJECTIVE_STATUS"); var checkbox = widget.Get<CheckboxWidget>("OBJECTIVE_STATUS");
checkbox.IsChecked = () => objective.State != ObjectiveState.Incomplete; checkbox.IsChecked = () => objective.State != ObjectiveState.Incomplete;
checkbox.GetCheckType = () => objective.State == ObjectiveState.Completed ? "checked" : "crossed"; checkbox.GetCheckmark = () => objective.State == ObjectiveState.Completed ? "tick" : "cross";
checkbox.GetText = () => objective.Description; checkbox.GetText = () => objective.Description;
parent.AddChild(widget); parent.AddChild(widget);

View File

@@ -34,8 +34,7 @@ namespace OpenRA.Mods.Common.Widgets.Logic
var statusLabel = widget.Get<LabelWidget>("STATS_STATUS"); var statusLabel = widget.Get<LabelWidget>("STATS_STATUS");
checkbox.IsChecked = () => player.WinState != WinState.Undefined; checkbox.IsChecked = () => player.WinState != WinState.Undefined;
checkbox.GetCheckType = () => player.WinState == WinState.Won ? checkbox.GetCheckmark = () => player.WinState == WinState.Won ? "tick" : "cross";
"checked" : "crossed";
if (player.HasObjectives) if (player.HasObjectives)
{ {

View File

@@ -394,13 +394,29 @@ reload-icon:
disabled-10: 955, 34, 16, 16 disabled-10: 955, 34, 16, 16
disabled-11: 972, 34, 16, 16 disabled-11: 972, 34, 16, 16
checkbox-bits: checkmark-tick:
Inherits: ^Chrome Inherits: ^Chrome
Regions: Regions:
checked: 972, 17, 16, 16 checked: 972, 17, 16, 16
checked-pressed: 989, 17, 16, 16
checked-disabled: 989, 17, 16, 16 checked-disabled: 989, 17, 16, 16
crossed: 972, 0, 16, 16 unchecked: 0, 0, 0, 0
crossed-disabled: 989, 0, 16, 16 unchecked-pressed: 989, 17, 16, 16
checkmark-highlighted-tick:
Inherits: checkmark-tick
checkmark-cross:
Inherits: ^Chrome
Regions:
checked: 972, 0, 16, 16
checked-pressed: 989, 0, 16, 16
checked-disabled: 989, 0, 16, 16
unchecked: 0, 0, 0, 0
unchecked-pressed: 989, 0, 16, 16
checkmark-highlighted-cross:
Inherits: checkmark-cross
flags: flags:
Inherits: ^Chrome Inherits: ^Chrome

View File

@@ -166,7 +166,7 @@ Container@LOBBY_PLAYER_BIN:
Y: 4 Y: 4
Width: 20 Width: 20
Height: 20 Height: 20
ImageCollection: checkbox-bits ImageCollection: checkmark-tick
ImageName: checked ImageName: checked
Visible: false Visible: false
Checkbox@STATUS_CHECKBOX: Checkbox@STATUS_CHECKBOX:
@@ -294,7 +294,7 @@ Container@LOBBY_PLAYER_BIN:
Y: 4 Y: 4
Width: 20 Width: 20
Height: 20 Height: 20
ImageCollection: checkbox-bits ImageCollection: checkmark-tick
ImageName: checked ImageName: checked
Visible: false Visible: false
Container@TEMPLATE_EMPTY: Container@TEMPLATE_EMPTY:
@@ -376,7 +376,7 @@ Container@LOBBY_PLAYER_BIN:
Y: 4 Y: 4
Width: 20 Width: 20
Height: 20 Height: 20
ImageCollection: checkbox-bits ImageCollection: checkmark-tick
ImageName: checked ImageName: checked
Visible: false Visible: false
Checkbox@STATUS_CHECKBOX: Checkbox@STATUS_CHECKBOX:
@@ -457,7 +457,7 @@ Container@LOBBY_PLAYER_BIN:
Y: 4 Y: 4
Width: 20 Width: 20
Height: 20 Height: 20
ImageCollection: checkbox-bits ImageCollection: checkmark-tick
ImageName: checked ImageName: checked
Visible: false Visible: false
Container@TEMPLATE_NEW_SPECTATOR: Container@TEMPLATE_NEW_SPECTATOR:

View File

@@ -3,5 +3,4 @@
Metrics: Metrics:
ButtonDepth: 0 ButtonDepth: 0
ButtonFont: Bold ButtonFont: Bold
CheckboxPressedState: true
TextfieldColorHighlight: 800000 TextfieldColorHighlight: 800000

View File

@@ -168,7 +168,7 @@ Container@LOBBY_PLAYER_BIN:
Y: 4 Y: 4
Width: 20 Width: 20
Height: 20 Height: 20
ImageCollection: checkbox-bits ImageCollection: checkmark-tick
ImageName: checked ImageName: checked
Visible: false Visible: false
Container@TEMPLATE_NONEDITABLE_PLAYER: Container@TEMPLATE_NONEDITABLE_PLAYER:
@@ -288,7 +288,7 @@ Container@LOBBY_PLAYER_BIN:
Y: 4 Y: 4
Width: 20 Width: 20
Height: 20 Height: 20
ImageCollection: checkbox-bits ImageCollection: checkmark-tick
ImageName: checked ImageName: checked
Visible: false Visible: false
Container@TEMPLATE_EMPTY: Container@TEMPLATE_EMPTY:
@@ -373,7 +373,7 @@ Container@LOBBY_PLAYER_BIN:
Y: 4 Y: 4
Width: 20 Width: 20
Height: 20 Height: 20
ImageCollection: checkbox-bits ImageCollection: checkmark-tick
ImageName: checked ImageName: checked
Visible: false Visible: false
Container@TEMPLATE_NONEDITABLE_SPECTATOR: Container@TEMPLATE_NONEDITABLE_SPECTATOR:
@@ -448,7 +448,7 @@ Container@LOBBY_PLAYER_BIN:
Y: 4 Y: 4
Width: 20 Width: 20
Height: 20 Height: 20
ImageCollection: checkbox-bits ImageCollection: checkmark-tick
ImageName: checked ImageName: checked
Visible: false Visible: false
Container@TEMPLATE_NEW_SPECTATOR: Container@TEMPLATE_NEW_SPECTATOR:

View File

@@ -8,7 +8,6 @@ Metrics:
ButtonTextContrastColorLight: 7F7F7F ButtonTextContrastColorLight: 7F7F7F
ButtonTextContrastRadius: 1 ButtonTextContrastRadius: 1
ButtonTextShadow: false ButtonTextShadow: false
CheckboxPressedState: false
GameStartedColor: FFA500 GameStartedColor: FFA500
HotkeyColor: FFFFFF HotkeyColor: FFFFFF
HotkeyColorDisabled: 808080 HotkeyColorDisabled: 808080

View File

@@ -373,18 +373,35 @@ slider-thumb-disabled:
checkbox: checkbox:
Inherits: button-pressed Inherits: button-pressed
checkbox-bits: checkmark-tick:
Inherits: ^Glyphs Inherits: ^Glyphs
Regions: Regions:
checked: 187, 0, 16, 16 checked: 187, 0, 16, 16
checked-disabled: 204, 0, 16, 16 checked-pressed: 204, 0, 16, 16
crossed: 221, 0, 16, 16 unchecked: 0, 0, 0, 0
crossed-disabled: 238, 0, 16, 16 unchecked-pressed: 204, 0, 16, 16
checkmark-highlighted-tick:
Inherits: checkmark-tick
checkmark-cross:
Inherits: ^Glyphs
Regions:
checked: 221, 0, 16, 16
checked-pressed: 238, 0, 16, 16
unchecked: 0, 0, 0, 0
unchecked-pressed: 238, 0, 16, 16
checkmark-highlighted-cross:
Inherits: checkmark-cross
checkbox-hover: checkbox-hover:
Inherits: ^Dialog Inherits: ^Dialog
PanelRegion: 641, 129, 2, 2, 122, 122, 2, 2 PanelRegion: 641, 129, 2, 2, 122, 122, 2, 2
checkbox-pressed:
Inherits: checkbox-hover
# Same as a button-disabled-pressed # Same as a button-disabled-pressed
checkbox-disabled: checkbox-disabled:
Inherits: ^Dialog Inherits: ^Dialog
@@ -396,6 +413,9 @@ checkbox-highlighted:
checkbox-highlighted-hover: checkbox-highlighted-hover:
Inherits: checkbox-highlighted Inherits: checkbox-highlighted
checkbox-highlighted-pressed:
Inherits: checkbox-highlighted
checkbox-highlighted-disabled: checkbox-highlighted-disabled:
Inherits: checkbox-disabled Inherits: checkbox-disabled

View File

@@ -168,7 +168,7 @@ Container@LOBBY_PLAYER_BIN:
Y: 4 Y: 4
Width: 20 Width: 20
Height: 20 Height: 20
ImageCollection: checkbox-bits ImageCollection: checkmark-tick
ImageName: checked ImageName: checked
Visible: false Visible: false
Container@TEMPLATE_NONEDITABLE_PLAYER: Container@TEMPLATE_NONEDITABLE_PLAYER:
@@ -288,7 +288,7 @@ Container@LOBBY_PLAYER_BIN:
Y: 4 Y: 4
Width: 20 Width: 20
Height: 20 Height: 20
ImageCollection: checkbox-bits ImageCollection: checkmark-tick
ImageName: checked ImageName: checked
Visible: false Visible: false
Container@TEMPLATE_EMPTY: Container@TEMPLATE_EMPTY:
@@ -373,7 +373,7 @@ Container@LOBBY_PLAYER_BIN:
Y: 4 Y: 4
Width: 20 Width: 20
Height: 20 Height: 20
ImageCollection: checkbox-bits ImageCollection: checkmark-tick
ImageName: checked ImageName: checked
Visible: false Visible: false
Container@TEMPLATE_NONEDITABLE_SPECTATOR: Container@TEMPLATE_NONEDITABLE_SPECTATOR:
@@ -448,7 +448,7 @@ Container@LOBBY_PLAYER_BIN:
Y: 4 Y: 4
Width: 20 Width: 20
Height: 20 Height: 20
ImageCollection: checkbox-bits ImageCollection: checkmark-tick
ImageName: checked ImageName: checked
Visible: false Visible: false
Container@TEMPLATE_NEW_SPECTATOR: Container@TEMPLATE_NEW_SPECTATOR:

View File

@@ -3,7 +3,6 @@
Metrics: Metrics:
ButtonDepth: 0 ButtonDepth: 0
ButtonFont: Bold ButtonFont: Bold
CheckboxPressedState: true
ChatLineSound: ChatLine ChatLineSound: ChatLine
ClickDisabledSound: ClickDisabledSound ClickDisabledSound: ClickDisabledSound
ClickSound: ClickSound ClickSound: ClickSound

View File

@@ -502,18 +502,35 @@ slider-thumb-disabled:
checkbox: checkbox:
Inherits: button-pressed Inherits: button-pressed
checkbox-bits: checkmark-tick:
Inherits: ^Glyphs Inherits: ^Glyphs
Regions: Regions:
checked: 187, 0, 16, 16 checked: 187, 0, 16, 16
checked-disabled: 204, 0, 16, 16 checked-pressed: 204, 0, 16, 16
crossed: 221, 0, 16, 16 unchecked: 0, 0, 0, 0
crossed-disabled: 238, 0, 16, 16 unchecked-pressed: 204, 0, 16, 16
checkmark-highlighted-tick:
Inherits: checkmark-tick
checkmark-cross:
Inherits: ^Glyphs
Regions:
checked: 221, 0, 16, 16
checked-pressed: 238, 0, 16, 16
unchecked: 0, 0, 0, 0
unchecked-pressed: 238, 0, 16, 16
checkmark-highlighted-cross:
Inherits: checkmark-cross
checkbox-hover: checkbox-hover:
Inherits: ^Dialog Inherits: ^Dialog
PanelRegion: 641, 129, 2, 2, 122, 122, 2, 2 PanelRegion: 641, 129, 2, 2, 122, 122, 2, 2
checkbox-pressed:
Inherits: checkbox-hover
checkbox-disabled: checkbox-disabled:
Inherits: ^Dialog Inherits: ^Dialog
PanelRegion: 641, 257, 2, 2, 122, 122, 2, 2 PanelRegion: 641, 257, 2, 2, 122, 122, 2, 2
@@ -525,6 +542,9 @@ checkbox-highlighted:
checkbox-highlighted-hover: checkbox-highlighted-hover:
Inherits: checkbox-highlighted Inherits: checkbox-highlighted
checkbox-highlighted-pressed:
Inherits: checkbox-highlighted
checkbox-highlighted-disabled: checkbox-highlighted-disabled:
Inherits: checkbox-disabled Inherits: checkbox-disabled

View File

@@ -635,18 +635,35 @@ slider-thumb-disabled:
checkbox: checkbox:
Inherits: button-pressed Inherits: button-pressed
checkbox-bits: checkmark-tick:
Inherits: ^Glyphs Inherits: ^Glyphs
Regions: Regions:
checked: 187, 0, 16, 16 checked: 187, 0, 16, 16
checked-disabled: 204, 0, 16, 16 checked-pressed: 204, 0, 16, 16
crossed: 221, 0, 16, 16 unchecked: 0, 0, 0, 0
crossed-disabled: 238, 0, 16, 16 unchecked-pressed: 204, 0, 16, 16
checkmark-highlighted-tick:
Inherits: checkmark-tick
checkmark-cross:
Inherits: ^Glyphs
Regions:
checked: 221, 0, 16, 16
checked-pressed: 238, 0, 16, 16
unchecked: 0, 0, 0, 0
unchecked-pressed: 238, 0, 16, 16
checkmark-highlighted-cross:
Inherits: checkmark-cross
checkbox-hover: checkbox-hover:
Inherits: ^Dialog Inherits: ^Dialog
PanelRegion: 641, 129, 2, 2, 122, 122, 2, 2 PanelRegion: 641, 129, 2, 2, 122, 122, 2, 2
checkbox-pressed:
Inherits: checkbox-hover
checkbox-disabled: checkbox-disabled:
Inherits: ^Dialog Inherits: ^Dialog
PanelRegion: 641, 257, 2, 2, 122, 122, 2, 2 PanelRegion: 641, 257, 2, 2, 122, 122, 2, 2
@@ -658,6 +675,9 @@ checkbox-highlighted:
checkbox-highlighted-hover: checkbox-highlighted-hover:
Inherits: checkbox-highlighted Inherits: checkbox-highlighted
checkbox-highlighted-pressed:
Inherits: checkbox-highlighted
checkbox-highlighted-disabled: checkbox-highlighted-disabled:
Inherits: checkbox-disabled Inherits: checkbox-disabled