Skip to content

Commit

Permalink
Use CachedTransform in ImageWidget
Browse files Browse the repository at this point in the history
  • Loading branch information
PunkPun committed Sep 13, 2022
1 parent 1536530 commit c732982
Showing 1 changed file with 20 additions and 7 deletions.
27 changes: 20 additions & 7 deletions OpenRA.Mods.Common/Widgets/ImageWidget.cs
Expand Up @@ -31,13 +31,17 @@ public class ImageWidget : Widget
readonly Lazy<TooltipContainerWidget> tooltipContainer;
public Func<string> GetTooltipText;

CachedTransform<(string, string), Sprite> getImageCache;

public ImageWidget()
{
GetImageName = () => ImageName;
GetImageCollection = () => ImageCollection;
GetTooltipText = () => TooltipText;
tooltipContainer = Exts.Lazy(() =>
Ui.Root.Get<TooltipContainerWidget>(TooltipContainer));

CreateImageCache();
}

protected ImageWidget(ImageWidget other)
Expand All @@ -54,19 +58,28 @@ protected ImageWidget(ImageWidget other)
GetTooltipText = other.GetTooltipText;
tooltipContainer = Exts.Lazy(() =>
Ui.Root.Get<TooltipContainerWidget>(TooltipContainer));

CreateImageCache();
}

void CreateImageCache()
{
getImageCache = new CachedTransform<(string, string), Sprite>(
((string collection, string image) args) =>
{
var sprite = ChromeProvider.GetImage(args.collection, args.image);
if (sprite == null)
throw new ArgumentException($"Sprite {args.collection}/{args.image} was not found.");
return sprite;
});
}

public override Widget Clone() { return new ImageWidget(this); }

public override void Draw()
{
var name = GetImageName();
var collection = GetImageCollection();

var sprite = ChromeProvider.GetImage(collection, name);
if (sprite == null)
throw new ArgumentException($"Sprite {collection}/{name} was not found.");

var sprite = getImageCache.Update((GetImageCollection(), GetImageName()));
WidgetUtils.DrawSprite(sprite, RenderOrigin);
}

Expand Down

0 comments on commit c732982

Please sign in to comment.