Skip to content

Commit ccd04ce

Browse files
author
Roberto De Ioris
committed
improved color management for drawing api
1 parent 3d3239b commit ccd04ce

6 files changed

Lines changed: 205 additions & 106 deletions

File tree

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

Lines changed: 55 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44

55
static FSlateBrush global_simple_brush;
66

7-
static PyObject *py_ue_fpaint_context_draw_line(ue_PyFPaintContext *self, PyObject * args) {
7+
static PyObject *py_ue_fpaint_context_draw_line(ue_PyFPaintContext *self, PyObject * args)
8+
{
89
float x1, y1, x2, y2;
910
PyObject *py_linear_color = nullptr;
1011
PyObject *py_antialias = nullptr;
@@ -15,12 +16,11 @@ static PyObject *py_ue_fpaint_context_draw_line(ue_PyFPaintContext *self, PyObje
1516

1617
FLinearColor tint = FLinearColor::White;
1718

18-
if (py_linear_color) {
19-
if (ue_PyFLinearColor *linear_color = py_ue_is_flinearcolor(py_linear_color)) {
20-
tint = linear_color->color;
21-
}
22-
else {
23-
return PyErr_Format(PyExc_Exception, "argument is not a FlinearColor");
19+
if (py_linear_color)
20+
{
21+
if (!py_ue_get_flinearcolor(py_linear_color, tint))
22+
{
23+
return PyErr_Format(PyExc_Exception, "argument is not a FLinearColor or FColor.");
2424
}
2525
}
2626

@@ -43,7 +43,8 @@ static PyObject *py_ue_fpaint_context_draw_line(ue_PyFPaintContext *self, PyObje
4343
Py_RETURN_NONE;
4444
}
4545

46-
static PyObject *py_ue_fpaint_context_draw_box(ue_PyFPaintContext *self, PyObject * args) {
46+
static PyObject *py_ue_fpaint_context_draw_box(ue_PyFPaintContext *self, PyObject * args)
47+
{
4748
float x, y, w, h;
4849

4950
PyObject *py_brush = nullptr;
@@ -54,18 +55,18 @@ static PyObject *py_ue_fpaint_context_draw_box(ue_PyFPaintContext *self, PyObjec
5455

5556
FLinearColor tint = FLinearColor::White;
5657

57-
if (py_linear_color) {
58-
if (ue_PyFLinearColor *linear_color = py_ue_is_flinearcolor(py_linear_color)) {
59-
tint = linear_color->color;
60-
}
61-
else {
62-
return PyErr_Format(PyExc_Exception, "argument is not a FlinearColor");
58+
if (py_linear_color)
59+
{
60+
if (!py_ue_get_flinearcolor(py_linear_color, tint))
61+
{
62+
return PyErr_Format(PyExc_Exception, "argument is not a FLinearColor or FColor.");
6363
}
6464
}
6565

6666
FSlateBrush *brush = &global_simple_brush;
6767

68-
if (py_brush) {
68+
if (py_brush)
69+
{
6970
FSlateBrush *custom_brush = ue_py_check_struct<FSlateBrush>(py_brush);
7071
if (!custom_brush)
7172
return PyErr_Format(PyExc_Exception, "argument is not a FSlateBrush");
@@ -87,7 +88,8 @@ static PyObject *py_ue_fpaint_context_draw_box(ue_PyFPaintContext *self, PyObjec
8788
}
8889

8990

90-
static PyObject *py_ue_fpaint_context_draw_text(ue_PyFPaintContext *self, PyObject * args) {
91+
static PyObject *py_ue_fpaint_context_draw_text(ue_PyFPaintContext *self, PyObject * args)
92+
{
9193
float x, y;
9294
char *text;
9395
float size = 0;
@@ -100,21 +102,23 @@ static PyObject *py_ue_fpaint_context_draw_text(ue_PyFPaintContext *self, PyObje
100102
FLinearColor tint = FLinearColor::White;
101103
FSlateFontInfo font = FCoreStyle::Get().GetWidgetStyle<FTextBlockStyle>("NormalText").Font;
102104

103-
if (py_linear_color) {
104-
if (ue_PyFLinearColor *linear_color = py_ue_is_flinearcolor(py_linear_color)) {
105-
tint = linear_color->color;
106-
}
107-
else {
108-
return PyErr_Format(PyExc_Exception, "argument is not a FlinearColor");
105+
if (py_linear_color)
106+
{
107+
if (!py_ue_get_flinearcolor(py_linear_color, tint))
108+
{
109+
return PyErr_Format(PyExc_Exception, "argument is not a FLinearColor or FColor.");
109110
}
110111
}
111112

112-
if (py_font) {
113+
if (py_font)
114+
{
113115
FSlateFontInfo *font_struct = ue_py_check_struct<FSlateFontInfo>(py_font);
114-
if (font_struct) {
116+
if (font_struct)
117+
{
115118
font = *font_struct;
116119
}
117-
else {
120+
else
121+
{
118122
return PyErr_Format(PyExc_Exception, "argument is not a SlateFontInfo USTRUCT");
119123
}
120124
}
@@ -140,7 +144,8 @@ static PyObject *py_ue_fpaint_context_draw_text(ue_PyFPaintContext *self, PyObje
140144
Py_RETURN_NONE;
141145
}
142146

143-
static PyObject *py_ue_fpaint_context_draw_spline(ue_PyFPaintContext *self, PyObject * args) {
147+
static PyObject *py_ue_fpaint_context_draw_spline(ue_PyFPaintContext *self, PyObject * args)
148+
{
144149
float x1, y1, dx1, dy1;
145150
float x2, y2, dx2, dy2;
146151
PyObject *py_linear_color = nullptr;
@@ -152,12 +157,11 @@ static PyObject *py_ue_fpaint_context_draw_spline(ue_PyFPaintContext *self, PyOb
152157

153158
FLinearColor tint = FLinearColor::White;
154159

155-
if (py_linear_color) {
156-
if (ue_PyFLinearColor *linear_color = py_ue_is_flinearcolor(py_linear_color)) {
157-
tint = linear_color->color;
158-
}
159-
else {
160-
return PyErr_Format(PyExc_Exception, "argument is not a FlinearColor");
160+
if (py_linear_color)
161+
{
162+
if (!py_ue_get_flinearcolor(py_linear_color, tint))
163+
{
164+
return PyErr_Format(PyExc_Exception, "argument is not a FLinearColor or FColor.");
161165
}
162166
}
163167

@@ -179,7 +183,8 @@ static PyObject *py_ue_fpaint_context_draw_spline(ue_PyFPaintContext *self, PyOb
179183
}
180184

181185

182-
static PyObject *py_ue_fpaint_context_draw_lines(ue_PyFPaintContext *self, PyObject * args) {
186+
static PyObject *py_ue_fpaint_context_draw_lines(ue_PyFPaintContext *self, PyObject * args)
187+
{
183188
PyObject *py_points;
184189
PyObject *py_linear_color = nullptr;
185190
PyObject *py_antialias = nullptr;
@@ -194,14 +199,17 @@ static PyObject *py_ue_fpaint_context_draw_lines(ue_PyFPaintContext *self, PyObj
194199
if (!py_iter)
195200
return PyErr_Format(PyExc_Exception, "argument is not an iterable of tuples");
196201

197-
while (PyObject *py_item = PyIter_Next(py_iter)) {
198-
if (!PyTuple_Check(py_item)) {
202+
while (PyObject *py_item = PyIter_Next(py_iter))
203+
{
204+
if (!PyTuple_Check(py_item))
205+
{
199206
return PyErr_Format(PyExc_Exception, "iterable item is not a tuple");
200207
Py_DECREF(py_iter);
201208
}
202209

203210
float x, y;
204-
if (!PyArg_ParseTuple(py_item, "ff", &x, &y)) {
211+
if (!PyArg_ParseTuple(py_item, "ff", &x, &y))
212+
{
205213
Py_DECREF(py_iter);
206214
return nullptr;
207215
}
@@ -211,12 +219,11 @@ static PyObject *py_ue_fpaint_context_draw_lines(ue_PyFPaintContext *self, PyObj
211219

212220
FLinearColor tint = FLinearColor::White;
213221

214-
if (py_linear_color) {
215-
if (ue_PyFLinearColor *linear_color = py_ue_is_flinearcolor(py_linear_color)) {
216-
tint = linear_color->color;
217-
}
218-
else {
219-
return PyErr_Format(PyExc_Exception, "argument is not a FlinearColor");
222+
if (py_linear_color)
223+
{
224+
if (!py_ue_get_flinearcolor(py_linear_color, tint))
225+
{
226+
return PyErr_Format(PyExc_Exception, "argument is not a FLinearColor or FColor.");
220227
}
221228
}
222229

@@ -235,7 +242,8 @@ static PyObject *py_ue_fpaint_context_draw_lines(ue_PyFPaintContext *self, PyObj
235242
Py_RETURN_NONE;
236243
}
237244

238-
static PyObject *py_ue_fpaint_context_get_geometry(ue_PyFPaintContext *self, PyObject * args) {
245+
static PyObject *py_ue_fpaint_context_get_geometry(ue_PyFPaintContext *self, PyObject * args)
246+
{
239247
return py_ue_new_fgeometry(self->paint_context.AllottedGeometry);
240248
}
241249

@@ -286,7 +294,8 @@ static PyTypeObject ue_PyFPaintContextType = {
286294
ue_PyFPaintContext_methods, /* tp_methods */
287295
};
288296

289-
void ue_python_init_fpaint_context(PyObject *ue_module) {
297+
void ue_python_init_fpaint_context(PyObject *ue_module)
298+
{
290299
ue_PyFPaintContextType.tp_new = PyType_GenericNew;
291300

292301
if (PyType_Ready(&ue_PyFPaintContextType) < 0)
@@ -296,7 +305,8 @@ void ue_python_init_fpaint_context(PyObject *ue_module) {
296305
PyModule_AddObject(ue_module, "FPaintContext", (PyObject *)&ue_PyFPaintContextType);
297306
}
298307

299-
PyObject *py_ue_new_fpaint_context(FPaintContext paint_context) {
308+
PyObject *py_ue_new_fpaint_context(FPaintContext paint_context)
309+
{
300310
ue_PyFPaintContext *ret = (ue_PyFPaintContext *)PyObject_New(ue_PyFPaintContext, &ue_PyFPaintContextType);
301311
ret->paint_context = paint_context;
302312
return (PyObject *)ret;

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

Lines changed: 50 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,81 +1,94 @@
11
#include "UnrealEnginePythonPrivatePCH.h"
22
#include "Runtime/Engine/Classes/GameFramework/HUD.h"
33

4-
PyObject *py_ue_hud_draw_2d_line(ue_PyUObject *self, PyObject * args) {
4+
PyObject *py_ue_hud_draw_2d_line(ue_PyUObject *self, PyObject * args)
5+
{
56

67
ue_py_check(self);
78

89
int x1, y1, x2, y2;
910
PyObject *py_fcolor;
1011

11-
if (!PyArg_ParseTuple(args, "iiiiO:hud_draw_2d_line", &x1, &y1, &x2, &y2, &py_fcolor)) {
12+
if (!PyArg_ParseTuple(args, "iiiiO:hud_draw_2d_line", &x1, &y1, &x2, &y2, &py_fcolor))
13+
{
1214
return nullptr;
1315
}
1416

1517
AHUD *hud = ue_py_check_type<AHUD>(self);
1618
if (!hud)
1719
return PyErr_Format(PyExc_Exception, "UObject is not a AHUD.");
1820

19-
ue_PyFColor *fcolor = py_ue_is_fcolor(py_fcolor);
20-
if (!fcolor)
21-
return PyErr_Format(PyExc_Exception, "argument is not a FColor.");
21+
FColor color;
22+
if (!py_ue_get_fcolor(py_fcolor, color))
23+
{
24+
return PyErr_Format(PyExc_Exception, "argument is not a FColor or FLinearColor.");
25+
}
2226

23-
hud->Draw2DLine(x1, y1, x2, y2, fcolor->color);
27+
hud->Draw2DLine(x1, y1, x2, y2, color);
2428

2529
Py_RETURN_NONE;
2630
}
2731

28-
PyObject *py_ue_hud_draw_line(ue_PyUObject *self, PyObject * args) {
32+
PyObject *py_ue_hud_draw_line(ue_PyUObject *self, PyObject * args)
33+
{
2934

3035
ue_py_check(self);
3136

3237
float x1, y1, x2, y2;
3338
PyObject *py_fcolor;
3439
float thickness;
3540

36-
if (!PyArg_ParseTuple(args, "ffffOf:hud_draw_line", &x1, &y1, &x2, &y2, &py_fcolor, &thickness)) {
41+
if (!PyArg_ParseTuple(args, "ffffOf:hud_draw_line", &x1, &y1, &x2, &y2, &py_fcolor, &thickness))
42+
{
3743
return nullptr;
3844
}
3945

4046
AHUD *hud = ue_py_check_type<AHUD>(self);
4147
if (!hud)
4248
return PyErr_Format(PyExc_Exception, "UObject is not a AHUD.");
4349

44-
ue_PyFColor *fcolor = py_ue_is_fcolor(py_fcolor);
45-
if (!fcolor)
46-
return PyErr_Format(PyExc_Exception, "argument is not a FColor.");
50+
FLinearColor color;
51+
if (!py_ue_get_flinearcolor(py_fcolor, color))
52+
{
53+
return PyErr_Format(PyExc_Exception, "argument is not a FLinearColor or FColor.");
54+
}
4755

48-
hud->DrawLine(x1, y1, x2, y2, fcolor->color, thickness);
56+
hud->DrawLine(x1, y1, x2, y2, color, thickness);
4957

5058
Py_RETURN_NONE;
5159
}
5260

53-
PyObject *py_ue_hud_draw_rect(ue_PyUObject *self, PyObject * args) {
61+
PyObject *py_ue_hud_draw_rect(ue_PyUObject *self, PyObject * args)
62+
{
5463

5564
ue_py_check(self);
5665

5766
float x, y, w, h;
5867
PyObject *py_fcolor;
5968

60-
if (!PyArg_ParseTuple(args, "ffffO:hud_draw_line", &x, &y, &w, &h, &py_fcolor)) {
69+
if (!PyArg_ParseTuple(args, "ffffO:hud_draw_line", &x, &y, &w, &h, &py_fcolor))
70+
{
6171
return nullptr;
6272
}
6373

6474
AHUD *hud = ue_py_check_type<AHUD>(self);
6575
if (!hud)
6676
return PyErr_Format(PyExc_Exception, "UObject is not a AHUD.");
6777

68-
ue_PyFLinearColor *fcolor = py_ue_is_flinearcolor(py_fcolor);
69-
if (!fcolor)
70-
return PyErr_Format(PyExc_Exception, "argument is not a FColor.");
78+
FLinearColor color;
79+
if (!py_ue_get_flinearcolor(py_fcolor, color))
80+
{
81+
return PyErr_Format(PyExc_Exception, "argument is not a FLinearColor or FColor.");
82+
}
7183

72-
hud->DrawRect(fcolor->color, x, y, w, h);
84+
hud->DrawRect(color, x, y, w, h);
7385

7486
Py_RETURN_NONE;
7587
}
7688

7789

78-
PyObject *py_ue_hud_draw_text(ue_PyUObject *self, PyObject * args) {
90+
PyObject *py_ue_hud_draw_text(ue_PyUObject *self, PyObject * args)
91+
{
7992

8093
ue_py_check(self);
8194

@@ -84,24 +97,28 @@ PyObject *py_ue_hud_draw_text(ue_PyUObject *self, PyObject * args) {
8497
PyObject *py_fcolor;
8598
float scale = 1;
8699

87-
if (!PyArg_ParseTuple(args, "sffO|f:hud_draw_text", &text, &x, &y, &py_fcolor, &scale)) {
100+
if (!PyArg_ParseTuple(args, "sffO|f:hud_draw_text", &text, &x, &y, &py_fcolor, &scale))
101+
{
88102
return nullptr;
89103
}
90104

91105
AHUD *hud = ue_py_check_type<AHUD>(self);
92106
if (!hud)
93107
return PyErr_Format(PyExc_Exception, "UObject is not a AHUD.");
94108

95-
ue_PyFLinearColor *fcolor = py_ue_is_flinearcolor(py_fcolor);
96-
if (!fcolor)
97-
return PyErr_Format(PyExc_Exception, "argument is not a FColor.");
109+
FLinearColor color;
110+
if (!py_ue_get_flinearcolor(py_fcolor, color))
111+
{
112+
return PyErr_Format(PyExc_Exception, "argument is not a FLinearColor or FColor.");
113+
}
98114

99-
hud->DrawText(FString(UTF8_TO_TCHAR(text)), fcolor->color, x, y, nullptr, scale);
115+
hud->DrawText(FString(UTF8_TO_TCHAR(text)), color, x, y, nullptr, scale);
100116

101117
Py_RETURN_NONE;
102118
}
103119

104-
PyObject *py_ue_hud_draw_texture(ue_PyUObject *self, PyObject * args) {
120+
PyObject *py_ue_hud_draw_texture(ue_PyUObject *self, PyObject * args)
121+
{
105122

106123
ue_py_check(self);
107124

@@ -116,7 +133,8 @@ PyObject *py_ue_hud_draw_texture(ue_PyUObject *self, PyObject * args) {
116133
float pivot_rot_x = 0, pivot_rot_y = 0;
117134

118135

119-
if (!PyArg_ParseTuple(args, "Offff|ffffOifOf(ff):hud_draw_texture", &py_texture, &x, &y, &w, &h, &u, &v, &uw, &vh, &py_flinear_color, &blend_mode, &scale, &py_scale_position, &rotation, &pivot_rot_x, &pivot_rot_y)) {
136+
if (!PyArg_ParseTuple(args, "Offff|ffffOifOf(ff):hud_draw_texture", &py_texture, &x, &y, &w, &h, &u, &v, &uw, &vh, &py_flinear_color, &blend_mode, &scale, &py_scale_position, &rotation, &pivot_rot_x, &pivot_rot_y))
137+
{
120138
return nullptr;
121139
}
122140

@@ -130,11 +148,12 @@ PyObject *py_ue_hud_draw_texture(ue_PyUObject *self, PyObject * args) {
130148

131149
FLinearColor tint = FLinearColor::White;
132150

133-
if (py_flinear_color) {
134-
ue_PyFLinearColor *flinear_color = py_ue_is_flinearcolor(py_flinear_color);
135-
if (!flinear_color)
136-
return PyErr_Format(PyExc_Exception, "argument is not a FLinearColor.");
137-
tint = flinear_color->color;
151+
if (py_flinear_color)
152+
{
153+
if (!py_ue_get_flinearcolor(py_flinear_color, tint))
154+
{
155+
return PyErr_Format(PyExc_Exception, "argument is not a FLinearColor or FColor.");
156+
}
138157
}
139158

140159
bool scale_position = false;

0 commit comments

Comments
 (0)