Updated UIMenu to a map on the C++ side, Python gets the title property so changes can be properly, jankily, looked up

This commit is contained in:
John McCardle 2023-03-02 06:35:13 -05:00
parent a1e9129923
commit c8124e84dc
3 changed files with 68 additions and 22 deletions

View File

@ -4,8 +4,8 @@
#include "Grid.h" #include "Grid.h"
// static class members...? // static class members...?
std::vector<UIMenu> McRFPy_API::menus; std::map<std::string, UIMenu*> McRFPy_API::menus;
std::vector<Grid> McRFPy_API::grids; std::map<std::string, Grid*> McRFPy_API::grids;
static PyMethodDef mcrfpyMethods[] = { static PyMethodDef mcrfpyMethods[] = {
{"drawSprite", McRFPy_API::_drawSprite, METH_VARARGS, {"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) { PyObject* McRFPy_API::_createMenu(PyObject *self, PyObject *args) {
const char* title_cstr;
int posx, posy, sizex, sizey; int posx, posy, sizex, sizey;
if (!PyArg_ParseTuple(args, "iiii", &posx, &posy, &sizex, &sizey)) return NULL; if (!PyArg_ParseTuple(args, "siiii", &title_cstr, &posx, &posy, &sizex, &sizey)) return NULL;
menus.push_back(createMenu(posx, posy, sizex, sizey)); std::string title = title_cstr;
menus[title] = createMenu(posx, posy, sizex, sizey);
Py_INCREF(Py_None); Py_INCREF(Py_None);
return Py_None; return Py_None;
} }
@ -203,20 +204,26 @@ PyObject* McRFPy_API::_listMenus(PyObject*, PyObject*) {
PyObject* spr_type = PyObject_GetAttrString(uimodule, "Sprite"); PyObject* spr_type = PyObject_GetAttrString(uimodule, "Sprite");
PyObject* menulist = PyList_New(menus.size()); PyObject* menulist = PyList_New(menus.size());
for (int i = 0; i < menus.size(); i++) { std::map<std::string, UIMenu*>::iterator it = menus.begin();
auto p = menus[i].box.getPosition(); //for (int i = 0; i < menus.size(); i++) {
auto s = menus[i].box.getSize(); int i = 0;
auto g = menus[i].box.getFillColor(); for (auto it = menus.begin(); it != menus.end(); it++) {
PyObject* menu_args = Py_BuildValue("(iiii(iii)O)", 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)p.x, (int)p.y, (int)s.x, (int)s.y,
(int)g.r, (int)g.g, (int)g.b, (int)g.r, (int)g.g, (int)g.b,
menus[i].visible ? Py_True: Py_False); menu->visible ? Py_True: Py_False);
menus[i].visible ? Py_INCREF(Py_True) : Py_INCREF(Py_False); menu->visible ? Py_INCREF(Py_True) : Py_INCREF(Py_False);
PyObject* menuobj = PyObject_CallObject((PyObject*) uimenu_type, menu_args); PyObject* menuobj = PyObject_CallObject((PyObject*) uimenu_type, menu_args);
// Loop: Convert Button objects to Python Objects // Loop: Convert Button objects to Python Objects
PyObject* button_list = PyObject_GetAttrString(menuobj, "buttons"); PyObject* button_list = PyObject_GetAttrString(menuobj, "buttons");
for(auto& b : menus[i].buttons) { for(auto& b : menu->buttons) {
auto bp = b.rect.getPosition(); auto bp = b.rect.getPosition();
auto bs = b.rect.getSize(); auto bs = b.rect.getSize();
auto bg = b.rect.getFillColor(); auto bg = b.rect.getFillColor();
@ -233,7 +240,7 @@ PyObject* McRFPy_API::_listMenus(PyObject*, PyObject*) {
// Loop: Convert Caption objects to Python Objects // Loop: Convert Caption objects to Python Objects
PyObject* caption_list = PyObject_GetAttrString(menuobj, "captions"); PyObject* caption_list = PyObject_GetAttrString(menuobj, "captions");
for (auto& c : menus[i].captions) { for (auto& c : menu->captions) {
auto cc = c.getFillColor(); auto cc = c.getFillColor();
PyObject* cap_args = Py_BuildValue("si(iii)", PyObject* cap_args = Py_BuildValue("si(iii)",
c.getString().toAnsiString().c_str(), c.getString().toAnsiString().c_str(),
@ -245,19 +252,20 @@ PyObject* McRFPy_API::_listMenus(PyObject*, PyObject*) {
// Loop: Convert Sprite objects to Python Objects // Loop: Convert Sprite objects to Python Objects
PyObject* sprite_list = PyObject_GetAttrString(menuobj, "sprites"); PyObject* sprite_list = PyObject_GetAttrString(menuobj, "sprites");
for (auto& s : menus[i].sprites) { for (auto& s : menu->sprites) {
PyObject* spr_args = Py_BuildValue("(iiii)", PyObject* spr_args = Py_BuildValue("(iiii)",
s.texture_index, s.sprite_index, s.x, s.y); s.texture_index, s.sprite_index, s.x, s.y);
} }
PyList_SET_ITEM(menulist, i, menuobj); PyList_SET_ITEM(menulist, i, menuobj);
i++; // count iterator steps
} }
return menulist; return menulist;
} }
UIMenu McRFPy_API::createMenu(int posx, int posy, int sizex, int sizey) { UIMenu *McRFPy_API::createMenu(int posx, int posy, int sizex, int sizey) {
auto m = UIMenu(game->getFont()); auto m = new UIMenu(game->getFont());
m.box.setPosition(sf::Vector2f(posx, posy)); m->box.setPosition(sf::Vector2f(posx, posy));
m.box.setSize(sf::Vector2f(sizex, sizey)); m->box.setSize(sf::Vector2f(sizex, sizey));
return m; return m;
} }

View File

@ -41,9 +41,9 @@ public:
static void REPL(); static void REPL();
// Jank mode engage: let the API hold data for Python to hack on // Jank mode engage: let the API hold data for Python to hack on
static std::vector<UIMenu> menus; static std::map<std::string, UIMenu*> menus;
EntityManager entities; // this is also kinda good, entities not on the current grid can still act (like monsters following you through doors??) 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<Grid> grids; static std::map<std::string, Grid*> grids;
// Jank Python Method Exposures // Jank Python Method Exposures
static PyObject* _createMenu(PyObject*, PyObject*); // creates a new menu object in McRFPy_API::menus static PyObject* _createMenu(PyObject*, PyObject*); // creates a new menu object in McRFPy_API::menus
@ -59,7 +59,7 @@ public:
//static PyObject* _createSprite(PyObject*, PyObject*); //static PyObject* _createSprite(PyObject*, PyObject*);
// Jank Functionality // 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 Button createButton(UIMenu & menu, int x, int y, int w, int h);
//static sf::Sprite createSprite(int tex_index, int x, int y); //static sf::Sprite createSprite(int tex_index, int x, int y);
//static void playSound(const char * filename); //static void playSound(const char * filename);

38
src/scripts/UIMenu.py Normal file
View File

@ -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"<UIMenu title={repr(self.title)}, x={self.x}, y={self.y}, w={self.w}, h={self.h}, bgcolor={self.bgcolor}, visible={self.visible}, {len(self.captions)} captions, {len(self.buttons)} buttons, {len(self.sprites)} sprites>"