Squashed commit of the following: [standardize_color_handling]

closes #11

Check the abandoned feature branch for PyLinkedColor, a time-expensive but now abandoned feature to link a color value to a UIDrawable.

There are some TODOs left in the PyColor class, but that can go under cleanup. I'm way over time on this, so I'm taking a small victory :)

commit 572aa52605
Author: John McCardle <mccardle.john@gmail.com>
Date:   Sat Mar 30 21:18:26 2024 -0400

    More color table updates

commit 01706bd59d
Author: John McCardle <mccardle.john@gmail.com>
Date:   Sat Mar 30 21:13:31 2024 -0400

    Color wrapup... Cutting PyLinkedColor to simplify my cursedly mortal, finite existence

commit 3991ac13d6
Author: John McCardle <mccardle.john@gmail.com>
Date:   Thu Mar 28 23:50:50 2024 -0400

    Still having segfaults with LinkedColor and captions (specifically outline color, but that might not be the actual cause). PyColor shaping back up in simplified form.

commit 06e24a1b27
Author: John McCardle <mccardle.john@gmail.com>
Date:   Thu Mar 28 20:53:49 2024 -0400

    LinkedColor now reflecting changes to the linked color value. Needs set method + RGBA / color properties

commit 41509dfe96
Author: John McCardle <mccardle.john@gmail.com>
Date:   Wed Mar 27 21:10:03 2024 -0400

    Addressing issues with PyColor by splitting behavior off into PyLinkedColor

commit 13a4ddf41b
Author: John McCardle <mccardle.john@gmail.com>
Date:   Tue Mar 26 23:02:00 2024 -0400

    Build runs again. PyColor objects are being instantiated, with bugs and no test of color changing

commit 1601fc7fab
Author: John McCardle <mccardle.john@gmail.com>
Date:   Mon Mar 25 20:48:08 2024 -0400

    Still doesn't compile, but now the issue is in UI.h overcoupling. Progress!

commit 13672c8fdb
Author: John McCardle <mccardle.john@gmail.com>
Date:   Sun Mar 24 21:19:37 2024 -0400

    Dabbling around this morning; still not building

commit 79090b553f
Author: John McCardle <mccardle.john@gmail.com>
Date:   Sun Mar 24 08:36:06 2024 -0400

    Unsaved changes from last night

commit 2cac6f03c6
Author: John McCardle <mccardle.john@gmail.com>
Date:   Sat Mar 23 23:07:10 2024 -0400

    untested PyColor base implementation

commit 3728e5fcc8
Author: John McCardle <mccardle.john@gmail.com>
Date:   Sat Mar 23 23:06:36 2024 -0400

    Color naming prototype
This commit is contained in:
John McCardle 2024-03-30 21:20:40 -04:00
commit f82508b753
10 changed files with 1974 additions and 300 deletions

222
src/UI.h
View file

@ -5,8 +5,13 @@
#include "IndexTexture.h"
#include "Resources.h"
#include <list>
#define ui_h
#include "PyCallable.h"
#include "PyTexture.h"
#include "PyColor.h"
//#include "PyLinkedColor.h"
enum PyObjectsEnum : int
{
@ -46,10 +51,12 @@ typedef struct {
} PyColorObject;
*/
/* // Moved to PyColor.h
typedef struct {
PyObject_HEAD
std::shared_ptr<sf::Color> data;
} PyColorObject;
*/
class UIFrame: public UIDrawable
{
@ -444,89 +451,89 @@ static int PyUIDrawable_set_click(PyUIGridObject* self, PyObject* value, void* c
*/
static PyObject* PyColor_get_member(PyColorObject* self, void* closure)
{
auto member_ptr = reinterpret_cast<long>(closure);
if (member_ptr == 0)
return PyLong_FromLong(self->data->r);
else if (member_ptr == 1)
return PyLong_FromLong(self->data->g);
else if (member_ptr == 2)
return PyLong_FromLong(self->data->b);
else if (member_ptr == 3)
return PyLong_FromLong(self->data->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 == 0)
self->data->r = static_cast<sf::Uint8>(int_val);
else if (member_ptr == 1)
self->data->g = static_cast<sf::Uint8>(int_val);
else if (member_ptr == 2)
self->data->b = static_cast<sf::Uint8>(int_val);
else if (member_ptr == 3)
self->data->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*)0},
{"g", (getter)PyColor_get_member, (setter)PyColor_set_member, "Green component", (void*)1},
{"b", (getter)PyColor_get_member, (setter)PyColor_set_member, "Blue component", (void*)2},
{"a", (getter)PyColor_get_member, (setter)PyColor_set_member, "Alpha component", (void*)3},
{NULL}
};
static PyTypeObject PyColorType = {
//PyVarObject_HEAD_INIT(NULL, 0)
.tp_name = "mcrfpy.Color",
.tp_basicsize = sizeof(PyColorObject),
.tp_itemsize = 0,
.tp_dealloc = (destructor)[](PyObject* self)
{
PyColorObject* obj = (PyColorObject*)self;
obj->data.reset();
Py_TYPE(self)->tp_free(self);
},
//.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 = [](PyTypeObject* type, PyObject* args, PyObject* kwds) -> PyObject*
{
PyColorObject* self = (PyColorObject*)type->tp_alloc(type, 0);
if (self) self->data = std::make_shared<sf::Color>();
return (PyObject*)self;
}
};
// static PyObject* PyColor_get_member(PyColorObject* self, void* closure)
// {
// auto member_ptr = reinterpret_cast<long>(closure);
// if (member_ptr == 0)
// return PyLong_FromLong(self->data->r);
// else if (member_ptr == 1)
// return PyLong_FromLong(self->data->g);
// else if (member_ptr == 2)
// return PyLong_FromLong(self->data->b);
// else if (member_ptr == 3)
// return PyLong_FromLong(self->data->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 == 0)
// self->data->r = static_cast<sf::Uint8>(int_val);
// else if (member_ptr == 1)
// self->data->g = static_cast<sf::Uint8>(int_val);
// else if (member_ptr == 2)
// self->data->b = static_cast<sf::Uint8>(int_val);
// else if (member_ptr == 3)
// self->data->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*)0},
// {"g", (getter)PyColor_get_member, (setter)PyColor_set_member, "Green component", (void*)1},
// {"b", (getter)PyColor_get_member, (setter)PyColor_set_member, "Blue component", (void*)2},
// {"a", (getter)PyColor_get_member, (setter)PyColor_set_member, "Alpha component", (void*)3},
// {NULL}
// };
//
//
// static PyTypeObject PyColorType = {
// //PyVarObject_HEAD_INIT(NULL, 0)
// .tp_name = "mcrfpy.Color",
// .tp_basicsize = sizeof(PyColorObject),
// .tp_itemsize = 0,
// .tp_dealloc = (destructor)[](PyObject* self)
// {
// PyColorObject* obj = (PyColorObject*)self;
// obj->data.reset();
// Py_TYPE(self)->tp_free(self);
// },
// //.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 = [](PyTypeObject* type, PyObject* args, PyObject* kwds) -> PyObject*
// {
// PyColorObject* self = (PyColorObject*)type->tp_alloc(type, 0);
// if (self) self->data = std::make_shared<sf::Color>();
// return (PyObject*)self;
// }
// };
/*
*
@ -591,32 +598,50 @@ static int PyUIDrawable_set_click(PyUIGridObject* self, PyObject* value, void* c
}
// TODO: manually calling tp_alloc to create a PyColorObject seems like an antipattern
PyTypeObject* colorType = &PyColorType;
PyObject* pyColor = colorType->tp_alloc(colorType, 0);
/*
PyTypeObject* colorType = &PyLinkedColorType;
PyObject* pyColor = colorType->tp_alloc(colorLinkedType, 0);
if (pyColor == NULL)
{
std::cout << "failure to allocate mcrfpy.Color / PyColorType" << std::endl;
std::cout << "failure to allocate mcrfpy.LinkedColor / PyLinkedColorType" << std::endl;
return NULL;
}
PyColorObject* pyColorObj = reinterpret_cast<PyColorObject*>(pyColor);
PyColorObject* pyColorObj = reinterpret_cast<PyLinkedColorObject*>(pyColor);
*/
// fetch correct member data
sf::Color color;
//sf::Color (*cgetter)();
//void (*csetter)(sf::Color);
//std::function<void(sf::Color)> csetter;
//std::function<sf::Color()> cgetter;
if (member_ptr == 0)
{
color = self->data->text.getFillColor();
//return Py_BuildValue("(iii)", color.r, color.g, color.b);
//csetter = &self->data->text.setFillColor;
//cgetter = &self->data->text.getFillColor;
//csetter = [s = self->data](sf::Color c){s->text.setFillColor(c);};
//cgetter = [s = self->data](){return s->text.getFillColor();};
}
else if (member_ptr == 1)
{
color = self->data->text.getOutlineColor();
//return Py_BuildValue("(iii)", color.r, color.g, color.b);
//csetter = &self->data->text.setOutlineColor;
//cgetter = &self->data->text.getOutlineColor;
//csetter = [s = self->data](sf::Color c){s->text.setOutlineColor(c);};
//cgetter = [s = self->data](){return s->text.getOutlineColor();};
}
// initialize new mcrfpy.Color instance
pyColorObj->data = std::make_shared<sf::Color>(color);
//pyColorObj->data = std::make_shared<sf::Color>(color);
//PyLinkedColor::fromPy(pyColorObj).set(color);
//auto linkedcolor = PyLinkedColor(csetter, cgetter, self->data, member_ptr);
//linkedcolor.set(color); // don't need to set a linked color!
return pyColor;
//return pyColor;
return PyColor(color).pyObject();
}
static int PyUICaption_set_color_member(PyUICaptionObject* self, PyObject* value, void* closure)
@ -627,11 +652,20 @@ static int PyUIDrawable_set_click(PyUIGridObject* self, PyObject* value, void* c
if (PyObject_IsInstance(value, (PyObject*)&PyColorType))
{
// get value from mcrfpy.Color instance
/*
PyColorObject* color = reinterpret_cast<PyColorObject*>(value);
r = color->data->r;
g = color->data->g;
b = color->data->b;
a = color->data->a;
*/
//std::cout << "Build LinkedColor" << std::endl;
//auto lc = PyLinkedColor::fromPy(value);
auto c = ((PyColorObject*)value)->data;
//std::cout << "Fetch value" << std::endl;
//auto c = lc.get();
r = c.r; g = c.g; b = c.b; a = c.a;
std::cout << "got " << int(r) << ", " << int(g) << ", " << int(b) << ", " << int(a) << std::endl;
}
else if (!PyTuple_Check(value) || PyTuple_Size(value) < 3 || PyTuple_Size(value) > 4)
{
@ -898,9 +932,11 @@ static int PyUIDrawable_set_click(PyUIGridObject* self, PyObject* value, void* c
}
// initialize new mcrfpy.Color instance
pyColorObj->data = std::make_shared<sf::Color>(color);
//pyColorObj->data = std::make_shared<sf::Color>(color);
//PyColor::fromPy(pyColorObj).set(color);
return pyColor;
// TODO - supposed to return Linked
return PyColor(color).pyObject();
}
static int PyUIFrame_set_color_member(PyUIFrameObject* self, PyObject* value, void* closure)
@ -911,12 +947,16 @@ static int PyUIDrawable_set_click(PyUIGridObject* self, PyObject* value, void* c
if (PyObject_IsInstance(value, (PyObject*)&PyColorType))
{
// get value from mcrfpy.Color instance
/*
PyColorObject* color = reinterpret_cast<PyColorObject*>(value);
r = color->data->r;
g = color->data->g;
b = color->data->b;
a = color->data->a;
std::cout << "using color: " << r << " " << g << " " << b << " " << a << std::endl;
*/
sf::Color c = ((PyColorObject*)value)->data;
r = c.r; g = c.g; b = c.b; a = c.a;
}
else if (!PyTuple_Check(value) || PyTuple_Size(value) < 3 || PyTuple_Size(value) > 4)
{