I have a widget that I want to be able to drag with the mouse. To do that I need to update the widget position every frame based on changes in the mouse position. To do that i need to override the tick function. I have already checked this UMG, overriding of UsingWidget.Tick and It has not helped. Here is my header
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "CoreMinimal.h"
#include "Blueprint/UserWidget.h"
#include "DragWindow.generated.h"
/**
*
*/
UCLASS()
class HELLOWORLDGODLL_API UDragWindow : public UUserWidget
{
GENERATED_BODY()
protected:
UPROPERTY(BluePrintReadWrite, meta = (BindWidget))
class UButton* DragBar;
UPROPERTY(BluePrintReadWrite, meta = (BindWidget))
class UTextBlock* Title;
public:
// Called every frame
virtual void OnMouseMove(FGeometry MyGeometry, FPointerEvent& MouseEvent);
UFUNCTION(BlueprintCallable)
void onButtonClicked();
UFUNCTION(BlueprintCallable)
void onButtonReleased();
virtual void NativeConstruct() override;
void SetTitle(FString title);
virtual void Tick_Implementation(const FGeometry& geo, float deltaTime);
};
and my CPP
// Fill out your copyright notice in the Description page of Project Settings.
#include "Layout/Geometry.h"
#include "Components/Image.h"
#include "DragWindow.h"
#include "Components/Button.h"
#include <Components/TextBlock.h>
#include <Runtime/Engine/Classes/Kismet/GameplayStatics.h>
bool isDragging;
float lastX;
float lastY;
void UDragWindow::OnMouseMove(FGeometry MyGeometry, FPointerEvent& MouseEvent) {
float mouseX;
float mouseY;
UGameplayStatics::GetPlayerController(GetWorld(), 0)->GetMousePosition(mouseX, mouseY);
int diffx = mouseX - lastX;
int diffy = mouseY - lastY;
const FGeometry Geometry = this->GetCachedGeometry();
float x = Geometry.AbsolutePosition.X + diffx;
float y = Geometry.AbsolutePosition.Y + diffy;
FVector2D NewPos = FVector2D(x, y);
this->SetPositionInViewport(NewPos);
}
void UDragWindow::onButtonClicked() {
UE_LOG(LogTemp, Warning, TEXT("clicked"));
float mouseX;
float mouseY;
UGameplayStatics::GetPlayerController(GetWorld(), 0)->GetMousePosition(mouseX, mouseY);
lastX = mouseX;
lastY = mouseY;
isDragging = true;
}
void UDragWindow::onButtonReleased() {
UE_LOG(LogTemp, Warning, TEXT("released"));
isDragging = false;
}
void UDragWindow::NativeConstruct()
{
UE_LOG(LogTemp, Warning, TEXT("built"));
DragBar->OnPressed.AddDynamic(this, &UDragWindow::onButtonClicked);
DragBar->OnReleased.AddDynamic(this, &UDragWindow::onButtonReleased);
}
void UDragWindow::SetTitle(FString title) {
Title->SetText(FText::FromString(title));
}
void UDragWindow::Tick_Implementation(const FGeometry& geo, float deltaTime) {
Super::Tick(geo, deltaTime);
UE_LOG(LogTemp, Warning, TEXT("yahoo"));
if (isDragging){
float mouseX;
float mouseY;
UGameplayStatics::GetPlayerController(GetWorld(), 0)->GetMousePosition(mouseX, mouseY);
int diffx = mouseX - lastX;
int diffy = mouseY - lastY;
const FGeometry Geometry = this->GetCachedGeometry();
float x = Geometry.AbsolutePosition.X + diffx;
float y = Geometry.AbsolutePosition.Y + diffy;
FVector2D NewPos = FVector2D(x, y);
this->SetPositionInViewport(NewPos);
}
}
I know my other code is likely wrong, but how do I get the Tick function to be called automatically?