添加背包形状资源图标
This commit is contained in:
parent
bd75d739f1
commit
9171347f2e
@ -3,6 +3,9 @@
|
|||||||
#include "AssetTypeActions_Base.h"
|
#include "AssetTypeActions_Base.h"
|
||||||
#include "DataTableRowSelectorCustomization.h"
|
#include "DataTableRowSelectorCustomization.h"
|
||||||
#include "AssetActions/BagShapeAssetTypeAction.h"
|
#include "AssetActions/BagShapeAssetTypeAction.h"
|
||||||
|
#include "AssetRegistry/AssetRegistryModule.h"
|
||||||
|
#include "ProjectFish/DataAsset/BagShapeAsset.h"
|
||||||
|
#include "Thimbnail/BagShapeAssetThumbnailRenderer.h"
|
||||||
|
|
||||||
#define LOCTEXT_NAMESPACE "FProjectFishEditorModule"
|
#define LOCTEXT_NAMESPACE "FProjectFishEditorModule"
|
||||||
|
|
||||||
@ -36,6 +39,9 @@ void FProjectFishEditorModule::RegisterAssetTypeActions()
|
|||||||
TSharedRef<FAssetTypeActions_Base> BagShapeAction = MakeShareable(new FBagShapeAssetTypeAction(BoxShapeAssetCategory));
|
TSharedRef<FAssetTypeActions_Base> BagShapeAction = MakeShareable(new FBagShapeAssetTypeAction(BoxShapeAssetCategory));
|
||||||
AssetTools.RegisterAssetTypeActions(BagShapeAction);
|
AssetTools.RegisterAssetTypeActions(BagShapeAction);
|
||||||
CreatedAssetTypeActions.Add(BagShapeAction);
|
CreatedAssetTypeActions.Add(BagShapeAction);
|
||||||
|
|
||||||
|
|
||||||
|
RegisterThumbnailRenderers();
|
||||||
}
|
}
|
||||||
|
|
||||||
void FProjectFishEditorModule::UnregisterAssetTypeActions()
|
void FProjectFishEditorModule::UnregisterAssetTypeActions()
|
||||||
@ -50,6 +56,66 @@ void FProjectFishEditorModule::UnregisterAssetTypeActions()
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
CreatedAssetTypeActions.Empty();
|
CreatedAssetTypeActions.Empty();
|
||||||
|
|
||||||
|
UnregisterThumbnailRenderers();
|
||||||
|
}
|
||||||
|
|
||||||
|
void FProjectFishEditorModule::RegisterThumbnailRenderers()
|
||||||
|
{
|
||||||
|
UThumbnailManager::Get().RegisterCustomRenderer(UBagShapeAsset::StaticClass(), UBagShapeAssetThumbnailRenderer::StaticClass());
|
||||||
|
RefreshExistingAssetThumbnails();
|
||||||
|
}
|
||||||
|
|
||||||
|
void FProjectFishEditorModule::UnregisterThumbnailRenderers()
|
||||||
|
{
|
||||||
|
if (UObjectInitialized())
|
||||||
|
{
|
||||||
|
UThumbnailManager::Get().UnregisterCustomRenderer(UBagShapeAsset::StaticClass());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void FProjectFishEditorModule::RefreshExistingAssetThumbnails()
|
||||||
|
{
|
||||||
|
// 延迟执行以确保所有模块都已加载
|
||||||
|
FTSTicker::GetCoreTicker().AddTicker(FTickerDelegate::CreateLambda([](float DeltaTime) -> bool
|
||||||
|
{
|
||||||
|
if (FModuleManager::Get().IsModuleLoaded("AssetRegistry"))
|
||||||
|
{
|
||||||
|
FAssetRegistryModule& AssetRegistryModule = FModuleManager::GetModuleChecked<FAssetRegistryModule>("AssetRegistry");
|
||||||
|
IAssetRegistry& AssetRegistry = AssetRegistryModule.Get();
|
||||||
|
|
||||||
|
// 查找所有BagShapeAsset资产
|
||||||
|
TArray<FAssetData> BagShapeAssetList;
|
||||||
|
AssetRegistry.GetAssetsByClass(UBagShapeAsset::StaticClass()->GetClassPathName(), BagShapeAssetList);
|
||||||
|
|
||||||
|
// 查找所有BagClass资产
|
||||||
|
TArray<FAssetData> BagClassAssetList;
|
||||||
|
//AssetRegistry.GetAssetsByClass(UBagClass::StaticClass()->GetClassPathName(), BagClassAssetList);
|
||||||
|
|
||||||
|
// 合并资产列表
|
||||||
|
TArray<FAssetData> AllAssets;
|
||||||
|
AllAssets.Append(BagShapeAssetList);
|
||||||
|
AllAssets.Append(BagClassAssetList);
|
||||||
|
|
||||||
|
// 刷新每个资产的缩略图
|
||||||
|
UThumbnailManager& ThumbnailManager = UThumbnailManager::Get();
|
||||||
|
for (const FAssetData& AssetData : AllAssets)
|
||||||
|
{
|
||||||
|
if (UObject* Asset = AssetData.GetAsset())
|
||||||
|
{
|
||||||
|
// 通知系统资源已修改
|
||||||
|
if (GEditor)
|
||||||
|
{
|
||||||
|
GEditor->GetEditorSubsystem<UImportSubsystem>()->BroadcastAssetPostImport(nullptr, Asset);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 只执行一次,返回false停止ticker
|
||||||
|
return false;
|
||||||
|
}), 1.0f); // 1秒延迟
|
||||||
}
|
}
|
||||||
|
|
||||||
#undef LOCTEXT_NAMESPACE
|
#undef LOCTEXT_NAMESPACE
|
||||||
|
|||||||
@ -0,0 +1,117 @@
|
|||||||
|
// Fill out your copyright notice in the Description page of Project Settings.
|
||||||
|
|
||||||
|
|
||||||
|
#include "Thimbnail/BagShapeAssetThumbnailRenderer.h"
|
||||||
|
|
||||||
|
#include "CanvasItem.h"
|
||||||
|
#include "CanvasTypes.h"
|
||||||
|
#include "ProjectFish/DataAsset/BagShapeAsset.h"
|
||||||
|
|
||||||
|
bool UBagShapeAssetThumbnailRenderer::CanVisualizeAsset(UObject* Object)
|
||||||
|
{
|
||||||
|
return Cast<UBagShapeAsset>(Object) != nullptr;
|
||||||
|
}
|
||||||
|
|
||||||
|
void UBagShapeAssetThumbnailRenderer::Draw(UObject* Object, int32 X, int32 Y, uint32 Width, uint32 Height,
|
||||||
|
FRenderTarget* RenderTarget, FCanvas* Canvas, bool bAdditionalViewFamily)
|
||||||
|
{
|
||||||
|
UBagShapeAsset* BagShapeAsset = Cast<UBagShapeAsset>(Object);
|
||||||
|
if (!BagShapeAsset || !Canvas)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
DrawBagShape(BagShapeAsset, Canvas, X, Y, Width, Height);
|
||||||
|
}
|
||||||
|
|
||||||
|
void UBagShapeAssetThumbnailRenderer::DrawBagShape(UBagShapeAsset* BagShapeAsset, FCanvas* Canvas, int32 X, int32 Y,
|
||||||
|
uint32 Width, uint32 Height)
|
||||||
|
{
|
||||||
|
if (!BagShapeAsset || !Canvas)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Clear the background
|
||||||
|
FCanvasBoxItem BackgroundBox(FVector2D(X, Y), FVector2D(Width, Height));
|
||||||
|
BackgroundBox.SetColor(FLinearColor(0.1f, 0.1f, 0.1f, 1.0f));
|
||||||
|
Canvas->DrawItem(BackgroundBox);
|
||||||
|
|
||||||
|
// Calculate the best fit scale with spacing for separators
|
||||||
|
float SeparatorWidth = 2.0f; // White separator width
|
||||||
|
float Scale = GetBestFitScale(BagShapeAsset->BagWidth, BagShapeAsset->BagHeight, Width, Height);
|
||||||
|
float CellSize = Scale - SeparatorWidth; // Reduce cell size to make room for separators
|
||||||
|
|
||||||
|
// Calculate starting position to center the grid (including separators)
|
||||||
|
float GridWidth = BagShapeAsset->BagWidth * Scale - SeparatorWidth;
|
||||||
|
float GridHeight = BagShapeAsset->BagHeight * Scale - SeparatorWidth;
|
||||||
|
float StartX = X + (Width - GridWidth) * 0.5f;
|
||||||
|
float StartY = Y + (Height - GridHeight) * 0.5f;
|
||||||
|
|
||||||
|
// Draw the bag slots (background) with separators
|
||||||
|
for (int32 GridY = 0; GridY < BagShapeAsset->BagHeight; GridY++)
|
||||||
|
{
|
||||||
|
for (int32 GridX = 0; GridX < BagShapeAsset->BagWidth; GridX++)
|
||||||
|
{
|
||||||
|
float CellStartX = StartX + GridX * Scale;
|
||||||
|
float CellStartY = StartY + GridY * Scale;
|
||||||
|
|
||||||
|
bool bIsActive = BagShapeAsset->IsSlotActive(GridX, GridY);
|
||||||
|
|
||||||
|
// Draw filled cell (smaller to leave space for separators)
|
||||||
|
FCanvasTileItem CellTile(FVector2D(CellStartX, CellStartY), FVector2D(CellSize, CellSize),
|
||||||
|
bIsActive ? FLinearColor(0.2f, 0.8f, 0.2f, 1.0f) : FLinearColor(0.15f, 0.15f, 0.15f, 1.0f));
|
||||||
|
CellTile.BlendMode = SE_BLEND_Opaque;
|
||||||
|
Canvas->DrawItem(CellTile);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// // Draw white separators
|
||||||
|
// // Vertical separators
|
||||||
|
// for (int32 GridX = 1; GridX < BagShapeAsset->BagWidth; GridX++)
|
||||||
|
// {
|
||||||
|
// float SeparatorX = StartX + GridX * Scale - SeparatorWidth;
|
||||||
|
// FCanvasTileItem VerticalSeparator(FVector2D(SeparatorX, StartY), FVector2D(SeparatorWidth, GridHeight), FLinearColor::White);
|
||||||
|
// VerticalSeparator.BlendMode = SE_BLEND_Opaque;
|
||||||
|
// Canvas->DrawItem(VerticalSeparator);
|
||||||
|
// }
|
||||||
|
|
||||||
|
// // Horizontal separators
|
||||||
|
// for (int32 GridY = 1; GridY < BagShapeAsset->BagHeight; GridY++)
|
||||||
|
// {
|
||||||
|
// float SeparatorY = StartY + GridY * Scale - SeparatorWidth;
|
||||||
|
// FCanvasTileItem HorizontalSeparator(FVector2D(StartX, SeparatorY), FVector2D(GridWidth, SeparatorWidth), FLinearColor::White);
|
||||||
|
// HorizontalSeparator.BlendMode = SE_BLEND_Opaque;
|
||||||
|
// Canvas->DrawItem(HorizontalSeparator);
|
||||||
|
// }
|
||||||
|
|
||||||
|
// // Draw outer border
|
||||||
|
// FCanvasBoxItem OuterBorder(FVector2D(StartX, StartY), FVector2D(GridWidth, GridHeight));
|
||||||
|
// OuterBorder.SetColor(FLinearColor::White);
|
||||||
|
// OuterBorder.LineThickness = 5.0f;
|
||||||
|
// Canvas->DrawItem(OuterBorder);
|
||||||
|
}
|
||||||
|
|
||||||
|
float UBagShapeAssetThumbnailRenderer::GetBestFitScale(int32 BagWidth, int32 BagHeight, uint32 CanvasWidth,
|
||||||
|
uint32 CanvasHeight) const
|
||||||
|
{
|
||||||
|
if (BagWidth == 0 || BagHeight == 0)
|
||||||
|
{
|
||||||
|
return 1.0f;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Leave some padding
|
||||||
|
float PaddedWidth = CanvasWidth * 0.9f;
|
||||||
|
float PaddedHeight = CanvasHeight * 0.9f;
|
||||||
|
|
||||||
|
float ScaleX = PaddedWidth / BagWidth;
|
||||||
|
float ScaleY = PaddedHeight / BagHeight;
|
||||||
|
|
||||||
|
// Use the smaller scale to ensure everything fits
|
||||||
|
float Scale = FMath::Min(ScaleX, ScaleY);
|
||||||
|
|
||||||
|
// Ensure minimum cell size for visibility
|
||||||
|
Scale = FMath::Max(Scale, 4.0f);
|
||||||
|
|
||||||
|
return Scale;
|
||||||
|
}
|
||||||
@ -236,4 +236,21 @@ void SBagShapeEditorWidget::OnSlotClicked(int32 X, int32 Y)
|
|||||||
|
|
||||||
void SBagShapeEditorWidget::RefreshThumbnail()
|
void SBagShapeEditorWidget::RefreshThumbnail()
|
||||||
{
|
{
|
||||||
|
if (!BagShapeAsset.IsValid())
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 强制资源重新生成缩略图的最可靠方法
|
||||||
|
//BagShapeAsset->MarkPackageDirty();
|
||||||
|
|
||||||
|
// // 触发完整的PostEditChange流程
|
||||||
|
// FPropertyChangedEvent PropertyChangedEvent(nullptr);
|
||||||
|
// BagShapeAsset->PostEditChangeProperty(PropertyChangedEvent);
|
||||||
|
//
|
||||||
|
// // 通知系统资源已修改
|
||||||
|
if (GEditor)
|
||||||
|
{
|
||||||
|
GEditor->GetEditorSubsystem<UImportSubsystem>()->BroadcastAssetPostImport(nullptr, BagShapeAsset.Get());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -11,10 +11,15 @@ public:
|
|||||||
virtual void ShutdownModule() override;
|
virtual void ShutdownModule() override;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
//Bagshap 编辑器相关
|
//资源 编辑器相关
|
||||||
void RegisterAssetTypeActions();
|
void RegisterAssetTypeActions();
|
||||||
void UnregisterAssetTypeActions();
|
void UnregisterAssetTypeActions();
|
||||||
|
|
||||||
|
//资源图标
|
||||||
|
void RegisterThumbnailRenderers();
|
||||||
|
void UnregisterThumbnailRenderers();
|
||||||
|
|
||||||
|
void RefreshExistingAssetThumbnails();
|
||||||
/** Asset type actions */
|
/** Asset type actions */
|
||||||
TArray<TSharedPtr<FAssetTypeActions_Base>> CreatedAssetTypeActions;
|
TArray<TSharedPtr<FAssetTypeActions_Base>> CreatedAssetTypeActions;
|
||||||
};
|
};
|
||||||
|
|||||||
@ -0,0 +1,26 @@
|
|||||||
|
// Fill out your copyright notice in the Description page of Project Settings.
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include "CoreMinimal.h"
|
||||||
|
#include "ThumbnailRendering/DefaultSizedThumbnailRenderer.h"
|
||||||
|
#include "BagShapeAssetThumbnailRenderer.generated.h"
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
UCLASS()
|
||||||
|
class PROJECTFISHEDITOR_API UBagShapeAssetThumbnailRenderer : public UDefaultSizedThumbnailRenderer
|
||||||
|
{
|
||||||
|
GENERATED_BODY()
|
||||||
|
public:
|
||||||
|
virtual bool CanVisualizeAsset(UObject* Object) override;
|
||||||
|
virtual void Draw(UObject* Object, int32 X, int32 Y, uint32 Width, uint32 Height, FRenderTarget* RenderTarget, FCanvas* Canvas, bool bAdditionalViewFamily) override;
|
||||||
|
|
||||||
|
private:
|
||||||
|
/** Draw the bag shape on canvas */
|
||||||
|
void DrawBagShape(class UBagShapeAsset* BagShapeAsset, FCanvas* Canvas, int32 X, int32 Y, uint32 Width, uint32 Height);
|
||||||
|
|
||||||
|
/** Get the best fit scale for the bag shape */
|
||||||
|
float GetBestFitScale(int32 BagWidth, int32 BagHeight, uint32 CanvasWidth, uint32 CanvasHeight) const;
|
||||||
|
};
|
||||||
Loading…
x
Reference in New Issue
Block a user