// Fill out your copyright notice in the Description page of Project Settings. #include "Widgets/BagShapeEditorWidget.h" #include "Widgets/Input/SSpinBox.h" #include "EditorStyleSet.h" void SBagShapeEditorWidget::Construct(const FArguments& InArgs) { BagShapeAsset = InArgs._BagShapeAsset; 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) [ SAssignNew(GridWidget, SBagShapeGridWidget) .BagShapeAsset(BagShapeAsset.Get()) .OnSlotClicked(this, &SBagShapeEditorWidget::OnSlotClicked) ] ] ] ] ]; } void SBagShapeEditorWidget::RefreshGrid() { if (GridWidget.IsValid()) { GridWidget->UpdateBagShapeAsset(BagShapeAsset.Get()); } } TSharedRef SBagShapeEditorWidget::CreateSizeControls() { 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) .MinValue(1) .MaxValue(10) .Value(this, &SBagShapeEditorWidget::GetBagWidth) .OnValueChanged(this, &SBagShapeEditorWidget::OnWidthChanged) ] ] + SHorizontalBox::Slot() .AutoWidth() .VAlign(VAlign_Center) .Padding(20, 0, 10, 0) [ SNew(STextBlock) .Text(FText::FromString("Width:")) ] + SHorizontalBox::Slot() .AutoWidth() [ SNew(SBox) .WidthOverride(100.0f) [ SNew(SSpinBox) .MinValue(1) .MaxValue(10) .Value(this, &SBagShapeEditorWidget::GetBagHeight) .OnValueChanged(this, &SBagShapeEditorWidget::OnHeightChanged) ] ] ] ]; } int32 SBagShapeEditorWidget::GetBagWidth() const { return BagShapeAsset.IsValid() ? BagShapeAsset->BagWidth : 5; } int32 SBagShapeEditorWidget::GetBagHeight() const { return BagShapeAsset.IsValid() ? BagShapeAsset->BagHeight : 5; } void SBagShapeEditorWidget::OnWidthChanged(int32 NewWidth) { if (BagShapeAsset.IsValid() && NewWidth != BagShapeAsset->BagWidth) { BagShapeAsset->BagWidth = NewWidth; BagShapeAsset->InitializeBagShape(); BagShapeAsset->MarkPackageDirty(); RefreshGrid(); } } void SBagShapeEditorWidget::OnHeightChanged(int32 NewHeight) { if (BagShapeAsset.IsValid() && NewHeight != BagShapeAsset->BagHeight) { BagShapeAsset->BagHeight = NewHeight; BagShapeAsset->InitializeBagShape(); BagShapeAsset->MarkPackageDirty(); RefreshGrid(); } } TSharedRef SBagShapeEditorWidget::CreateGridControls() { 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")) .OnClicked(this, &SBagShapeEditorWidget::OnAllEnableClicked) ] + SHorizontalBox::Slot() .AutoWidth() .Padding(0, 0, 10, 0) [ SNew(SButton) .Text(FText::FromString("All Disable")) .OnClicked(this, &SBagShapeEditorWidget::OnAllDisableClicked) ] ]; } FReply SBagShapeEditorWidget::OnAllEnableClicked() { if (BagShapeAsset.IsValid()) { BagShapeAsset->InitializeBagShape(); BagShapeAsset->MarkPackageDirty(); RefreshGrid(); RefreshThumbnail(); } return FReply::Handled(); } FReply SBagShapeEditorWidget::OnAllDisableClicked() { if (BagShapeAsset.IsValid()) { for (int32 X = 0; X < BagShapeAsset->BagWidth; X++) { for (int32 Y = 0; Y < BagShapeAsset->BagHeight; Y++) { BagShapeAsset->SetSlotActive(X, Y, false); } } BagShapeAsset->MarkPackageDirty(); RefreshGrid(); RefreshThumbnail(); } return FReply::Handled(); } void SBagShapeEditorWidget::OnSlotClicked(int32 X, int32 Y) { 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(); } void SBagShapeEditorWidget::RefreshThumbnail() { }