Agrarsense
Public Member Functions | Private Member Functions | Private Attributes | List of all members
FUnrealWindow Class Reference

#include <UnrealWindow.h>

Public Member Functions

 FUnrealWindow (int32 Width, int32 Height, FString WindowName="Camera")
 
 ~FUnrealWindow ()
 
void SetupComponent (UTextureRenderTarget2D *TextureRenderTarget)
 
void ResizeWindow (int32 Width, int32 Height)
 
void DestroyWindow ()
 
void UpdateTexture (FTextureResource *Texture)
 
void DrawManually ()
 
TSharedPtr< FSceneViewport > GetSceneViewport ()
 
TSharedPtr< SWindow > GetSWindow ()
 
void SetAutomaticDraw (bool Draw)
 
bool IsDrawing ()
 
void AddUWidgetToWindow (UWidget *WidgetToAdd)
 
void RemoveUWidgetFromWindow (UWidget *WidgetToRemove)
 
int GetWindowWidth ()
 
int GetWindowHeight ()
 

Private Member Functions

void OnWindowClosed (const TSharedRef< SWindow > &Window)
 

Private Attributes

int WindowWidth = 1280
 
int WindowHeight = 720
 
int AddedWidgetCount = 0
 
bool DrawToScreen = true
 
FOnWindowClosed onWindowClosedDelegate
 
TSharedPtr< FUnrealWindowViewportClientViewportClient
 
TSharedPtr< SUnrealWindowViewportViewport
 
TSharedPtr< FSceneViewport > SceneViewport
 
TSharedPtr< SViewport > sViewport
 
TSharedPtr< SWindow > sWindow
 
TSharedPtr< SOverlay > FullScreenOverlay
 
TArray< FAddedWidgetInfoAddedWidgets
 

Detailed Description

Definition at line 35 of file UnrealWindow.h.

Constructor & Destructor Documentation

◆ FUnrealWindow()

FUnrealWindow::FUnrealWindow ( int32  Width,
int32  Height,
FString  WindowName = "Camera" 
)

Create new Unreal Window

Parameters
widthwidth of Window
heightheight of Window
windowNameWindow title name

Definition at line 14 of file UnrealWindow.cpp.

15{
16 // Set window width and height
17 WindowWidth = Width;
18 WindowHeight = Height;
19
20 // Do not support Window minimize button,
21 // because this causes Camera sensors to stop rendering when window is minimized.
22 bool SupportMinimize = false;
23
24#ifdef EngineAllowRenderingMinimized
25 // Unless when we are using AGRARSENSE Unreal Engine fork,
26 // where SWindow has extra boolean which allows rendering when a window is minimized.
27 SupportMinimize = true;
28#endif
29
30 // Create SWindow
31 sWindow = SNew(SWindow)
32 .SupportsMinimize(SupportMinimize)
34 .Title(FText::FromString(WindowName))
35 .ScreenPosition(FVector2D(0, 0))
36 .ClientSize(FVector2D(WindowWidth, WindowHeight))
37 .AutoCenter(EAutoCenter::PreferredWorkArea)
38 .UseOSWindowBorder(true)
39 .SaneWindowPlacement(false)
40 .SizingRule(ESizingRule::UserSized);
41
42#ifdef EngineAllowRenderingMinimized
43 // Set SWindow AllowRenderingMinimized to true that allows SWindow to be rendered even when minimized.
44 // Defined and implemented in AGRARSENSE Unreal Engine fork
45 sWindow->AllowRenderingMinimized = true;
46#endif
47
48 // Add window to FSlateApplication
49 FSlateApplication::Get().AddWindow(sWindow.ToSharedRef());
50
51 // Assign window events delegates
53 sWindow->SetOnWindowClosed(onWindowClosedDelegate);
54
55 // Create and assign viewport to window
57 sWindow->SetContent(Viewport.ToSharedRef());
58
59 // Set references
60 ViewportClient = Viewport->ViewportClient;
61 SceneViewport = Viewport->SceneViewport;
62 sViewport = Viewport->Viewport;
63
64 // Resize window
66}
TSharedPtr< FUnrealWindowViewportClient > ViewportClient
Definition: UnrealWindow.h:160
FOnWindowClosed onWindowClosedDelegate
Definition: UnrealWindow.h:158
TSharedPtr< SUnrealWindowViewport > Viewport
Definition: UnrealWindow.h:161
void OnWindowClosed(const TSharedRef< SWindow > &Window)
void ResizeWindow(int32 Width, int32 Height)
TSharedPtr< SWindow > sWindow
Definition: UnrealWindow.h:165
TSharedPtr< FSceneViewport > SceneViewport
Definition: UnrealWindow.h:163
TSharedPtr< SViewport > sViewport
Definition: UnrealWindow.h:164
static bool ShouldStartWindowMinimized()

References OnWindowClosed(), onWindowClosedDelegate, ResizeWindow(), SceneViewport, UAgrarsenseSettings::ShouldStartWindowMinimized(), sViewport, sWindow, Viewport, ViewportClient, WindowHeight, and WindowWidth.

◆ ~FUnrealWindow()

FUnrealWindow::~FUnrealWindow ( )

Definition at line 68 of file UnrealWindow.cpp.

69{
71
72 FullScreenOverlay.Reset();
73 ViewportClient.Reset();
74 SceneViewport.Reset();
75 sViewport.Reset();
76 Viewport.Reset();
77 sWindow.Reset();
78
79 AddedWidgets.Empty();
80}
TArray< FAddedWidgetInfo > AddedWidgets
Definition: UnrealWindow.h:170
TSharedPtr< SOverlay > FullScreenOverlay
Definition: UnrealWindow.h:167

References AddedWidgets, FullScreenOverlay, onWindowClosedDelegate, SceneViewport, sViewport, sWindow, Viewport, and ViewportClient.

Member Function Documentation

◆ AddUWidgetToWindow()

void FUnrealWindow::AddUWidgetToWindow ( UWidget *  WidgetToAdd)

Add a UWidget to this window.

Parameters
WidgetToAdd- The UWidget to add to this window.

Definition at line 147 of file UnrealWindow.cpp.

148{
149 if (!WidgetToAdd || !sWindow.IsValid())
150 {
151 return;
152 }
153
154 if (!FullScreenOverlay.IsValid())
155 {
156 // Create a full-screen SOverlay to contain the camera image and custom widget
157 FullScreenOverlay = SNew(SOverlay);
158
159 // Create a slot for current viewport widget
160 FullScreenOverlay->AddSlot(0)
161 .HAlign(HAlign_Fill)
162 .VAlign(VAlign_Fill)
163 [
164 Viewport.ToSharedRef()
165 ];
166 }
167
169
170 // Create a slot for the new widget
172 .HAlign(HAlign_Fill)
173 .VAlign(VAlign_Fill)
174 [
175 WidgetToAdd->TakeWidget()
176 ];
177
178 // Store information about the added widget
179 FAddedWidgetInfo WidgetInfo;
180 WidgetInfo.Widget = WidgetToAdd;
181 WidgetInfo.Index = AddedWidgetCount;
182 AddedWidgets.Add(WidgetInfo);
183
184 // Set the updated overlay as the window content
185 sWindow->SetContent(FullScreenOverlay.ToSharedRef());
186}
UWidget * Widget
Definition: UnrealWindow.h:22

References AddedWidgetCount, AddedWidgets, FullScreenOverlay, FAddedWidgetInfo::Index, sWindow, Viewport, and FAddedWidgetInfo::Widget.

◆ DestroyWindow()

void FUnrealWindow::DestroyWindow ( )

Destroy this Window

Definition at line 124 of file UnrealWindow.cpp.

125{
126 if (sWindow.IsValid())
127 {
128 sWindow->RequestDestroyWindow();
129 }
130}

References sWindow.

Referenced by OnWindowClosed().

◆ DrawManually()

void FUnrealWindow::DrawManually ( )

Definition at line 103 of file UnrealWindow.cpp.

104{
105 if (DrawToScreen)
106 {
107#if WITH_EDITOR
108 UE_LOG(LogTemp, Warning, TEXT("UnrealWindow.cpp: We are already drawing automatically, we should not draw manually!"));
109#endif
110 return;
111 }
112
113 if (SceneViewport.IsValid())
114 {
115 SceneViewport->Draw();
116 }
117}

References DrawToScreen, and SceneViewport.

◆ GetSceneViewport()

TSharedPtr< FSceneViewport > FUnrealWindow::GetSceneViewport ( )
inline

Get FSceneViewport TSharedPtr

Returns
TSharedPtr<FSceneViewport>

Definition at line 83 of file UnrealWindow.h.

84 {
85 return SceneViewport;
86 }

References SceneViewport.

◆ GetSWindow()

TSharedPtr< SWindow > FUnrealWindow::GetSWindow ( )
inline

Get SWindow TSharedPtr

Returns
TSharedPtr<SWindow>

Definition at line 92 of file UnrealWindow.h.

93 {
94 return sWindow;
95 }

References sWindow.

◆ GetWindowHeight()

int FUnrealWindow::GetWindowHeight ( )
inline

Definition at line 138 of file UnrealWindow.h.

139 {
140 return WindowHeight;
141 }

References WindowHeight.

◆ GetWindowWidth()

int FUnrealWindow::GetWindowWidth ( )
inline

Definition at line 133 of file UnrealWindow.h.

134 {
135 return WindowWidth;
136 }

References WindowWidth.

◆ IsDrawing()

bool FUnrealWindow::IsDrawing ( )
inline

Check if this window is currently in the process of drawing to the screen.

Returns
True if drawing to the screen is enabled, false otherwise.

Definition at line 115 of file UnrealWindow.h.

116 {
117 return DrawToScreen;
118 }

References DrawToScreen.

◆ OnWindowClosed()

void FUnrealWindow::OnWindowClosed ( const TSharedRef< SWindow > &  Window)
private

On Window closed event from SWindow

Parameters
WindowWindow reference

Definition at line 119 of file UnrealWindow.cpp.

120{
122}
void DestroyWindow()

References DestroyWindow().

Referenced by FUnrealWindow().

◆ RemoveUWidgetFromWindow()

void FUnrealWindow::RemoveUWidgetFromWindow ( UWidget *  WidgetToRemove)

Remove a UWidget from this window.

Parameters
WidgetToRemove- The UWidget to remove from this window.

Definition at line 188 of file UnrealWindow.cpp.

189{
190 if (!WidgetToRemove || !sWindow.IsValid() || !FullScreenOverlay.IsValid())
191 {
192 return;
193 }
194
195 // Not the prettiest but works for this use case.
196 int32 WidgetIndexToRemove = -1;
197 for (int32 Index = 0; Index < AddedWidgets.Num(); ++Index)
198 {
199 if (AddedWidgets[Index].Widget == WidgetToRemove)
200 {
201 WidgetIndexToRemove = Index;
202 break;
203 }
204 }
205
206 if (WidgetIndexToRemove != -1)
207 {
208 // Remove Widget from slot (arrays start at 0 but slot 0 is the camera view)
209 FullScreenOverlay->RemoveSlot(WidgetIndexToRemove + 1);
210
211 // Set the updated overlay as the window content
212 sWindow->SetContent(FullScreenOverlay.ToSharedRef());
213
214 // Remove the widget info from the array
215 AddedWidgets.RemoveAt(WidgetIndexToRemove);
216 }
217}

References AddedWidgets, FullScreenOverlay, and sWindow.

◆ ResizeWindow()

void FUnrealWindow::ResizeWindow ( int32  Width,
int32  Height 
)

Resize Window into given size.

Parameters
widthwidth of Window
heightheight of Window

Definition at line 132 of file UnrealWindow.cpp.

133{
134 if (ViewportClient.IsValid() && SceneViewport.IsValid() && sWindow.IsValid())
135 {
136 WindowWidth = width;
137 WindowHeight = height;
138
141
142 SceneViewport->SetViewportSize(WindowWidth, WindowHeight);
143 sWindow->Resize(FVector2D(WindowWidth, WindowHeight));
144 }
145}

References SceneViewport, sWindow, ViewportClient, WindowHeight, and WindowWidth.

Referenced by FUnrealWindow().

◆ SetAutomaticDraw()

void FUnrealWindow::SetAutomaticDraw ( bool  Draw)
inline

Set whether automatic drawing to the screen is enabled. If set to false, you must call DrawManually() whenever you want to draw anything to the window.

Parameters
draw- If true, automatic drawing is enabled; if false, manual drawing is required.

Definition at line 102 of file UnrealWindow.h.

103 {
104 DrawToScreen = Draw;
105 if (Viewport.IsValid())
106 {
107 Viewport->DrawToScreen = DrawToScreen;
108 }
109 }

References DrawToScreen, and Viewport.

◆ SetupComponent()

void FUnrealWindow::SetupComponent ( UTextureRenderTarget2D *  TextureRenderTarget)

Setup UTextureRenderTarget2D in order to render to this Window

Parameters
textureRenderTargetUTextureRenderTarget2D for rendering into Window

Definition at line 82 of file UnrealWindow.cpp.

83{
84 if (ViewportClient.IsValid() && TextureRenderTarget)
85 {
86 ViewportClient->TextRenderTarget2D = TextureRenderTarget;
87 ViewportClient->texture = TextureRenderTarget->GetResource();
90 }
91}

References ViewportClient, WindowHeight, and WindowWidth.

◆ UpdateTexture()

void FUnrealWindow::UpdateTexture ( FTextureResource *  Texture)

Definition at line 93 of file UnrealWindow.cpp.

94{
95 if (ViewportClient.IsValid() && Texture)
96 {
97 ViewportClient->texture = Texture;
100 }
101}

References ViewportClient, WindowHeight, and WindowWidth.

Member Data Documentation

◆ AddedWidgetCount

int FUnrealWindow::AddedWidgetCount = 0
private

Definition at line 154 of file UnrealWindow.h.

Referenced by AddUWidgetToWindow().

◆ AddedWidgets

TArray<FAddedWidgetInfo> FUnrealWindow::AddedWidgets
private

Definition at line 170 of file UnrealWindow.h.

Referenced by AddUWidgetToWindow(), RemoveUWidgetFromWindow(), and ~FUnrealWindow().

◆ DrawToScreen

bool FUnrealWindow::DrawToScreen = true
private

Definition at line 156 of file UnrealWindow.h.

Referenced by DrawManually(), IsDrawing(), and SetAutomaticDraw().

◆ FullScreenOverlay

TSharedPtr<SOverlay> FUnrealWindow::FullScreenOverlay
private

Definition at line 167 of file UnrealWindow.h.

Referenced by AddUWidgetToWindow(), RemoveUWidgetFromWindow(), and ~FUnrealWindow().

◆ onWindowClosedDelegate

FOnWindowClosed FUnrealWindow::onWindowClosedDelegate
private

Definition at line 158 of file UnrealWindow.h.

Referenced by FUnrealWindow(), and ~FUnrealWindow().

◆ SceneViewport

TSharedPtr<FSceneViewport> FUnrealWindow::SceneViewport
private

◆ sViewport

TSharedPtr<SViewport> FUnrealWindow::sViewport
private

Definition at line 164 of file UnrealWindow.h.

Referenced by FUnrealWindow(), and ~FUnrealWindow().

◆ sWindow

TSharedPtr<SWindow> FUnrealWindow::sWindow
private

◆ Viewport

TSharedPtr<SUnrealWindowViewport> FUnrealWindow::Viewport
private

◆ ViewportClient

TSharedPtr<FUnrealWindowViewportClient> FUnrealWindow::ViewportClient
private

◆ WindowHeight

int FUnrealWindow::WindowHeight = 720
private

◆ WindowWidth

int FUnrealWindow::WindowWidth = 1280
private

The documentation for this class was generated from the following files: