// Copyright 2020 Phyronnaz #pragma once #include "CoreMinimal.h" #include "Templates/SubclassOf.h" #include "VoxelToolManager.generated.h" class UVoxelToolSharedConfig; class UVoxelTool; UCLASS(BlueprintType, Blueprintable) class VOXEL_API UVoxelToolManager : public UObject { GENERATED_BODY() public: UVoxelToolManager(); private: UPROPERTY() TObjectPtr SharedConfig = nullptr; public: UFUNCTION(BlueprintCallable, Category = "Voxel|Tools", meta = (DisplayName = "Get Shared Config")) UVoxelToolSharedConfig* K2_GetSharedConfig() const { return SharedConfig; } UVoxelToolSharedConfig& GetSharedConfig() const { check(SharedConfig); return *SharedConfig; } UFUNCTION(BlueprintCallable, Category = "Voxel|Tools") UVoxelTool* GetActiveTool() const { return ActiveTool; } UFUNCTION(BlueprintCallable, Category = "Voxel|Tools") const TArray& GetTools() const { return Tools; } public: // If bLoadBlueprints is true, all the blueprints inheriting from VoxelTool will be force loaded // If false, tools whose blueprints are not loaded won't show up UFUNCTION(BlueprintCallable, Category = "Voxel|Tools") void CreateDefaultTools(bool bLoadBlueprints = false); UFUNCTION(BlueprintCallable, Category = "Voxel|Tools") void SetActiveTool(UVoxelTool* NewActiveTool); UFUNCTION(BlueprintCallable, Category = "Voxel|Tools") void SetActiveToolByClass(TSubclassOf NewActiveTool); UFUNCTION(BlueprintCallable, Category = "Voxel|Tools") void SetActiveToolByName(FName NewActiveTool); public: template T* GetActiveTool() const { return Cast(ActiveTool); } template void SetActiveTool() { static_assert(TIsDerivedFrom::IsDerived, "T must be derived from UVoxelTool"); SetActiveToolByClass(T::StaticClass()); } template T& GetOrSetActiveTool() { static_assert(TIsDerivedFrom::IsDerived, "T must be derived from UVoxelTool"); if (auto* Tool = Cast(ActiveTool)) { return *Tool; } SetActiveTool(); return *CastChecked(ActiveTool); } private: UPROPERTY(Transient) TObjectPtr ActiveTool = nullptr; UPROPERTY(Transient) TArray> Tools; };