257 lines
6.2 KiB
C++
Raw Normal View History

2025-08-30 13:43:09 +08:00
// Fill out your copyright notice in the Description page of Project Settings.
#include "Widgets/BagShapeEditorWidget.h"
2025-08-30 18:03:42 +08:00
#include "Widgets/Input/SSpinBox.h"
#include "EditorStyleSet.h"
2025-08-30 13:43:09 +08:00
2025-10-15 18:25:31 +08:00
void SShapeEditorWidget::Construct(const FArguments& InArgs)
2025-08-30 13:43:09 +08:00
{
BagShapeAsset = InArgs._BagShapeAsset;
2025-08-30 18:03:42 +08:00
ChildSlot
[
SNew(SSplitter)
.Orientation(Orient_Vertical)
+ SSplitter::Slot()
.Value(0.2f)
[
CreateSizeControls()
]
+ SSplitter::Slot()
.Value(0.1f)
[
CreateGridControls()
]
+ SSplitter::Slot()
.Value(0.7f)
[
SNew(SScrollBox)
+ SScrollBox::Slot()
[
SNew(SBorder)
.BorderImage(FEditorStyle::GetBrush("ToolPanel.GroupBorder"))
.Padding(10.0f)
[
SNew(SBox)
.HAlign(HAlign_Center)
.VAlign(VAlign_Center)
[
2025-10-15 18:25:31 +08:00
SAssignNew(GridWidget, SShapeGridWidget)
2025-08-30 18:03:42 +08:00
.BagShapeAsset(BagShapeAsset.Get())
2025-10-15 18:25:31 +08:00
.OnSlotClicked(this, &SShapeEditorWidget::OnSlotClicked)
2025-08-30 18:03:42 +08:00
]
]
]
]
];
}
2025-10-15 18:25:31 +08:00
void SShapeEditorWidget::RefreshGrid()
2025-08-30 18:03:42 +08:00
{
if (GridWidget.IsValid())
{
GridWidget->UpdateBagShapeAsset(BagShapeAsset.Get());
}
}
2025-10-15 18:25:31 +08:00
TSharedRef<SWidget> SShapeEditorWidget::CreateSizeControls()
2025-08-30 18:03:42 +08:00
{
return SNew(SBorder)
.BorderImage(FEditorStyle::GetBrush("ToolPanel.GroupBorder"))
.Padding(10.0f)
[
SNew(SVerticalBox)
+ SVerticalBox::Slot()
.AutoHeight()
.Padding(0, 5)
[
SNew(STextBlock)
.Text(FText::FromString("Bag Size Settings"))
.Font(FCoreStyle::GetDefaultFontStyle("Bold", 14))
]
+ SVerticalBox::Slot()
.AutoHeight()
.Padding(0, 5)
[
SNew(SHorizontalBox)
+ SHorizontalBox::Slot()
.AutoWidth()
.VAlign(VAlign_Center)
.Padding(0, 0, 10, 0)
[
SNew(STextBlock)
.Text(FText::FromString("Width:"))
]
+ SHorizontalBox::Slot()
.AutoWidth()
[
SNew(SBox)
.WidthOverride(100.0f)
[
SNew(SSpinBox<int32>)
.MinValue(1)
.MaxValue(10)
2025-10-15 18:25:31 +08:00
.Value(this, &SShapeEditorWidget::GetShapeWidth)
.OnValueChanged(this, &SShapeEditorWidget::OnWidthChanged)
2025-08-30 18:03:42 +08:00
]
]
+ SHorizontalBox::Slot()
.AutoWidth()
.VAlign(VAlign_Center)
.Padding(20, 0, 10, 0)
[
SNew(STextBlock)
2025-09-01 10:20:49 +08:00
.Text(FText::FromString("Height:"))
2025-08-30 18:03:42 +08:00
]
+ SHorizontalBox::Slot()
.AutoWidth()
[
SNew(SBox)
.WidthOverride(100.0f)
[
SNew(SSpinBox<int32>)
.MinValue(1)
.MaxValue(10)
2025-10-15 18:25:31 +08:00
.Value(this, &SShapeEditorWidget::GetShapeHeight)
.OnValueChanged(this, &SShapeEditorWidget::OnHeightChanged)
2025-08-30 18:03:42 +08:00
]
]
]
];
}
2025-10-15 18:25:31 +08:00
int32 SShapeEditorWidget::GetShapeWidth() const
2025-08-30 18:03:42 +08:00
{
2025-10-15 18:25:31 +08:00
return BagShapeAsset.IsValid() ? BagShapeAsset->ShapeWidth : 5;
2025-08-30 18:03:42 +08:00
}
2025-10-15 18:25:31 +08:00
int32 SShapeEditorWidget::GetShapeHeight() const
2025-08-30 18:03:42 +08:00
{
2025-10-15 18:25:31 +08:00
return BagShapeAsset.IsValid() ? BagShapeAsset->ShapeHeight : 5;
2025-08-30 18:03:42 +08:00
}
2025-10-15 18:25:31 +08:00
void SShapeEditorWidget::OnWidthChanged(int32 NewWidth)
2025-08-30 18:03:42 +08:00
{
2025-10-15 18:25:31 +08:00
if (BagShapeAsset.IsValid() && NewWidth != BagShapeAsset->ShapeWidth)
2025-08-30 18:03:42 +08:00
{
2025-10-15 18:25:31 +08:00
BagShapeAsset->ShapeWidth = NewWidth;
BagShapeAsset->InitializeShape();
2025-08-30 18:03:42 +08:00
BagShapeAsset->MarkPackageDirty();
RefreshGrid();
}
}
2025-10-15 18:25:31 +08:00
void SShapeEditorWidget::OnHeightChanged(int32 NewHeight)
2025-08-30 18:03:42 +08:00
{
2025-10-15 18:25:31 +08:00
if (BagShapeAsset.IsValid() && NewHeight != BagShapeAsset->ShapeHeight)
2025-08-30 18:03:42 +08:00
{
2025-10-15 18:25:31 +08:00
BagShapeAsset->ShapeHeight = NewHeight;
BagShapeAsset->InitializeShape();
2025-08-30 18:03:42 +08:00
BagShapeAsset->MarkPackageDirty();
RefreshGrid();
}
}
2025-10-15 18:25:31 +08:00
TSharedRef<SWidget> SShapeEditorWidget::CreateGridControls()
2025-08-30 18:03:42 +08:00
{
return SNew(SBorder)
.BorderImage(FEditorStyle::GetBrush("ToolPanel.GroupBorder"))
.Padding(10.0f)
[
SNew(SHorizontalBox)
+ SHorizontalBox::Slot()
.AutoWidth()
.Padding(0, 0, 10, 0)
[
SNew(SButton)
.Text(FText::FromString("All Enable"))
2025-10-15 18:25:31 +08:00
.OnClicked(this, &SShapeEditorWidget::OnAllEnableClicked)
2025-08-30 18:03:42 +08:00
]
+ SHorizontalBox::Slot()
.AutoWidth()
.Padding(0, 0, 10, 0)
[
SNew(SButton)
.Text(FText::FromString("All Disable"))
2025-10-15 18:25:31 +08:00
.OnClicked(this, &SShapeEditorWidget::OnAllDisableClicked)
2025-08-30 18:03:42 +08:00
]
];
}
2025-10-15 18:25:31 +08:00
FReply SShapeEditorWidget::OnAllEnableClicked()
2025-08-30 18:03:42 +08:00
{
if (BagShapeAsset.IsValid())
{
2025-10-15 18:25:31 +08:00
BagShapeAsset->InitializeShape();
2025-08-30 18:03:42 +08:00
BagShapeAsset->MarkPackageDirty();
RefreshGrid();
RefreshThumbnail();
}
return FReply::Handled();
}
2025-10-15 18:25:31 +08:00
FReply SShapeEditorWidget::OnAllDisableClicked()
2025-08-30 18:03:42 +08:00
{
if (BagShapeAsset.IsValid())
{
2025-10-15 18:25:31 +08:00
for (int32 X = 0; X < BagShapeAsset->ShapeWidth; X++)
2025-08-30 18:03:42 +08:00
{
2025-10-15 18:25:31 +08:00
for (int32 Y = 0; Y < BagShapeAsset->ShapeHeight; Y++)
2025-08-30 18:03:42 +08:00
{
BagShapeAsset->SetSlotActive(X, Y, false);
}
}
BagShapeAsset->MarkPackageDirty();
RefreshGrid();
RefreshThumbnail();
}
return FReply::Handled();
}
2025-10-15 18:25:31 +08:00
void SShapeEditorWidget::OnSlotClicked(int32 X, int32 Y)
2025-08-30 18:03:42 +08:00
{
if (!BagShapeAsset.IsValid())
{
return;
}
bool bCurrentState = BagShapeAsset->IsSlotActive(X, Y);
BagShapeAsset->SetSlotActive(X, Y, !bCurrentState);
// Refresh the grid widget
if (GridWidget.IsValid())
{
GridWidget->UpdateBagShapeAsset(BagShapeAsset.Get());
}
// Mark the asset as modified
BagShapeAsset->MarkPackageDirty();
// 强制刷新缩略图
RefreshThumbnail();
}
2025-10-15 18:25:31 +08:00
void SShapeEditorWidget::RefreshThumbnail()
2025-08-30 18:03:42 +08:00
{
2025-08-30 19:55:46 +08:00
if (!BagShapeAsset.IsValid())
{
return;
}
// 强制资源重新生成缩略图的最可靠方法
//BagShapeAsset->MarkPackageDirty();
// // 触发完整的PostEditChange流程
// FPropertyChangedEvent PropertyChangedEvent(nullptr);
// BagShapeAsset->PostEditChangeProperty(PropertyChangedEvent);
//
// // 通知系统资源已修改
if (GEditor)
{
GEditor->GetEditorSubsystem<UImportSubsystem>()->BroadcastAssetPostImport(nullptr, BagShapeAsset.Get());
}
2025-08-30 13:43:09 +08:00
}