The plugin does not contain errors and does not crash. However, it does not simulate mouse left click holds and releases, even though the callbacks are triggered and it goes through the functions properly. It only simulates the click hold and release, and enters the widget’s On Mouse Button Down and On Mouse Button Up, only when the inventory slot is empty and not loaded with an item.
What am I doing wrong?
Here is the code for the plugin:
GamepadToMouse.cpp
// Copyright Epic Games, Inc. All Rights Reserved.
#include "GamepadToMouse.h"
#include "InputCoreTypes.h"
#include "Engine/Engine.h"
#include "GameFramework/PlayerController.h"
#include "Widgets/SWindow.h"
#include "Input/HittestGrid.h"
#include "Widgets/DeclarativeSyntaxSupport.h"
#include "Framework/Application/SlateApplication.h"
#define LOCTEXT_NAMESPACE "FGamepadToMouseModule"
void FGamepadToMouseModule::StartupModule()
{
FWorldDelegates::OnWorldPostActorTick.AddRaw(this, &FGamepadToMouseModule::OnEngineInitialized);
}
void FGamepadToMouseModule::ShutdownModule()
{
FCoreDelegates::OnPostEngineInit.RemoveAll(this);
}
void FGamepadToMouseModule::OnEngineInitialized(UWorld* World, ELevelTick TickType, float DeltaSeconds)
{
// Now that the engine is initialized, you can safely access GEngine
if (APlayerController* PC = GetPlayerController())
{
// Bind to gamepad button press and release
PC->InputComponent->BindAction("GamepadBottomPress", IE_Pressed, this, &FGamepadToMouseModule::OnGamepadButtonPressed);
PC->InputComponent->BindAction("GamepadBottomPress", IE_Released, this, &FGamepadToMouseModule::OnGamepadButtonReleased);
}
}
void FGamepadToMouseModule::OnGamepadButtonPressed()
{
SimulateMouseClickHold();
}
void FGamepadToMouseModule::OnGamepadButtonReleased()
{
SimulateMouseClickRelease();
}
void FGamepadToMouseModule::SimulateMouseClickHold()
{
if (APlayerController* PC = GetPlayerController())
{
FVector2D MousePosition;
PC->GetMousePosition(MousePosition.X, MousePosition.Y);
FSlateApplication& SlateApp = FSlateApplication::Get();
TSharedPtr<SWindow> TopWindow = SlateApp.GetActiveTopLevelWindow();
if (TopWindow.IsValid())
{
TSharedPtr<FGenericWindow> NativeWindow = TopWindow->GetNativeWindow();
if (NativeWindow.IsValid())
{
FPointerEvent PointerEvent(
0, // Pointer Index
MousePosition, // Screen Space Position
MousePosition, // Last Screen Space Position
SlateApp.GetPressedMouseButtons(), // Pressed Buttons
EKeys::LeftMouseButton, // Effecting Button
0.0f, // Wheel Delta
FModifierKeysState() // Modifier Keys State
);
// Simulate the mouse button down event
SlateApp.ProcessMouseButtonDownEvent(NativeWindow.ToSharedRef(), PointerEvent);
UE_LOG(LogTemp, Warning, TEXT("Mouse Left Button Press simulated for viewport."));
}
}
}
}
void FGamepadToMouseModule::SimulateMouseClickRelease()
{
if (APlayerController* PC = GetPlayerController())
{
FVector2D MousePosition;
PC->GetMousePosition(MousePosition.X, MousePosition.Y);
FSlateApplication& SlateApp = FSlateApplication::Get();
TSharedPtr<SWindow> TopWindow = SlateApp.GetActiveTopLevelWindow();
if (TopWindow.IsValid())
{
TSharedPtr<FGenericWindow> NativeWindow = TopWindow->GetNativeWindow();
if (NativeWindow.IsValid())
{
FPointerEvent PointerEvent(
0, // Pointer Index
MousePosition, // Screen Space Position
MousePosition, // Last Screen Space Position
SlateApp.GetPressedMouseButtons(), // Pressed Buttons
EKeys::LeftMouseButton, // Effecting Button
0.0f, // Wheel Delta
FModifierKeysState() // Modifier Keys State
);
// Simulate the mouse button down event
SlateApp.ProcessMouseButtonUpEvent(PointerEvent);
UE_LOG(LogTemp, Warning, TEXT("Mouse Left Button Release simulated after hold."));
}
}
}
}
APlayerController* FGamepadToMouseModule::GetPlayerController() const
{
if (GEngine && GEngine->GameViewport)
{
UWorld* World = GEngine->GameViewport->GetWorld();
if (World)
{
return World->GetFirstPlayerController();
}
}
//UE_LOG(LogTemp, Warning, TEXT("Failed to retrieve PlayerController: GEngine or GameViewport is null."));
return nullptr;
}
#undef LOCTEXT_NAMESPACE
IMPLEMENT_MODULE(FGamepadToMouseModule, GamepadToMouse)
GamepadToMouse.h
// Copyright Epic Games, Inc. All Rights Reserved.
#pragma once
#include "CoreMinimal.h"
#include "Modules/ModuleManager.h"
#include "InputCoreTypes.h"
#include "Widgets/SWindow.h"
#include "Framework/Application/SlateApplication.h"
class FGamepadToMouseModule : public IModuleInterface
{
public:
virtual void StartupModule() override;
virtual void ShutdownModule() override;
void OnEngineInitialized(UWorld* World, ELevelTick TickType, float DeltaSeconds);
private:
void OnGamepadButtonPressed();
void OnGamepadButtonReleased();
void SimulateMouseClickHold();
void SimulateMouseClickRelease();
APlayerController* GetPlayerController() const;
FDelegateHandle OnPressedHandle;
FDelegateHandle OnReleasedHandle;
};