From b8af8bc870c553caa7cfdc6b9d905ea4c866cf95 Mon Sep 17 00:00:00 2001 From: John McCardle Date: Sat, 2 Sep 2023 19:52:11 -0400 Subject: [PATCH] UIDrawable to Python object (untested, but compiling) --- src/UI.cpp | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ src/UI.h | 34 +++++++++++++++++++++++++++++++++- 2 files changed, 82 insertions(+), 1 deletion(-) diff --git a/src/UI.cpp b/src/UI.cpp index 81140dc..afd45fb 100644 --- a/src/UI.cpp +++ b/src/UI.cpp @@ -52,6 +52,10 @@ UIFrame::~UIFrame() void outlineColor(PyObject* pyColor); // Python setter */ +PyObjectsEnum UIFrame::derived_type() +{ + return PyObjectsEnum::UIFRAME; +} void UIFrame::render(sf::Vector2f offset) { @@ -86,3 +90,48 @@ void UISprite::render(sf::Vector2f offset) Resources::game->getWindow().draw(sprite); sprite.move(-offset); } + +PyObjectsEnum UICaption::derived_type() +{ + return PyObjectsEnum::UICAPTION; +} + +PyObjectsEnum UISprite::derived_type() +{ + return PyObjectsEnum::UISPRITE; +} + +PyObject* py_instance(std::shared_ptr source) +{ + // takes a UI drawable, calls its derived_type virtual function, and builds a Python object based on the return value. + using namespace mcrfpydef; + PyObject* newobj; + switch (source->derived_type()) + { + case PyObjectsEnum::UIFRAME: + { + PyUIFrameObject* o = (PyUIFrameObject*)PyUIFrameType.tp_alloc(&PyUIFrameType, 0); + if (o) + o->data = std::static_pointer_cast(source); + newobj = (PyObject*)o; + break; + } + /* not yet implemented + case PyObjectsEnum::UICAPTION: + PyUICaptionObject* o = (PyUICaptionObject*)PyUICaptionType.tp_alloc(&PyUICaptionType, 0); + if (o) + o->data = std::static_pointer_cast(source); + break; + case PyObjectsEnum::UISPRITE: + PyUISpriteObject* o = (PyUISpriteObject*)PyUISpriteType.tp_alloc(&PyUISpriteType, 0); + if (o) + o->data = std::static_pointer_cast(source); + break; + */ + default: + return NULL; + break; + } + + return newobj; +} diff --git a/src/UI.h b/src/UI.h index e00150a..ad214dd 100644 --- a/src/UI.h +++ b/src/UI.h @@ -3,6 +3,14 @@ #include "Python.h" #include "structmember.h" +enum PyObjectsEnum +{ + UIFRAME = 1, + UICAPTION, + UISPRITE, + UIGRID +}; + class UIDrawable { public: @@ -13,6 +21,7 @@ public: //virtual sf::Vector2i position(); bool handle_event(/* ??? click, scroll, keystroke*/); std::string action; + virtual PyObjectsEnum derived_type() = 0; }; //Python object types & forward declarations @@ -42,6 +51,7 @@ public: void render(sf::Vector2f) override final; void move(sf::Vector2f); + PyObjectsEnum derived_type() override final; // { return PyObjectsEnum::UIFrame; }; /* sf::Color fillColor(); // getter void fillColor(sf::Color c); // C++ setter @@ -65,6 +75,7 @@ class UICaption: public UIDrawable public: sf::Text text; void render(sf::Vector2f) override final; + PyObjectsEnum derived_type() override final; // { return PyObjectsEnum::UICaption; }; }; class UISprite: public UIDrawable @@ -74,6 +85,7 @@ public: int texture_index, sprite_index; float scale; sf::Sprite sprite; + PyObjectsEnum derived_type() override final; // { return PyObjectsEnum::UISprite; }; }; /* @@ -101,8 +113,9 @@ typedef struct { std::shared_ptr data; } PyUISpriteObject; +PyObject* py_instance(std::shared_ptr source); + namespace mcrfpydef { - // Color Definitions // struct, members, new, set_member, PyTypeObject @@ -451,6 +464,25 @@ namespace mcrfpydef { * */ + /* + * + * Begin Python Class Instantiator (iterator helper) + * + */ + + /* // definition can't go in the header file + PyObject* py_instance(UIDrawable* obj) + { + + } + */ + + /* + * + * End Python Class Instantitator (iterator helper) + * + */ + /* * * Begin PyUICollectionIter defs