|
| 1 | +#include "UnrealEnginePythonPrivatePCH.h" |
| 2 | +#include "PyUserWidget.h" |
| 3 | + |
| 4 | +#include "PythonDelegate.h" |
| 5 | + |
| 6 | +bool UPyUserWidget::Initialize() |
| 7 | +{ |
| 8 | + if (!Super::Initialize()) |
| 9 | + return false; |
| 10 | + |
| 11 | + if (PythonModule.IsEmpty()) |
| 12 | + return false; |
| 13 | + |
| 14 | + FScopePythonGIL gil; |
| 15 | + |
| 16 | + py_uobject = ue_get_python_wrapper(this); |
| 17 | + if (!py_uobject) { |
| 18 | + unreal_engine_py_log_error(); |
| 19 | + return false; |
| 20 | + } |
| 21 | + |
| 22 | + PyObject *py_user_widget_module = PyImport_ImportModule(TCHAR_TO_UTF8(*PythonModule)); |
| 23 | + if (!py_user_widget_module) { |
| 24 | + unreal_engine_py_log_error(); |
| 25 | + return false; |
| 26 | + } |
| 27 | + |
| 28 | +#if WITH_EDITOR |
| 29 | + // todo implement autoreload with a dictionary of module timestamps |
| 30 | + py_user_widget_module = PyImport_ReloadModule(py_user_widget_module); |
| 31 | + if (!py_user_widget_module) { |
| 32 | + unreal_engine_py_log_error(); |
| 33 | + return false; |
| 34 | + } |
| 35 | +#endif |
| 36 | + |
| 37 | + if (PythonClass.IsEmpty()) |
| 38 | + return false; |
| 39 | + |
| 40 | + PyObject *py_user_widget_module_dict = PyModule_GetDict(py_user_widget_module); |
| 41 | + PyObject *py_user_widget_class = PyDict_GetItemString(py_user_widget_module_dict, TCHAR_TO_UTF8(*PythonClass)); |
| 42 | + |
| 43 | + if (!py_user_widget_class) { |
| 44 | + UE_LOG(LogPython, Error, TEXT("Unable to find class %s in module %s"), *PythonClass, *PythonModule); |
| 45 | + return false; |
| 46 | + } |
| 47 | + |
| 48 | + py_user_widget_instance = PyObject_CallObject(py_user_widget_class, NULL); |
| 49 | + if (!py_user_widget_instance) { |
| 50 | + unreal_engine_py_log_error(); |
| 51 | + return false; |
| 52 | + } |
| 53 | + |
| 54 | + py_uobject->py_proxy = py_user_widget_instance; |
| 55 | + |
| 56 | + PyObject_SetAttrString(py_user_widget_instance, (char*)"uobject", (PyObject *)py_uobject); |
| 57 | + |
| 58 | + return true; |
| 59 | +} |
| 60 | + |
| 61 | + |
| 62 | +void UPyUserWidget::NativeConstruct() |
| 63 | +{ |
| 64 | + Super::NativeConstruct(); |
| 65 | + |
| 66 | + if (!py_user_widget_instance) |
| 67 | + return; |
| 68 | + |
| 69 | + FScopePythonGIL gil; |
| 70 | + |
| 71 | + if (!PyObject_HasAttrString(py_user_widget_instance, (char *)"construct")) |
| 72 | + return; |
| 73 | + |
| 74 | + PyObject *bp_ret = PyObject_CallMethod(py_user_widget_instance, (char *)"construct", NULL); |
| 75 | + if (!bp_ret) { |
| 76 | + unreal_engine_py_log_error(); |
| 77 | + return; |
| 78 | + } |
| 79 | + Py_DECREF(bp_ret); |
| 80 | +} |
| 81 | + |
| 82 | +void UPyUserWidget::NativeDestruct() |
| 83 | +{ |
| 84 | + Super::NativeDestruct(); |
| 85 | + |
| 86 | + if (!py_user_widget_instance) |
| 87 | + return; |
| 88 | + |
| 89 | + FScopePythonGIL gil; |
| 90 | + |
| 91 | + if (!PyObject_HasAttrString(py_user_widget_instance, (char *)"destruct")) |
| 92 | + return; |
| 93 | + |
| 94 | + PyObject *bp_ret = PyObject_CallMethod(py_user_widget_instance, (char *)"destruct", NULL); |
| 95 | + if (!bp_ret) { |
| 96 | + unreal_engine_py_log_error(); |
| 97 | + return; |
| 98 | + } |
| 99 | + Py_DECREF(bp_ret); |
| 100 | +} |
| 101 | + |
| 102 | +// Called every frame |
| 103 | +void UPyUserWidget::NativeTick(const FGeometry & MyGeometry, float InDeltaTime) |
| 104 | +{ |
| 105 | + Super::NativeTick(MyGeometry, InDeltaTime); |
| 106 | + |
| 107 | + if (!py_user_widget_instance) |
| 108 | + return; |
| 109 | + |
| 110 | + FScopePythonGIL gil; |
| 111 | + |
| 112 | + if (!PyObject_HasAttrString(py_user_widget_instance, (char *)"tick")) |
| 113 | + return; |
| 114 | + |
| 115 | + PyObject *ret = PyObject_CallMethod(py_user_widget_instance, (char *)"tick", (char *)"Of", py_ue_new_uscriptstruct(FGeometry::StaticStruct(), (uint8 *) &MyGeometry), InDeltaTime); |
| 116 | + if (!ret) { |
| 117 | + unreal_engine_py_log_error(); |
| 118 | + return; |
| 119 | + } |
| 120 | + Py_DECREF(ret); |
| 121 | + |
| 122 | +} |
| 123 | + |
| 124 | +UPyUserWidget::~UPyUserWidget() |
| 125 | +{ |
| 126 | + FScopePythonGIL gil; |
| 127 | + |
| 128 | + ue_pydelegates_cleanup(py_uobject); |
| 129 | + |
| 130 | +#if defined(UEPY_MEMORY_DEBUG) |
| 131 | + if (py_user_widget_instance && py_user_widget_instance->ob_refcnt != 1) { |
| 132 | + UE_LOG(LogPython, Error, TEXT("Inconsistent Python UUserWidget wrapper refcnt = %d"), py_user_widget_instance->ob_refcnt); |
| 133 | + } |
| 134 | +#endif |
| 135 | + Py_XDECREF(py_user_widget_instance); |
| 136 | + |
| 137 | +#if defined(UEPY_MEMORY_DEBUG) |
| 138 | + UE_LOG(LogPython, Warning, TEXT("Python UUserWidget %p (mapped to %p) wrapper XDECREF'ed"), this, py_uobject ? py_uobject->ue_object : nullptr); |
| 139 | +#endif |
| 140 | + |
| 141 | + // this could trigger the distruction of the python/uobject mapper |
| 142 | + Py_XDECREF(py_uobject); |
| 143 | +} |
0 commit comments