Agrarsense
WindowActor.cpp
Go to the documentation of this file.
1// Copyright (c) 2024 FrostBit Software Lab at the Lapland University of Applied Sciences
2//
3// This work is licensed under the terms of the MIT license.
4// For a copy, see <https://opensource.org/licenses/MIT>.
5
6#include "WindowActor.h"
7
8AWindowActor::AWindowActor(const FObjectInitializer& ObjectInitializer) : Super(ObjectInitializer)
9{
10 PrimaryActorTick.bCanEverTick = false;
11}
12
13void AWindowActor::SetupWindow(int Width, int Height, FString Title, UTextureRenderTarget2D* TextureRenderTarget)
14{
15 if (!UnrealWindow.IsValid())
16 {
17 // Create FUnrealWindow
18 UnrealWindow = MakeShareable(new FUnrealWindow(Width, Height, Title));
19
20 // Setup on window closed event (user clicks Window's closed button)
22 UnrealWindow->GetSWindow()->SetOnWindowClosed(OnWindowClosedDelegate);
23
24 if (TextureRenderTarget)
25 {
26 SetupTextureRenderTarget(TextureRenderTarget);
27 }
28 }
29}
30
31void AWindowActor::SetupTextureRenderTarget(UTextureRenderTarget2D* TextureRenderTarget)
32{
33 if (UnrealWindow.IsValid())
34 {
35 UnrealWindow->SetupComponent(TextureRenderTarget);
36 }
37}
38
40{
41 Super::BeginPlay();
42}
43
44void AWindowActor::EndPlay(const EEndPlayReason::Type EndPlayReason)
45{
46 Super::EndPlay(EndPlayReason);
47
48 // At EndPlay destroy Window
49 if (UnrealWindow.IsValid())
50 {
52 UnrealWindow->DestroyWindow();
53 UnrealWindow.Reset();
54 }
55}
56
57void AWindowActor::AddWidgetToWindow(UWidget* WidgetToAdd)
58{
59 if (UnrealWindow.IsValid())
60 {
61 UnrealWindow->AddUWidgetToWindow(WidgetToAdd);
62 }
63}
64
65void AWindowActor::RemoveWidgetFromWindow(UWidget* WidgetToAdd)
66{
67 if (UnrealWindow.IsValid())
68 {
69 UnrealWindow->RemoveUWidgetFromWindow(WidgetToAdd);
70 }
71}
72
73void AWindowActor::ResizeWindow(int Width, int Height)
74{
75 if (UnrealWindow.IsValid())
76 {
77 UnrealWindow->ResizeWindow(Width, Height);
78 }
79}
80
81void AWindowActor::OnWindowClosed(const TSharedRef<SWindow>& Window)
82{
83 // Destroy this Actor when Window is closed
84 this->Destroy();
85}
FOnWindowClosed OnWindowClosedDelegate
Definition: WindowActor.h:54
void SetupTextureRenderTarget(UTextureRenderTarget2D *TextureRenderTarget)
Definition: WindowActor.cpp:31
void AddWidgetToWindow(UWidget *WidgetToAdd)
Definition: WindowActor.cpp:57
virtual void BeginPlay() override
Definition: WindowActor.cpp:39
virtual void EndPlay(const EEndPlayReason::Type EndPlayReason) override
Definition: WindowActor.cpp:44
AWindowActor(const FObjectInitializer &ObjectInitializer)
Definition: WindowActor.cpp:8
void ResizeWindow(int Width=1280, int Height=720)
Definition: WindowActor.cpp:73
void OnWindowClosed(const TSharedRef< SWindow > &Window)
Definition: WindowActor.cpp:81
void RemoveWidgetFromWindow(UWidget *WidgetToAdd)
Definition: WindowActor.cpp:65
TSharedPtr< FUnrealWindow > UnrealWindow
Definition: WindowActor.h:66
void SetupWindow(int Width=1280, int Height=720, FString Title="Window", UTextureRenderTarget2D *TextureRenderTarget=nullptr)
Definition: WindowActor.cpp:13