118 lines
4.3 KiB
C++
Raw Permalink Normal View History

2025-08-30 19:55:46 +08:00
// Fill out your copyright notice in the Description page of Project Settings.
2025-10-15 18:25:31 +08:00
#include "ProjectFishEditor/Public/Thumbnail/ShapeAssetThumbnailRenderer.h"
2025-08-30 19:55:46 +08:00
#include "CanvasItem.h"
#include "CanvasTypes.h"
2025-10-15 18:25:31 +08:00
#include "ProjectFish/DataAsset/ShapeAsset.h"
2025-08-30 19:55:46 +08:00
2025-10-15 18:25:31 +08:00
bool UShapeAssetThumbnailRenderer::CanVisualizeAsset(UObject* Object)
2025-08-30 19:55:46 +08:00
{
2025-10-15 18:25:31 +08:00
return Cast<UShapeAsset>(Object) != nullptr;
2025-08-30 19:55:46 +08:00
}
2025-10-15 18:25:31 +08:00
void UShapeAssetThumbnailRenderer::Draw(UObject* Object, int32 X, int32 Y, uint32 Width, uint32 Height,
2025-08-30 19:55:46 +08:00
FRenderTarget* RenderTarget, FCanvas* Canvas, bool bAdditionalViewFamily)
{
2025-10-15 18:25:31 +08:00
UShapeAsset* BagShapeAsset = Cast<UShapeAsset>(Object);
2025-08-30 19:55:46 +08:00
if (!BagShapeAsset || !Canvas)
{
return;
}
DrawBagShape(BagShapeAsset, Canvas, X, Y, Width, Height);
}
2025-10-15 18:25:31 +08:00
void UShapeAssetThumbnailRenderer::DrawBagShape(UShapeAsset* BagShapeAsset, FCanvas* Canvas, int32 X, int32 Y,
2025-08-30 19:55:46 +08:00
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
2025-10-15 18:25:31 +08:00
float Scale = GetBestFitScale(BagShapeAsset->ShapeWidth, BagShapeAsset->ShapeHeight, Width, Height);
2025-08-30 19:55:46 +08:00
float CellSize = Scale - SeparatorWidth; // Reduce cell size to make room for separators
// Calculate starting position to center the grid (including separators)
2025-10-15 18:25:31 +08:00
float GridWidth = BagShapeAsset->ShapeWidth * Scale - SeparatorWidth;
float GridHeight = BagShapeAsset->ShapeHeight * Scale - SeparatorWidth;
2025-08-30 19:55:46 +08:00
float StartX = X + (Width - GridWidth) * 0.5f;
float StartY = Y + (Height - GridHeight) * 0.5f;
// Draw the bag slots (background) with separators
2025-10-15 18:25:31 +08:00
for (int32 GridY = 0; GridY < BagShapeAsset->ShapeHeight; GridY++)
2025-08-30 19:55:46 +08:00
{
2025-10-15 18:25:31 +08:00
for (int32 GridX = 0; GridX < BagShapeAsset->ShapeWidth; GridX++)
2025-08-30 19:55:46 +08:00
{
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);
}
2025-10-15 18:25:31 +08:00
float UShapeAssetThumbnailRenderer::GetBestFitScale(int32 BagWidth, int32 BagHeight, uint32 CanvasWidth,
2025-08-30 19:55:46 +08:00
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;
}