Skip to content

Commit 3197739

Browse files
author
Roberto De Ioris
committed
preliminary infrastructure for FSlateApplication automations
1 parent ac19f6a commit 3197739

10 files changed

Lines changed: 266 additions & 0 deletions

File tree

‎Source/UnrealEnginePython/Private/Slate/UEPyFCharacterEvent.cpp‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,31 @@ static PyTypeObject ue_PyFCharacterEventType = {
5353
ue_PyFCharacterEvent_methods, /* tp_methods */
5454
};
5555

56+
static int ue_py_fcharacter_event_init(ue_PyFCharacterEvent *self, PyObject *args, PyObject *kwargs)
57+
{
58+
char *key;
59+
if (!PyArg_ParseTuple(args, "s", &key))
60+
{
61+
return -1;
62+
}
63+
64+
// TODO make it configurable
65+
FModifierKeysState modifier;
66+
67+
// TODO make repeat configurable
68+
FCharacterEvent Event(*UTF8_TO_TCHAR(key), modifier, 0, false);
69+
70+
new(&self->character_event) FCharacterEvent(Event);
71+
new(&self->f_input.input) FInputEvent(Event);
72+
73+
return 0;
74+
}
75+
5676
void ue_python_init_fcharacter_event(PyObject *ue_module)
5777
{
5878

5979
ue_PyFCharacterEventType.tp_base = &ue_PyFInputEventType;
80+
ue_PyFCharacterEventType.tp_init = (initproc)ue_py_fcharacter_event_init;
6081

6182
if (PyType_Ready(&ue_PyFCharacterEventType) < 0)
6283
return;
@@ -72,3 +93,10 @@ PyObject *py_ue_new_fcharacter_event(FCharacterEvent key_event)
7293
new(&ret->f_input.input) FInputEvent(key_event);
7394
return (PyObject *)ret;
7495
}
96+
97+
ue_PyFCharacterEvent *py_ue_is_fcharacter_event(PyObject *obj)
98+
{
99+
if (!PyObject_IsInstance(obj, (PyObject *)&ue_PyFCharacterEventType))
100+
return nullptr;
101+
return (ue_PyFCharacterEvent *)obj;
102+
}

‎Source/UnrealEnginePython/Private/Slate/UEPyFCharacterEvent.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,4 @@ typedef struct {
1515
void ue_python_init_fcharacter_event(PyObject *);
1616

1717
PyObject *py_ue_new_fcharacter_event(FCharacterEvent);
18+
ue_PyFCharacterEvent *py_ue_is_fcharacter_event(PyObject *);

‎Source/UnrealEnginePython/Private/Slate/UEPyFKeyEvent.cpp‎

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,33 @@ static PyTypeObject ue_PyFKeyEventType = {
5555
ue_PyFKeyEvent_methods, /* tp_methods */
5656
};
5757

58+
static int ue_py_fkey_event_init(ue_PyFKeyEvent *self, PyObject *args, PyObject *kwargs)
59+
{
60+
char *key;
61+
if (!PyArg_ParseTuple(args, "s", &key))
62+
{
63+
return -1;
64+
}
65+
66+
FKey InKey(key);
67+
68+
// TODO make it configurable
69+
FModifierKeysState modifier;
70+
71+
// TODO configure repeat
72+
FKeyEvent Event(InKey, modifier, 0, false, 0, 0);
73+
74+
new(&self->key_event) FKeyEvent(Event);
75+
new(&self->f_input.input) FInputEvent(Event);
76+
77+
return 0;
78+
}
79+
80+
5881
void ue_python_init_fkey_event(PyObject *ue_module) {
5982

6083
ue_PyFKeyEventType.tp_base = &ue_PyFInputEventType;
84+
ue_PyFKeyEventType.tp_init = (initproc)ue_py_fkey_event_init;
6185

6286
if (PyType_Ready(&ue_PyFKeyEventType) < 0)
6387
return;
@@ -72,3 +96,10 @@ PyObject *py_ue_new_fkey_event(FKeyEvent key_event) {
7296
new(&ret->f_input.input) FInputEvent(key_event);
7397
return (PyObject *)ret;
7498
}
99+
100+
ue_PyFKeyEvent *py_ue_is_fkey_event(PyObject *obj)
101+
{
102+
if (!PyObject_IsInstance(obj, (PyObject *)&ue_PyFKeyEventType))
103+
return nullptr;
104+
return (ue_PyFKeyEvent *)obj;
105+
}

‎Source/UnrealEnginePython/Private/Slate/UEPyFKeyEvent.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,5 @@ typedef struct {
1515
void ue_python_init_fkey_event(PyObject *);
1616

1717
PyObject *py_ue_new_fkey_event(FKeyEvent);
18+
19+
ue_PyFKeyEvent *py_ue_is_fkey_event(PyObject *);
Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
1+
2+
#include "UnrealEnginePythonPrivatePCH.h"
3+
4+
#include "UEPyFSlateApplication.h"
5+
6+
7+
static PyObject *py_ue_get_average_delta_time(PyObject *cls, PyObject * args)
8+
{
9+
return PyFloat_FromDouble(FSlateApplication::Get().GetAverageDeltaTime());
10+
}
11+
12+
static PyObject *py_ue_get_delta_time(PyObject *cls, PyObject * args)
13+
{
14+
return PyFloat_FromDouble(FSlateApplication::Get().GetDeltaTime());
15+
}
16+
17+
static PyObject *py_ue_goto_line_in_source(PyObject *cls, PyObject * args)
18+
{
19+
char *filename;
20+
int line_number;
21+
if (!PyArg_ParseTuple(args, "si:goto_line_in_source", &filename, &line_number))
22+
{
23+
return nullptr;
24+
}
25+
26+
FSlateApplication::Get().GotoLineInSource(FString(UTF8_TO_TCHAR(filename)), line_number);
27+
28+
Py_RETURN_NONE;
29+
}
30+
31+
static PyObject *py_ue_is_gamepad_attached(PyObject *cls, PyObject * args)
32+
{
33+
bool bAttached = FSlateApplication::Get().IsGamepadAttached();
34+
if (bAttached)
35+
Py_RETURN_TRUE;
36+
Py_RETURN_FALSE;
37+
}
38+
39+
static PyObject *py_ue_is_mouse_attached(PyObject *cls, PyObject * args)
40+
{
41+
bool bAttached = FSlateApplication::Get().IsMouseAttached();
42+
if (bAttached)
43+
Py_RETURN_TRUE;
44+
Py_RETURN_FALSE;
45+
}
46+
47+
static PyObject *py_ue_process_key_down_event(PyObject *cls, PyObject * args)
48+
{
49+
PyObject *py_event;
50+
if (!PyArg_ParseTuple(args, "O:process_key_down_event", &py_event))
51+
{
52+
return nullptr;
53+
}
54+
55+
FKeyEvent *InKeyEvent = ue_py_check_struct<FKeyEvent>(py_event);
56+
if (!InKeyEvent)
57+
{
58+
ue_PyFKeyEvent *py_fkey_event = py_ue_is_fkey_event(py_event);
59+
if (!py_fkey_event)
60+
{
61+
return PyErr_Format(PyExc_Exception, "argument is not a FKeyEvent");
62+
}
63+
InKeyEvent = &py_fkey_event->key_event;
64+
}
65+
66+
if (FSlateApplication::Get().ProcessKeyDownEvent(*InKeyEvent))
67+
Py_RETURN_TRUE;
68+
69+
Py_RETURN_FALSE;
70+
}
71+
72+
static PyObject *py_ue_process_key_char_event(PyObject *cls, PyObject * args)
73+
{
74+
PyObject *py_event;
75+
if (!PyArg_ParseTuple(args, "O:process_key_char_event", &py_event))
76+
{
77+
return nullptr;
78+
}
79+
80+
FCharacterEvent *InCharEvent = ue_py_check_struct<FCharacterEvent>(py_event);
81+
if (!InCharEvent)
82+
{
83+
ue_PyFCharacterEvent *py_fchar_event = py_ue_is_fcharacter_event(py_event);
84+
if (!py_fchar_event)
85+
{
86+
return PyErr_Format(PyExc_Exception, "argument is not a FCharacterEvent");
87+
}
88+
InCharEvent = &py_fchar_event->character_event;
89+
}
90+
91+
if (FSlateApplication::Get().ProcessKeyCharEvent(*InCharEvent))
92+
Py_RETURN_TRUE;
93+
94+
Py_RETURN_FALSE;
95+
}
96+
97+
static PyMethodDef ue_PyFSlateApplication_methods[] = {
98+
{ "get_average_delta_time", (PyCFunction)py_ue_get_average_delta_time, METH_VARARGS | METH_CLASS, "" },
99+
{ "get_delta_time", (PyCFunction)py_ue_get_delta_time, METH_VARARGS | METH_CLASS, "" },
100+
{ "goto_line_in_source", (PyCFunction)py_ue_goto_line_in_source, METH_VARARGS | METH_CLASS, "" },
101+
{ "is_gamepad_attached", (PyCFunction)py_ue_is_gamepad_attached, METH_VARARGS | METH_CLASS, "" },
102+
{ "is_mouse_attached", (PyCFunction)py_ue_is_mouse_attached, METH_VARARGS | METH_CLASS, "" },
103+
{ "process_key_down_event", (PyCFunction)py_ue_process_key_down_event, METH_VARARGS | METH_CLASS, "" },
104+
{ "process_key_char_event", (PyCFunction)py_ue_process_key_char_event, METH_VARARGS | METH_CLASS, "" },
105+
{ NULL } /* Sentinel */
106+
};
107+
108+
109+
static PyTypeObject ue_PyFSlateApplicationType = {
110+
PyVarObject_HEAD_INIT(NULL, 0)
111+
"unreal_engine.FSlateApplication", /* tp_name */
112+
sizeof(ue_PyFSlateApplication), /* tp_basicsize */
113+
0, /* tp_itemsize */
114+
0, /* tp_dealloc */
115+
0, /* tp_print */
116+
0, /* tp_getattr */
117+
0, /* tp_setattr */
118+
0, /* tp_reserved */
119+
0, /* tp_repr */
120+
0, /* tp_as_number */
121+
0, /* tp_as_sequence */
122+
0, /* tp_as_mapping */
123+
0, /* tp_hash */
124+
0, /* tp_call */
125+
0, /* tp_str */
126+
0, /* tp_getattro */
127+
0, /* tp_setattro */
128+
0, /* tp_as_buffer */
129+
Py_TPFLAGS_DEFAULT, /* tp_flags */
130+
"Unreal Engine SlateApplication", /* tp_doc */
131+
0, /* tp_traverse */
132+
0, /* tp_clear */
133+
0, /* tp_richcompare */
134+
0, /* tp_weaklistoffset */
135+
0, /* tp_iter */
136+
0, /* tp_iternext */
137+
ue_PyFSlateApplication_methods, /* tp_methods */
138+
0,
139+
0,
140+
};
141+
142+
static int py_ue_fslate_application_init(ue_PyFSlateApplication *self, PyObject * args)
143+
{
144+
PyErr_SetString(PyExc_Exception, "FSlateApplication is a singleton");
145+
return -1;
146+
}
147+
148+
void ue_python_init_fslate_application(PyObject *ue_module)
149+
{
150+
ue_PyFSlateApplicationType.tp_new = PyType_GenericNew;
151+
ue_PyFSlateApplicationType.tp_init = (initproc)py_ue_fslate_application_init;
152+
153+
if (PyType_Ready(&ue_PyFSlateApplicationType) < 0)
154+
return;
155+
156+
Py_INCREF(&ue_PyFSlateApplicationType);
157+
PyModule_AddObject(ue_module, "FSlateApplication", (PyObject *)&ue_PyFSlateApplicationType);
158+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#pragma once
2+
3+
#include "UnrealEnginePython.h"
4+
5+
typedef struct
6+
{
7+
PyObject_HEAD
8+
/* Type-specific fields go here. */
9+
} ue_PyFSlateApplication;
10+
11+
void ue_python_init_fslate_application(PyObject *);

‎Source/UnrealEnginePython/Private/UEPyModule.cpp‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -475,6 +475,10 @@ static PyMethodDef ue_PyUObject_methods[] = {
475475
{ "get_path_name", (PyCFunction)py_ue_get_path_name, METH_VARARGS, "" },
476476
{ "get_full_name", (PyCFunction)py_ue_get_full_name, METH_VARARGS, "" },
477477

478+
#if WITH_EDITOR
479+
{ "import_custom_properties", (PyCFunction)py_ue_import_custom_properties, METH_VARARGS, "" },
480+
#endif
481+
478482
#if ENGINE_MINOR_VERSION >= 15
479483
{ "can_modify", (PyCFunction)py_ue_can_modify, METH_VARARGS, "" },
480484
#endif
@@ -1941,6 +1945,8 @@ void unreal_engine_init_py_module()
19411945

19421946
ue_python_init_iconsole_manager(new_unreal_engine_module);
19431947

1948+
ue_python_init_fslate_application(new_unreal_engine_module);
1949+
19441950
#if WITH_EDITOR
19451951
ue_python_init_icollection_manager(new_unreal_engine_module);
19461952
#endif

‎Source/UnrealEnginePython/Private/UObject/UEPyObject.cpp‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include "Runtime/AssetRegistry/Public/AssetRegistryModule.h"
88
#include "ObjectTools.h"
99
#include "UnrealEd.h"
10+
#include "Runtime/Core/Public/HAL/FeedbackContextAnsi.h"
1011
#endif
1112

1213
PyObject *py_ue_get_class(ue_PyUObject * self, PyObject * args)
@@ -1737,6 +1738,31 @@ PyObject *py_ue_save_package(ue_PyUObject * self, PyObject * args)
17371738
return PyErr_Format(PyExc_Exception, "unable to save package");
17381739
}
17391740

1741+
PyObject *py_ue_import_custom_properties(ue_PyUObject * self, PyObject * args)
1742+
{
1743+
ue_py_check(self);
1744+
1745+
char *t3d;
1746+
1747+
if (!PyArg_ParseTuple(args, "s:import_custom_properties", &t3d))
1748+
{
1749+
return nullptr;
1750+
}
1751+
1752+
FFeedbackContextAnsi context;
1753+
self->ue_object->ImportCustomProperties(UTF8_TO_TCHAR(t3d), &context);
1754+
1755+
TArray<FString> errors;
1756+
context.GetErrors(errors);
1757+
1758+
if (errors.Num() > 0)
1759+
{
1760+
return PyErr_Format(PyExc_Exception, "%s", TCHAR_TO_UTF8(*errors[0]));
1761+
}
1762+
1763+
Py_RETURN_NONE;
1764+
}
1765+
17401766
PyObject *py_ue_asset_can_reimport(ue_PyUObject * self, PyObject * args)
17411767
{
17421768

‎Source/UnrealEnginePython/Private/UObject/UEPyObject.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,8 @@ PyObject *py_ue_asset_reimport(ue_PyUObject *, PyObject *);
8282
PyObject *py_ue_get_metadata(ue_PyUObject *, PyObject *);
8383
PyObject *py_ue_set_metadata(ue_PyUObject *, PyObject *);
8484
PyObject *py_ue_has_metadata(ue_PyUObject *, PyObject *);
85+
86+
PyObject *py_ue_import_custom_properties(ue_PyUObject *, PyObject *);
8587
#endif
8688

8789
PyObject *py_ue_get_thumbnail(ue_PyUObject *, PyObject *);

‎Source/UnrealEnginePython/Private/UnrealEnginePythonPrivatePCH.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@
7373
#include "Slate/UEPySlate.h"
7474
#include "Http/UEPyIHttp.h"
7575
#include "ConsoleManager/UEPyIConsoleManager.h"
76+
#include "SlateApplication/UEPyFSlateApplication.h"
7677
#include "Voice/UEPyIVoiceCapture.h"
7778

7879

0 commit comments

Comments
 (0)