187 lines
5.3 KiB
C++
187 lines
5.3 KiB
C++
//
|
|
|
|
|
|
#include "UI/ItemStacks/ItemStack_Base.h"
|
|
|
|
#include "GIS_ItemDefinition.h"
|
|
#include "GIS_ItemInstance.h"
|
|
#include "Components/ListView.h"
|
|
#include "Components/MenuAnchor.h"
|
|
#include "Components/OverlaySlot.h"
|
|
#include "UI/Actions/GUIS_UIActionWidget.h"
|
|
#include "UI/ItemStacks/ItemData.h"
|
|
#include "UI/ItemStacks/ItemDataDragDropOperation.h"
|
|
#include "UI/ItemStacks/ItemDataDraggingWidget.h"
|
|
#include "UI/ItemStacks/ItemStackContainer.h"
|
|
#include "UI/Widgets/Normal/AmountContainerBase.h"
|
|
|
|
void UItemStack_Base::UpdateAmount()
|
|
{
|
|
if (!AmountContainer) return;
|
|
if (ItemData && ItemData->IsValidItem())
|
|
{
|
|
AmountContainer->UpdateAmount(ItemData->GetItemInfo().Amount);
|
|
AmountContainer->SetVisibility(ESlateVisibility::SelfHitTestInvisible);
|
|
return;
|
|
}
|
|
AmountContainer->SetVisibility(ESlateVisibility::Collapsed);
|
|
}
|
|
|
|
void UItemStack_Base::ResetState()
|
|
{
|
|
if (DynamicUIActionWidget) {
|
|
DynamicUIActionWidget->UnregisterActions();
|
|
}
|
|
if (ActionsAnchor) {
|
|
ActionsAnchor->Close();
|
|
}
|
|
}
|
|
|
|
void UItemStack_Base::NativeOnListItemObjectSet(UObject* ListItemObject)
|
|
{
|
|
Super::NativeOnListItemObjectSet(ListItemObject);
|
|
ItemData = Cast<UItemData>(ListItemObject);
|
|
UpdateAmount();
|
|
ResetState();
|
|
//将UI数据与UI操作空间关联
|
|
if (DynamicUIActionWidget) {
|
|
DynamicUIActionWidget->SetAssociatedData(ItemData);
|
|
}
|
|
}
|
|
|
|
|
|
|
|
void UItemStack_Base::NativeOnEntryReleased()
|
|
{
|
|
Super::NativeOnEntryReleased();
|
|
ResetState();
|
|
if (DynamicUIActionWidget) {
|
|
DynamicUIActionWidget->SetAssociatedData(nullptr);
|
|
}
|
|
}
|
|
|
|
void UItemStack_Base::NativePreConstruct()
|
|
{
|
|
Super::NativePreConstruct();
|
|
if (ActionsAnchor)
|
|
{
|
|
if (UOverlaySlot* OverlaySlot = Cast<UOverlaySlot>(ActionsAnchor->Slot))
|
|
{
|
|
OverlaySlot->SetHorizontalAlignment(ActionAnchorHorizontalAlignment);
|
|
OverlaySlot->SetVerticalAlignment(ActionAnchorVerticalAlignment);
|
|
}
|
|
}
|
|
}
|
|
|
|
void UItemStack_Base::NativeOnDeselected(bool bBroadcast)
|
|
{
|
|
Super::NativeOnDeselected(bBroadcast);
|
|
if (ItemData && ItemData->GetContainer())
|
|
{
|
|
if (ItemData->GetContainer()->IsItemActionAllowed())
|
|
{
|
|
if (DynamicUIActionWidget)
|
|
{
|
|
DynamicUIActionWidget->UnregisterActions();
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
void UItemStack_Base::NativeOnSelected(bool bBroadcast)
|
|
{
|
|
Super::NativeOnSelected(bBroadcast);
|
|
if (ItemData && ItemData->GetContainer())
|
|
{
|
|
if (ItemData->GetContainer()->IsItemActionAllowed())
|
|
{
|
|
if (DynamicUIActionWidget)
|
|
{
|
|
DynamicUIActionWidget->RegisterActionsWithFactory(UIActionFactory);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
UItemDataDragDropOperation* UItemStack_Base::DragFromSource(const FString& Tag)
|
|
{
|
|
if (!DraggingWidgetClass || !ItemData || !ItemData->IsValidItem()) return nullptr;
|
|
|
|
// 创建拖拽视觉widget
|
|
UItemDataDraggingWidget* DraggingWidget = CreateWidget<UItemDataDraggingWidget>(this, DraggingWidgetClass);
|
|
if (!DraggingWidget) return nullptr;
|
|
if (const UGIS_ItemDefinition* ItemDef = ItemData->GetItemInfo().Item.Get()->GetDefinition(); ItemDef && ItemDef->Icon)
|
|
{
|
|
DraggingWidget->SetIcon(ItemDef->Icon);
|
|
}
|
|
DraggingWidget->SetAmount(ItemData->GetItemInfo().Amount);
|
|
// 创建拖拽操作
|
|
UItemDataDragDropOperation* DragDropOp = NewObject<UItemDataDragDropOperation>(this);
|
|
DragDropOp->DefaultDragVisual = DraggingWidget;
|
|
DragDropOp->Pivot = EDragPivot::CenterCenter;
|
|
DragDropOp->Tag = Tag;
|
|
// 设置源数据
|
|
DragDropOp->SetSourceItemData(ItemData);
|
|
DragDropOp->SetSourceItemStackView(ItemData->GetContainer());
|
|
DragDropOp->SetSourceItemIndex(ItemData->GetItemSlotIndex());
|
|
return DragDropOp;
|
|
}
|
|
|
|
void UItemStack_Base::DropToDest(UItemDataDragDropOperation& DragOperation) const
|
|
{
|
|
if (!ItemData || !ItemData->IsValidItem()) return
|
|
DragOperation.SetTargetItemData(ItemData);
|
|
DragOperation.SetTargetItemStackView(ItemData->GetContainer());
|
|
DragOperation.SetTargetItemIndex(ItemData->GetItemSlotIndex());
|
|
}
|
|
|
|
void UItemStack_Base::NativeOnDragDetected(const FGeometry& InGeometry, const FPointerEvent& InMouseEvent,
|
|
UDragDropOperation*& OutOperation)
|
|
{
|
|
Super::NativeOnDragDetected(InGeometry, InMouseEvent, OutOperation);
|
|
if (!ItemData || !ItemData->GetContainer()) return;
|
|
if (ItemData->GetContainer()->IsItemActionAllowed() && ItemData->IsValidItem())
|
|
{
|
|
ActionsAnchor->Close();
|
|
if (UItemDataDragDropOperation* DragOp = DragFromSource(TEXT("MoveIndex")))
|
|
{
|
|
OutOperation = DragOp;
|
|
}
|
|
}
|
|
}
|
|
|
|
bool UItemStack_Base::NativeOnDrop(const FGeometry& InGeometry, const FDragDropEvent& InDragDropEvent,
|
|
UDragDropOperation* InOperation)
|
|
{
|
|
if (ItemData && ItemData->GetContainer() && ItemData->GetContainer()->IsItemActionAllowed())
|
|
{
|
|
if (UItemDataDragDropOperation* DragOp = Cast<UItemDataDragDropOperation>(InOperation))
|
|
{
|
|
DropToDest(*DragOp);
|
|
if (DragOp->Tag == TEXT("MoveIndex"))
|
|
{
|
|
UItemStackContainer* SourceContainer = DragOp->GetSourceItemStackView();
|
|
UItemStackContainer* TargetContainer = DragOp->GetTargetItemStackView();
|
|
const int32 SourceIndex = DragOp->GwtSourceItemIndex();
|
|
const int32 TargetIndex = DragOp->GetTargetItemIndex();
|
|
if (SourceContainer && TargetContainer)
|
|
{
|
|
if (SourceContainer == TargetContainer)
|
|
{
|
|
// 交换数据槽位
|
|
SourceContainer->SwapItemDataSlots(SourceIndex, TargetIndex);
|
|
return true;
|
|
}
|
|
|
|
{
|
|
// 跨容器交换数据槽位
|
|
SourceContainer->SwapItemDataToOtherContainer(SourceIndex, TargetContainer, TargetIndex);
|
|
return true;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
return Super::NativeOnDrop(InGeometry, InDragDropEvent, InOperation);
|
|
}
|