Squashed commit of the following: [reprs_and_member_names]

Closes #22
Closes #23
Closes #24
Closes #25
Closes #31
Closes #56

commit 43fac8f4f3
Author: John McCardle <mccardle.john@gmail.com>
Date:   Sat Apr 20 18:32:52 2024 -0400

    Typo in UIFrame repr

commit 3fd5ad93e2
Author: John McCardle <mccardle.john@gmail.com>
Date:   Sat Apr 20 18:32:30 2024 -0400

    Add UIGridPoint and UIGridPointState repr

commit 03376897b8
Author: John McCardle <mccardle.john@gmail.com>
Date:   Sat Apr 20 18:32:17 2024 -0400

    Add UIGrid repr

commit 48af072a33
Author: John McCardle <mccardle.john@gmail.com>
Date:   Sat Apr 20 18:32:05 2024 -0400

    Add UIEntity repr
This commit is contained in:
John McCardle 2024-04-20 18:33:18 -04:00
parent c2de9b08d6
commit 232105a893
7 changed files with 76 additions and 5 deletions

View File

@ -163,3 +163,15 @@ PyGetSetDef UIEntity::getsetters[] = {
{"sprite_number", (getter)UIEntity::get_spritenumber, (setter)UIEntity::set_spritenumber, "Sprite number (index) on the texture on the display", NULL},
{NULL} /* Sentinel */
};
PyObject* UIEntity::repr(PyUIEntityObject* self) {
std::ostringstream ss;
if (!self->data) ss << "<Entity (invalid internal object)>";
else {
auto ent = self->data;
ss << "<Entity (x=" << self->data->position.x << ", y=" << self->data->position.y << ", sprite_number=" << self->data->sprite.getSpriteIndex() <<
")>";
}
std::string repr_str = ss.str();
return PyUnicode_DecodeUTF8(repr_str.c_str(), repr_str.size(), "replace");
}

View File

@ -55,6 +55,7 @@ public:
static int set_spritenumber(PyUIEntityObject* self, PyObject* value, void* closure);
static PyMethodDef methods[];
static PyGetSetDef getsetters[];
static PyObject* repr(PyUIEntityObject* self);
};
namespace mcrfpydef {
@ -63,7 +64,7 @@ namespace mcrfpydef {
.tp_name = "mcrfpy.Entity",
.tp_basicsize = sizeof(PyUIEntityObject),
.tp_itemsize = 0,
// Methods omitted for brevity
.tp_repr = (reprfunc)UIEntity::repr,
.tp_flags = Py_TPFLAGS_DEFAULT,
.tp_doc = "UIEntity objects",
.tp_methods = UIEntity::methods,

View File

@ -225,7 +225,7 @@ PyObject* UIFrame::repr(PyUIFrameObject* self)
auto box = self->data->box;
auto fc = box.getFillColor();
auto oc = box.getOutlineColor();
ss << "<Frame (x=" << box.getPosition().x << ", y=" << box.getPosition().y << ", x=" <<
ss << "<Frame (x=" << box.getPosition().x << ", y=" << box.getPosition().y << ", w=" <<
box.getSize().x << ", w=" << box.getSize().y << ", " <<
"outline=" << box.getOutlineThickness() << ", " <<
"fill_color=(" << (int)fc.r << ", " << (int)fc.g << ", " << (int)fc.b << ", " << (int)fc.a <<"), " <<

View File

@ -421,6 +421,37 @@ PyObject* UIGrid::get_children(PyUIGridObject* self, void* closure)
return (PyObject*)o;
}
PyObject* UIGrid::repr(PyUIGridObject* self)
{
// if (member_ptr == 0) // x
// self->data->box.setPosition(val, self->data->box.getPosition().y);
// else if (member_ptr == 1) // y
// self->data->box.setPosition(self->data->box.getPosition().x, val);
// else if (member_ptr == 2) // w
// self->data->box.setSize(sf::Vector2f(val, self->data->box.getSize().y));
// else if (member_ptr == 3) // h
// self->data->box.setSize(sf::Vector2f(self->data->box.getSize().x, val));
// else if (member_ptr == 4) // center_x
// self->data->center_x = val;
// else if (member_ptr == 5) // center_y
// self->data->center_y = val;
// else if (member_ptr == 6) // zoom
// self->data->zoom = val;
std::ostringstream ss;
if (!self->data) ss << "<Grid (invalid internal object)>";
else {
auto grid = self->data;
auto box = grid->box;
ss << "<Grid (x=" << box.getPosition().x << ", y=" << box.getPosition().y << ", w=" << box.getSize().x << ", h=" << box.getSize().y << ", " <<
"center=(" << grid->center_x << ", " << grid->center_y << "), zoom=" << grid->zoom <<
")>";
}
std::string repr_str = ss.str();
return PyUnicode_DecodeUTF8(repr_str.c_str(), repr_str.size(), "replace");
}
/* // TODO standard pointer would need deleted, but I opted for a shared pointer. tp_dealloc currently not even defined in the PyTypeObject
void PyUIGrid_dealloc(PyUIGridObject* self) {
delete self->data; // Clean up the allocated UIGrid object

View File

@ -58,6 +58,7 @@ public:
static PyMethodDef methods[];
static PyGetSetDef getsetters[];
static PyObject* get_children(PyUIGridObject* self, void* closure);
static PyObject* repr(PyUIGridObject* self);
};
@ -109,7 +110,7 @@ namespace mcrfpydef {
// Py_TYPE(self)->tp_free(self);
//},
//TODO - PyUIGrid REPR def:
// .tp_repr = (reprfunc)UIGrid::repr,
.tp_repr = (reprfunc)UIGrid::repr,
//.tp_hash = NULL,
//.tp_iter
//.tp_iternext

View File

@ -98,6 +98,19 @@ PyGetSetDef UIGridPoint::getsetters[] = {
{NULL} /* Sentinel */
};
PyObject* UIGridPoint::repr(PyUIGridPointObject* self) {
std::ostringstream ss;
if (!self->data) ss << "<GridPoint (invalid internal object)>";
else {
auto gp = self->data;
ss << "<GridPoint (walkable=" << (gp->walkable ? "True" : "False") << ", transparent=" << (gp->transparent ? "True" : "False") <<
", tilesprite=" << gp->tilesprite << ", tile_overlay=" << gp->tile_overlay << ", uisprite=" << gp->uisprite <<
")>";
}
std::string repr_str = ss.str();
return PyUnicode_DecodeUTF8(repr_str.c_str(), repr_str.size(), "replace");
}
PyObject* UIGridPointState::get_bool_member(PyUIGridPointStateObject* self, void* closure) {
if (reinterpret_cast<long>(closure) == 0) { // visible
return PyBool_FromLong(self->data->visible);
@ -132,3 +145,14 @@ PyGetSetDef UIGridPointState::getsetters[] = {
{NULL} /* Sentinel */
};
PyObject* UIGridPointState::repr(PyUIGridPointStateObject* self) {
std::ostringstream ss;
if (!self->data) ss << "<GridPointState (invalid internal object)>";
else {
auto gps = self->data;
ss << "<GridPointState (visible=" << (gps->visible ? "True" : "False") << ", discovered=" << (gps->discovered ? "True" : "False") <<
")>";
}
std::string repr_str = ss.str();
return PyUnicode_DecodeUTF8(repr_str.c_str(), repr_str.size(), "replace");
}

View File

@ -49,6 +49,7 @@ public:
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);
static PyObject* repr(PyUIGridPointObject* self);
};
// UIGridPointState - entity-specific info for each cell
@ -60,6 +61,7 @@ public:
static PyObject* get_bool_member(PyUIGridPointStateObject* self, void* closure);
static int set_bool_member(PyUIGridPointStateObject* self, PyObject* value, void* closure);
static PyGetSetDef getsetters[];
static PyObject* repr(PyUIGridPointStateObject* self);
};
namespace mcrfpydef {
@ -68,7 +70,7 @@ namespace mcrfpydef {
.tp_name = "mcrfpy.GridPoint",
.tp_basicsize = sizeof(PyUIGridPointObject),
.tp_itemsize = 0,
// Methods omitted for brevity
.tp_repr = (reprfunc)UIGridPoint::repr,
.tp_flags = Py_TPFLAGS_DEFAULT,
.tp_doc = "UIGridPoint object",
.tp_getset = UIGridPoint::getsetters,
@ -81,7 +83,7 @@ namespace mcrfpydef {
.tp_name = "mcrfpy.GridPointState",
.tp_basicsize = sizeof(PyUIGridPointStateObject),
.tp_itemsize = 0,
// Methods omitted for brevity
.tp_repr = (reprfunc)UIGridPointState::repr,
.tp_flags = Py_TPFLAGS_DEFAULT,
.tp_doc = "UIGridPointState object", // TODO: Add PyUIGridPointState tp_init
.tp_getset = UIGridPointState::getsetters,