Skip to content

Commit 3ed2937

Browse files
author
Roberto De Ioris
authored
Merge pull request 20tab#408 from HughMacdonald/fslow_task
FSlowTask support
2 parents 0a95537 + e93afc9 commit 3ed2937

4 files changed

Lines changed: 240 additions & 0 deletions

File tree

‎Source/UnrealEnginePython/Private/UEPyModule.cpp‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@
8282
#include "UEPyUScriptStruct.h"
8383

8484
#if WITH_EDITOR
85+
#include "Wrappers/UEPyFSlowTask.h"
8586
#include "Wrappers/UEPyFAssetData.h"
8687
#include "Wrappers/UEPyFARFilter.h"
8788
#include "Wrappers/UEPyFRawMesh.h"
@@ -439,6 +440,7 @@ static PyMethodDef unreal_engine_methods[] = {
439440
{ "clipboard_copy", py_unreal_engine_clipboard_copy, METH_VARARGS, "" },
440441
{ "clipboard_paste", py_unreal_engine_clipboard_paste, METH_VARARGS, "" },
441442

443+
442444
#pragma warning(suppress: 4191)
443445
{ "copy_properties_for_unrelated_objects", (PyCFunction)py_unreal_engine_copy_properties_for_unrelated_objects, METH_VARARGS | METH_KEYWORDS, "" },
444446

@@ -1517,6 +1519,7 @@ void unreal_engine_init_py_module()
15171519

15181520

15191521
#if WITH_EDITOR
1522+
ue_python_init_fslowtask(new_unreal_engine_module);
15201523
ue_python_init_swidget(new_unreal_engine_module);
15211524
ue_python_init_farfilter(new_unreal_engine_module);
15221525
ue_python_init_fassetdata(new_unreal_engine_module);
Lines changed: 192 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,192 @@
1+
#include "UEPyFSlowTask.h"
2+
3+
#include "Misc/FeedbackContext.h"
4+
5+
#if WITH_EDITOR
6+
7+
static PyObject *py_ue_fslowtask_initialize(ue_PyFSlowTask *self, PyObject * args)
8+
{
9+
self->slowtask.Initialize();
10+
11+
Py_RETURN_NONE;
12+
}
13+
14+
static PyObject *py_ue_fslowtask_destroy(ue_PyFSlowTask *self, PyObject * args)
15+
{
16+
self->slowtask.Destroy();
17+
18+
Py_RETURN_NONE;
19+
}
20+
21+
static PyObject *py_ue_fslowtask_make_dialog_delayed(ue_PyFSlowTask *self, PyObject * args)
22+
{
23+
float threshold;
24+
PyObject *py_show_cancel_button = nullptr;
25+
PyObject *py_allow_in_pie = nullptr;
26+
if(!PyArg_ParseTuple(args, "f|OO:make_dialog_delayed", &threshold, &py_show_cancel_button, &py_allow_in_pie))
27+
{
28+
return nullptr;
29+
}
30+
31+
bool show_cancel_button = false;
32+
if (nullptr != py_show_cancel_button && PyObject_IsTrue(py_show_cancel_button))
33+
{
34+
show_cancel_button = true;
35+
}
36+
37+
bool allow_in_pie = false;
38+
if (nullptr != py_allow_in_pie && PyObject_IsTrue(py_allow_in_pie))
39+
{
40+
allow_in_pie = true;
41+
}
42+
43+
self->slowtask.MakeDialogDelayed(threshold, show_cancel_button, allow_in_pie);
44+
45+
Py_RETURN_NONE;
46+
}
47+
48+
static PyObject *py_ue_fslowtask_make_dialog(ue_PyFSlowTask *self, PyObject * args)
49+
{
50+
PyObject *py_show_cancel_button = nullptr;
51+
PyObject *py_allow_in_pie = nullptr;
52+
if(!PyArg_ParseTuple(args, "|OO:make_dialog", &py_show_cancel_button, &py_allow_in_pie))
53+
{
54+
return nullptr;
55+
}
56+
57+
bool show_cancel_button = false;
58+
if (nullptr != py_show_cancel_button && PyObject_IsTrue(py_show_cancel_button))
59+
{
60+
show_cancel_button = true;
61+
}
62+
63+
bool allow_in_pie = false;
64+
if (nullptr != py_allow_in_pie && PyObject_IsTrue(py_allow_in_pie))
65+
{
66+
allow_in_pie = true;
67+
}
68+
69+
self->slowtask.MakeDialog(show_cancel_button, allow_in_pie);
70+
71+
Py_RETURN_NONE;
72+
}
73+
74+
static PyObject *py_ue_fslowtask_enter_progress_frame(ue_PyFSlowTask *self, PyObject * args)
75+
{
76+
float expected_work_this_frame = 1.0;
77+
char *text = "";
78+
if(!PyArg_ParseTuple(args, "|fs:enter_progress_frame", &expected_work_this_frame, &text))
79+
{
80+
return nullptr;
81+
}
82+
83+
self->slowtask.EnterProgressFrame(expected_work_this_frame, FText::FromString(UTF8_TO_TCHAR(text)));
84+
85+
Py_RETURN_NONE;
86+
}
87+
88+
static PyObject *py_ue_fslowtask_get_current_message(ue_PyFSlowTask *self, PyObject * args)
89+
{
90+
FText current_message = self->slowtask.GetCurrentMessage();
91+
return PyUnicode_FromString(TCHAR_TO_UTF8(*current_message.ToString()));
92+
}
93+
94+
static PyObject *py_ue_fslowtask_received_user_cancel(ue_PyFSlowTask *self, PyObject * args )
95+
{
96+
if(GWarn->ReceivedUserCancel())
97+
{
98+
Py_INCREF(Py_True);
99+
return Py_True;
100+
}
101+
Py_INCREF(Py_False);
102+
return Py_False;
103+
}
104+
105+
static PyMethodDef ue_PyFSlowTask_methods[] = {
106+
{ "initialize", (PyCFunction)py_ue_fslowtask_initialize, METH_VARARGS, "" },
107+
{ "destroy", (PyCFunction)py_ue_fslowtask_destroy, METH_VARARGS, "" },
108+
{ "make_dialog_delayed", (PyCFunction)py_ue_fslowtask_make_dialog_delayed, METH_VARARGS, "" },
109+
{ "make_dialog", (PyCFunction)py_ue_fslowtask_make_dialog, METH_VARARGS, "" },
110+
{ "enter_progress_frame", (PyCFunction)py_ue_fslowtask_enter_progress_frame, METH_VARARGS, "" },
111+
{ "get_current_message", (PyCFunction)py_ue_fslowtask_get_current_message, METH_VARARGS, "" },
112+
{ "received_user_cancel", (PyCFunction)py_ue_fslowtask_received_user_cancel, METH_VARARGS, "" },
113+
{ NULL } /* Sentinel */
114+
};
115+
116+
static PyTypeObject ue_PyFSlowTaskType = {
117+
PyVarObject_HEAD_INIT(NULL, 0)
118+
"unreal_engine.FSlowTask", /* tp_name */
119+
sizeof(ue_PyFSlowTask), /* tp_basicsize */
120+
0, /* tp_itemsize */
121+
0, /* tp_dealloc */
122+
0, /* tp_print */
123+
0, /* tp_getattr */
124+
0, /* tp_setattr */
125+
0, /* tp_reserved */
126+
0, /* tp_repr */
127+
0, /* tp_as_number */
128+
0, /* tp_as_sequence */
129+
0, /* tp_as_mapping */
130+
0, /* tp_hash */
131+
0, /* tp_call */
132+
0, /* tp_str */
133+
0, /* tp_getattro */
134+
0, /* tp_setattro */
135+
0, /* tp_as_buffer */
136+
#if PY_MAJOR_VERSION < 3
137+
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_CHECKTYPES, /* tp_flags */
138+
#else
139+
Py_TPFLAGS_DEFAULT, /* tp_flags */
140+
#endif
141+
"Unreal Engine FSlowTask", /* tp_doc */
142+
0, /* tp_traverse */
143+
0, /* tp_clear */
144+
0, /* tp_richcompare */
145+
0, /* tp_weaklistoffset */
146+
0, /* tp_iter */
147+
0, /* tp_iternext */
148+
ue_PyFSlowTask_methods, /* tp_methods */
149+
0,
150+
0,
151+
};
152+
153+
static int py_ue_fslowtask_init(ue_PyFSlowTask *self, PyObject * args)
154+
{
155+
float amount_of_work;
156+
char *default_message = "";
157+
PyObject *py_enabled = nullptr;
158+
if(!PyArg_ParseTuple(args, "f|sO:__init__", &amount_of_work, &default_message, &py_enabled))
159+
{
160+
return -1;
161+
}
162+
163+
bool enabled = true;
164+
if (nullptr != py_enabled && !PyObject_IsTrue(py_enabled))
165+
{
166+
enabled = false;
167+
}
168+
169+
new(&self->slowtask) FSlowTask(amount_of_work, FText::FromString(UTF8_TO_TCHAR(default_message)), enabled);
170+
return 0;
171+
}
172+
173+
void ue_python_init_fslowtask(PyObject *ue_module)
174+
{
175+
ue_PyFSlowTaskType.tp_new = PyType_GenericNew;
176+
ue_PyFSlowTaskType.tp_init = (initproc)py_ue_fslowtask_init;
177+
178+
if (PyType_Ready(&ue_PyFSlowTaskType) < 0)
179+
return;
180+
181+
Py_INCREF(&ue_PyFSlowTaskType);
182+
PyModule_AddObject(ue_module, "FSlowTask", (PyObject *)&ue_PyFSlowTaskType);
183+
}
184+
185+
ue_PyFSlowTask *py_ue_is_fslowtask(PyObject *obj)
186+
{
187+
if (!PyObject_IsInstance(obj, (PyObject *)&ue_PyFSlowTaskType))
188+
return nullptr;
189+
return (ue_PyFSlowTask *)obj;
190+
}
191+
192+
#endif
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#pragma once
2+
#include "UnrealEnginePython.h"
3+
4+
#if WITH_EDITOR
5+
6+
struct ue_PyFSlowTask {
7+
PyObject_HEAD
8+
/* Type-specific fields go here. */
9+
10+
FSlowTask slowtask;
11+
};
12+
13+
void ue_python_init_fslowtask(PyObject *);
14+
15+
ue_PyFSlowTask *py_ue_is_fslowtask(PyObject *);
16+
17+
#endif

‎examples/slow_task.py‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
from unreal_engine import FSlowTask
2+
import time
3+
4+
# Create an FSlowTask object, defining the amount of work that
5+
# will be done, and the initial message.
6+
t = FSlowTask(10, "Doing Something")
7+
t.initialize()
8+
9+
# Make the dialog, and include a Cancel button (default is not to
10+
# allow a cancel button).
11+
t.make_dialog(True)
12+
13+
time.sleep(1)
14+
15+
for i in range(10) :
16+
# Update the progress bar. Note that the first argument is the
17+
# amount of work to be done this frame, not the overall work
18+
# done so far.
19+
t.enter_progress_frame(1, "Progress Position : {}".format(i))
20+
time.sleep(0.2)
21+
22+
# If there was a cancel button included, we can check if it was
23+
# pressed.
24+
if t.received_user_cancel():
25+
print("Cancelled")
26+
break
27+
28+
t.destroy()

0 commit comments

Comments
 (0)