-
Notifications
You must be signed in to change notification settings - Fork 763
Expand file tree
/
Copy pathUEPyFSlateIcon.cpp
More file actions
141 lines (121 loc) · 4.43 KB
/
UEPyFSlateIcon.cpp
File metadata and controls
141 lines (121 loc) · 4.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
#include "UEPyFSlateIcon.h"
#include "Engine/Texture2D.h"
#include "Engine/Engine.h"
static PyObject *py_ue_fslate_icon_get_icon(ue_PyFSlateIcon *self, PyObject * args)
{
return py_ue_new_uscriptstruct(FSlateBrush::StaticStruct(), (uint8*)self->icon.GetIcon());
}
static PyMethodDef ue_PyFSlateIcon_methods[] = {
{ "get_icon", (PyCFunction)py_ue_fslate_icon_get_icon, METH_VARARGS, "" },
{ NULL } /* Sentinel */
};
static PyObject *ue_PyFSlateIcon_str(ue_PyFSlateIcon *self)
{
return PyUnicode_FromFormat("<unreal_engine.SlateIcon {'name': %s}>",
TCHAR_TO_UTF8(*self->icon.GetStyleName().ToString()));
}
static PyTypeObject ue_PyFSlateIconType = {
PyVarObject_HEAD_INIT(NULL, 0)
"unreal_engine.FSlateIcon", /* tp_name */
sizeof(ue_PyFSlateIcon), /* tp_basicsize */
0, /* tp_itemsize */
0, /* tp_dealloc */
0, /* tp_print */
0, /* tp_getattr */
0, /* tp_setattr */
0, /* tp_reserved */
0, /* tp_repr */
0, /* tp_as_number */
0, /* tp_as_sequence */
0, /* tp_as_mapping */
0, /* tp_hash */
0, /* tp_call */
(reprfunc)ue_PyFSlateIcon_str, /* tp_str */
0, /* tp_getattro */
0, /* tp_setattro */
0, /* tp_as_buffer */
Py_TPFLAGS_DEFAULT, /* tp_flags */
"Unreal Engine FSlateIcon", /* tp_doc */
0, /* tp_traverse */
0, /* tp_clear */
0, /* tp_richcompare */
0, /* tp_weaklistoffset */
0, /* tp_iter */
0, /* tp_iternext */
ue_PyFSlateIcon_methods, /* tp_methods */
};
static int ue_py_fslate_icon_init(ue_PyFSlateIcon *self, PyObject *args, PyObject *kwargs)
{
char *style_set = nullptr;
char *style = nullptr;
if (!PyArg_ParseTuple(args, "|ss", &style_set, &style))
{
return -1;
}
if (style_set)
{
if (!style)
{
PyErr_SetString(PyExc_ValueError, "you have not specified as style name");
return -1;
}
ISlateStyle const* const foundStyleSet = FSlateStyleRegistry::FindSlateStyle(FName(style_set));
const FSlateBrush * iconBrush = foundStyleSet->GetBrush(style);
FString Path = iconBrush->GetResourceName().ToString();
// Hack to load in the texture resource object of the icon brush in case it hasn't been already loaded
if (!Path.IsEmpty() && iconBrush->GetResourceObject() == nullptr)
{
if (Path.StartsWith(FSlateBrush::UTextureIdentifier()))
{
Path = Path.RightChop(FSlateBrush::UTextureIdentifier().Len());
}
UObject* TextureObject = LoadObject<UTexture2D>(NULL, *Path, NULL, LOAD_None, NULL);
FSlateBrush* Brush = const_cast<FSlateBrush*>(iconBrush);
// Set the texture object to a default texture to prevent constant loading of missing textures
if (!TextureObject)
{
UE_LOG(LogSlate, Warning, TEXT("Error loading loading UTexture from path: %s not found"), *Path);
if (GEngine)
{
TextureObject = GEngine->DefaultTexture;
}
}
else
{
// We do this here because this deprecated system of loading textures will not report references and we dont want the Slate RHI resource manager to manage references
TextureObject->AddToRoot();
}
if (TextureObject)
{
Brush->SetResourceObject(TextureObject);
}
}
new(&self->icon) FSlateIcon(FName(style_set), FName(style));
}
else
{
new(&self->icon) FSlateIcon();
}
return 0;
}
void ue_python_init_fslate_icon(PyObject *ue_module)
{
ue_PyFSlateIconType.tp_new = PyType_GenericNew;
ue_PyFSlateIconType.tp_init = (initproc)ue_py_fslate_icon_init;
if (PyType_Ready(&ue_PyFSlateIconType) < 0)
return;
Py_INCREF(&ue_PyFSlateIconType);
PyModule_AddObject(ue_module, "FSlateIcon", (PyObject *)&ue_PyFSlateIconType);
}
ue_PyFSlateIcon *py_ue_new_fslate_icon(const FSlateIcon slate_icon)
{
ue_PyFSlateIcon *ret = (ue_PyFSlateIcon *)PyObject_New(ue_PyFSlateIcon, &ue_PyFSlateIconType);
ret->icon = slate_icon;
return ret;
}
ue_PyFSlateIcon *py_ue_is_fslate_icon(PyObject *obj)
{
if (!PyObject_IsInstance(obj, (PyObject *)&ue_PyFSlateIconType))
return nullptr;
return (ue_PyFSlateIcon *)obj;
}