Refactor ScrollItemWidget to use stateful image names

This commit is contained in:
Gustas
2022-08-23 16:40:47 +03:00
committed by Pavel Penev
parent 11a2e6e19b
commit 08dbfe0cbd
7 changed files with 65 additions and 24 deletions

View File

@@ -10,6 +10,7 @@
#endregion #endregion
using System; using System;
using OpenRA.Graphics;
using OpenRA.Widgets; using OpenRA.Widgets;
namespace OpenRA.Mods.Common.Widgets namespace OpenRA.Mods.Common.Widgets
@@ -20,12 +21,15 @@ namespace OpenRA.Mods.Common.Widgets
public readonly bool EnableChildMouseOver = false; public readonly bool EnableChildMouseOver = false;
public string ItemKey; public string ItemKey;
readonly CachedTransform<(bool, bool, bool, bool, bool), Sprite[]> getPanelCache;
[ObjectCreator.UseCtor] [ObjectCreator.UseCtor]
public ScrollItemWidget(ModData modData) public ScrollItemWidget(ModData modData)
: base(modData) : base(modData)
{ {
IsVisible = () => false; IsVisible = () => false;
VisualHeight = 0; VisualHeight = 0;
getPanelCache = WidgetUtils.GetCachedStatefulPanelImages(BaseName);
} }
protected ScrollItemWidget(ScrollItemWidget other) protected ScrollItemWidget(ScrollItemWidget other)
@@ -36,6 +40,7 @@ namespace OpenRA.Mods.Common.Widgets
Key = other.Key; Key = other.Key;
BaseName = other.BaseName; BaseName = other.BaseName;
EnableChildMouseOver = other.EnableChildMouseOver; EnableChildMouseOver = other.EnableChildMouseOver;
getPanelCache = WidgetUtils.GetCachedStatefulPanelImages(BaseName);
} }
public override void Initialize(WidgetArgs args) public override void Initialize(WidgetArgs args)
@@ -51,17 +56,18 @@ namespace OpenRA.Mods.Common.Widgets
public override void Draw() public override void Draw()
{ {
if (string.IsNullOrEmpty(BaseName))
return;
// PERF: Only check for ourself or our direct children // PERF: Only check for ourself or our direct children
var isHover = Ui.MouseOverWidget == this; var hover = Ui.MouseOverWidget == this;
if (!IgnoreChildMouseOver && !isHover) if (!IgnoreChildMouseOver && !hover)
isHover = Children.Contains(Ui.MouseOverWidget); hover = Children.Contains(Ui.MouseOverWidget);
var state = IsSelected() ? BaseName + "-selected" : var panel = getPanelCache.Update((IsDisabled(), Depressed, hover, false, IsSelected() || IsHighlighted()));
isHover ? BaseName + "-hover" :
null;
if (state != null) if (panel != null)
WidgetUtils.DrawPanel(state, RenderBounds); WidgetUtils.DrawPanel(RenderBounds, panel);
} }
public override Widget Clone() { return new ScrollItemWidget(this); } public override Widget Clone() { return new ScrollItemWidget(this); }

View File

@@ -41,6 +41,18 @@ namespace OpenRA.Mods.Common.Widgets
}); });
} }
// TODO: refactor buttons and related UI to use this function
public static CachedTransform<(bool Disabled, bool Pressed, bool Hover, bool Focused, bool Highlighted), Sprite[]> GetCachedStatefulPanelImages(string collection)
{
return new CachedTransform<(bool, bool, bool, bool, bool), Sprite[]>(
((bool Disabled, bool Pressed, bool Hover, bool Focused, bool Highlighted) args) =>
{
var collectionName = collection + (args.Highlighted ? "-highlighted" : "");
var variantCollectionName = GetStatefulImageName(collectionName, args.Disabled, args.Pressed, args.Hover, args.Focused);
return ChromeProvider.GetPanelImages(variantCollectionName) ?? ChromeProvider.GetPanelImages(collectionName);
});
}
public static void DrawSprite(Sprite s, float2 pos) public static void DrawSprite(Sprite s, float2 pos)
{ {
Game.Renderer.RgbaSpriteRenderer.DrawSprite(s, pos); Game.Renderer.RgbaSpriteRenderer.DrawSprite(s, pos);

View File

@@ -205,16 +205,23 @@ scrollpanel-button-gdi-disabled:
scrollpanel-button-gdi-pressed: scrollpanel-button-gdi-pressed:
Inherits: button-gdi-pressed Inherits: button-gdi-pressed
scrollitem:
scrollitem-hover: scrollitem-hover:
Inherits: button Inherits: button
scrollitem-selected: scrollitem-highlighted:
Inherits: button-pressed Inherits: button-pressed
scrollitem-nohover: scrollitem-nohover:
scrollitem-nohover-hover: scrollitem-nohover-highlighted:
scrollheader:
Inherits: button
scrollheader-highlighted:
Inherits: button
# #
# Slider # Slider

View File

@@ -24,6 +24,7 @@ Container@MISSIONBROWSER_PANEL:
Height: PARENT_BOTTOM - 30 Height: PARENT_BOTTOM - 30
Children: Children:
ScrollItem@HEADER: ScrollItem@HEADER:
BaseName: scrollheader
Width: PARENT_RIGHT - 27 Width: PARENT_RIGHT - 27
Height: 13 Height: 13
X: 2 X: 2

View File

@@ -446,18 +446,23 @@ checkbox-toggle-highlighted-hover:
checkbox-toggle-highlighted-pressed: checkbox-toggle-highlighted-pressed:
Inherits: checkbox-highlighted-pressed Inherits: checkbox-highlighted-pressed
scrollitem-selected: scrollitem:
Inherits: button-pressed
scrollitem-hover: scrollitem-hover:
Inherits: button Inherits: button
scrollheader-selected: scrollitem-highlighted:
Inherits: button Inherits: button-pressed
scrollitem-nohover: scrollitem-nohover:
scrollitem-nohover-hover: scrollitem-nohover-highlighted:
scrollheader:
Inherits: button
scrollheader-highlighted:
Inherits: button
scrollpanel-decorations: scrollpanel-decorations:
Inherits: ^Glyphs Inherits: ^Glyphs

View File

@@ -575,18 +575,23 @@ checkbox-toggle-highlighted-hover:
checkbox-toggle-highlighted-pressed: checkbox-toggle-highlighted-pressed:
Inherits: checkbox-highlighted-pressed Inherits: checkbox-highlighted-pressed
scrollitem-selected: scrollitem:
Inherits: button-pressed
scrollitem-hover: scrollitem-hover:
Inherits: button Inherits: button
scrollheader-selected: scrollitem-highlighted:
Inherits: button Inherits: button-pressed
scrollitem-nohover: scrollitem-nohover:
scrollitem-nohover-hover: scrollitem-nohover-highlighted:
scrollheader:
Inherits: button
scrollheader-highlighted:
Inherits: button
logos: logos:
Inherits: ^LoadScreen Inherits: ^LoadScreen

View File

@@ -708,17 +708,22 @@ checkbox-toggle-highlighted-hover:
checkbox-toggle-highlighted-pressed: checkbox-toggle-highlighted-pressed:
Inherits: checkbox-highlighted-pressed Inherits: checkbox-highlighted-pressed
scrollitem-selected: scrollitem:
Inherits: button-pressed
scrollitem-hover: scrollitem-hover:
Inherits: button Inherits: button
scrollitem-highlighted:
Inherits: button-pressed
scrollitem-nohover: scrollitem-nohover:
scrollitem-nohover-hover: scrollitem-nohover-highlighted:
scrollheader-selected: scrollheader:
Inherits: button
scrollheader-highlighted:
Inherits: button Inherits: button
mainmenu-border: mainmenu-border: