diff --git a/src/UIGridPoint.cpp b/src/UIGridPoint.cpp index b30d493..18fffc7 100644 --- a/src/UIGridPoint.cpp +++ b/src/UIGridPoint.cpp @@ -4,131 +4,3 @@ UIGridPoint::UIGridPoint() : color(1.0f, 1.0f, 1.0f), color_overlay(0.0f, 0.0f, 0.0f), walkable(false), transparent(false), tilesprite(-1), tile_overlay(-1), uisprite(-1) {} - -// Utility function to convert sf::Color to PyObject* -PyObject* sfColor_to_PyObject(sf::Color color) { - return Py_BuildValue("(iiii)", color.r, color.g, color.b, color.a); -} - -// Utility function to convert PyObject* to sf::Color -sf::Color PyObject_to_sfColor(PyObject* obj) { - int r, g, b, a = 255; // Default alpha to fully opaque if not specified - if (!PyArg_ParseTuple(obj, "iii|i", &r, &g, &b, &a)) { - return sf::Color(); // Return default color on parse error - } - return sf::Color(r, g, b, a); -} - -PyObject* UIGridPoint::get_color(PyUIGridPointObject* self, void* closure) { - if (reinterpret_cast(closure) == 0) { // color - return sfColor_to_PyObject(self->data->color); - } else { // color_overlay - return sfColor_to_PyObject(self->data->color_overlay); - } -} - -int UIGridPoint::set_color(PyUIGridPointObject* self, PyObject* value, void* closure) { - sf::Color color = PyObject_to_sfColor(value); - if (reinterpret_cast(closure) == 0) { // color - self->data->color = color; - } else { // color_overlay - self->data->color_overlay = color; - } - return 0; -} - -PyObject* UIGridPoint::get_bool_member(PyUIGridPointObject* self, void* closure) { - if (reinterpret_cast(closure) == 0) { // walkable - return PyBool_FromLong(self->data->walkable); - } else { // transparent - return PyBool_FromLong(self->data->transparent); - } -} - -int UIGridPoint::set_bool_member(PyUIGridPointObject* self, PyObject* value, void* closure) { - if (value == Py_True) { - if (reinterpret_cast(closure) == 0) { // walkable - self->data->walkable = true; - } else { // transparent - self->data->transparent = true; - } - } else if (value == Py_False) { - if (reinterpret_cast(closure) == 0) { // walkable - self->data->walkable = false; - } else { // transparent - self->data->transparent = false; - } - } else { - PyErr_SetString(PyExc_ValueError, "Expected a boolean value"); - return -1; - } - return 0; -} - -PyObject* UIGridPoint::get_int_member(PyUIGridPointObject* self, void* closure) { - switch(reinterpret_cast(closure)) { - case 0: return PyLong_FromLong(self->data->tilesprite); - case 1: return PyLong_FromLong(self->data->tile_overlay); - case 2: return PyLong_FromLong(self->data->uisprite); - default: PyErr_SetString(PyExc_RuntimeError, "Invalid closure"); return nullptr; - } -} - -int UIGridPoint::set_int_member(PyUIGridPointObject* self, PyObject* value, void* closure) { - long val = PyLong_AsLong(value); - if (PyErr_Occurred()) return -1; - - switch(reinterpret_cast(closure)) { - case 0: self->data->tilesprite = val; break; - case 1: self->data->tile_overlay = val; break; - case 2: self->data->uisprite = val; break; - default: PyErr_SetString(PyExc_RuntimeError, "Invalid closure"); return -1; - } - return 0; -} - -PyGetSetDef UIGridPoint::getsetters[] = { - {"color", (getter)UIGridPoint::get_color, (setter)UIGridPoint::set_color, "GridPoint color", (void*)0}, - {"color_overlay", (getter)UIGridPoint::get_color, (setter)UIGridPoint::set_color, "GridPoint color overlay", (void*)1}, - {"walkable", (getter)UIGridPoint::get_bool_member, (setter)UIGridPoint::set_bool_member, "Is the GridPoint walkable", (void*)0}, - {"transparent", (getter)UIGridPoint::get_bool_member, (setter)UIGridPoint::set_bool_member, "Is the GridPoint transparent", (void*)1}, - {"tilesprite", (getter)UIGridPoint::get_int_member, (setter)UIGridPoint::set_int_member, "Tile sprite index", (void*)0}, - {"tile_overlay", (getter)UIGridPoint::get_int_member, (setter)UIGridPoint::set_int_member, "Tile overlay sprite index", (void*)1}, - {"uisprite", (getter)UIGridPoint::get_int_member, (setter)UIGridPoint::set_int_member, "UI sprite index", (void*)2}, - {NULL} /* Sentinel */ -}; - -PyObject* UIGridPointState::get_bool_member(PyUIGridPointStateObject* self, void* closure) { - if (reinterpret_cast(closure) == 0) { // visible - return PyBool_FromLong(self->data->visible); - } else { // discovered - return PyBool_FromLong(self->data->discovered); - } -} - -int UIGridPointState::set_bool_member(PyUIGridPointStateObject* self, PyObject* value, void* closure) { - if (!PyBool_Check(value)) { - PyErr_SetString(PyExc_TypeError, "Value must be a boolean"); - return -1; - } - - int truthValue = PyObject_IsTrue(value); - if (truthValue < 0) { - return -1; // PyObject_IsTrue returns -1 on error - } - - if (reinterpret_cast(closure) == 0) { // visible - self->data->visible = truthValue; - } else { // discovered - self->data->discovered = truthValue; - } - - return 0; -} - -PyGetSetDef UIGridPointState::getsetters[] = { - {"visible", (getter)UIGridPointState::get_bool_member, (setter)UIGridPointState::set_bool_member, "Is the GridPointState visible", (void*)0}, - {"discovered", (getter)UIGridPointState::get_bool_member, (setter)UIGridPointState::set_bool_member, "Has the GridPointState been discovered", (void*)1}, - {NULL} /* Sentinel */ -}; - diff --git a/src/UIGridPoint.h b/src/UIGridPoint.h index 36d3561..4827bd9 100644 --- a/src/UIGridPoint.h +++ b/src/UIGridPoint.h @@ -12,13 +12,25 @@ #include "PyVector.h" #include "PyFont.h" -static PyObject* sfColor_to_PyObject(sf::Color color); -static sf::Color PyObject_to_sfColor(PyObject* obj); +// UIGridPoint - revised grid data for each point +class UIGridPoint +{ +public: + sf::Color color, color_overlay; + bool walkable, transparent; + int tilesprite, tile_overlay, uisprite; + UIGridPoint(); +}; + +// UIGridPointState - entity-specific info for each cell +class UIGridPointState +{ +public: + bool visible, discovered; +}; class UIGrid; class UIEntity; -class UIGridPoint; -class UIGridPointState; typedef struct { PyObject_HEAD @@ -33,36 +45,110 @@ typedef struct { std::shared_ptr entity; } PyUIGridPointStateObject; -// UIGridPoint - revised grid data for each point -class UIGridPoint -{ -public: - sf::Color color, color_overlay; - bool walkable, transparent; - int tilesprite, tile_overlay, uisprite; - UIGridPoint(); - - static int set_int_member(PyUIGridPointObject* self, PyObject* value, void* closure); - static PyGetSetDef getsetters[]; - static PyObject* get_color(PyUIGridPointObject* self, void* closure); - static PyObject* get_int_member(PyUIGridPointObject* self, void* closure); - static int set_bool_member(PyUIGridPointObject* self, PyObject* value, void* closure); - static PyObject* get_bool_member(PyUIGridPointObject* self, void* closure); - static int set_color(PyUIGridPointObject* self, PyObject* value, void* closure); -}; - -// UIGridPointState - entity-specific info for each cell -class UIGridPointState -{ -public: - bool visible, discovered; - - static PyObject* get_bool_member(PyUIGridPointStateObject* self, void* closure); - static int set_bool_member(PyUIGridPointStateObject* self, PyObject* value, void* closure); - static PyGetSetDef getsetters[]; -}; - namespace mcrfpydef { +// TODO: question: are sfColor_to_PyObject and PyObject_to_sfColor duplicitive? How does UIFrame get/set colors? + +//TODO: add this method to class scope; move implementation to .cpp file; reconsider for moving to "UIBase.h/.cpp" +// Utility function to convert sf::Color to PyObject* +static PyObject* sfColor_to_PyObject(sf::Color color) { + return Py_BuildValue("(iiii)", color.r, color.g, color.b, color.a); +} + +//TODO: add this method to class scope; move implementation to .cpp file; reconsider for moving to "UIBase.h/.cpp" +// Utility function to convert PyObject* to sf::Color +static sf::Color PyObject_to_sfColor(PyObject* obj) { + int r, g, b, a = 255; // Default alpha to fully opaque if not specified + if (!PyArg_ParseTuple(obj, "iii|i", &r, &g, &b, &a)) { + return sf::Color(); // Return default color on parse error + } + return sf::Color(r, g, b, a); +} + +//TODO: add this method to class scope; move implementation to .cpp file +static PyObject* PyUIGridPoint_get_color(PyUIGridPointObject* self, void* closure) { + if (reinterpret_cast(closure) == 0) { // color + return sfColor_to_PyObject(self->data->color); + } else { // color_overlay + return sfColor_to_PyObject(self->data->color_overlay); + } +} + +//TODO: add this method to class scope; move implementation to .cpp file +static int PyUIGridPoint_set_color(PyUIGridPointObject* self, PyObject* value, void* closure) { + sf::Color color = PyObject_to_sfColor(value); + if (reinterpret_cast(closure) == 0) { // color + self->data->color = color; + } else { // color_overlay + self->data->color_overlay = color; + } + return 0; +} + +//TODO: add this method to class scope; move implementation to .cpp file +static PyObject* PyUIGridPoint_get_bool_member(PyUIGridPointObject* self, void* closure) { + if (reinterpret_cast(closure) == 0) { // walkable + return PyBool_FromLong(self->data->walkable); + } else { // transparent + return PyBool_FromLong(self->data->transparent); + } +} + +//TODO: add this method to class scope; move implementation to .cpp file +static int PyUIGridPoint_set_bool_member(PyUIGridPointObject* self, PyObject* value, void* closure) { + if (value == Py_True) { + if (reinterpret_cast(closure) == 0) { // walkable + self->data->walkable = true; + } else { // transparent + self->data->transparent = true; + } + } else if (value == Py_False) { + if (reinterpret_cast(closure) == 0) { // walkable + self->data->walkable = false; + } else { // transparent + self->data->transparent = false; + } + } else { + PyErr_SetString(PyExc_ValueError, "Expected a boolean value"); + return -1; + } + return 0; +} + +//TODO: add this method to class scope; move implementation to .cpp file +static PyObject* PyUIGridPoint_get_int_member(PyUIGridPointObject* self, void* closure) { + switch(reinterpret_cast(closure)) { + case 0: return PyLong_FromLong(self->data->tilesprite); + case 1: return PyLong_FromLong(self->data->tile_overlay); + case 2: return PyLong_FromLong(self->data->uisprite); + default: PyErr_SetString(PyExc_RuntimeError, "Invalid closure"); return nullptr; + } +} + +//TODO: add this method to class scope; move implementation to .cpp file +static int PyUIGridPoint_set_int_member(PyUIGridPointObject* self, PyObject* value, void* closure) { + long val = PyLong_AsLong(value); + if (PyErr_Occurred()) return -1; + + switch(reinterpret_cast(closure)) { + case 0: self->data->tilesprite = val; break; + case 1: self->data->tile_overlay = val; break; + case 2: self->data->uisprite = val; break; + default: PyErr_SetString(PyExc_RuntimeError, "Invalid closure"); return -1; + } + return 0; +} + +//TODO: add this static array to class scope; move implementation to .cpp file +static PyGetSetDef PyUIGridPoint_getsetters[] = { + {"color", (getter)PyUIGridPoint_get_color, (setter)PyUIGridPoint_set_color, "GridPoint color", (void*)0}, + {"color_overlay", (getter)PyUIGridPoint_get_color, (setter)PyUIGridPoint_set_color, "GridPoint color overlay", (void*)1}, + {"walkable", (getter)PyUIGridPoint_get_bool_member, (setter)PyUIGridPoint_set_bool_member, "Is the GridPoint walkable", (void*)0}, + {"transparent", (getter)PyUIGridPoint_get_bool_member, (setter)PyUIGridPoint_set_bool_member, "Is the GridPoint transparent", (void*)1}, + {"tilesprite", (getter)PyUIGridPoint_get_int_member, (setter)PyUIGridPoint_set_int_member, "Tile sprite index", (void*)0}, + {"tile_overlay", (getter)PyUIGridPoint_get_int_member, (setter)PyUIGridPoint_set_int_member, "Tile overlay sprite index", (void*)1}, + {"uisprite", (getter)PyUIGridPoint_get_int_member, (setter)PyUIGridPoint_set_int_member, "UI sprite index", (void*)2}, + {NULL} /* Sentinel */ +}; static PyTypeObject PyUIGridPointType = { //PyVarObject_HEAD_INIT(NULL, 0) @@ -71,12 +157,49 @@ static PyTypeObject PyUIGridPointType = { .tp_itemsize = 0, // Methods omitted for brevity .tp_flags = Py_TPFLAGS_DEFAULT, - .tp_doc = "UIGridPoint object", - .tp_getset = UIGridPoint::getsetters, + .tp_doc = "UIGridPoint objects", + .tp_getset = PyUIGridPoint_getsetters, //.tp_init = (initproc)PyUIGridPoint_init, // TODO Define the init function .tp_new = PyType_GenericNew, }; +//TODO: add this method to class scope; move implementation to .cpp file +static PyObject* PyUIGridPointState_get_bool_member(PyUIGridPointStateObject* self, void* closure) { + if (reinterpret_cast(closure) == 0) { // visible + return PyBool_FromLong(self->data->visible); + } else { // discovered + return PyBool_FromLong(self->data->discovered); + } +} + +//TODO: add this method to class scope; move implementation to .cpp file +static int PyUIGridPointState_set_bool_member(PyUIGridPointStateObject* self, PyObject* value, void* closure) { + if (!PyBool_Check(value)) { + PyErr_SetString(PyExc_TypeError, "Value must be a boolean"); + return -1; + } + + int truthValue = PyObject_IsTrue(value); + if (truthValue < 0) { + return -1; // PyObject_IsTrue returns -1 on error + } + + if (reinterpret_cast(closure) == 0) { // visible + self->data->visible = truthValue; + } else { // discovered + self->data->discovered = truthValue; + } + + return 0; +} + +//TODO: add this static array to class scope; move implementation to .cpp file +static PyGetSetDef PyUIGridPointState_getsetters[] = { + {"visible", (getter)PyUIGridPointState_get_bool_member, (setter)PyUIGridPointState_set_bool_member, "Is the GridPointState visible", (void*)0}, + {"discovered", (getter)PyUIGridPointState_get_bool_member, (setter)PyUIGridPointState_set_bool_member, "Has the GridPointState been discovered", (void*)1}, + {NULL} /* Sentinel */ +}; + static PyTypeObject PyUIGridPointStateType = { //PyVarObject_HEAD_INIT(NULL, 0) .tp_name = "mcrfpy.GridPointState", @@ -84,8 +207,8 @@ static PyTypeObject PyUIGridPointStateType = { .tp_itemsize = 0, // Methods omitted for brevity .tp_flags = Py_TPFLAGS_DEFAULT, - .tp_doc = "UIGridPointState object", // TODO: Add PyUIGridPointState tp_init - .tp_getset = UIGridPointState::getsetters, + .tp_doc = "UIGridPointState objects", // TODO: Add PyUIGridPointState tp_init + .tp_getset = PyUIGridPointState_getsetters, .tp_new = PyType_GenericNew, }; diff --git a/src/UISprite.cpp b/src/UISprite.cpp index 1f3a214..47acc11 100644 --- a/src/UISprite.cpp +++ b/src/UISprite.cpp @@ -80,136 +80,3 @@ PyObjectsEnum UISprite::derived_type() { return PyObjectsEnum::UISPRITE; } - -PyObject* UISprite::get_float_member(PyUISpriteObject* self, void* closure) -{ - auto member_ptr = reinterpret_cast(closure); - if (member_ptr == 0) - return PyFloat_FromDouble(self->data->getPosition().x); - else if (member_ptr == 1) - return PyFloat_FromDouble(self->data->getPosition().y); - else if (member_ptr == 2) - return PyFloat_FromDouble(self->data->getScale().x); // scale X and Y are identical, presently - else - { - PyErr_SetString(PyExc_AttributeError, "Invalid attribute"); - return nullptr; - } -} - -int UISprite::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->setPosition(sf::Vector2f(val, self->data->getPosition().y)); - else if (member_ptr == 1) //y - self->data->setPosition(sf::Vector2f(self->data->getPosition().x, val)); - else if (member_ptr == 2) // scale - self->data->setScale(sf::Vector2f(val, val)); - return 0; -} - -PyObject* UISprite::get_int_member(PyUISpriteObject* self, void* closure) -{ - auto member_ptr = reinterpret_cast(closure); - if (true) {} - else - { - PyErr_SetString(PyExc_AttributeError, "Invalid attribute"); - return nullptr; - } - - return PyLong_FromDouble(self->data->getSpriteIndex()); -} - -int UISprite::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->setSpriteIndex(val); - return 0; -} - -PyObject* UISprite::get_texture(PyUISpriteObject* self, void* closure) -{ - return self->data->getTexture()->pyObject(); -} - -int UISprite::set_texture(PyUISpriteObject* self, PyObject* value, void* closure) -{ - return -1; -} - -PyGetSetDef UISprite::getsetters[] = { - {"x", (getter)UISprite::get_float_member, (setter)UISprite::set_float_member, "X coordinate of top-left corner", (void*)0}, - {"y", (getter)UISprite::get_float_member, (setter)UISprite::set_float_member, "Y coordinate of top-left corner", (void*)1}, - {"scale", (getter)UISprite::get_float_member, (setter)UISprite::set_float_member, "Size factor", (void*)2}, - {"sprite_number", (getter)UISprite::get_int_member, (setter)UISprite::set_int_member, "Which sprite on the texture is shown", NULL}, - {"texture", (getter)UISprite::get_texture, (setter)UISprite::set_texture, "Texture object", NULL}, - {"click", (getter)UIDrawable::get_click, (setter)UIDrawable::set_click, "Object called with (x, y, button) when clicked", (void*)PyObjectsEnum::UISPRITE}, - {NULL} -}; - -PyObject* UISprite::repr(PyUISpriteObject* self) -{ - std::ostringstream ss; - if (!self->data) ss << ""; - else { - //auto sprite = self->data->sprite; - ss << ""; - } - std::string repr_str = ss.str(); - return PyUnicode_DecodeUTF8(repr_str.c_str(), repr_str.size(), "replace"); -} - -int UISprite::init(PyUISpriteObject* self, PyObject* args, PyObject* kwds) -{ - //std::cout << "Init called\n"; - static const char* keywords[] = { "x", "y", "texture", "sprite_index", "scale", nullptr }; - float x = 0.0f, y = 0.0f, scale = 1.0f; - int sprite_index; - PyObject* texture; - - if (!PyArg_ParseTupleAndKeywords(args, kwds, "|ffOif", - const_cast(keywords), &x, &y, &texture, &sprite_index, &scale)) - { - return -1; - } - - // check types for texture - //if (texture != NULL && !PyObject_IsInstance(texture, (PyObject*)&PyTextureType)){ - if (texture != NULL && !PyObject_IsInstance(texture, PyObject_GetAttrString(McRFPy_API::mcrf_module, "Texture"))){ - PyErr_SetString(PyExc_TypeError, "texture must be a mcrfpy.Texture instance"); - return -1; - } - auto pytexture = (PyTextureObject*)texture; - self->data = std::make_shared(pytexture->data, sprite_index, sf::Vector2f(x, y), scale); - self->data->setPosition(sf::Vector2f(x, y)); - - return 0; -} diff --git a/src/UISprite.h b/src/UISprite.h index c589408..ce22be8 100644 --- a/src/UISprite.h +++ b/src/UISprite.h @@ -41,18 +41,6 @@ public: std::shared_ptr getTexture(); PyObjectsEnum derived_type() override final; - - - static PyObject* get_float_member(PyUISpriteObject* self, void* closure); - static int set_float_member(PyUISpriteObject* self, PyObject* value, void* closure); - static PyObject* get_int_member(PyUISpriteObject* self, void* closure); - static int set_int_member(PyUISpriteObject* self, PyObject* value, void* closure); - static PyObject* get_texture(PyUISpriteObject* self, void* closure); - static int set_texture(PyUISpriteObject* self, PyObject* value, void* closure); - static PyGetSetDef getsetters[]; - static PyObject* repr(PyUISpriteObject* self); - static int init(PyUISpriteObject* self, PyObject* args, PyObject* kwds); - }; //typedef struct { @@ -61,7 +49,146 @@ public: //} PyUISpriteObject; namespace mcrfpydef { + //TODO: add this method to class scope; move implementation to .cpp file + 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->getPosition().x); + else if (member_ptr == 1) + return PyFloat_FromDouble(self->data->getPosition().y); + else if (member_ptr == 2) + return PyFloat_FromDouble(self->data->getScale().x); // scale X and Y are identical, presently + else + { + PyErr_SetString(PyExc_AttributeError, "Invalid attribute"); + return nullptr; + } + } + //TODO: add this method to class scope; move implementation to .cpp file + 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->setPosition(sf::Vector2f(val, self->data->getPosition().y)); + else if (member_ptr == 1) //y + self->data->setPosition(sf::Vector2f(self->data->getPosition().x, val)); + else if (member_ptr == 2) // scale + self->data->setScale(sf::Vector2f(val, val)); + return 0; + } + + //TODO: add this method to class scope; move implementation to .cpp file + static PyObject* PyUISprite_get_int_member(PyUISpriteObject* self, void* closure) + { + auto member_ptr = reinterpret_cast(closure); + if (true) {} + else + { + PyErr_SetString(PyExc_AttributeError, "Invalid attribute"); + return nullptr; + } + + return PyLong_FromDouble(self->data->getSpriteIndex()); + } + + //TODO: add this method to class scope; move implementation to .cpp file + 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->setSpriteIndex(val); + return 0; + } + + //TODO: add this method to class scope; move implementation to .cpp file + static PyObject* PyUISprite_get_texture(PyUISpriteObject* self, void* closure) + { + return self->data->getTexture()->pyObject(); + } + + //TODO: add this method to class scope; move implementation to .cpp file + static int PyUISprite_set_texture(PyUISpriteObject* self, PyObject* value, void* closure) + { + return -1; + } + + //TODO: add this method to static array scope; move implementation to .cpp file + 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}, + {"click", (getter)UIDrawable::get_click, (setter)UIDrawable::set_click, "Object called with (x, y, button) when clicked", (void*)PyObjectsEnum::UISPRITE}, + {NULL} + }; + + //TODO: add this method to class scope; move implementation to .cpp file + static PyObject* PyUISprite_repr(PyUISpriteObject* self) + { + std::ostringstream ss; + if (!self->data) ss << ""; + else { + //auto sprite = self->data->sprite; + ss << ""; + } + std::string repr_str = ss.str(); + return PyUnicode_DecodeUTF8(repr_str.c_str(), repr_str.size(), "replace"); + } + + //TODO: add this method to class scope; move implementation to .cpp file + static int PyUISprite_init(PyUISpriteObject* self, PyObject* args, PyObject* kwds) + { + //std::cout << "Init called\n"; + static const char* keywords[] = { "x", "y", "texture", "sprite_index", "scale", nullptr }; + float x = 0.0f, y = 0.0f, scale = 1.0f; + int sprite_index; + PyObject* texture; + + if (!PyArg_ParseTupleAndKeywords(args, kwds, "|ffOif", + const_cast(keywords), &x, &y, &texture, &sprite_index, &scale)) + { + return -1; + } + + // check types for texture + if (texture != NULL && !PyObject_IsInstance(texture, (PyObject*)&PyTextureType)){ + PyErr_SetString(PyExc_TypeError, "texture must be a mcrfpy.Texture instance"); + return -1; + } + auto pytexture = (PyTextureObject*)texture; + self->data = std::make_shared(pytexture->data, sprite_index, sf::Vector2f(x, y), scale); + self->data->setPosition(sf::Vector2f(x, y)); + + return 0; + } static PyTypeObject PyUISpriteType = { //PyVarObject_HEAD_INIT(NULL, 0) @@ -76,7 +203,7 @@ namespace mcrfpydef { obj->data.reset(); Py_TYPE(self)->tp_free(self); }, - .tp_repr = (reprfunc)UISprite::repr, + .tp_repr = (reprfunc)PyUISprite_repr, //.tp_hash = NULL, //.tp_iter //.tp_iternext @@ -84,9 +211,9 @@ namespace mcrfpydef { .tp_doc = PyDoc_STR("docstring"), //.tp_methods = PyUIFrame_methods, //.tp_members = PyUIFrame_members, - .tp_getset = UISprite::getsetters, + .tp_getset = PyUISprite_getsetters, //.tp_base = NULL, - .tp_init = (initproc)UISprite::init, + .tp_init = (initproc)PyUISprite_init, .tp_new = [](PyTypeObject* type, PyObject* args, PyObject* kwds) -> PyObject* { PyUISpriteObject* self = (PyUISpriteObject*)type->tp_alloc(type, 0);