LinkedColor now reflecting changes to the linked color value. Needs set method + RGBA / color properties
This commit is contained in:
parent
41509dfe96
commit
06e24a1b27
|
@ -56,7 +56,7 @@ PyObject* PyInit_mcrfpy()
|
||||||
using namespace mcrfpydef;
|
using namespace mcrfpydef;
|
||||||
PyTypeObject* pytypes[] = {
|
PyTypeObject* pytypes[] = {
|
||||||
/*SFML exposed types*/
|
/*SFML exposed types*/
|
||||||
&PyColorType, &PyFontType, &PyTextureType,
|
&PyColorType, &PyLinkedColorType, &PyFontType, &PyTextureType,
|
||||||
|
|
||||||
/*UI widgets*/
|
/*UI widgets*/
|
||||||
&PyUICaptionType, &PyUISpriteType, &PyUIFrameType, &PyUIEntityType, &PyUIGridType,
|
&PyUICaptionType, &PyUISpriteType, &PyUIFrameType, &PyUIEntityType, &PyUIGridType,
|
||||||
|
|
|
@ -13,12 +13,12 @@ PyLinkedColor::PyLinkedColor(_PyLinkedColorData d)
|
||||||
data = 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.parent = parent;
|
||||||
data.setter = setter;
|
data.index = index;
|
||||||
data.getter = getter;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
PyObject* PyLinkedColor::pyObject()
|
PyObject* PyLinkedColor::pyObject()
|
||||||
|
@ -42,6 +42,7 @@ PyLinkedColor PyLinkedColor::fromPy(PyLinkedColorObject* self)
|
||||||
|
|
||||||
void PyLinkedColor::set(sf::Color color)
|
void PyLinkedColor::set(sf::Color color)
|
||||||
{
|
{
|
||||||
|
std::cout << "PyLinkedColor: call to set()" << std::endl;
|
||||||
auto ptr = data.parent.lock();
|
auto ptr = data.parent.lock();
|
||||||
if (ptr)
|
if (ptr)
|
||||||
{
|
{
|
||||||
|
@ -52,6 +53,7 @@ void PyLinkedColor::set(sf::Color color)
|
||||||
|
|
||||||
sf::Color PyLinkedColor::get()
|
sf::Color PyLinkedColor::get()
|
||||||
{
|
{
|
||||||
|
std::cout << "PyLinkedColor: call to get()" << std::endl;
|
||||||
auto ptr = data.parent.lock();
|
auto ptr = data.parent.lock();
|
||||||
if (ptr)
|
if (ptr)
|
||||||
{
|
{
|
||||||
|
@ -92,7 +94,7 @@ Py_hash_t PyLinkedColor::hash(PyObject* obj)
|
||||||
auto ptr = self->data.parent.lock();
|
auto ptr = self->data.parent.lock();
|
||||||
if (ptr)
|
if (ptr)
|
||||||
{
|
{
|
||||||
auto color = PyLinkedColor(self->data);
|
auto linkedcolor = PyLinkedColor(self->data);
|
||||||
auto c = linkedcolor.get();
|
auto c = linkedcolor.get();
|
||||||
value += c.r;
|
value += c.r;
|
||||||
value << 8; value += c.g;
|
value << 8; value += c.g;
|
||||||
|
@ -114,7 +116,7 @@ PyObject* PyLinkedColor::repr(PyObject* obj)
|
||||||
std::ostringstream ss;
|
std::ostringstream ss;
|
||||||
PyLinkedColor color = PyLinkedColor(self->data);
|
PyLinkedColor color = PyLinkedColor(self->data);
|
||||||
sf::Color c = color.get();
|
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();
|
std::string repr_str = ss.str();
|
||||||
return PyUnicode_DecodeUTF8(repr_str.c_str(), repr_str.size(), "replace");
|
return PyUnicode_DecodeUTF8(repr_str.c_str(), repr_str.size(), "replace");
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
#pragma once
|
#pragma once
|
||||||
#include "Common.h"
|
#include "Common.h"
|
||||||
#include "Python.h"
|
#include "Python.h"
|
||||||
|
#include <functional>
|
||||||
|
|
||||||
class PyLinkedColor;
|
class PyLinkedColor;
|
||||||
class UIDrawable; // forward declare for pointer
|
class UIDrawable; // forward declare for pointer
|
||||||
|
@ -8,8 +9,10 @@ class UIDrawable; // forward declare for pointer
|
||||||
typedef struct {
|
typedef struct {
|
||||||
std::weak_ptr<UIDrawable> parent; // lifetime management: parent must still exist
|
std::weak_ptr<UIDrawable> parent; // lifetime management: parent must still exist
|
||||||
int index; // specific to the parent class, which color is it?
|
int index; // specific to the parent class, which color is it?
|
||||||
sf::Color(*getter)();
|
//sf::Color(*getter)();
|
||||||
void(*setter)(sf::Color);
|
//void(*setter)(sf::Color);
|
||||||
|
std::function<void(sf::Color)> setter;
|
||||||
|
std::function<sf::Color()> getter;
|
||||||
} _PyLinkedColorData;
|
} _PyLinkedColorData;
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
|
@ -21,10 +24,11 @@ class PyLinkedColor
|
||||||
{
|
{
|
||||||
private:
|
private:
|
||||||
_PyLinkedColorData data;
|
_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:
|
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
|
void set(sf::Color); // change target value, behavior determined by the mode
|
||||||
sf::Color get(); // retrieve 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
|
PyObject* pyParent(); // UIDrawable derived parent object or None
|
||||||
|
|
26
src/UI.h
26
src/UI.h
|
@ -608,27 +608,33 @@ static int PyUIDrawable_set_click(PyUIGridObject* self, PyObject* value, void* c
|
||||||
|
|
||||||
// fetch correct member data
|
// fetch correct member data
|
||||||
//sf::Color color;
|
//sf::Color color;
|
||||||
sf::Color (*cgetter)();
|
//sf::Color (*cgetter)();
|
||||||
void (*csetter)(sf::Color);
|
//void (*csetter)(sf::Color);
|
||||||
|
std::function<void(sf::Color)> csetter;
|
||||||
|
std::function<sf::Color()> cgetter;
|
||||||
if (member_ptr == 0)
|
if (member_ptr == 0)
|
||||||
{
|
{
|
||||||
//color = self->data->text.getFillColor();
|
//color = self->data->text.getFillColor();
|
||||||
//return Py_BuildValue("(iii)", color.r, color.g, color.b);
|
//return Py_BuildValue("(iii)", color.r, color.g, color.b);
|
||||||
csetter = &self->data->text.setFillColor;
|
//csetter = &self->data->text.setFillColor;
|
||||||
cgetter = &self->data->text.getFillColor;
|
//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)
|
else if (member_ptr == 1)
|
||||||
{
|
{
|
||||||
//color = self->data->text.getOutlineColor();
|
//color = self->data->text.getOutlineColor();
|
||||||
//return Py_BuildValue("(iii)", color.r, color.g, color.b);
|
//return Py_BuildValue("(iii)", color.r, color.g, color.b);
|
||||||
csetter = &self->data->text.setOutlineColor;
|
//csetter = &self->data->text.setOutlineColor;
|
||||||
cgetter = &self->data->text.getOutlineColor;
|
//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
|
// 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);
|
//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!
|
//linkedcolor.set(color); // don't need to set a linked color!
|
||||||
|
|
||||||
//return pyColor;
|
//return pyColor;
|
||||||
|
@ -650,8 +656,12 @@ static int PyUIDrawable_set_click(PyUIGridObject* self, PyObject* value, void* c
|
||||||
b = color->data->b;
|
b = color->data->b;
|
||||||
a = color->data->a;
|
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;
|
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)
|
else if (!PyTuple_Check(value) || PyTuple_Size(value) < 3 || PyTuple_Size(value) > 4)
|
||||||
{
|
{
|
||||||
|
|
Loading…
Reference in New Issue