From c8124e84dcd5bfcf7c010cb5dae2feb8d64218f0 Mon Sep 17 00:00:00 2001 From: John McCardle Date: Thu, 2 Mar 2023 06:35:13 -0500 Subject: [PATCH] Updated UIMenu to a map on the C++ side, Python gets the title property so changes can be properly, jankily, looked up --- src/McRFPy_API.cpp | 46 +++++++++++++++++++++++++------------------ src/McRFPy_API.h | 6 +++--- src/scripts/UIMenu.py | 38 +++++++++++++++++++++++++++++++++++ 3 files changed, 68 insertions(+), 22 deletions(-) create mode 100644 src/scripts/UIMenu.py diff --git a/src/McRFPy_API.cpp b/src/McRFPy_API.cpp index 0d0696e..e028a4c 100644 --- a/src/McRFPy_API.cpp +++ b/src/McRFPy_API.cpp @@ -4,8 +4,8 @@ #include "Grid.h" // static class members...? -std::vector McRFPy_API::menus; -std::vector McRFPy_API::grids; +std::map McRFPy_API::menus; +std::map McRFPy_API::grids; static PyMethodDef mcrfpyMethods[] = { {"drawSprite", McRFPy_API::_drawSprite, METH_VARARGS, @@ -185,10 +185,11 @@ void McRFPy_API::REPL_device(FILE * fp, const char *filename) } PyObject* McRFPy_API::_createMenu(PyObject *self, PyObject *args) { - + const char* title_cstr; int posx, posy, sizex, sizey; - if (!PyArg_ParseTuple(args, "iiii", &posx, &posy, &sizex, &sizey)) return NULL; - menus.push_back(createMenu(posx, posy, sizex, sizey)); + if (!PyArg_ParseTuple(args, "siiii", &title_cstr, &posx, &posy, &sizex, &sizey)) return NULL; + std::string title = title_cstr; + menus[title] = createMenu(posx, posy, sizex, sizey); Py_INCREF(Py_None); return Py_None; } @@ -203,20 +204,26 @@ PyObject* McRFPy_API::_listMenus(PyObject*, PyObject*) { PyObject* spr_type = PyObject_GetAttrString(uimodule, "Sprite"); PyObject* menulist = PyList_New(menus.size()); - for (int i = 0; i < menus.size(); i++) { - auto p = menus[i].box.getPosition(); - auto s = menus[i].box.getSize(); - auto g = menus[i].box.getFillColor(); - PyObject* menu_args = Py_BuildValue("(iiii(iii)O)", + std::map::iterator it = menus.begin(); + //for (int i = 0; i < menus.size(); i++) { + int i = 0; + for (auto it = menus.begin(); it != menus.end(); it++) { + std::string title = it->first; + auto menu = it->second; + auto p = menu->box.getPosition(); + auto s = menu->box.getSize(); + auto g = menu->box.getFillColor(); + PyObject* menu_args = Py_BuildValue("(siiii(iii)O)", + title.c_str(), (int)p.x, (int)p.y, (int)s.x, (int)s.y, (int)g.r, (int)g.g, (int)g.b, - menus[i].visible ? Py_True: Py_False); - menus[i].visible ? Py_INCREF(Py_True) : Py_INCREF(Py_False); + menu->visible ? Py_True: Py_False); + menu->visible ? Py_INCREF(Py_True) : Py_INCREF(Py_False); PyObject* menuobj = PyObject_CallObject((PyObject*) uimenu_type, menu_args); // Loop: Convert Button objects to Python Objects PyObject* button_list = PyObject_GetAttrString(menuobj, "buttons"); - for(auto& b : menus[i].buttons) { + for(auto& b : menu->buttons) { auto bp = b.rect.getPosition(); auto bs = b.rect.getSize(); auto bg = b.rect.getFillColor(); @@ -233,7 +240,7 @@ PyObject* McRFPy_API::_listMenus(PyObject*, PyObject*) { // Loop: Convert Caption objects to Python Objects PyObject* caption_list = PyObject_GetAttrString(menuobj, "captions"); - for (auto& c : menus[i].captions) { + for (auto& c : menu->captions) { auto cc = c.getFillColor(); PyObject* cap_args = Py_BuildValue("si(iii)", c.getString().toAnsiString().c_str(), @@ -245,19 +252,20 @@ PyObject* McRFPy_API::_listMenus(PyObject*, PyObject*) { // Loop: Convert Sprite objects to Python Objects PyObject* sprite_list = PyObject_GetAttrString(menuobj, "sprites"); - for (auto& s : menus[i].sprites) { + for (auto& s : menu->sprites) { PyObject* spr_args = Py_BuildValue("(iiii)", s.texture_index, s.sprite_index, s.x, s.y); } PyList_SET_ITEM(menulist, i, menuobj); + i++; // count iterator steps } return menulist; } -UIMenu McRFPy_API::createMenu(int posx, int posy, int sizex, int sizey) { - auto m = UIMenu(game->getFont()); - m.box.setPosition(sf::Vector2f(posx, posy)); - m.box.setSize(sf::Vector2f(sizex, sizey)); +UIMenu *McRFPy_API::createMenu(int posx, int posy, int sizex, int sizey) { + auto m = new UIMenu(game->getFont()); + m->box.setPosition(sf::Vector2f(posx, posy)); + m->box.setSize(sf::Vector2f(sizex, sizey)); return m; } diff --git a/src/McRFPy_API.h b/src/McRFPy_API.h index bd07931..8c7f139 100644 --- a/src/McRFPy_API.h +++ b/src/McRFPy_API.h @@ -41,9 +41,9 @@ public: static void REPL(); // Jank mode engage: let the API hold data for Python to hack on - static std::vector menus; + static std::map menus; EntityManager entities; // this is also kinda good, entities not on the current grid can still act (like monsters following you through doors??) - static std::vector grids; + static std::map grids; // Jank Python Method Exposures static PyObject* _createMenu(PyObject*, PyObject*); // creates a new menu object in McRFPy_API::menus @@ -59,7 +59,7 @@ public: //static PyObject* _createSprite(PyObject*, PyObject*); // Jank Functionality - static UIMenu createMenu(int posx, int posy, int sizex, int sizey); + static UIMenu* createMenu(int posx, int posy, int sizex, int sizey); //static Button createButton(UIMenu & menu, int x, int y, int w, int h); //static sf::Sprite createSprite(int tex_index, int x, int y); //static void playSound(const char * filename); diff --git a/src/scripts/UIMenu.py b/src/scripts/UIMenu.py new file mode 100644 index 0000000..b02e0bb --- /dev/null +++ b/src/scripts/UIMenu.py @@ -0,0 +1,38 @@ +class Caption: + def __init__(self, text, textsize, color): + self.text = text + self.textsize = textsize + self.color = color + +class Button: + def __init__(self, x, y, w, h, bgcolor, textcolor, text, actioncode): + self.x = x + self.y = y + self.w = w + self.h = h + self.bgcolor = bgcolor + self.textcolor = textcolor + self.text = text + self.actioncode = actioncode + +class Sprite: + def __init__(self, tex_index, x, y): + self.tex_index = tex_index + self.x = x + self.y = y + +class UIMenu: + def __init__(self, title, x, y, w, h, bgcolor, visible=False): + self.title = title + self.x = x + self.y = y + self.w = w + self.h = h + self.bgcolor = bgcolor + self.visible = visible + self.captions = [] + self.buttons = [] + self.sprites = [] + + def __repr__(self): + return f""