From 183a6011709493cf23bae6c7518b9fea1b482263 Mon Sep 17 00:00:00 2001 From: Steve Streeting Date: Mon, 23 Nov 2020 11:50:40 +0000 Subject: [PATCH] Useful utility to set Brush content from a sprite without using a UImage --- .../Private/StevesGameSubsystem.cpp | 25 +++++++++++++++++++ .../Public/StevesGameSubsystem.h | 9 +++++++ 2 files changed, 34 insertions(+) diff --git a/Source/StevesUEHelpers/Private/StevesGameSubsystem.cpp b/Source/StevesUEHelpers/Private/StevesGameSubsystem.cpp index 0ae5ee8..a57c9b0 100644 --- a/Source/StevesUEHelpers/Private/StevesGameSubsystem.cpp +++ b/Source/StevesUEHelpers/Private/StevesGameSubsystem.cpp @@ -191,6 +191,31 @@ UPaperSprite* UStevesGameSubsystem::GetImageSpriteFromTable(const FKey& InKey, return nullptr; } +void UStevesGameSubsystem::SetBrushFromAtlas(FSlateBrush* Brush, TScriptInterface AtlasRegion, bool bMatchSize) +{ + if(Brush->GetResourceObject() != AtlasRegion.GetObject()) + { + Brush->SetResourceObject(AtlasRegion.GetObject()); + + if (bMatchSize) + { + if (AtlasRegion) + { + const FSlateAtlasData AtlasData = AtlasRegion->GetSlateAtlasData(); + Brush->ImageSize = AtlasData.GetSourceDimensions(); + } + else + { + Brush->ImageSize = FVector2D(0, 0); + } + } + } +} + + + + + UStevesGameSubsystem::FInputModeDetector::FInputModeDetector() { // 4 local players should be plenty usually (will expand if necessary) diff --git a/Source/StevesUEHelpers/Public/StevesGameSubsystem.h b/Source/StevesUEHelpers/Public/StevesGameSubsystem.h index adf44e8..a638f36 100644 --- a/Source/StevesUEHelpers/Public/StevesGameSubsystem.h +++ b/Source/StevesUEHelpers/Public/StevesGameSubsystem.h @@ -166,4 +166,13 @@ public: */ UPaperSprite* GetInputImageSpriteFromKey(const FKey& Key, const UUiTheme* Theme = nullptr); + /** + * @brief Set the content of a slate brush from an atlas (e.g. sprite) + * @param Brush The brush to update + * @param AtlasRegion Atlas to use as source e.g. a Sprite + * @param bMatchSize Whether to resize the brush to match the atlas entry + */ + void SetBrushFromAtlas(FSlateBrush* Brush, TScriptInterface AtlasRegion, + bool bMatchSize); + };