您的当前位置:首页正文

UE C++ 判断鼠标是否双击的蓝图函数库

2024-11-22 来源:个人技术集锦

新的方法 

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

#pragma once

#include "CoreMinimal.h"
#include "Kismet/BlueprintFunctionLibrary.h"
#include "MyFunction.generated.h"

/**
 * 
 */
UCLASS()
class BUTTONCTEST_API UMyFunction : public UBlueprintFunctionLibrary
{
	GENERATED_BODY()

public:
	UFUNCTION(BlueprintCallable, Category = "Mouse Events")
	static bool IsMouseButtonDoubleClicked(float DoubleClickInterval);
};

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


#include "MyFunction.h"

float LastClickTime = 0.0f;
bool bIsLastClickDouble = false;

bool UMyFunction::IsMouseButtonDoubleClicked(float DoubleClickInterval)
{
	//FPlatformTime::Cycles()返回当前的 CPU 时钟周期数	
	double CurrentTime = FPlatformTime::ToSeconds(FPlatformTime::Cycles());
	double ClickInterval = CurrentTime - LastClickTime;

	if (ClickInterval < DoubleClickInterval && !bIsLastClickDouble) // 双击间隔为 0.2 秒
	{
		LastClickTime = 0.0;
		bIsLastClickDouble = true;
		return true;
	}
	else
	{
		LastClickTime = CurrentTime;
		bIsLastClickDouble = false;
		return false;
	}
}

---------------------------------------------------------------------------------------------------------------------------------

创建C++蓝图函数库


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

#pragma once

#include "CoreMinimal.h"
#include "Kismet/BlueprintFunctionLibrary.h"
#include "MouseDoubleClick.generated.h"

/**
 * 
 */
UCLASS()
class WRJCPP_API UMouseDoubleClick : public UBlueprintFunctionLibrary
{
	GENERATED_BODY()

public:
	UFUNCTION(BlueprintCallable)
	static bool IsMouseDoubleClick(float MaxDoubleClickTime);
};
.cpp

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


#include "MouseDoubleClick.h"

bool UMouseDoubleClick::IsMouseDoubleClick(float MaxDoubleClickTime)
{
	UWorld* World = GEngine->GameViewport->GetWorld();
	APlayerController* PlayerController = World->GetFirstPlayerController();
	if (!PlayerController)
	{
		return false;
	}
	float CurrentTime = PlayerController->GetWorld()->GetTimeSeconds();
	static float LastClickedTime = 0.0f;
	if (CurrentTime - LastClickedTime <= MaxDoubleClickTime)
	{
		LastClickedTime = 0.0f;
		return true;
	}
	LastClickedTime = CurrentTime;
	return false;
}

显示全文