Skip to content

Commit 4223550

Browse files
author
Roberto De Ioris
committed
improved GameViewportClient api
1 parent 50fa29a commit 4223550

File tree

8 files changed

+99
-4
lines changed

8 files changed

+99
-4
lines changed

Source/UnrealEnginePython/Private/Slate/UEPySWindow.cpp

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,15 @@ static PyObject *py_ue_swindow_set_title(ue_PySWindow *self, PyObject * args)
2525
Py_RETURN_SLATE_SELF;
2626
}
2727

28+
static PyObject *py_ue_swindow_get_title(ue_PySWindow *self, PyObject * args)
29+
{
30+
ue_py_slate_cast(SWindow);
31+
32+
const char *title = TCHAR_TO_UTF8(*py_SWindow->GetTitle().ToString());
33+
34+
return PyUnicode_FromString(title);
35+
}
36+
2837
static PyObject *py_ue_swindow_resize(ue_PySWindow *self, PyObject * args)
2938
{
3039
ue_py_slate_cast(SWindow);
@@ -155,8 +164,23 @@ static PyObject *py_ue_swindow_add_child(ue_PySWindow *self, PyObject * args)
155164
Py_RETURN_NONE;
156165
}
157166

167+
static PyObject *py_ue_swindow_get_child_windows(ue_PySWindow *self, PyObject * args)
168+
{
169+
ue_py_slate_cast(SWindow);
170+
TArray<TSharedRef<SWindow>>& ChildWindows = py_SWindow->GetChildWindows();
171+
PyObject *py_list = PyList_New(0);
172+
for (TSharedRef<SWindow> ChildWindow : ChildWindows)
173+
{
174+
PyList_Append(py_list, (PyObject *)py_ue_new_swindow(ChildWindow));
175+
}
176+
177+
return py_list;
178+
}
179+
158180
static PyMethodDef ue_PySWindow_methods[] = {
181+
{ "get_child_windows", (PyCFunction)py_ue_swindow_get_child_windows, METH_VARARGS, "" },
159182
{ "set_title", (PyCFunction)py_ue_swindow_set_title, METH_VARARGS, "" },
183+
{ "get_title", (PyCFunction)py_ue_swindow_get_title, METH_VARARGS, "" },
160184
{ "set_sizing_rule", (PyCFunction)py_ue_swindow_set_sizing_rule, METH_VARARGS, "" },
161185
{ "minimize", (PyCFunction)py_ue_swindow_minimize, METH_VARARGS, "" },
162186
{ "resize", (PyCFunction)py_ue_swindow_resize, METH_VARARGS, "" },
@@ -296,4 +320,13 @@ ue_PySWindow *py_ue_is_swindow(PyObject *obj)
296320
if (!PyObject_IsInstance(obj, (PyObject *)&ue_PySWindowType))
297321
return nullptr;
298322
return (ue_PySWindow *)obj;
323+
}
324+
325+
ue_PySWindow *py_ue_new_swindow(TSharedRef<SWindow> s_window)
326+
{
327+
ue_PySWindow *ret = (ue_PySWindow *)PyObject_New(ue_PySWindow, &ue_PySWindowType);
328+
329+
new(&ret->s_compound_widget.s_widget.Widget) TSharedRef<SWindow>(s_window);
330+
ret->s_compound_widget.s_widget.weakreflist = nullptr;
331+
return ret;
299332
}

Source/UnrealEnginePython/Private/Slate/UEPySWindow.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,6 @@ typedef struct {
1313

1414
void ue_python_init_swindow(PyObject *);
1515

16-
ue_PySWindow *py_ue_is_swindow(PyObject *obj);
16+
ue_PySWindow *py_ue_is_swindow(PyObject *obj);
17+
18+
ue_PySWindow *py_ue_new_swindow(TSharedRef<SWindow>);

Source/UnrealEnginePython/Private/SlateApplication/UEPyFSlateApplication.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include "Slate/UEPySWidget.h"
55
#include "Runtime/Slate/Public/Framework/Application/SlateApplication.h"
66
#include "Runtime/SlateRHIRenderer/Public/Interfaces/ISlateRHIRendererModule.h"
7+
#include "Slate/UEPySWindow.h"
78

89
static PyObject *py_ue_get_average_delta_time(PyObject *cls, PyObject * args)
910
{
@@ -166,6 +167,17 @@ static PyObject *py_ue_create(PyObject *cls, PyObject * args)
166167
Py_RETURN_NONE;
167168
}
168169

170+
171+
static PyObject *py_ue_get_active_top_level_window(PyObject *cls, PyObject * args)
172+
{
173+
174+
TSharedPtr<SWindow> Window = FSlateApplication::Get().GetActiveTopLevelWindow();
175+
if (!Window.IsValid())
176+
return PyErr_Format(PyExc_Exception, "no active TopLevel Window found");
177+
178+
return (PyObject *)py_ue_new_swindow(Window.ToSharedRef());
179+
}
180+
169181
static PyMethodDef ue_PyFSlateApplication_methods[] = {
170182
{ "create", (PyCFunction)py_ue_create, METH_VARARGS | METH_CLASS, "" },
171183
{ "get_average_delta_time", (PyCFunction)py_ue_get_average_delta_time, METH_VARARGS | METH_CLASS, "" },
@@ -180,6 +192,7 @@ static PyMethodDef ue_PyFSlateApplication_methods[] = {
180192
{ "set_application_scale", (PyCFunction)py_ue_set_application_scale, METH_VARARGS | METH_CLASS, "" },
181193
{ "set_all_user_focus", (PyCFunction)py_ue_set_all_user_focus, METH_VARARGS | METH_CLASS, "" },
182194
{ "set_cursor_pos", (PyCFunction)py_ue_set_cursor_pos, METH_VARARGS | METH_CLASS, "" },
195+
{ "get_active_top_level_window", (PyCFunction)py_ue_get_active_top_level_window, METH_VARARGS | METH_CLASS, "" },
183196
{ NULL } /* Sentinel */
184197
};
185198

Source/UnrealEnginePython/Private/UEPyModule.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -445,7 +445,7 @@ static PyMethodDef unreal_engine_methods[] = {
445445
{ "get_viewport_size", py_unreal_engine_get_viewport_size, METH_VARARGS, "" },
446446
{ "get_resolution", py_unreal_engine_get_resolution, METH_VARARGS, "" },
447447
{ "get_game_viewport_size", py_unreal_engine_get_game_viewport_size, METH_VARARGS, "" },
448-
448+
449449
{ "get_game_viewport_client", py_unreal_engine_get_game_viewport_client, METH_VARARGS, "" },
450450
#pragma warning(suppress: 4191)
451451
{ "open_color_picker", (PyCFunction)py_unreal_engine_open_color_picker, METH_VARARGS | METH_KEYWORDS, "" },
@@ -880,6 +880,8 @@ static PyMethodDef ue_PyUObject_methods[] = {
880880
{ "get_world_location_at_distance_along_spline", (PyCFunction)py_ue_get_world_location_at_distance_along_spline, METH_VARARGS, "" },
881881
{ "get_spline_length", (PyCFunction)py_ue_get_spline_length, METH_VARARGS, "" },
882882

883+
{ "game_viewport_client_get_window", (PyCFunction)py_ue_game_viewport_client_get_window, METH_VARARGS, "" },
884+
883885
// Widget
884886
{ "take_widget", (PyCFunction)py_ue_take_widget, METH_VARARGS, "" },
885887
{ "create_widget", (PyCFunction)py_ue_create_widget, METH_VARARGS, "" },

Source/UnrealEnginePython/Private/UObject/UEPyViewport.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,25 @@
77
#endif
88

99
#include "Slate/UEPySWidget.h"
10+
#include "Slate/UEPySWindow.h"
1011
// required for GEngine access
1112
#include "Engine/Engine.h"
1213

14+
PyObject *py_ue_game_viewport_client_get_window(ue_PyUObject *self, PyObject *args)
15+
{
16+
ue_py_check(self);
17+
18+
UGameViewportClient *viewport = ue_py_check_type<UGameViewportClient>(self);
19+
if (!viewport)
20+
return PyErr_Format(PyExc_Exception, "uobject is not a GameViewportClient");
21+
22+
TSharedPtr<SWindow> Window = viewport->GetWindow();
23+
if (!Window.IsValid())
24+
return PyErr_Format(PyExc_Exception, "GameViewportClient has no window");
25+
26+
return (PyObject *)py_ue_new_swindow(Window.ToSharedRef());
27+
}
28+
1329
PyObject *py_unreal_engine_get_game_viewport_client(PyObject * self, PyObject * args)
1430
{
1531

Source/UnrealEnginePython/Private/UObject/UEPyViewport.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,4 @@ PyObject *py_ue_add_viewport_widget_content(ue_PyUObject *, PyObject *);
1717
PyObject *py_ue_remove_viewport_widget_content(ue_PyUObject *, PyObject *);
1818
PyObject *py_ue_remove_all_viewport_widgets(ue_PyUObject *, PyObject *);
1919
PyObject *py_ue_game_viewport_client_set_rendering_flag(ue_PyUObject *, PyObject *);
20+
PyObject *py_ue_game_viewport_client_get_window(ue_PyUObject *, PyObject *);

Source/UnrealEnginePython/Public/UnrealEnginePython.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@
1010
//#define UEPY_MEMORY_DEBUG 1
1111

1212
#include "CoreMinimal.h"
13-
#include "ModuleManager.h"
14-
#include "Styling/SlateStyle.h"
13+
#include "Runtime/Core/Public/Modules/ModuleManager.h"
14+
#include "Runtime/SlateCore/Public/Styling/SlateStyle.h"
1515
#include "UObject/ScriptMacros.h"
1616
#include "Runtime/Launch/Resources/Version.h"
1717

examples/get_windows.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import unreal_engine as ue
2+
from unreal_engine import FSlateApplication
3+
from unreal_engine.classes import GameViewportClient
4+
5+
def iterate_window(window):
6+
print(window.get_title())
7+
for child in window.get_child_windows():
8+
iterate_window(child)
9+
10+
# get the active top level window
11+
top_window = FSlateApplication.get_active_top_level_window()
12+
iterate_window(top_window)
13+
14+
# get Game GameViewportClient
15+
try:
16+
iterate_window(ue.get_game_viewport_client().game_viewport_client_get_window())
17+
except:
18+
pass
19+
20+
# get PIE GameViewportClient
21+
try:
22+
iterate_window(ue.get_editor_pie_game_viewport_client().game_viewport_client_get_window())
23+
except:
24+
pass
25+
26+
# iterate all GameViewportClient uobject's
27+
for game_viewport_client in ue.tobject_iterator(GameViewportClient):
28+
iterate_window(game_viewport_client.game_viewport_client_get_window())

0 commit comments

Comments
 (0)