117 lines
3.8 KiB
C++
117 lines
3.8 KiB
C++
// Fill out your copyright notice in the Description page of Project Settings.
|
|
|
|
|
|
#include "DragableUserWidget.h"
|
|
|
|
#include "Blueprint/WidgetLayoutLibrary.h"
|
|
#include "Components/CanvasPanelSlot.h"
|
|
#include "ProjectFish/Gameplay/Subsystem/UIManagerSubsystem.h"
|
|
|
|
void UDragableUserWidget::SetWidgetDragable(bool Dragable)
|
|
{
|
|
bDragable = Dragable;
|
|
}
|
|
|
|
void UDragableUserWidget::BeginDrag(const FGeometry& InGeometry)
|
|
{
|
|
bDraging = true;
|
|
//OriginWidgetScreenPos = InGeometry.GetAbsolutePosition();
|
|
UUIManagerSubsystem* UIManager = GetGameInstance()->GetSubsystem<UUIManagerSubsystem>();
|
|
UIManager->AddDragWidget(this);
|
|
|
|
}
|
|
|
|
void UDragableUserWidget::EndDrag()
|
|
{
|
|
//取消拖拽 不处理点击事件
|
|
bDraging = false;
|
|
UUIManagerSubsystem* UIManager = GetGameInstance()->GetSubsystem<UUIManagerSubsystem>();
|
|
UIManager->RemoveDragWidget();
|
|
}
|
|
|
|
// FReply UDragableUserWidget::NativeOnMouseButtonDown(const FGeometry& InGeometry, const FPointerEvent& InMouseEvent)
|
|
// {
|
|
// if (bDragable && InMouseEvent.GetEffectingButton() == EKeys::LeftMouseButton)
|
|
// {
|
|
// if (!bDraging)
|
|
// {
|
|
// BeginDrag(InGeometry);
|
|
// return FReply::Unhandled();
|
|
// }
|
|
// }
|
|
// else
|
|
// {
|
|
// return Super::NativeOnMouseButtonDown(InGeometry, InMouseEvent);
|
|
// }
|
|
// return FReply::Unhandled();
|
|
// }
|
|
//
|
|
// FReply UDragableUserWidget::NativeOnMouseButtonUp(const FGeometry& InGeometry, const FPointerEvent& InMouseEvent)
|
|
// {
|
|
// return Super::NativeOnMouseButtonUp(InGeometry, InMouseEvent);
|
|
// }
|
|
//
|
|
// FReply UDragableUserWidget::NativeOnMouseMove(const FGeometry& InGeometry, const FPointerEvent& InMouseEvent)
|
|
// {
|
|
// if (bDraging)
|
|
// {
|
|
// Super::NativeOnMouseMove(InGeometry, InMouseEvent);
|
|
//
|
|
// // 获取鼠标在屏幕空间的绝对坐标
|
|
// FVector2D MouseScreenPos = InMouseEvent.GetScreenSpacePosition();
|
|
//
|
|
// // 获取Widget在屏幕上的实际渲染尺寸
|
|
//
|
|
// FVector2D WidgetAbsoluteSize = InGeometry.GetDrawSize();
|
|
//
|
|
// // 计算Widget中心点应对应的屏幕位置
|
|
// FVector2D DesiredCenter = MouseScreenPos;
|
|
//
|
|
// // 计算新的左上角位置(让中心点对准鼠标)
|
|
// FVector2D NewTopLeft =FVector2D(DesiredCenter.X - WidgetAbsoluteSize.X * 0.5f, DesiredCenter.Y - WidgetAbsoluteSize.Y* 0.5f ); ;
|
|
//
|
|
// // 边界检查
|
|
// FVector2D ViewportSize;
|
|
// if (GEngine && GEngine->GameViewport)
|
|
// {
|
|
// GEngine->GameViewport->GetViewportSize(ViewportSize);
|
|
// }
|
|
// else
|
|
// {
|
|
// ViewportSize = FVector2D(1920, 1080);
|
|
// }
|
|
//
|
|
// // 确保Widget不会完全移出屏幕
|
|
// NewTopLeft.X = FMath::Clamp(NewTopLeft.X, 0.0f, ViewportSize.X - WidgetAbsoluteSize.X);
|
|
// NewTopLeft.Y = FMath::Clamp(NewTopLeft.Y, 0.0f, ViewportSize.Y - WidgetAbsoluteSize.Y);
|
|
//
|
|
// // // 计算相对于原始位置的偏移量
|
|
// // FVector2D ParentAbsolutePosition = FVector2D::ZeroVector;
|
|
// // if (InGeometry.GetParentGeometry().IsValid())
|
|
// // {
|
|
// // ParentAbsolutePosition = InGeometry.GetParentGeometry().GetAbsolutePosition();
|
|
// // }
|
|
// //
|
|
// FVector2D LocalOffset = NewTopLeft - OriginWidgetScreenPos;
|
|
//
|
|
// // 应用变换
|
|
// FWidgetTransform WidgetTransform = GetRenderTransform();
|
|
// WidgetTransform.Translation = LocalOffset/InGeometry.Scale;
|
|
// SetRenderTransform(WidgetTransform);
|
|
//
|
|
// // FString Scale = FString::Printf(TEXT("%.0f"), InGeometry.Scale);
|
|
// // UE_LOG(LogTemp, Warning, TEXT("Mouse: %s, CurrentWidgetScreenPos: %s, WidgetAbsoluteSize: %s NewTopLeft : %s LocalOffset : %s Scale = %s"),
|
|
// // *MouseScreenPos.ToString(),
|
|
// // *OriginWidgetScreenPos.ToString(),
|
|
// // *WidgetAbsoluteSize.ToString(),
|
|
// // *NewTopLeft.ToString(),
|
|
// // *LocalOffset.ToString(),
|
|
// // *Scale
|
|
// // );
|
|
//
|
|
// return FReply::Unhandled();
|
|
// }
|
|
// else
|
|
// return Super::NativeOnMouseMove(InGeometry, InMouseEvent);
|
|
// }
|