diff --git a/src/PyColor.cpp b/src/PyColor.cpp index 4f7f5f8..dce6286 100644 --- a/src/PyColor.cpp +++ b/src/PyColor.cpp @@ -1,4 +1,5 @@ #include "PyColor.h" +#include "McRFPy_API.h" PyGetSetDef PyColor::getsetters[] = { {"r", (getter)PyColor::get_member, (setter)PyColor::set_member, "Red component", (void*)0}, @@ -134,3 +135,16 @@ int PyColor::set_member(PyObject* obj, PyObject* value, void* closure) // TODO return 0; } + +PyColorObject* PyColor::from_arg(PyObject* args) +{ + auto type = (PyTypeObject*)PyObject_GetAttrString(McRFPy_API::mcrf_module, "Color"); + if (PyObject_IsInstance(args, (PyObject*)type)) return (PyColorObject*)args; + auto obj = (PyColorObject*)type->tp_alloc(type, 0); + int err = init(obj, args, NULL); + if (err) { + Py_DECREF(obj); + return NULL; + } + return obj; +} diff --git a/src/PyColor.h b/src/PyColor.h index a111b89..5b7f1b5 100644 --- a/src/PyColor.h +++ b/src/PyColor.h @@ -29,6 +29,7 @@ public: static int set_member(PyObject*, PyObject*, void*); static PyGetSetDef getsetters[]; + static PyColorObject* from_arg(PyObject*); }; namespace mcrfpydef { diff --git a/src/UICaption.cpp b/src/UICaption.cpp index 21c9c36..9001333 100644 --- a/src/UICaption.cpp +++ b/src/UICaption.cpp @@ -212,7 +212,7 @@ PyObject* UICaption::repr(PyUICaptionObject* self) "text='" << (std::string)text.getString() << "', " << "outline=" << text.getOutlineThickness() << ", " << "fill_color=(" << (int)fc.r << ", " << (int)fc.g << ", " << (int)fc.b << ", " << (int)fc.a <<"), " << - "outlinecolor=(" << (int)oc.r << ", " << (int)oc.g << ", " << (int)oc.b << ", " << (int)oc.a <<"), " << + "outline_color=(" << (int)oc.r << ", " << (int)oc.g << ", " << (int)oc.b << ", " << (int)oc.a <<"), " << ")>"; } std::string repr_str = ss.str(); @@ -222,13 +222,13 @@ PyObject* UICaption::repr(PyUICaptionObject* self) int UICaption::init(PyUICaptionObject* self, PyObject* args, PyObject* kwds) { using namespace mcrfpydef; - static const char* keywords[] = { "x", "y", "text", "font", "fill_color", "outline_color", nullptr }; - float x = 0.0f, y = 0.0f; + static const char* keywords[] = { "x", "y", "text", "font", "fill_color", "outline_color", "outline", nullptr }; + float x = 0.0f, y = 0.0f, outline = 0.0f; char* text; - PyObject* font, fill_color, outline_color; + PyObject* font=NULL, *fill_color=NULL, *outline_color=NULL; - if (!PyArg_ParseTupleAndKeywords(args, kwds, "|ffzOOO", - const_cast(keywords), &x, &y, &text, &font, &fill_color, &outline_color)) + if (!PyArg_ParseTupleAndKeywords(args, kwds, "|ffzOOOf", + const_cast(keywords), &x, &y, &text, &font, &fill_color, &outline_color, &outline)) { return -1; } @@ -253,8 +253,30 @@ int UICaption::init(PyUICaptionObject* self, PyObject* args, PyObject* kwds) self->data->text.setPosition(sf::Vector2f(x, y)); self->data->text.setString((std::string)text); - self->data->text.setFillColor(sf::Color(0,0,0,255)); - self->data->text.setOutlineColor(sf::Color(128,128,128,255)); + self->data->text.setOutlineThickness(outline); + if (fill_color) { + auto fc = PyColor::from_arg(fill_color); + if (!fc) { + PyErr_SetString(PyExc_TypeError, "fill_color must be mcrfpy.Color or arguments to mcrfpy.Color.__init__"); + return -1; + } + self->data->text.setFillColor(PyColor::fromPy(fc)); + //Py_DECREF(fc); + } else { + self->data->text.setFillColor(sf::Color(0,0,0,255)); + } + + if (outline_color) { + auto oc = PyColor::from_arg(outline_color); + if (!oc) { + PyErr_SetString(PyExc_TypeError, "outline_color must be mcrfpy.Color or arguments to mcrfpy.Color.__init__"); + return -1; + } + self->data->text.setOutlineColor(PyColor::fromPy(oc)); + //Py_DECREF(oc); + } else { + self->data->text.setOutlineColor(sf::Color(128,128,128,255)); + } return 0; }