Add highlighted state to GetCachedStatefulImage

This commit is contained in:
Gustas
2022-09-13 15:28:32 +03:00
committed by Pavel Penev
parent a75818026a
commit ba763ac0f0
9 changed files with 40 additions and 45 deletions

View File

@@ -23,7 +23,14 @@ namespace OpenRA.Mods.Common.Widgets
public Func<string> GetCheckmark;
public Func<bool> IsChecked = () => false;
CachedTransform<(string, bool, bool), CachedTransform<(bool, bool, bool, bool), Sprite>> getCheckmarkImageCache;
readonly CachedTransform<(string, bool), CachedTransform<(bool, bool, bool, bool, bool), Sprite>> getCheckmarkImageCache
= new CachedTransform<(string, bool), CachedTransform<(bool, bool, bool, bool, bool), Sprite>>(
((string CheckType, bool Checked) args) =>
{
var variantImageCollection = "checkmark-" + args.CheckType;
var variantBaseName = args.Checked ? "checked" : "unchecked";
return WidgetUtils.GetCachedStatefulImage(variantImageCollection, variantBaseName);
});
[ObjectCreator.UseCtor]
public CheckboxWidget(ModData modData)
@@ -34,7 +41,6 @@ namespace OpenRA.Mods.Common.Widgets
TextColorDisabled = ChromeMetrics.Get<Color>("TextDisabledColor");
GetColor = () => TextColor;
GetColorDisabled = () => TextColorDisabled;
CreateCheckmarkImageCache();
}
protected CheckboxWidget(CheckboxWidget other)
@@ -48,18 +54,6 @@ namespace OpenRA.Mods.Common.Widgets
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()
@@ -83,8 +77,8 @@ namespace OpenRA.Mods.Common.Widgets
disabled ? colordisabled : color);
var checkmarkImage = getCheckmarkImageCache
.Update((GetCheckmark(), IsHighlighted(), IsChecked()))
.Update((disabled, Depressed, hover, false));
.Update((GetCheckmark(), IsChecked()))
.Update((disabled, Depressed, hover, false, IsHighlighted()));
WidgetUtils.DrawSprite(checkmarkImage, new float2(rect.Right - (int)((rect.Height + checkmarkImage.Size.X) / 2), rect.Top + (int)((rect.Height - checkmarkImage.Size.Y) / 2)));
}

View File

@@ -30,8 +30,8 @@ namespace OpenRA.Mods.Common.Widgets
Widget panel;
MaskWidget fullscreenMask;
Widget panelRoot;
CachedTransform<(bool Disabled, bool Pressed, bool Hover, bool Focused), Sprite> getMarkerImage;
CachedTransform<(bool Disabled, bool Pressed, bool Hover, bool Focused), Sprite> getSeparatorImage;
CachedTransform<(bool Disabled, bool Pressed, bool Hover, bool Focused, bool Highlighted), Sprite> getMarkerImage;
CachedTransform<(bool Disabled, bool Pressed, bool Hover, bool Focused, bool Highlighted), Sprite> getSeparatorImage;
[ObjectCreator.UseCtor]
public DropDownButtonWidget(ModData modData)
@@ -59,13 +59,13 @@ namespace OpenRA.Mods.Common.Widgets
if (getMarkerImage == null)
getMarkerImage = WidgetUtils.GetCachedStatefulImage(Decorations, DecorationMarker);
var arrowImage = getMarkerImage.Update((isDisabled, Depressed, isHover, false));
var arrowImage = getMarkerImage.Update((isDisabled, Depressed, isHover, false, IsHighlighted()));
WidgetUtils.DrawSprite(arrowImage, stateOffset + new float2(rb.Right - (int)((rb.Height + arrowImage.Size.X) / 2), rb.Top + (int)((rb.Height - arrowImage.Size.Y) / 2)));
if (getSeparatorImage == null)
getSeparatorImage = WidgetUtils.GetCachedStatefulImage(Separators, SeparatorImage);
var separatorImage = getSeparatorImage.Update((isDisabled, Depressed, isHover, false));
var separatorImage = getSeparatorImage.Update((isDisabled, Depressed, isHover, false, IsHighlighted()));
if (separatorImage != null)
WidgetUtils.DrawSprite(separatorImage, stateOffset + new float2(-3, 0) + new float2(rb.Right - rb.Height + 4, rb.Top + (int)((rb.Height - separatorImage.Size.Y) / 2)));
}

View File

@@ -86,8 +86,8 @@ namespace OpenRA.Mods.Common.Widgets
public readonly string Decorations = "scrollpanel-decorations";
public readonly string DecorationScrollLeft = "left";
public readonly string DecorationScrollRight = "right";
readonly CachedTransform<(bool Disabled, bool Pressed, bool Hover, bool Focused), Sprite> getLeftArrowImage;
readonly CachedTransform<(bool Disabled, bool Pressed, bool Hover, bool Focused), Sprite> getRightArrowImage;
readonly CachedTransform<(bool Disabled, bool Pressed, bool Hover, bool Focused, bool Highlighted), Sprite> getLeftArrowImage;
readonly CachedTransform<(bool Disabled, bool Pressed, bool Hover, bool Focused, bool Highlighted), Sprite> getRightArrowImage;
int contentWidth = 0;
float listOffset = 0;
@@ -193,11 +193,11 @@ namespace OpenRA.Mods.Common.Widgets
ButtonWidget.DrawBackground(Button, leftButtonRect, leftDisabled, leftPressed, leftHover, false);
ButtonWidget.DrawBackground(Button, rightButtonRect, rightDisabled, rightPressed, rightHover, false);
var leftArrowImage = getLeftArrowImage.Update((leftDisabled, leftPressed, leftHover, false));
var leftArrowImage = getLeftArrowImage.Update((leftDisabled, leftPressed, leftHover, false, false));
WidgetUtils.DrawSprite(leftArrowImage,
new float2(leftButtonRect.Left + 2, leftButtonRect.Top + 2));
var rightArrowImage = getRightArrowImage.Update((rightDisabled, rightPressed, rightHover, false));
var rightArrowImage = getRightArrowImage.Update((rightDisabled, rightPressed, rightHover, false, false));
WidgetUtils.DrawSprite(rightArrowImage,
new float2(rightButtonRect.Left + 2, rightButtonRect.Top + 2));

View File

@@ -52,8 +52,8 @@ namespace OpenRA.Mods.Common.Widgets
public string Decorations = "scrollpanel-decorations";
public readonly string DecorationScrollUp = "up";
public readonly string DecorationScrollDown = "down";
readonly CachedTransform<(bool Disabled, bool Pressed, bool Hover, bool Focused), Sprite> getUpArrowImage;
readonly CachedTransform<(bool Disabled, bool Pressed, bool Hover, bool Focused), Sprite> getDownArrowImage;
readonly CachedTransform<(bool Disabled, bool Pressed, bool Hover, bool Focused, bool Highlighted), Sprite> getUpArrowImage;
readonly CachedTransform<(bool Disabled, bool Pressed, bool Hover, bool Focused, bool Highlighted), Sprite> getDownArrowImage;
public int ContentHeight;
public ILayout Layout;
public int MinimumThumbSize = 10;
@@ -211,11 +211,11 @@ namespace OpenRA.Mods.Common.Widgets
var upOffset = !upPressed || upDisabled ? 4 : 4 + ButtonDepth;
var downOffset = !downPressed || downDisabled ? 4 : 4 + ButtonDepth;
var upArrowImage = getUpArrowImage.Update((upDisabled, upPressed, upHover, false));
var upArrowImage = getUpArrowImage.Update((upDisabled, upPressed, upHover, false, false));
WidgetUtils.DrawSprite(upArrowImage,
new float2(upButtonRect.Left + upOffset, upButtonRect.Top + upOffset));
var downArrowImage = getDownArrowImage.Update((downDisabled, downPressed, downHover, false));
var downArrowImage = getDownArrowImage.Update((downDisabled, downPressed, downHover, false, false));
WidgetUtils.DrawSprite(downArrowImage,
new float2(downButtonRect.Left + downOffset, downButtonRect.Top + downOffset));
}

View File

@@ -31,13 +31,14 @@ namespace OpenRA.Mods.Common.Widgets
return baseName + suffix;
}
public static CachedTransform<(bool Disabled, bool Pressed, bool Hover, bool Focused), Sprite> GetCachedStatefulImage(string collection, string baseName)
public static CachedTransform<(bool Disabled, bool Pressed, bool Hover, bool Focused, bool Highlighted), Sprite> GetCachedStatefulImage(string collection, string imageName)
{
return new CachedTransform<(bool, bool, bool, bool), Sprite>(
((bool Disabled, bool Pressed, bool Hover, bool Focused) args) =>
return new CachedTransform<(bool, bool, bool, bool, bool), Sprite>(
((bool Disabled, bool Pressed, bool Hover, bool Focused, bool Highlighted) args) =>
{
var imageName = GetStatefulImageName(baseName, args.Disabled, args.Pressed, args.Hover, args.Focused);
return ChromeProvider.GetImage(collection, imageName) ?? ChromeProvider.GetImage(collection, baseName);
var collectionName = collection + (args.Highlighted ? "-highlighted" : "");
var variantImageName = GetStatefulImageName(imageName, args.Disabled, args.Pressed, args.Hover, args.Focused);
return ChromeProvider.GetImage(collectionName, variantImageName) ?? ChromeProvider.GetImage(collectionName, imageName);
});
}

View File

@@ -435,7 +435,7 @@ checkmark-tick:
unchecked: 0, 0, 0, 0
unchecked-pressed: 989, 17, 16, 16
checkmark-highlighted-tick:
checkmark-tick-highlighted:
Inherits: checkmark-tick
checkmark-cross:
@@ -447,7 +447,7 @@ checkmark-cross:
unchecked: 0, 0, 0, 0
unchecked-pressed: 989, 0, 16, 16
checkmark-highlighted-cross:
checkmark-cross-highlighted:
Inherits: checkmark-cross
checkmark-mute:
@@ -458,7 +458,7 @@ checkmark-mute:
checked: 1006, 34, 16, 16
checked-pressed: 989, 34, 16, 16
checkmark-highlighted-mute:
checkmark-mute-highlighted:
Inherits: checkmark-mute
flags:

View File

@@ -381,7 +381,7 @@ checkmark-tick:
unchecked: 0, 0, 0, 0
unchecked-pressed: 204, 0, 16, 16
checkmark-highlighted-tick:
checkmark-tick-highlighted:
Inherits: checkmark-tick
checkmark-cross:
@@ -392,7 +392,7 @@ checkmark-cross:
unchecked: 0, 0, 0, 0
unchecked-pressed: 238, 0, 16, 16
checkmark-highlighted-cross:
checkmark-cross-highlighted:
Inherits: checkmark-cross
checkmark-mute:
@@ -403,7 +403,7 @@ checkmark-mute:
checked: 238, 34, 16, 16
checked-pressed: 221, 34, 16, 16
checkmark-highlighted-mute:
checkmark-mute-highlighted:
Inherits: checkmark-mute
checkbox-hover:

View File

@@ -518,7 +518,7 @@ checkmark-tick:
unchecked: 0, 0, 0, 0
unchecked-pressed: 204, 0, 16, 16
checkmark-highlighted-tick:
checkmark-tick-highlighted:
Inherits: checkmark-tick
checkmark-cross:
@@ -529,7 +529,7 @@ checkmark-cross:
unchecked: 0, 0, 0, 0
unchecked-pressed: 238, 0, 16, 16
checkmark-highlighted-cross:
checkmark-cross-highlighted:
Inherits: checkmark-cross
checkmark-mute:
@@ -540,7 +540,7 @@ checkmark-mute:
checked: 17, 170, 16, 16
checked-pressed: 0, 170, 16, 16
checkmark-highlighted-mute:
checkmark-mute-highlighted:
Inherits: checkmark-mute
checkbox-hover:

View File

@@ -643,7 +643,7 @@ checkmark-tick:
unchecked: 0, 0, 0, 0
unchecked-pressed: 204, 0, 16, 16
checkmark-highlighted-tick:
checkmark-tick-highlighted:
Inherits: checkmark-tick
checkmark-cross:
@@ -654,7 +654,7 @@ checkmark-cross:
unchecked: 0, 0, 0, 0
unchecked-pressed: 238, 0, 16, 16
checkmark-highlighted-cross:
checkmark-cross-highlighted:
Inherits: checkmark-cross
checkmark-mute:
@@ -665,7 +665,7 @@ checkmark-mute:
checked: 238, 34, 16, 16
checked-pressed: 221, 34, 16, 16
checkmark-highlighted-mute:
checkmark-mute-highlighted:
Inherits: checkmark-mute
checkbox-hover: