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 string CheckType = "checked";
public Func<string> GetCheckType;
public new string Background = "checkbox";
public string Checkmark = "tick";
public Func<string> GetCheckmark;
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]
public CheckboxWidget(ModData modData)
: base(modData)
{
GetCheckType = () => CheckType;
GetCheckmark = () => Checkmark;
TextColor = ChromeMetrics.Get<Color>("TextColor");
TextColorDisabled = ChromeMetrics.Get<Color>("TextDisabledColor");
GetColor = () => TextColor;
GetColorDisabled = () => TextColorDisabled;
CreateCheckmarkImageCache();
}
protected CheckboxWidget(CheckboxWidget other)
: base(other)
{
CheckType = other.CheckType;
GetCheckType = other.GetCheckType;
Background = other.Background;
Checkmark = other.Checkmark;
GetCheckmark = other.GetCheckmark;
IsChecked = other.IsChecked;
CheckOffset = other.CheckOffset;
HasPressedState = other.HasPressedState;
TextColor = other.TextColor;
TextColorDisabled = other.TextColorDisabled;
GetColor = other.GetColor;
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()
{
var disabled = IsDisabled();
var font = Game.Renderer.Fonts[Font];
var hover = Ui.MouseOverWidget == this;
var color = GetColor();
var colordisabled = GetColorDisabled();
var bgDark = GetContrastColorDark();
var bgLight = GetContrastColorLight();
var rect = RenderBounds;
var text = GetText();
var textSize = font.Measure(text);
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);
var rect = new Rectangle(RenderBounds.Location, new Size(Bounds.Height, Bounds.Height));
WidgetUtils.DrawPanel(state, check);
var topOffset = font.TopOffset;
var position = new float2(rect.Left + rect.Height * 1.5f, RenderOrigin.Y + (Bounds.Height - textSize.Y - topOffset) / 2);
DrawBackground(Background, rect, disabled, Depressed, hover, IsHighlighted());
var textPosition = new float2(RenderBounds.Left + RenderBounds.Height * 1.5f, RenderOrigin.Y + (Bounds.Height - font.Measure(text).Y - font.TopOffset) / 2);
if (Contrast)
font.DrawTextWithContrast(text, position,
disabled ? colordisabled : color, bgDark, bgLight, 2);
font.DrawTextWithContrast(text, textPosition,
disabled ? colordisabled : color, GetContrastColorDark(), GetContrastColorLight(), 2);
else
font.DrawText(text, position,
font.DrawText(text, textPosition,
disabled ? colordisabled : color);
if (IsChecked() || (Depressed && HasPressedState && !disabled))
{
var checkType = GetCheckType();
if (HasPressedState && (Depressed || disabled))
checkType += "-disabled";
var offset = new float2(rect.Left + CheckOffset, rect.Top + CheckOffset);
WidgetUtils.DrawSprite(ChromeProvider.GetImage("checkbox-bits", checkType), offset);
}
var checkmarkImage = getCheckmarkImageCache
.Update((GetCheckmark(), IsHighlighted(), IsChecked()))
.Update((disabled, Depressed, hover, false));
WidgetUtils.DrawSprite(checkmarkImage, new float2(rect.Right - (int)((rect.Height + checkmarkImage.Size.X) / 2), rect.Top + (int)((rect.Height - checkmarkImage.Size.Y) / 2)));
}
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");
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;
parent.AddChild(widget);

View File

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

View File

@@ -394,13 +394,29 @@ reload-icon:
disabled-10: 955, 34, 16, 16
disabled-11: 972, 34, 16, 16
checkbox-bits:
checkmark-tick:
Inherits: ^Chrome
Regions:
checked: 972, 17, 16, 16
checked-pressed: 989, 17, 16, 16
checked-disabled: 989, 17, 16, 16
crossed: 972, 0, 16, 16
crossed-disabled: 989, 0, 16, 16
unchecked: 0, 0, 0, 0
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:
Inherits: ^Chrome

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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