ue4 自定义CanvasPanel 可以接收鼠标事件

自定义 SConstraintCanvas 和 UCanvasPanel  具体看下面代码

.h
// Fill out your copyright notice in the Description page of Project Settings.

#pragma once

#include "CoreMinimal.h"
#include "Widgets/Layout/SConstraintCanvas.h"


/**
 * 
 */
class BJ_SCENE_API SScrollCanvas: public SConstraintCanvas
{
public:
	/** 必须设置Canvas可以被点击 */
	SLATE_BEGIN_ARGS(SScrollCanvas)
	{
		_Visibility = EVisibility::Visible;
	}

	SLATE_SUPPORTS_SLOT(SScrollCanvas::FSlot)
		
		SLATE_END_ARGS()

    //必须添加 否则会报错
		void Construct(const FArguments& InArgs);

};



.cpp

#include "SScrollCanvas.h"

void SScrollCanvas::Construct(const FArguments& InArgs)
{
	const int32 NumSlots = InArgs.Slots.Num();
	for (int32 SlotIndex = 0; SlotIndex < NumSlots; ++SlotIndex)
	{
		Children.Add(InArgs.Slots[SlotIndex]);
	}
}
.h

// Fill out your copyright notice in the Description page of Project Settings.

#pragma once

#include "CoreMinimal.h"
#include "Components/CanvasPanel.h"
#include "ScrollCanvasPanel.generated.h"

DECLARE_DYNAMIC_MULTICAST_DELEGATE_OneParam(FPanelScrollDelegate, float, Offset);
/**
 * 
 */
UCLASS()
class BJ_SCENE_API UScrollCanvasPanel : public UCanvasPanel
{
	GENERATED_BODY()
//设置默认的Visible状态
		UScrollCanvasPanel();
protected:
//必须添加
	virtual TSharedRef<SWidget> RebuildWidget() override;

	virtual FReply HandleScrollBegin(const FGeometry& Geometry, const FPointerEvent& MouseEvent);
	virtual FReply HandleScrollMoving(const FGeometry& Geometry, const FPointerEvent& MouseEvent);
	virtual FReply HandleScrollEnd(const FGeometry& Geometry, const FPointerEvent& MouseEvent);

	UPROPERTY(BlueprintAssignable, Category = ScrollCanvasPanel)
		FPanelScrollDelegate ScrollDeleaget;
};


.cpp
//// Fill out your copyright notice in the Description page of Project Settings.
#include "ScrollCanvasPanel.h"
#include "SScrollCanvas.h"
#include "Components/CanvasPanelSlot.h"

UScrollCanvasPanel::UScrollCanvasPanel()
{
	bIsVariable = false;
	Visibility = ESlateVisibility::Visible;
}

TSharedRef<SWidget> UScrollCanvasPanel::RebuildWidget()
{
	MyCanvas = SNew(SScrollCanvas);
	for (UPanelSlot* PanelSlot : Slots)
	{
		if (UCanvasPanelSlot* TypedSlot = Cast<UCanvasPanelSlot>(PanelSlot))
		{
			TypedSlot->Parent = this;
			TypedSlot->BuildSlot(MyCanvas.ToSharedRef());
		}
	}
	//绑定鼠标事件
	MyCanvas->SetOnMouseButtonDown(BIND_UOBJECT_DELEGATE(FPointerEventHandler, HandleScrollBegin));
	MyCanvas->SetOnMouseMove(BIND_UOBJECT_DELEGATE(FPointerEventHandler, HandleScrollMoving));
	MyCanvas->SetOnMouseButtonUp(BIND_UOBJECT_DELEGATE(FPointerEventHandler, HandleScrollEnd));
	//绑定Touch事件

	return MyCanvas.ToSharedRef();
}

FReply UScrollCanvasPanel::HandleScrollBegin(const FGeometry& Geometry, const FPointerEvent& MouseEvent)
{
	return FReply::Handled();
}

FReply UScrollCanvasPanel::HandleScrollMoving(const FGeometry& Geometry, const FPointerEvent& MouseEvent)
{
	const float ScrollByAmount = MouseEvent.GetCursorDelta().Y / Geometry.Scale;
	ScrollDeleaget.Broadcast(ScrollByAmount);
	UE_LOG(LogTemp, Warning, TEXT("distance = %f"), MouseEvent.GetCursorDelta().Y);
	return FReply::Unhandled();
}

FReply UScrollCanvasPanel::HandleScrollEnd(const FGeometry& Geometry, const FPointerEvent& MouseEvent)
{
	return FReply::Unhandled();
}
发布了144 篇原创文章 · 获赞 15 · 访问量 9万+

猜你喜欢

转载自blog.csdn.net/maxiaosheng521/article/details/103743854
今日推荐