From 884a49a63aa34d60c17d0bf1827c4f7731ca070e Mon Sep 17 00:00:00 2001 From: John McCardle Date: Wed, 30 Aug 2023 14:38:49 -0400 Subject: [PATCH] Switching UIFrame to sf::Color* for outline and fill members. Haven't tested with Python integration, but I wrote the methods to take a crack at it --- src/UI.cpp | 86 +++++++++++++++++++++++++++++++++++++++++---- src/UI.h | 28 +++++++++++---- src/UITestScene.cpp | 24 ++++++------- 3 files changed, 114 insertions(+), 24 deletions(-) diff --git a/src/UI.cpp b/src/UI.cpp index 6e36a9c..97f4b35 100644 --- a/src/UI.cpp +++ b/src/UI.cpp @@ -8,12 +8,86 @@ void UIDrawable::render() render(sf::Vector2f()); } UIFrame::UIFrame(): -x(0), y(0), w(0), h(0), outline(0), fillColor(0,0,0,255), outlineColor(0,0,0,0) -{} +x(0), y(0), w(0), h(0), outline(0) +{ + pyOutlineColor = NULL; + pyFillColor = NULL; + _outlineColor = NULL; + _fillColor = NULL; +} UIFrame::UIFrame(float _x, float _y, float _w, float _h): -x(_x), y(_y), w(_w), h(_h), outline(0), fillColor(0,0,0,255), outlineColor(0,0,0,0) -{} +x(_x), y(_y), w(_w), h(_h), outline(0) +{ + pyOutlineColor = NULL; + pyFillColor = NULL; + _outlineColor = NULL; + _fillColor = NULL; +} + +UIFrame::~UIFrame() +{ + if (pyOutlineColor) { Py_DECREF(pyOutlineColor); } + else { if (_outlineColor) { delete _outlineColor; } } + if (pyFillColor) { Py_DECREF(pyFillColor); } + else { if (_fillColor) { delete _fillColor; } } +} + +/* + sf::Color& fillColor(); // getter + void fillColor(sf::Color c); // C++ setter + void fillColor(PyObject* pyColor); // Python setter + + sf::Color& outlineColor(); // getter + void outlineColor(sf::Color c); // C++ setter + void outlineColor(PyObject* pyColor); // Python setter +*/ + +sf::Color UIFrame::fillColor() +{ + if (_fillColor == NULL) return sf::Color(); + return *_fillColor; +} + +void UIFrame::fillColor(sf::Color c) +{ + if (pyFillColor) { Py_DECREF(pyFillColor); } + else { delete _fillColor; } + _fillColor = new sf::Color(c.r, c.g, c.b, c.a); + pyFillColor = NULL; +} + +void UIFrame::fillColor(PyColorObject* pyColor) +{ + if (pyFillColor) { Py_DECREF(pyFillColor); } + else { delete _fillColor; } + Py_INCREF(pyColor); + pyFillColor = pyColor; + _fillColor = &(pyFillColor->color); +} + +sf::Color UIFrame::outlineColor() +{ + if (_outlineColor == NULL) return sf::Color(); + return *_outlineColor; +} + +void UIFrame::outlineColor(sf::Color c) +{ + if (pyOutlineColor) { Py_DECREF(pyOutlineColor); } + else { delete _outlineColor; } + _outlineColor = new sf::Color(c.r, c.g, c.b, c.a); + pyOutlineColor = NULL; +} + +void UIFrame::outlineColor(PyColorObject* pyColor) +{ + if (pyOutlineColor) { Py_DECREF(pyOutlineColor); } + else { delete _outlineColor; } + Py_INCREF(pyColor); + pyOutlineColor = pyColor; + _outlineColor = &(pyOutlineColor->color); +} void UIFrame::render(sf::Vector2f offset) { @@ -25,8 +99,8 @@ void UIFrame::render(sf::Vector2f offset) sf::RectangleShape box = sf::RectangleShape(sf::Vector2f(w,h)); sf::Vector2f pos = sf::Vector2f(x, y); box.setPosition(offset + pos); - box.setFillColor(fillColor); - box.setOutlineColor(outlineColor); + if (_fillColor) { box.setFillColor(fillColor()); } + if (_outlineColor) { box.setOutlineColor(outlineColor()); } box.setOutlineThickness(outline); Resources::game->getWindow().draw(box); for (auto drawable : children) { diff --git a/src/UI.h b/src/UI.h index d5bec87..bc2f853 100644 --- a/src/UI.h +++ b/src/UI.h @@ -14,19 +14,38 @@ public: std::string action; }; +//PyColorObject struct required to embed Python colors into UIFrame + +typedef struct { + PyObject_HEAD + sf::Color color; +} PyColorObject; + class UIFrame: public UIDrawable { public: UIFrame(float, float, float, float); UIFrame(); + ~UIFrame(); //sf::RectangleShape box; - + //Simulate RectangleShape - sf::Color fillColor, outlineColor; float x, y, w, h, outline; std::vector children; void render(sf::Vector2f) override final; void move(sf::Vector2f); + + sf::Color fillColor(); // getter + void fillColor(sf::Color c); // C++ setter + void fillColor(PyColorObject* pyColor); // Python setter + + sf::Color outlineColor(); // getter + void outlineColor(sf::Color c); // C++ setter + void outlineColor(PyColorObject* pyColor); // Python setter + +private: + sf::Color *_fillColor, *_outlineColor; + PyColorObject *pyFillColor, *pyOutlineColor; }; class UICaption: public UIDrawable @@ -49,10 +68,7 @@ namespace mcrfpydef { // Color Definitions // struct, members, new, set_member, PyTypeObject - typedef struct { - PyObject_HEAD - sf::Color color; - } PyColorObject; + static PyMemberDef PyColor_members[] { diff --git a/src/UITestScene.cpp b/src/UITestScene.cpp index e21784b..0fc96e6 100644 --- a/src/UITestScene.cpp +++ b/src/UITestScene.cpp @@ -15,23 +15,23 @@ UITestScene::UITestScene(GameEngine* g) : Scene(g) // Create a UI element or three? e1 = UIFrame(100,150,400,400); - //e1.box.setPosition(100, 150); - //e1.box.setSize(sf::Vector2f(400,400)); - //e1.box.setFillColor(sf::Color(255, 0, 0)); - e1.fillColor = sf::Color(255,0,0); + //e1.box.setPosition(100, 150); + //e1.box.setSize(sf::Vector2f(400,400)); + //e1.box.setFillColor(sf::Color(255, 0, 0)); + e1.fillColor(sf::Color(255,0,0)); e1a = UIFrame(50,50,200,200); - //e1a.box.setPosition(50, 50); - //e1a.box.setSize(sf::Vector2f(200,200)); - //e1a.box.setFillColor(sf::Color(0, 255, 0)); - e1a.fillColor = sf::Color(0, 255, 0); + //e1a.box.setPosition(50, 50); + //e1a.box.setSize(sf::Vector2f(200,200)); + //e1a.box.setFillColor(sf::Color(0, 255, 0)); + e1a.fillColor(sf::Color(0, 255, 0)); e1.children.push_back(&e1a); e1aa = UIFrame(5,5,100,100); - //e1aa.box.setPosition(5, 5); - //e1aa.box.setSize(sf::Vector2f(100,100)); - //e1aa.box.setFillColor(sf::Color(0, 0, 255)); - e1aa.fillColor = sf::Color(0, 0, 255); + //e1aa.box.setPosition(5, 5); + //e1aa.box.setSize(sf::Vector2f(100,100)); + //e1aa.box.setFillColor(sf::Color(0, 0, 255)); + e1aa.fillColor(sf::Color(0, 0, 255)); e1a.children.push_back(&e1aa); e2 = UICaption();