|
| 1 | +#include "UnrealEnginePythonPrivatePCH.h" |
| 2 | + |
| 3 | +#include "UEPyFPaintContext.h" |
| 4 | + |
| 5 | +static PyObject *py_ue_fpaint_context_draw_line(ue_PyFPaintContext *self, PyObject * args) { |
| 6 | + float x1, y1, x2, y2; |
| 7 | + PyObject *py_linear_color = nullptr; |
| 8 | + PyObject *py_antialias = nullptr; |
| 9 | + float thickness = 1.0; |
| 10 | + |
| 11 | + if (!PyArg_ParseTuple(args, "(ff)(ff)|OOf:draw_line", &x1, &y1, &x2, &y2, &py_linear_color, &py_antialias, &thickness)) |
| 12 | + return nullptr; |
| 13 | + |
| 14 | + FLinearColor tint = FLinearColor::White; |
| 15 | + |
| 16 | + if (py_linear_color) { |
| 17 | + if (ue_PyFLinearColor *linear_color = py_ue_is_flinearcolor(py_linear_color)) { |
| 18 | + tint = linear_color->color; |
| 19 | + } |
| 20 | + else { |
| 21 | + return PyErr_Format(PyExc_Exception, "argument is not a FlinearColor"); |
| 22 | + } |
| 23 | + } |
| 24 | + |
| 25 | + FPaintContext context = self->paint_context; |
| 26 | + |
| 27 | + context.MaxLayer++; |
| 28 | + |
| 29 | + TArray<FVector2D> points; |
| 30 | + points.Add(FVector2D(x1, y1)); |
| 31 | + points.Add(FVector2D(x2, y2)); |
| 32 | + |
| 33 | + FSlateDrawElement::MakeLines(context.OutDrawElements, context.MaxLayer, context.AllottedGeometry.ToPaintGeometry(), |
| 34 | + points, context.MyClippingRect, ESlateDrawEffect::None, tint, (py_antialias && !PyObject_IsTrue(py_antialias)), thickness); |
| 35 | + |
| 36 | + Py_RETURN_NONE; |
| 37 | +} |
| 38 | + |
| 39 | +static PyObject *py_ue_fpaint_context_draw_lines(ue_PyFPaintContext *self, PyObject * args) { |
| 40 | + PyObject *py_points; |
| 41 | + PyObject *py_linear_color = nullptr; |
| 42 | + PyObject *py_antialias = nullptr; |
| 43 | + float thickness = 1.0; |
| 44 | + |
| 45 | + if (!PyArg_ParseTuple(args, "O|OOf:draw_line", &py_points, &py_linear_color, &py_antialias, &thickness)) |
| 46 | + return nullptr; |
| 47 | + |
| 48 | + TArray<FVector2D> points; |
| 49 | + |
| 50 | + PyObject *py_iter = PyObject_GetIter(py_points); |
| 51 | + if (!py_iter) |
| 52 | + return PyErr_Format(PyExc_Exception, "argument is not an iterable of tuples"); |
| 53 | + |
| 54 | + while (PyObject *py_item = PyIter_Next(py_iter)) { |
| 55 | + if (!PyTuple_Check(py_item)) { |
| 56 | + return PyErr_Format(PyExc_Exception, "iterable item is not a tuple"); |
| 57 | + Py_DECREF(py_iter); |
| 58 | + } |
| 59 | + |
| 60 | + float x, y; |
| 61 | + if (!PyArg_ParseTuple(py_item, "ff", &x, &y)) { |
| 62 | + Py_DECREF(py_iter); |
| 63 | + return nullptr; |
| 64 | + } |
| 65 | + points.Add(FVector2D(x, y)); |
| 66 | + } |
| 67 | + Py_DECREF(py_iter); |
| 68 | + |
| 69 | + FLinearColor tint = FLinearColor::White; |
| 70 | + |
| 71 | + if (py_linear_color) { |
| 72 | + if (ue_PyFLinearColor *linear_color = py_ue_is_flinearcolor(py_linear_color)) { |
| 73 | + tint = linear_color->color; |
| 74 | + } |
| 75 | + else { |
| 76 | + return PyErr_Format(PyExc_Exception, "argument is not a FlinearColor"); |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + FPaintContext context = self->paint_context; |
| 81 | + |
| 82 | + context.MaxLayer++; |
| 83 | + |
| 84 | + FSlateDrawElement::MakeLines(context.OutDrawElements, context.MaxLayer, context.AllottedGeometry.ToPaintGeometry(), |
| 85 | + points, context.MyClippingRect, ESlateDrawEffect::None, tint, (py_antialias && !PyObject_IsTrue(py_antialias)), thickness); |
| 86 | + |
| 87 | + Py_RETURN_NONE; |
| 88 | +} |
| 89 | + |
| 90 | +static PyMethodDef ue_PyFPaintContext_methods[] = { |
| 91 | + { "draw_line", (PyCFunction)py_ue_fpaint_context_draw_line, METH_VARARGS, "" }, |
| 92 | + { "draw_lines", (PyCFunction)py_ue_fpaint_context_draw_lines, METH_VARARGS, "" }, |
| 93 | + { NULL } /* Sentinel */ |
| 94 | +}; |
| 95 | + |
| 96 | +static PyObject *ue_PyFPaintContext_str(ue_PyFPaintContext *self) |
| 97 | +{ |
| 98 | + return PyUnicode_FromFormat("<unreal_engine.FPaintContext '%s'>", |
| 99 | + TCHAR_TO_UTF8(*self->paint_context.AllottedGeometry.ToString())); |
| 100 | +} |
| 101 | + |
| 102 | +static PyTypeObject ue_PyFPaintContextType = { |
| 103 | + PyVarObject_HEAD_INIT(NULL, 0) |
| 104 | + "unreal_engine.FPaintContext", /* tp_name */ |
| 105 | + sizeof(ue_PyFPaintContext), /* tp_basicsize */ |
| 106 | + 0, /* tp_itemsize */ |
| 107 | + 0, /* tp_dealloc */ |
| 108 | + 0, /* tp_print */ |
| 109 | + 0, /* tp_getattr */ |
| 110 | + 0, /* tp_setattr */ |
| 111 | + 0, /* tp_reserved */ |
| 112 | + 0, /* tp_repr */ |
| 113 | + 0, /* tp_as_number */ |
| 114 | + 0, /* tp_as_sequence */ |
| 115 | + 0, /* tp_as_mapping */ |
| 116 | + 0, /* tp_hash */ |
| 117 | + 0, /* tp_call */ |
| 118 | + (reprfunc)ue_PyFPaintContext_str, /* tp_str */ |
| 119 | + 0, /* tp_getattro */ |
| 120 | + 0, /* tp_setattro */ |
| 121 | + 0, /* tp_as_buffer */ |
| 122 | + Py_TPFLAGS_DEFAULT, /* tp_flags */ |
| 123 | + "Unreal Engine FPaintContext", /* tp_doc */ |
| 124 | + 0, /* tp_traverse */ |
| 125 | + 0, /* tp_clear */ |
| 126 | + 0, /* tp_richcompare */ |
| 127 | + 0, /* tp_weaklistoffset */ |
| 128 | + 0, /* tp_iter */ |
| 129 | + 0, /* tp_iternext */ |
| 130 | + ue_PyFPaintContext_methods, /* tp_methods */ |
| 131 | +}; |
| 132 | + |
| 133 | +void ue_python_init_fpaint_context(PyObject *ue_module) { |
| 134 | + ue_PyFPaintContextType.tp_new = PyType_GenericNew; |
| 135 | + |
| 136 | + if (PyType_Ready(&ue_PyFPaintContextType) < 0) |
| 137 | + return; |
| 138 | + |
| 139 | + Py_INCREF(&ue_PyFPaintContextType); |
| 140 | + PyModule_AddObject(ue_module, "FPaintContext", (PyObject *)&ue_PyFPaintContextType); |
| 141 | +} |
| 142 | + |
| 143 | +PyObject *py_ue_new_fpaint_context(FPaintContext paint_context) { |
| 144 | + ue_PyFPaintContext *ret = (ue_PyFPaintContext *)PyObject_New(ue_PyFPaintContext, &ue_PyFPaintContextType); |
| 145 | + ret->paint_context = paint_context; |
| 146 | + return (PyObject *)ret; |
| 147 | +} |
0 commit comments