Create Python base class for UIDrawables, and derive Python UI types from it #71

Open
opened 2024-04-13 03:47:13 +00:00 by john · 0 comments
Owner

It just makes good sense to mirror the type hierarchy in both Python and C++.

if .tp_base of each UIDrawable type points to a mcrfpy.Drawable class, then I wouldn't have to do this:

    if (!PyObject_IsInstance(o, (PyObject*)&PyUIFrameType) &&
        !PyObject_IsInstance(o, (PyObject*)&PyUISpriteType) &&
        !PyObject_IsInstance(o, (PyObject*)&PyUICaptionType) &&
        !PyObject_IsInstance(o, (PyObject*)&PyUIGridType)
        )

which is also fragile to new UIDrawable derived classes.

Probably the new base class should be mcrfpy._Drawable, an abstract base class which Python code can't derive from. But I can also define a separate mcrfpy.Drawable class, which derives from the base class, CAN be subclassed, and has a C++ implementation (as UserDrawable or something) to implement the interface by calling Python code.

It would be a good opportunity to test the performance of using a Python rendering function... ...but Python doesn't have draw functions yet, so user render will have to wait.

An additional possibility is making UICollection derive from mcrfpy._Drawable, and allowing users to subclass that - they could define any __init__ they want, manage the collection internally with self.append, self.remove, etc. This would be a great way to keep all render functions in C++ while making it easier to encapsulate behavior requiring multiple UI widgets, like text boxes with captions, dialogue with a sprite that changes or moves, etc.

It just makes good sense to mirror the type hierarchy in both Python and C++. if `.tp_base` of each UIDrawable type points to a `mcrfpy.Drawable` class, then I wouldn't have to do this: ``` if (!PyObject_IsInstance(o, (PyObject*)&PyUIFrameType) && !PyObject_IsInstance(o, (PyObject*)&PyUISpriteType) && !PyObject_IsInstance(o, (PyObject*)&PyUICaptionType) && !PyObject_IsInstance(o, (PyObject*)&PyUIGridType) ) ``` which is also fragile to new UIDrawable derived classes. Probably the new base class should be `mcrfpy._Drawable`, an abstract base class which Python code can't derive from. But I can also define a separate `mcrfpy.Drawable` class, which derives from the base class, CAN be subclassed, and has a C++ implementation (as `UserDrawable` or something) to implement the interface by calling Python code. It would be a good opportunity to test the performance of using a Python rendering function... ...but Python doesn't have draw functions yet, so user render will have to wait. An additional possibility is making UICollection derive from `mcrfpy._Drawable`, and allowing users to subclass that - they could define any `__init__` they want, manage the collection internally with `self.append`, `self.remove`, etc. This would be a great way to keep all render functions in C++ while making it easier to encapsulate behavior requiring multiple UI widgets, like text boxes with captions, dialogue with a sprite that changes or moves, etc.
john added the
Refactoring & Cleanup
Minor Feature
labels 2024-04-13 03:47:13 +00:00
Sign in to join this conversation.
No Milestone
No project
No Assignees
1 Participants
Notifications
Due Date
The due date is invalid or out of range. Please use the format 'yyyy-mm-dd'.

No due date set.

Dependencies

No dependencies set.

Reference: john/McRogueFace#71
No description provided.