Advertisement
Guest User

Untitled

a guest
Nov 15th, 2019
183
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.00 KB | None | 0 0
  1. #pragma once
  2.  
  3. #include "Async/Future.h"
  4. #include "Containers/Array.h"
  5. #include "Containers/UnrealString.h"
  6. #include "PixelFormat.h"
  7. #include "Templates/Function.h"
  8. #include "UObject/Object.h"
  9. #include "ImageLoader.generated.h"
  10.  
  11. // Forward declarations
  12. class UTexture2D;
  13.  
  14. /**
  15. Utility class for asynchronously loading an image into a texture.
  16. Allows Blueprint scripts to request asynchronous loading of an image and be notified when loading is complete.
  17. */
  18. UCLASS( BlueprintType )
  19. class UImageLoader : public UObject
  20. {
  21.     GENERATED_BODY()
  22.  
  23. public:
  24.     /**
  25.     Loads an image file from disk into a texture on a worker thread. This will not block the calling thread.
  26.     @return An image loader object with an OnLoadCompleted event that users can bind to, to get notified when loading is done.
  27.     */
  28.     UFUNCTION( BlueprintCallable, Category = ImageLoader, meta = ( HidePin = "Outer", DefaultToSelf = "Outer" ) )
  29.     static UImageLoader* LoadImageFromDiskAsync( UObject* Outer, const FString& ImagePath );
  30.  
  31.     /**
  32.     Loads an image file from disk into a texture on a worker thread. This will not block the calling thread.
  33.     @return A future object which will hold the image texture once loading is done.
  34.     */
  35.     static TFuture< UTexture2D* > LoadImageFromDiskAsync( UObject* Outer, const FString& ImagePath, TFunction< void() > CompletionCallback );
  36.  
  37.     /**
  38.     Loads an image file from disk into a texture. This will block the calling thread until completed.
  39.     @return A texture created from the loaded image file.
  40.     */
  41.     UFUNCTION( BlueprintCallable, Category = ImageLoader, meta = ( HidePin = "Outer", DefaultToSelf = "Outer" ) )
  42.     static UTexture2D* LoadImageFromDisk( UObject* Outer, const FString& ImagePath );
  43.  
  44. public:
  45.     /**
  46.     Declare a broadcast-style delegate type, which is used for the load completed event.
  47.     Dynamic multicast delegates are the only type of event delegates that Blueprint scripts can bind to.
  48.     */
  49.     DECLARE_DYNAMIC_MULTICAST_DELEGATE_OneParam( FOnImageLoadCompleted, UTexture2D*, Texture );
  50.  
  51.     /** This accessor function allows C++ code to bind to the event. */
  52.     FOnImageLoadCompleted& OnLoadCompleted()
  53.     {
  54.         return LoadCompleted;
  55.     }
  56.  
  57. private:
  58.     /** Helper function that initiates the loading operation and fires the event when loading is done. */
  59.     void LoadImageAsync( UObject* Outer, const FString& ImagePath );
  60.  
  61.     /** Helper function to dynamically create a new texture from raw pixel data. */
  62.     static UTexture2D* CreateTexture(
  63.         UObject* Outer, const TArray< uint8 >& PixelData, int32 InSizeX, int32 InSizeY, EPixelFormat PixelFormat = EPixelFormat::PF_B8G8R8A8, FName BaseName = NAME_None );
  64.  
  65. private:
  66.     /**
  67.     Holds the load completed event delegate.
  68.     Giving Blueprint access to this private variable allows Blueprint scripts to bind to the event.
  69.     */
  70.     UPROPERTY( BlueprintAssignable, Category = ImageLoader, meta = ( AllowPrivateAccess = true ) )
  71.     FOnImageLoadCompleted LoadCompleted;
  72.  
  73.     /** Holds the future value which represents the asynchronous loading operation. */
  74.     TFuture< UTexture2D* > Future;
  75. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement