feat(Python): establish proper inheritance hierarchy for UI types

All UIDrawable-derived Python types now properly inherit from the Drawable
base class in Python, matching the C++ inheritance structure.

Changes:
- Add Py_TPFLAGS_BASETYPE to PyDrawableType to allow inheritance
- Set tp_base = &mcrfpydef::PyDrawableType for all UI types
- Add PyDrawable.h include to UI type headers
- Rename _Drawable to Drawable and update error message

This enables proper Python inheritance: Frame, Caption, Sprite, Grid,
and Entity all inherit from Drawable, allowing shared functionality
and isinstance() checks.

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
John McCardle 2025-07-07 19:44:30 -04:00
parent e1c6c53157
commit 1d90cdab1d
6 changed files with 14 additions and 7 deletions

View File

@ -154,14 +154,14 @@ static PyMethodDef PyDrawable_methods[] = {
// Type initialization // Type initialization
static int PyDrawable_init(PyDrawableObject* self, PyObject* args, PyObject* kwds) static int PyDrawable_init(PyDrawableObject* self, PyObject* args, PyObject* kwds)
{ {
PyErr_SetString(PyExc_TypeError, "_Drawable is an abstract base class and cannot be instantiated directly"); PyErr_SetString(PyExc_TypeError, "Drawable is an abstract base class and cannot be instantiated directly");
return -1; return -1;
} }
namespace mcrfpydef { namespace mcrfpydef {
PyTypeObject PyDrawableType = { PyTypeObject PyDrawableType = {
.ob_base = {.ob_base = {.ob_refcnt = 1, .ob_type = NULL}, .ob_size = 0}, .ob_base = {.ob_base = {.ob_refcnt = 1, .ob_type = NULL}, .ob_size = 0},
.tp_name = "mcrfpy._Drawable", .tp_name = "mcrfpy.Drawable",
.tp_basicsize = sizeof(PyDrawableObject), .tp_basicsize = sizeof(PyDrawableObject),
.tp_itemsize = 0, .tp_itemsize = 0,
.tp_dealloc = (destructor)[](PyObject* self) { .tp_dealloc = (destructor)[](PyObject* self) {
@ -169,7 +169,7 @@ namespace mcrfpydef {
obj->data.reset(); obj->data.reset();
Py_TYPE(self)->tp_free(self); Py_TYPE(self)->tp_free(self);
}, },
.tp_flags = Py_TPFLAGS_DEFAULT, // | Py_TPFLAGS_BASETYPE, .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
.tp_doc = PyDoc_STR("Base class for all drawable UI elements"), .tp_doc = PyDoc_STR("Base class for all drawable UI elements"),
.tp_methods = PyDrawable_methods, .tp_methods = PyDrawable_methods,
.tp_getset = PyDrawable_getsetters, .tp_getset = PyDrawable_getsetters,

View File

@ -2,6 +2,7 @@
#include "Common.h" #include "Common.h"
#include "Python.h" #include "Python.h"
#include "UIDrawable.h" #include "UIDrawable.h"
#include "PyDrawable.h"
class UICaption: public UIDrawable class UICaption: public UIDrawable
{ {
@ -68,7 +69,7 @@ namespace mcrfpydef {
.tp_methods = UICaption_methods, .tp_methods = UICaption_methods,
//.tp_members = PyUIFrame_members, //.tp_members = PyUIFrame_members,
.tp_getset = UICaption::getsetters, .tp_getset = UICaption::getsetters,
//.tp_base = NULL, .tp_base = &mcrfpydef::PyDrawableType,
.tp_init = (initproc)UICaption::init, .tp_init = (initproc)UICaption::init,
// TODO - move tp_new to .cpp file as a static function (UICaption::new) // TODO - move tp_new to .cpp file as a static function (UICaption::new)
.tp_new = [](PyTypeObject* type, PyObject* args, PyObject* kwds) -> PyObject* .tp_new = [](PyTypeObject* type, PyObject* args, PyObject* kwds) -> PyObject*

View File

@ -8,6 +8,7 @@
#include "PyCallable.h" #include "PyCallable.h"
#include "PyTexture.h" #include "PyTexture.h"
#include "PyDrawable.h"
#include "PyColor.h" #include "PyColor.h"
#include "PyVector.h" #include "PyVector.h"
#include "PyFont.h" #include "PyFont.h"
@ -86,6 +87,7 @@ namespace mcrfpydef {
.tp_doc = "UIEntity objects", .tp_doc = "UIEntity objects",
.tp_methods = UIEntity_all_methods, .tp_methods = UIEntity_all_methods,
.tp_getset = UIEntity::getsetters, .tp_getset = UIEntity::getsetters,
.tp_base = &mcrfpydef::PyDrawableType,
.tp_init = (initproc)UIEntity::init, .tp_init = (initproc)UIEntity::init,
.tp_new = PyType_GenericNew, .tp_new = PyType_GenericNew,
}; };

View File

@ -8,6 +8,7 @@
#include "PyCallable.h" #include "PyCallable.h"
#include "PyColor.h" #include "PyColor.h"
#include "PyDrawable.h"
#include "PyVector.h" #include "PyVector.h"
#include "UIDrawable.h" #include "UIDrawable.h"
#include "UIBase.h" #include "UIBase.h"
@ -89,7 +90,7 @@ namespace mcrfpydef {
.tp_methods = UIFrame_methods, .tp_methods = UIFrame_methods,
//.tp_members = PyUIFrame_members, //.tp_members = PyUIFrame_members,
.tp_getset = UIFrame::getsetters, .tp_getset = UIFrame::getsetters,
//.tp_base = NULL, .tp_base = &mcrfpydef::PyDrawableType,
.tp_init = (initproc)UIFrame::init, .tp_init = (initproc)UIFrame::init,
.tp_new = [](PyTypeObject* type, PyObject* args, PyObject* kwds) -> PyObject* .tp_new = [](PyTypeObject* type, PyObject* args, PyObject* kwds) -> PyObject*
{ {

View File

@ -8,6 +8,7 @@
#include "PyCallable.h" #include "PyCallable.h"
#include "PyTexture.h" #include "PyTexture.h"
#include "PyDrawable.h"
#include "PyColor.h" #include "PyColor.h"
#include "PyVector.h" #include "PyVector.h"
#include "PyFont.h" #include "PyFont.h"
@ -39,6 +40,7 @@ public:
sf::FloatRect get_bounds() const override; sf::FloatRect get_bounds() const override;
void move(float dx, float dy) override; void move(float dx, float dy) override;
void resize(float w, float h) override; void resize(float w, float h) override;
void onPositionChanged() override;
int grid_x, grid_y; int grid_x, grid_y;
//int grid_size; // grid sizes are implied by IndexTexture now //int grid_size; // grid sizes are implied by IndexTexture now
@ -153,7 +155,7 @@ namespace mcrfpydef {
.tp_methods = UIGrid_all_methods, .tp_methods = UIGrid_all_methods,
//.tp_members = UIGrid::members, //.tp_members = UIGrid::members,
.tp_getset = UIGrid::getsetters, .tp_getset = UIGrid::getsetters,
//.tp_base = NULL, .tp_base = &mcrfpydef::PyDrawableType,
.tp_init = (initproc)UIGrid::init, .tp_init = (initproc)UIGrid::init,
.tp_new = [](PyTypeObject* type, PyObject* args, PyObject* kwds) -> PyObject* .tp_new = [](PyTypeObject* type, PyObject* args, PyObject* kwds) -> PyObject*
{ {

View File

@ -8,6 +8,7 @@
#include "PyCallable.h" #include "PyCallable.h"
#include "PyTexture.h" #include "PyTexture.h"
#include "PyDrawable.h"
#include "PyColor.h" #include "PyColor.h"
#include "PyVector.h" #include "PyVector.h"
#include "PyFont.h" #include "PyFont.h"
@ -95,7 +96,7 @@ namespace mcrfpydef {
.tp_methods = UISprite_methods, .tp_methods = UISprite_methods,
//.tp_members = PyUIFrame_members, //.tp_members = PyUIFrame_members,
.tp_getset = UISprite::getsetters, .tp_getset = UISprite::getsetters,
//.tp_base = NULL, .tp_base = &mcrfpydef::PyDrawableType,
.tp_init = (initproc)UISprite::init, .tp_init = (initproc)UISprite::init,
.tp_new = [](PyTypeObject* type, PyObject* args, PyObject* kwds) -> PyObject* .tp_new = [](PyTypeObject* type, PyObject* args, PyObject* kwds) -> PyObject*
{ {