2024-04-20 10:32:04 -04:00
|
|
|
#pragma once
|
|
|
|
|
#include "Common.h"
|
|
|
|
|
#include "Python.h"
|
|
|
|
|
#include "structmember.h"
|
|
|
|
|
#include "IndexTexture.h"
|
|
|
|
|
#include "Resources.h"
|
|
|
|
|
#include <list>
|
|
|
|
|
|
|
|
|
|
#include "PyCallable.h"
|
|
|
|
|
#include "PyTexture.h"
|
|
|
|
|
#include "PyColor.h"
|
|
|
|
|
#include "PyVector.h"
|
|
|
|
|
#include "PyFont.h"
|
|
|
|
|
|
|
|
|
|
#include "Resources.h"
|
|
|
|
|
#include "UIBase.h"
|
|
|
|
|
class UIFrame; class UICaption; class UISprite; class UIEntity; class UIGrid;
|
|
|
|
|
|
|
|
|
|
enum PyObjectsEnum : int
|
|
|
|
|
{
|
|
|
|
|
UIFRAME = 1,
|
|
|
|
|
UICAPTION,
|
|
|
|
|
UISPRITE,
|
|
|
|
|
UIGRID
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
class UIDrawable
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
void render();
|
2025-03-05 20:21:24 -05:00
|
|
|
//virtual void render(sf::Vector2f) = 0;
|
|
|
|
|
virtual void render(sf::Vector2f, sf::RenderTarget&) = 0;
|
2024-04-20 10:32:04 -04:00
|
|
|
virtual PyObjectsEnum derived_type() = 0;
|
|
|
|
|
|
|
|
|
|
// Mouse input handling - callable object, methods to find event's destination
|
|
|
|
|
std::unique_ptr<PyClickCallable> click_callable;
|
|
|
|
|
virtual UIDrawable* click_at(sf::Vector2f point) = 0;
|
|
|
|
|
void click_register(PyObject*);
|
|
|
|
|
void click_unregister();
|
|
|
|
|
|
|
|
|
|
UIDrawable();
|
|
|
|
|
|
|
|
|
|
static PyObject* get_click(PyObject* self, void* closure);
|
|
|
|
|
static int set_click(PyObject* self, PyObject* value, void* closure);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
|
PyObject_HEAD
|
|
|
|
|
std::shared_ptr<std::vector<std::shared_ptr<UIDrawable>>> data;
|
|
|
|
|
} PyUICollectionObject;
|
|
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
|
PyObject_HEAD
|
|
|
|
|
std::shared_ptr<std::vector<std::shared_ptr<UIDrawable>>> data;
|
|
|
|
|
int index;
|
|
|
|
|
int start_size;
|
|
|
|
|
} PyUICollectionIterObject;
|
|
|
|
|
|
|
|
|
|
namespace mcrfpydef {
|
Iterators, other Python C API improvements
closes #72
ref #69 - this resolves the "UICollection" (not "UIEntityCollection", perhaps renamed since the issue opened) and "UIEntityCollection" portion. The Grid point based iterators were not updated.
**RPATH updates**
Will this RPATH setting allow McRogueFace to execute using its included "lib" subdirectory after being unzipped on a new computer?
The change from "./lib" to "$ORIGIN/./lib" improves portability. The $ORIGIN token is a special Linux/Unix convention that refers to the directory containing the executable itself. This makes the path relative to the executable's location rather than the current working directory, which means McRogueFace will correctly find its libraries in the lib subdirectory regardless of where it's run from after being unzipped on a new computer.
**New standard object initialization**
PyColor, PyVector
- Fixed all 15 PyTypeObject definitions to use proper designated initializer syntax
- Replaced PyType_GenericAlloc usage in PyColor.cpp and PyVector.cpp
- Updated PyObject_New usage in UIEntity.cpp
- All object creation now uses module-based type lookups instead of static references
- Created centralized utilities in PyObjectUtils.h
**RAII Wrappers**
automatic reference counting via C++ object lifecycle
- Created PyRAII.h with PyObjectRef and PyTypeRef classes
- These provide automatic reference counting management
- Updated PyColor::from_arg() to demonstrate RAII usage
- Prevents memory leaks and reference counting errors
**Python object base in type defs:**
`.ob_base = {.ob_base = {.ob_refcnt = 1, .ob_type = NULL}, .ob_size = 0}`
PyColor, PyTexture, PyVector, UICaption, UICollection, UIEntity, UIFrame, UIGrid
**convertDrawableToPython**
replace crazy macro to detect the correct Python type of a UIDrawable instance
- Removed the problematic macro from UIDrawable.h
- Created template-based functions in PyObjectUtils.h
- Updated UICollection.cpp to use local helper function
- The new approach is cleaner, more debuggable, and avoids static type references
**Iterator fixes**
tp_iter on UICollection, UIGrid, UIGridPoint, UISprite
UIGrid logic improved, standard
**List vs Vector usage analysis**
there are different use cases that weren't standardized:
- UICollection (for Frame children) uses std::vector<std::shared_ptr<UIDrawable>>
- UIEntityCollection (for Grid entities) uses std::list<std::shared_ptr<UIEntity>>
The rationale is currently connected to frequency of expected changes.
* A "UICollection" is likely either all visible or not; it's also likely to be created once and have a static set of contents. They should be contiguous in memory in hopes that this helps rendering speed.
* A "UIEntityCollection" is expected to be rendered as a subset within the visible rectangle of the UIGrid. Scrolling the grid or gameplay logic is likely to frequently create and destroy entities. In general I expect Entity collections to have a much higher common size than UICollections. For these reasons I've made them Lists in hopes that they never have to be reallocated or moved during a frame.
2025-05-31 08:58:52 -04:00
|
|
|
// DEPRECATED: RET_PY_INSTANCE macro has been replaced with template functions in PyObjectUtils.h
|
|
|
|
|
// The macro was difficult to debug and used static type references that could cause initialization order issues.
|
|
|
|
|
// Use PyObjectUtils::convertDrawableToPython() or PyObjectUtils::createPyObject<T>() instead.
|
2024-04-20 10:32:04 -04:00
|
|
|
|
|
|
|
|
//TODO: add this method to class scope; move implementation to .cpp file
|
|
|
|
|
/*
|
|
|
|
|
static PyObject* PyUIDrawable_get_click(PyObject* self, void* closure) {
|
|
|
|
|
PyObjectsEnum objtype = static_cast<PyObjectsEnum>(reinterpret_cast<long>(closure)); // trust me bro, it's an Enum
|
|
|
|
|
PyObject* ptr;
|
|
|
|
|
|
|
|
|
|
switch (objtype)
|
|
|
|
|
{
|
|
|
|
|
case PyObjectsEnum::UIFRAME:
|
|
|
|
|
ptr = ((PyUIFrameObject*)self)->data->click_callable->borrow();
|
|
|
|
|
break;
|
|
|
|
|
case PyObjectsEnum::UICAPTION:
|
|
|
|
|
ptr = ((PyUICaptionObject*)self)->data->click_callable->borrow();
|
|
|
|
|
break;
|
|
|
|
|
case PyObjectsEnum::UISPRITE:
|
|
|
|
|
ptr = ((PyUISpriteObject*)self)->data->click_callable->borrow();
|
|
|
|
|
break;
|
|
|
|
|
case PyObjectsEnum::UIGRID:
|
|
|
|
|
ptr = ((PyUIGridObject*)self)->data->click_callable->borrow();
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
PyErr_SetString(PyExc_TypeError, "no idea how you did that; invalid UIDrawable derived instance for _get_click");
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
if (ptr && ptr != Py_None)
|
|
|
|
|
return ptr;
|
|
|
|
|
else
|
|
|
|
|
return Py_None;
|
|
|
|
|
}*/
|
|
|
|
|
|
|
|
|
|
//TODO: add this method to class scope; move implementation to .cpp file
|
|
|
|
|
/*
|
|
|
|
|
static int PyUIDrawable_set_click(PyObject* self, PyObject* value, void* closure) {
|
|
|
|
|
PyObjectsEnum objtype = static_cast<PyObjectsEnum>(reinterpret_cast<long>(closure)); // trust me bro, it's an Enum
|
|
|
|
|
UIDrawable* target;
|
|
|
|
|
switch (objtype)
|
|
|
|
|
{
|
|
|
|
|
case PyObjectsEnum::UIFRAME:
|
|
|
|
|
target = (((PyUIFrameObject*)self)->data.get());
|
|
|
|
|
break;
|
|
|
|
|
case PyObjectsEnum::UICAPTION:
|
|
|
|
|
target = (((PyUICaptionObject*)self)->data.get());
|
|
|
|
|
break;
|
|
|
|
|
case PyObjectsEnum::UISPRITE:
|
|
|
|
|
target = (((PyUISpriteObject*)self)->data.get());
|
|
|
|
|
break;
|
|
|
|
|
case PyObjectsEnum::UIGRID:
|
|
|
|
|
target = (((PyUIGridObject*)self)->data.get());
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
PyErr_SetString(PyExc_TypeError, "no idea how you did that; invalid UIDrawable derived instance for _set_click");
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (value == Py_None)
|
|
|
|
|
{
|
|
|
|
|
target->click_unregister();
|
|
|
|
|
} else {
|
|
|
|
|
target->click_register(value);
|
|
|
|
|
}
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
*/
|
|
|
|
|
}
|