Color container type for Python working. I still need to implement UIFrame using it.

This commit is contained in:
John McCardle 2023-08-28 05:44:26 -04:00
commit c4d5a497d4
4 changed files with 159 additions and 240 deletions

238
src/UI.h
View file

@ -17,9 +17,16 @@ public:
class UIFrame: public UIDrawable
{
public:
sf::RectangleShape box;
UIFrame(float, float, float, float);
UIFrame();
//sf::RectangleShape box;
//Simulate RectangleShape
sf::Color fillColor, outlineColor;
float x, y, w, h, outline;
std::vector<UIDrawable*> children;
void render(sf::Vector2f) override final;
void move(sf::Vector2f);
};
class UICaption: public UIDrawable
@ -39,7 +46,112 @@ public:
};
namespace mcrfpydef {
// Color Definitions
// struct, members, new, set_member, PyTypeObject
typedef struct {
PyObject_HEAD
sf::Color color;
} PyColorObject;
static PyMemberDef PyColor_members[]
{
{"r", T_BYTE, offsetof(PyColorObject, color.r), 0},
{"g", T_BYTE, offsetof(PyColorObject, color.g), 0},
{"b", T_BYTE, offsetof(PyColorObject, color.b), 0},
{"a", T_BYTE, offsetof(PyColorObject, color.a), 0},
{NULL}
};
static PyObject* PyColor_new(PyTypeObject* type, PyObject* args, PyObject* kwds)
{
PyColorObject* self = (PyColorObject*)type->tp_alloc(type, 0);
if (self != NULL)
{
self->color.r = 0;
self->color.g = 0;
self->color.b = 0;
self->color.a = 255;
}
return (PyObject*) self;
}
static PyObject* PyColor_get_member(PyColorObject* self, void* closure)
{
auto member_ptr = reinterpret_cast<long>(closure);
if (member_ptr == offsetof(PyColorObject, color.r))
return PyLong_FromLong(self->color.r);
else if (member_ptr == offsetof(PyColorObject, color.g))
return PyLong_FromLong(self->color.g);
else if (member_ptr == offsetof(PyColorObject, color.b))
return PyLong_FromLong(self->color.b);
else if (member_ptr == offsetof(PyColorObject, color.a))
return PyLong_FromLong(self->color.a);
else
{
PyErr_SetString(PyExc_AttributeError, "Invalid attribute");
return nullptr;
}
}
static int PyColor_set_member(PyColorObject* self, PyObject* value, void* closure)
{
if (PyLong_Check(value))
{
long int_val = PyLong_AsLong(value);
if (int_val < 0)
int_val = 0;
else if (int_val > 255)
int_val = 255;
auto member_ptr = reinterpret_cast<long>(closure);
if (member_ptr == offsetof(PyColorObject, color.r))
self->color.r = static_cast<sf::Uint8>(int_val);
else if (member_ptr == offsetof(PyColorObject, color.g))
self->color.g = static_cast<sf::Uint8>(int_val);
else if (member_ptr == offsetof(PyColorObject, color.b))
self->color.b = static_cast<sf::Uint8>(int_val);
else if (member_ptr == offsetof(PyColorObject, color.a))
self->color.a = static_cast<sf::Uint8>(int_val);
}
else
{
PyErr_SetString(PyExc_TypeError, "Value must be an integer.");
return -1;
}
return 0;
}
static PyGetSetDef PyColor_getsetters[] = {
{"r", (getter)PyColor_get_member, (setter)PyColor_set_member, "Red component", (void*)offsetof(PyColorObject, color.r)},
{"g", (getter)PyColor_get_member, (setter)PyColor_set_member, "Green component", (void*)offsetof(PyColorObject, color.g)},
{"b", (getter)PyColor_get_member, (setter)PyColor_set_member, "Blue component", (void*)offsetof(PyColorObject, color.b)},
{"a", (getter)PyColor_get_member, (setter)PyColor_set_member, "Alpha component", (void*)offsetof(PyColorObject, color.a)},
{NULL}
};
static PyTypeObject PyColorType = {
//PyVarObject_HEAD_INIT(NULL, 0)
.tp_name = "mcrfpy.Color",
.tp_basicsize = sizeof(PyColorObject),
.tp_itemsize = 0,
//.tp_dealloc = [](PyObject* obj) { Py_TYPE(obj)->tp_free(obj); },
//.tp_repr = (reprfunc)PyUIFrame_repr,
//.tp_hash = NULL,
//.tp_iter
//.tp_iternext
.tp_flags = Py_TPFLAGS_DEFAULT,
.tp_doc = PyDoc_STR("SFML Color object (RGBA)"),
//.tp_methods = PyUIFrame_methods,
//.tp_members = PyColor_members,
.tp_getset = PyColor_getsetters,
//.tp_base = NULL,
//.tp_init = (initproc)PyUIFrame_init,
.tp_new = PyColor_new, //PyType_GenericNew ?
};
// UIFrame Definitions
// new, init, repr, set_size, methods, members, PyTypeObject
static PyObject* PyUIFrame_new(PyTypeObject* type, PyObject* args, PyObject* kwds)
{
@ -80,9 +192,9 @@ namespace mcrfpydef {
static PyObject* PyUIFrame_setSize(UIFrame* self, PyObject* args)
{
float w, h;
if (!PyArg_ParseTuple(args, "ff", &w, &h)) return (PyObject*)-1;
self->box.setSize(sf::Vector2f(w, h));
//float w, h;
if (!PyArg_ParseTuple(args, "ff", &self->w, &self->h)) return (PyObject*)-1;
//self->box.setSize(sf::Vector2f(w, h));
Py_INCREF(Py_None);
//return PyFloat_FromDouble(mag);
return Py_None;
@ -103,7 +215,6 @@ namespace mcrfpydef {
static PyTypeObject PyUIFrameType = {
//PyVarObject_HEAD_INIT(NULL, 0)
//.tp_base = NULL,
.tp_name = "mcrfpy.UIFrame",
.tp_basicsize = sizeof(UIFrame),
.tp_itemsize = 0,
@ -114,123 +225,12 @@ namespace mcrfpydef {
//.tp_iternext
.tp_flags = Py_TPFLAGS_DEFAULT,
.tp_doc = PyDoc_STR("Custom UIFrame object"),
//.tp_methods = PyUIFrame_methods,
.tp_methods = PyUIFrame_methods,
//.tp_members = PyUIFrame_members,
//.tp_base = NULL,
.tp_init = (initproc)PyUIFrame_init,
.tp_new = PyUIFrame_new, //PyType_GenericNew ?
};
// module
/*
static PyModuleDef ui_module = {
PyModuleDef_HEAD_INIT,
.m_name = "ui",
.m_size = -1,
};
PyMODINIT_FUNC PyInit_my_module(void) {
PyObject* module = PyModule_Create(&my_module);
if (module == NULL) {
return NULL;
}
if (PyType_Ready(&MyType_Type) < 0) {
return NULL;
}
Py_INCREF(&MyType_Type);
PyModule_AddObject(module, "UIFrame", (PyObject*)&MyType_Type);
return module;
}
*/
// Point class example
class Point
{
public:
float x, y;
float magnitude() {
return std::sqrt(x*x + y*y);
}
};
static PyObject* PyPoint_new(PyTypeObject* type, PyObject* args, PyObject* kwds)
{
Point* self;
self = (Point*)type->tp_alloc(type, 0);
if (self != nullptr)
{
self->x = 0.0f;
self->y = 0.0f;
}
return (PyObject*)self;
}
// Method to initialize the Point object
static int PyPoint_init(Point* self, PyObject* args, PyObject* kwds)
{
static const char* keywords[] = { "x", "y", nullptr };
float x = 0.0f, y = 0.0f;
if (!PyArg_ParseTupleAndKeywords(args, kwds, "|ff", const_cast<char**>(keywords), &x, &y))
{
return -1;
}
self->x = x;
self->y = y;
return 0;
}
// Method to calculate the magnitude of the Point
static PyObject* PyPoint_magnitude(Point* self)
{
float mag = self->magnitude();
return PyFloat_FromDouble(mag);
}
static PyMethodDef PyPoint_methods[] = {
{"magnitude", (PyCFunction)PyPoint_magnitude, METH_NOARGS,
"Vector length, or distance from origin."},
{NULL, NULL, 0, NULL}
};
static PyMemberDef PyPoint_members[] = {
{"x", T_FLOAT, offsetof(Point, x), 0},
{"y", T_FLOAT, offsetof(Point, y), 0},
{NULL}
};
static PyTypeObject PyPointType = {
.tp_name = "mcrfpy.Point",
.tp_basicsize = sizeof(Point),
//.tp_itemsize = 0,
//.tp_dealloc = NULL,
//.tp_repr = NULL,
//.tp_hash = NULL,
//.tp_iter
//.tp_iternext
.tp_flags = Py_TPFLAGS_DEFAULT,
//.tp_doc = PyDoc_STR("Custom point object. (x, y)"),
//.tp_methods = PyPoint_methods,
//.tp_members = PyPoint_members,
//.tp_init = (initproc)PyPoint_init,
//.tp_new = PyPoint_new, //PyType_GenericNew ?
};
/*
static PyModuleDef PyPointModule = {
PyModuleDef_HEAD_INIT,
.m_name = "point",
.m_doc = "Custom point module.",
.m_size = -1,
//.m_methods = PyPoint_methods,
};
*/
}
}