diff --git a/src/McRFPy_API.cpp b/src/McRFPy_API.cpp index 51bd3c7..019ab74 100644 --- a/src/McRFPy_API.cpp +++ b/src/McRFPy_API.cpp @@ -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, diff --git a/src/PyLinkedColor.cpp b/src/PyLinkedColor.cpp index f349816..d509ab8 100644 --- a/src/PyLinkedColor.cpp +++ b/src/PyLinkedColor.cpp @@ -13,12 +13,12 @@ PyLinkedColor::PyLinkedColor(_PyLinkedColorData d) data = d; } -PyLinkedColor::PyLinkedColor(sf::Color (*getter)(), void (*setter)(sf::Color), std::weak_ptr parent, int index) +PyLinkedColor::PyLinkedColor(std::function _s, std::function _g, std::weak_ptr 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 << ""; + ss << ""; std::string repr_str = ss.str(); return PyUnicode_DecodeUTF8(repr_str.c_str(), repr_str.size(), "replace"); diff --git a/src/PyLinkedColor.h b/src/PyLinkedColor.h index 8b0d498..f41331e 100644 --- a/src/PyLinkedColor.h +++ b/src/PyLinkedColor.h @@ -1,6 +1,7 @@ #pragma once #include "Common.h" #include "Python.h" +#include class PyLinkedColor; class UIDrawable; // forward declare for pointer @@ -8,8 +9,10 @@ class UIDrawable; // forward declare for pointer typedef struct { std::weak_ptr 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 setter; + std::function 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, int); + //PyLinkedColor(sf::Color (*)(), void (*)(sf::Color), std::weak_ptr, int); + PyLinkedColor(std::function setter, std::function getter, std::weak_ptr 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 diff --git a/src/UI.h b/src/UI.h index aa0b335..82610f0 100644 --- a/src/UI.h +++ b/src/UI.h @@ -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 csetter; + std::function 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(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) {