UIDrawable to Python object (untested, but compiling)

This commit is contained in:
John McCardle 2023-09-02 19:52:11 -04:00
parent 0ef0a5d506
commit b8af8bc870
2 changed files with 82 additions and 1 deletions

View File

@ -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<UIDrawable> 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<UIFrame>(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<UICaption>(source);
break;
case PyObjectsEnum::UISPRITE:
PyUISpriteObject* o = (PyUISpriteObject*)PyUISpriteType.tp_alloc(&PyUISpriteType, 0);
if (o)
o->data = std::static_pointer_cast<UISprite>(source);
break;
*/
default:
return NULL;
break;
}
return newobj;
}

View File

@ -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<UISprite> data;
} PyUISpriteObject;
PyObject* py_instance(std::shared_ptr<UIDrawable> 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