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.

This commit is contained in:
John McCardle 2025-03-05 20:19:13 -05:00
parent b920a51736
commit d93311fea8
6 changed files with 72 additions and 16 deletions

View File

@ -109,3 +109,16 @@ int PyVector::set_member(PyObject* obj, PyObject* value, void* closure)
// TODO // TODO
return 0; 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;
}

View File

@ -1,6 +1,7 @@
#pragma once #pragma once
#include "Common.h" #include "Common.h"
#include "Python.h" #include "Python.h"
#include "McRFPy_API.h"
typedef struct { typedef struct {
PyObject_HEAD PyObject_HEAD
@ -22,6 +23,7 @@ public:
static PyObject* pynew(PyTypeObject* type, PyObject* args=NULL, PyObject* kwds=NULL); static PyObject* pynew(PyTypeObject* type, PyObject* args=NULL, PyObject* kwds=NULL);
static PyObject* get_member(PyObject*, void*); static PyObject* get_member(PyObject*, void*);
static int set_member(PyObject*, PyObject*, void*); static int set_member(PyObject*, PyObject*, void*);
static PyVectorObject* from_arg(PyObject*);
static PyGetSetDef getsetters[]; static PyGetSetDef getsetters[];
}; };

View File

@ -223,17 +223,30 @@ PyObject* UICaption::repr(PyUICaptionObject* self)
int UICaption::init(PyUICaptionObject* self, PyObject* args, PyObject* kwds) int UICaption::init(PyUICaptionObject* self, PyObject* args, PyObject* kwds)
{ {
using namespace mcrfpydef; using namespace mcrfpydef;
static const char* keywords[] = { "x", "y", "text", "font", "fill_color", "outline_color", "outline", nullptr }; // Constructor switch to Vector position
float x = 0.0f, y = 0.0f, outline = 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;
static const char* keywords[] = { "pos", "text", "font", "fill_color", "outline_color", "outline", nullptr };
PyObject* pos;
float outline = 0.0f;
char* text; char* text;
PyObject* font=NULL, *fill_color=NULL, *outline_color=NULL; PyObject* font=NULL, *fill_color=NULL, *outline_color=NULL;
if (!PyArg_ParseTupleAndKeywords(args, kwds, "|ffzOOOf", //if (!PyArg_ParseTupleAndKeywords(args, kwds, "|ffzOOOf",
const_cast<char**>(keywords), &x, &y, &text, &font, &fill_color, &outline_color, &outline)) // const_cast<char**>(keywords), &x, &y, &text, &font, &fill_color, &outline_color, &outline))
if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|zOOOf",
const_cast<char**>(keywords), &pos, &text, &font, &fill_color, &outline_color, &outline))
{ {
return -1; 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 // check types for font, fill_color, outline_color
std::cout << PyUnicode_AsUTF8(PyObject_Repr(font)) << std::endl; 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.setFont(Resources::game->getFont());
} }
self->data->text.setPosition(sf::Vector2f(x, y));
self->data->text.setString((std::string)text); self->data->text.setString((std::string)text);
self->data->text.setOutlineThickness(outline); self->data->text.setOutlineThickness(outline);
if (fill_color) { if (fill_color) {

View File

@ -34,18 +34,30 @@ PyObject* UIEntity::at(PyUIEntityObject* self, PyObject* o) {
} }
int UIEntity::init(PyUIEntityObject* self, PyObject* args, PyObject* kwds) { int UIEntity::init(PyUIEntityObject* self, PyObject* args, PyObject* kwds) {
static const char* keywords[] = { "x", "y", "texture", "sprite_index", "grid", nullptr }; //static const char* keywords[] = { "x", "y", "texture", "sprite_index", "grid", nullptr };
float x = 0.0f, y = 0.0f, scale = 1.0f; //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; int sprite_index = -1;
PyObject* texture = NULL; PyObject* texture = NULL;
PyObject* grid = NULL; PyObject* grid = NULL;
if (!PyArg_ParseTupleAndKeywords(args, kwds, "ffOi|O", //if (!PyArg_ParseTupleAndKeywords(args, kwds, "ffOi|O",
const_cast<char**>(keywords), &x, &y, &texture, &sprite_index, &grid)) // const_cast<char**>(keywords), &x, &y, &texture, &sprite_index, &grid))
if (!PyArg_ParseTupleAndKeywords(args, kwds, "OOi|O",
const_cast<char**>(keywords), &pos, &texture, &sprite_index, &grid))
{ {
return -1; 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 // check types for texture
// //
// Set 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 // 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->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) { if (grid != NULL) {
PyUIGridObject* pygrid = (PyUIGridObject*)grid; PyUIGridObject* pygrid = (PyUIGridObject*)grid;
self->data->grid = pygrid->data; self->data->grid = pygrid->data;

View File

@ -66,7 +66,7 @@ namespace mcrfpydef {
.tp_basicsize = sizeof(PyUIEntityObject), .tp_basicsize = sizeof(PyUIEntityObject),
.tp_itemsize = 0, .tp_itemsize = 0,
.tp_repr = (reprfunc)UIEntity::repr, .tp_repr = (reprfunc)UIEntity::repr,
.tp_flags = Py_TPFLAGS_DEFAULT, .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
.tp_doc = "UIEntity objects", .tp_doc = "UIEntity objects",
.tp_methods = UIEntity::methods, .tp_methods = UIEntity::methods,
.tp_getset = UIEntity::getsetters, .tp_getset = UIEntity::getsetters,

View File

@ -205,12 +205,28 @@ UIDrawable* UIGrid::click_at(sf::Vector2f point)
int UIGrid::init(PyUIGridObject* self, PyObject* args, PyObject* kwds) { int UIGrid::init(PyUIGridObject* self, PyObject* args, PyObject* kwds) {
int grid_x, grid_y; int grid_x, grid_y;
PyObject* textureObj; 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 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* // Convert PyObject texture to IndexTexture*
// This requires the texture object to have been initialized similar to UISprite's texture handling // 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 // 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 = new UIGrid(grid_x, grid_y, texture, sf::Vector2f(box_x, box_y), sf::Vector2f(box_w, box_h));
self->data = std::make_shared<UIGrid>(grid_x, grid_y, pyTexture->data, //self->data = std::make_shared<UIGrid>(grid_x, grid_y, pyTexture->data,
sf::Vector2f(box_x, box_y), sf::Vector2f(box_w, box_h)); // sf::Vector2f(box_x, box_y), sf::Vector2f(box_w, box_h));
self->data = std::make_shared<UIGrid>(grid_x, grid_y, pyTexture->data, pos_result->data, size_result->data);
return 0; // Success return 0; // Success
} }