diff --git a/src/McRFPy_API.cpp b/src/McRFPy_API.cpp index a6a4b19..9e6708c 100644 --- a/src/McRFPy_API.cpp +++ b/src/McRFPy_API.cpp @@ -126,6 +126,7 @@ PyObject* PyInit_mcrfpy() PyModule_AddType(m, &mcrfpydef::PyFontType); PyModule_AddType(m, &mcrfpydef::PyUICaptionType); PyModule_AddType(m, &mcrfpydef::PyTextureType); + PyModule_AddType(m, &mcrfpydef::PyUISpriteType); if (PyModule_AddType(m, &mcrfpydef::PyUIFrameType) < 0) { diff --git a/src/UI.h b/src/UI.h index 6fa4632..34ee63e 100644 --- a/src/UI.h +++ b/src/UI.h @@ -488,6 +488,7 @@ switch (target->derived_type()) \ static int PyUICaption_set_text(PyUICaptionObject* self, PyObject* value, void* closure) { + // asdf return 0; } @@ -914,6 +915,191 @@ switch (target->derived_type()) \ * */ + /* + * + * Begin template generation for PyUISpriteType + * + */ + + static PyObject* PyUISprite_get_float_member(PyUISpriteObject* self, void* closure) + { + auto member_ptr = reinterpret_cast(closure); + if (member_ptr == 0) + return PyFloat_FromDouble(self->data->sprite.getPosition().x); + else if (member_ptr == 1) + return PyFloat_FromDouble(self->data->sprite.getPosition().y); + else if (member_ptr == 2) + return PyFloat_FromDouble(self->data->sprite.getScale().x); // scale X and Y are identical, presently + else + { + PyErr_SetString(PyExc_AttributeError, "Invalid attribute"); + return nullptr; + } + } + + + static int PyUISprite_set_float_member(PyUISpriteObject* self, PyObject* value, void* closure) + { + float val; + auto member_ptr = reinterpret_cast(closure); + if (PyFloat_Check(value)) + { + val = PyFloat_AsDouble(value); + } + else if (PyLong_Check(value)) + { + val = PyLong_AsLong(value); + } + else + { + PyErr_SetString(PyExc_TypeError, "Value must be a floating point number."); + return -1; + } + if (member_ptr == 0) //x + self->data->sprite.setPosition(val, self->data->sprite.getPosition().y); + else if (member_ptr == 1) //y + self->data->sprite.setPosition(self->data->sprite.getPosition().x, val); + else if (member_ptr == 2) // scale + self->data->sprite.setScale(sf::Vector2f(val, val)); + return 0; + } + + static PyObject* PyUISprite_get_int_member(PyUISpriteObject* self, void* closure) + { + auto member_ptr = reinterpret_cast(closure); + if (false) {} + else + { + PyErr_SetString(PyExc_AttributeError, "Invalid attribute"); + return nullptr; + } + + return PyLong_FromDouble(self->data->sprite_index); + } + + + static int PyUISprite_set_int_member(PyUISpriteObject* self, PyObject* value, void* closure) + { + int val; + auto member_ptr = reinterpret_cast(closure); + if (PyLong_Check(value)) + { + val = PyLong_AsLong(value); + } + else + { + PyErr_SetString(PyExc_TypeError, "Value must be an integer."); + return -1; + } + self->data->sprite_index = val; + return 0; + } + + static PyObject* PyUISprite_get_texture(PyUISpriteObject* self, void* closure) + { + return NULL; + } + + static int PyUISprite_set_texture(PyUISpriteObject* self, PyObject* value, void* closure) + { + return -1; + } + + static PyGetSetDef PyUISprite_getsetters[] = { + {"x", (getter)PyUISprite_get_float_member, (setter)PyUISprite_set_float_member, "X coordinate of top-left corner", (void*)0}, + {"y", (getter)PyUISprite_get_float_member, (setter)PyUISprite_set_float_member, "Y coordinate of top-left corner", (void*)1}, + {"scale", (getter)PyUISprite_get_float_member, (setter)PyUISprite_set_float_member, "Size factor", (void*)2}, + {"sprite_number", (getter)PyUISprite_get_int_member, (setter)PyUISprite_set_int_member, "Which sprite on the texture is shown", NULL}, + {"texture", (getter)PyUISprite_get_texture, (setter)PyUISprite_set_texture, "Texture object", NULL}, + {NULL} + }; + + static PyObject* PyUISprite_repr(PyUISpriteObject* self) + { + std::ostringstream ss; + if (!self->data) ss << ""; + else { + auto sprite = self->data->sprite; + ss << "