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

This commit is contained in:
John McCardle 2024-03-28 20:53:49 -04:00
parent 41509dfe96
commit 06e24a1b27
4 changed files with 35 additions and 19 deletions

View File

@ -56,7 +56,7 @@ PyObject* PyInit_mcrfpy()
using namespace mcrfpydef;
PyTypeObject* pytypes[] = {
/*SFML exposed types*/
&PyColorType, &PyFontType, &PyTextureType,
&PyColorType, &PyLinkedColorType, &PyFontType, &PyTextureType,
/*UI widgets*/
&PyUICaptionType, &PyUISpriteType, &PyUIFrameType, &PyUIEntityType, &PyUIGridType,

View File

@ -13,12 +13,12 @@ PyLinkedColor::PyLinkedColor(_PyLinkedColorData d)
data = d;
}
PyLinkedColor::PyLinkedColor(sf::Color (*getter)(), void (*setter)(sf::Color), std::weak_ptr<UIDrawable> parent, int index)
PyLinkedColor::PyLinkedColor(std::function<void(sf::Color)> _s, std::function<sf::Color()> _g, std::weak_ptr<UIDrawable> parent, int index)
{
data.index = index;
data.setter = _s;
data.getter = _g;
data.parent = parent;
data.setter = setter;
data.getter = getter;
data.index = index;
}
PyObject* PyLinkedColor::pyObject()
@ -42,6 +42,7 @@ PyLinkedColor PyLinkedColor::fromPy(PyLinkedColorObject* self)
void PyLinkedColor::set(sf::Color color)
{
std::cout << "PyLinkedColor: call to set()" << std::endl;
auto ptr = data.parent.lock();
if (ptr)
{
@ -52,6 +53,7 @@ void PyLinkedColor::set(sf::Color color)
sf::Color PyLinkedColor::get()
{
std::cout << "PyLinkedColor: call to get()" << std::endl;
auto ptr = data.parent.lock();
if (ptr)
{
@ -92,7 +94,7 @@ Py_hash_t PyLinkedColor::hash(PyObject* obj)
auto ptr = self->data.parent.lock();
if (ptr)
{
auto color = PyLinkedColor(self->data);
auto linkedcolor = PyLinkedColor(self->data);
auto c = linkedcolor.get();
value += c.r;
value << 8; value += c.g;
@ -114,7 +116,7 @@ PyObject* PyLinkedColor::repr(PyObject* obj)
std::ostringstream ss;
PyLinkedColor color = PyLinkedColor(self->data);
sf::Color c = color.get();
ss << "<Color (" << c.r << ", " << c.g << ", " << c.b << ", " << c.a << ") " << color.mode() << ">";
ss << "<LinkedColor (" << int(c.r) << ", " << int(c.g) << ", " << int(c.b) << ", " << int(c.a) << ")" << ">";
std::string repr_str = ss.str();
return PyUnicode_DecodeUTF8(repr_str.c_str(), repr_str.size(), "replace");

View File

@ -1,6 +1,7 @@
#pragma once
#include "Common.h"
#include "Python.h"
#include <functional>
class PyLinkedColor;
class UIDrawable; // forward declare for pointer
@ -8,8 +9,10 @@ class UIDrawable; // forward declare for pointer
typedef struct {
std::weak_ptr<UIDrawable> parent; // lifetime management: parent must still exist
int index; // specific to the parent class, which color is it?
sf::Color(*getter)();
void(*setter)(sf::Color);
//sf::Color(*getter)();
//void(*setter)(sf::Color);
std::function<void(sf::Color)> setter;
std::function<sf::Color()> getter;
} _PyLinkedColorData;
typedef struct {
@ -21,10 +24,11 @@ class PyLinkedColor
{
private:
_PyLinkedColorData data;
PyColor(_PyColorData); // private constructor / for operations transferring between C++ and Python
PyLinkedColor(_PyLinkedColorData); // private constructor / for operations transferring between C++ and Python
public:
PyLinkedColor(sf::Color (*)(), void (*)(sf::Color), std::weak_ptr<UIDrawable>, int);
//PyLinkedColor(sf::Color (*)(), void (*)(sf::Color), std::weak_ptr<UIDrawable>, int);
PyLinkedColor(std::function<void(sf::Color)> setter, std::function<sf::Color()> getter, std::weak_ptr<UIDrawable> parent, int index);
void set(sf::Color); // change target value, behavior determined by the mode
sf::Color get(); // retrieve target value, behavior determined by the mode
PyObject* pyParent(); // UIDrawable derived parent object or None

View File

@ -608,27 +608,33 @@ static int PyUIDrawable_set_click(PyUIGridObject* self, PyObject* value, void* c
// fetch correct member data
//sf::Color color;
sf::Color (*cgetter)();
void (*csetter)(sf::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 = &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 = &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);
//PyLinkedColor::fromPy(pyColorObj).set(color);
auto linkedcolor = PyLinkedColor(getter, setter, self->data, member_ptr);
auto linkedcolor = PyLinkedColor(csetter, cgetter, self->data, member_ptr);
//linkedcolor.set(color); // don't need to set a linked color!
//return pyColor;
@ -650,8 +656,12 @@ static int PyUIDrawable_set_click(PyUIGridObject* self, PyObject* value, void* c
b = color->data->b;
a = color->data->a;
*/
sf::Color c = PyLinkedColor::fromPy(value).get();
std::cout << "Build LinkedColor" << std::endl;
auto lc = PyLinkedColor::fromPy(value);
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)
{