From d93311fea891ec3fd829084ae5156ca4b97c1ada Mon Sep 17 00:00:00 2001 From: John McCardle Date: Wed, 5 Mar 2025 20:19:13 -0500 Subject: [PATCH] changes to PyVector, UICaption, UIEntity, and UIGrid. They seem minor, but I can't remember what I was working on. They're going to be shipped with 7DRL, so I figured I better get them in branch. --- src/PyVector.cpp | 13 +++++++++++++ src/PyVector.h | 2 ++ src/UICaption.cpp | 24 ++++++++++++++++++------ src/UIEntity.cpp | 22 +++++++++++++++++----- src/UIEntity.h | 2 +- src/UIGrid.cpp | 25 +++++++++++++++++++++---- 6 files changed, 72 insertions(+), 16 deletions(-) diff --git a/src/PyVector.cpp b/src/PyVector.cpp index adf2aff..866431b 100644 --- a/src/PyVector.cpp +++ b/src/PyVector.cpp @@ -109,3 +109,16 @@ int PyVector::set_member(PyObject* obj, PyObject* value, void* closure) // TODO return 0; } + +PyVectorObject* PyVector::from_arg(PyObject* args) +{ + auto type = (PyTypeObject*)PyObject_GetAttrString(McRFPy_API::mcrf_module, "Vector"); + if (PyObject_IsInstance(args, (PyObject*)type)) return (PyVectorObject*)args; + auto obj = (PyVectorObject*)type->tp_alloc(type, 0); + int err = init(obj, args, NULL); + if (err) { + Py_DECREF(obj); + return NULL; + } + return obj; +} diff --git a/src/PyVector.h b/src/PyVector.h index 6ea0622..f678ad6 100644 --- a/src/PyVector.h +++ b/src/PyVector.h @@ -1,6 +1,7 @@ #pragma once #include "Common.h" #include "Python.h" +#include "McRFPy_API.h" typedef struct { PyObject_HEAD @@ -22,6 +23,7 @@ public: static PyObject* pynew(PyTypeObject* type, PyObject* args=NULL, PyObject* kwds=NULL); static PyObject* get_member(PyObject*, void*); static int set_member(PyObject*, PyObject*, void*); + static PyVectorObject* from_arg(PyObject*); static PyGetSetDef getsetters[]; }; diff --git a/src/UICaption.cpp b/src/UICaption.cpp index 5daddaf..d960c25 100644 --- a/src/UICaption.cpp +++ b/src/UICaption.cpp @@ -223,17 +223,30 @@ 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", "outline", nullptr }; - float x = 0.0f, y = 0.0f, outline = 0.0f; + // Constructor switch to Vector position + //static const char* keywords[] = { "x", "y", "text", "font", "fill_color", "outline_color", "outline", nullptr }; + //float x = 0.0f, y = 0.0f, outline = 0.0f; + static const char* keywords[] = { "pos", "text", "font", "fill_color", "outline_color", "outline", nullptr }; + PyObject* pos; + float outline = 0.0f; char* text; PyObject* font=NULL, *fill_color=NULL, *outline_color=NULL; - if (!PyArg_ParseTupleAndKeywords(args, kwds, "|ffzOOOf", - const_cast(keywords), &x, &y, &text, &font, &fill_color, &outline_color, &outline)) + //if (!PyArg_ParseTupleAndKeywords(args, kwds, "|ffzOOOf", + // const_cast(keywords), &x, &y, &text, &font, &fill_color, &outline_color, &outline)) + if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|zOOOf", + const_cast(keywords), &pos, &text, &font, &fill_color, &outline_color, &outline)) { return -1; } - + + PyVectorObject* pos_result = PyVector::from_arg(pos); + if (!pos_result) + { + PyErr_SetString(PyExc_TypeError, "pos must be a mcrfpy.Vector instance or arguments to mcrfpy.Vector.__init__"); + return -1; + } + self->data->text.setPosition(pos_result->data); // check types for font, fill_color, outline_color std::cout << PyUnicode_AsUTF8(PyObject_Repr(font)) << std::endl; @@ -252,7 +265,6 @@ int UICaption::init(PyUICaptionObject* self, PyObject* args, PyObject* kwds) //self->data->text.setFont(Resources::game->getFont()); } - self->data->text.setPosition(sf::Vector2f(x, y)); self->data->text.setString((std::string)text); self->data->text.setOutlineThickness(outline); if (fill_color) { diff --git a/src/UIEntity.cpp b/src/UIEntity.cpp index d88fbe2..db8073a 100644 --- a/src/UIEntity.cpp +++ b/src/UIEntity.cpp @@ -34,18 +34,30 @@ PyObject* UIEntity::at(PyUIEntityObject* self, PyObject* o) { } int UIEntity::init(PyUIEntityObject* self, PyObject* args, PyObject* kwds) { - static const char* keywords[] = { "x", "y", "texture", "sprite_index", "grid", nullptr }; - float x = 0.0f, y = 0.0f, scale = 1.0f; + //static const char* keywords[] = { "x", "y", "texture", "sprite_index", "grid", nullptr }; + //float x = 0.0f, y = 0.0f, scale = 1.0f; + static const char* keywords[] = { "pos", "texture", "sprite_index", "grid", nullptr }; + PyObject* pos; + float scale = 1.0f; int sprite_index = -1; PyObject* texture = NULL; PyObject* grid = NULL; - if (!PyArg_ParseTupleAndKeywords(args, kwds, "ffOi|O", - const_cast(keywords), &x, &y, &texture, &sprite_index, &grid)) + //if (!PyArg_ParseTupleAndKeywords(args, kwds, "ffOi|O", + // const_cast(keywords), &x, &y, &texture, &sprite_index, &grid)) + if (!PyArg_ParseTupleAndKeywords(args, kwds, "OOi|O", + const_cast(keywords), &pos, &texture, &sprite_index, &grid)) { return -1; } + PyVectorObject* pos_result = PyVector::from_arg(pos); + if (!pos_result) + { + PyErr_SetString(PyExc_TypeError, "pos must be a mcrfpy.Vector instance or arguments to mcrfpy.Vector.__init__"); + return -1; + } + // check types for texture // // Set Texture @@ -75,7 +87,7 @@ int UIEntity::init(PyUIEntityObject* self, PyObject* args, PyObject* kwds) { // TODO - PyTextureObjects and IndexTextures are a little bit of a mess with shared/unshared pointers self->data->sprite = UISprite(pytexture->data, sprite_index, sf::Vector2f(0,0), 1.0); - self->data->position = sf::Vector2f(x, y); + self->data->position = pos_result->data; if (grid != NULL) { PyUIGridObject* pygrid = (PyUIGridObject*)grid; self->data->grid = pygrid->data; diff --git a/src/UIEntity.h b/src/UIEntity.h index d65678c..042a933 100644 --- a/src/UIEntity.h +++ b/src/UIEntity.h @@ -66,7 +66,7 @@ namespace mcrfpydef { .tp_basicsize = sizeof(PyUIEntityObject), .tp_itemsize = 0, .tp_repr = (reprfunc)UIEntity::repr, - .tp_flags = Py_TPFLAGS_DEFAULT, + .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, .tp_doc = "UIEntity objects", .tp_methods = UIEntity::methods, .tp_getset = UIEntity::getsetters, diff --git a/src/UIGrid.cpp b/src/UIGrid.cpp index 0ea7d94..2d95120 100644 --- a/src/UIGrid.cpp +++ b/src/UIGrid.cpp @@ -205,12 +205,28 @@ UIDrawable* UIGrid::click_at(sf::Vector2f point) int UIGrid::init(PyUIGridObject* self, PyObject* args, PyObject* kwds) { int grid_x, grid_y; PyObject* textureObj; - float box_x, box_y, box_w, box_h; + //float box_x, box_y, box_w, box_h; + PyObject* pos, *size; - if (!PyArg_ParseTuple(args, "iiOffff", &grid_x, &grid_y, &textureObj, &box_x, &box_y, &box_w, &box_h)) { + //if (!PyArg_ParseTuple(args, "iiOffff", &grid_x, &grid_y, &textureObj, &box_x, &box_y, &box_w, &box_h)) { + if (!PyArg_ParseTuple(args, "iiOOO", &grid_x, &grid_y, &textureObj, &pos, &size)) { return -1; // If parsing fails, return an error } + PyVectorObject* pos_result = PyVector::from_arg(pos); + if (!pos_result) + { + PyErr_SetString(PyExc_TypeError, "pos must be a mcrfpy.Vector instance or arguments to mcrfpy.Vector.__init__"); + return -1; + } + + PyVectorObject* size_result = PyVector::from_arg(size); + if (!size_result) + { + PyErr_SetString(PyExc_TypeError, "pos must be a mcrfpy.Vector instance or arguments to mcrfpy.Vector.__init__"); + return -1; + } + // Convert PyObject texture to IndexTexture* // This requires the texture object to have been initialized similar to UISprite's texture handling @@ -225,8 +241,9 @@ int UIGrid::init(PyUIGridObject* self, PyObject* args, PyObject* kwds) { // Initialize UIGrid //self->data = new UIGrid(grid_x, grid_y, texture, sf::Vector2f(box_x, box_y), sf::Vector2f(box_w, box_h)); - self->data = std::make_shared(grid_x, grid_y, pyTexture->data, - sf::Vector2f(box_x, box_y), sf::Vector2f(box_w, box_h)); + //self->data = std::make_shared(grid_x, grid_y, pyTexture->data, + // sf::Vector2f(box_x, box_y), sf::Vector2f(box_w, box_h)); + self->data = std::make_shared(grid_x, grid_y, pyTexture->data, pos_result->data, size_result->data); return 0; // Success }