UI from Python now working fairly comprehensively

This commit is contained in:
John McCardle 2023-03-02 18:57:09 -05:00
parent c8124e84dc
commit de753713d5
10 changed files with 156 additions and 6 deletions

BIN
assets/test_portraits.ase Normal file

Binary file not shown.

BIN
assets/test_portraits.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.4 KiB

View File

@ -10,7 +10,11 @@ sf::Sprite IndexSprite::drawable()
sf::Sprite s;
auto& tex = IndexSprite::game->textures[texture_index];
s.setTexture(tex.texture);
s.setScale(sf::Vector2f(scale, scale));
s.setPosition(sf::Vector2f(x, y));
s.setTextureRect(tex.spriteCoordinates(sprite_index));
return s;
}
IndexSprite::IndexSprite(int _ti, int _si, int _x, int _y, float _s):
texture_index(_ti), sprite_index(_si), x(_x), y(_y), scale(_s) {}

View File

@ -5,6 +5,8 @@ class GameEngine; // forward declare
class IndexSprite {
public:
int texture_index, sprite_index, x, y;
float scale;
static GameEngine* game;
sf::Sprite drawable();
IndexSprite(int, int, int, int, float);
};

View File

@ -4,3 +4,8 @@ sf::IntRect IndexTexture::spriteCoordinates(int index) {
int tx = index % grid_width, ty = index / grid_width;
return sf::IntRect(tx * grid_size, ty * grid_size, grid_size, grid_size);
}
IndexTexture::IndexTexture (sf::Texture t, int gs, int gw, int gh):
grid_size(gs), grid_width(gw), grid_height(gh) {
texture = t;
}

View File

@ -8,4 +8,5 @@ public:
int grid_size, grid_width, grid_height;
static GameEngine* game;
sf::IntRect spriteCoordinates(int);
IndexTexture(sf::Texture, int, int, int);
};

View File

@ -12,11 +12,23 @@ static PyMethodDef mcrfpyMethods[] = {
"Draw a sprite (index, x, y)"},
{"createMenu", McRFPy_API::_createMenu, METH_VARARGS,
"Create a new uimenu (x, y, w, h)"},
"Create a new uimenu (name_str, x, y, w, h)"},
{"listMenus", McRFPy_API::_listMenus, METH_VARARGS,
"return a list of existing menus"},
{"createCaption", McRFPy_API::_createCaption, METH_VARARGS,
"Create a new text caption (menu_str, text_str, fontsize, r, g, b)"},
{"createButton", McRFPy_API::_createButton, METH_VARARGS,
"Create a new button (menu_str, x, y, w, h, (bg r, g, b), (text r, g, b), caption, action_code)"},
{"createSprite", McRFPy_API::_createSprite, METH_VARARGS,
"Create a new sprite (menu_str, texture_index, sprite_index, x, y, scale)"},
{"createTexture", McRFPy_API::_createTexture, METH_VARARGS,
"Create a new texture (filename_str, grid_size, width, height) - grid_size is in pixels (only square sprites for now), width and height are in tiles"},
{NULL, NULL, 0, NULL}
};
@ -263,9 +275,84 @@ PyObject* McRFPy_API::_listMenus(PyObject*, PyObject*) {
return menulist;
}
PyObject* McRFPy_API::_createCaption(PyObject* self, PyObject* args) {
const char* menukey_cstr, *text_cstr;
int fontsize, cr, cg, cb;
if (!PyArg_ParseTuple(args, "ssi(iii)",
&menukey_cstr, &text_cstr,
&fontsize, &cr, &cg, &cb)) return NULL;
createCaption(std::string(menukey_cstr), std::string(text_cstr), fontsize, sf::Color(cr, cg, cb));
Py_INCREF(Py_None);
return Py_None;
}
PyObject* McRFPy_API::_createButton(PyObject* self, PyObject* args) {
const char *menukey_cstr, *caption_cstr, *action_cstr;
int x, y, w, h, bgr, bgg, bgb, fgr, fgg, fgb;
if (!PyArg_ParseTuple(args, "siiii(iii)(iii)ss",
&menukey_cstr, &x, &y, &w, &h,
&bgr, &bgg, &bgb, &fgr, &fgg, &fgb,
&caption_cstr, &action_cstr
)) return NULL;
createButton(std::string(menukey_cstr), x, y, w, h, sf::Color(bgr, bgg, bgb), sf::Color(fgr, fgg, fgb), std::string(caption_cstr), std::string(action_cstr));
Py_INCREF(Py_None);
return Py_None;
}
PyObject* McRFPy_API::_createTexture(PyObject* self, PyObject* args) {
const char *fn_cstr;
int gs, gw, gh;
if (!PyArg_ParseTuple(args, "siii", &fn_cstr, &gs, &gw, &gh)) return NULL;
createTexture(std::string(fn_cstr), gs, gw, gh);
Py_INCREF(Py_None);
return Py_None;
}
PyObject* McRFPy_API::_listTextures(PyObject*, PyObject*) {
Py_INCREF(Py_None);
return Py_None;
}
PyObject* McRFPy_API::_createSprite(PyObject* self, PyObject* args) {
const char * menu_cstr;
int ti, si, x, y;
float s;
if (!PyArg_ParseTuple(args, "siiiif", &menu_cstr, &ti, &si, &x, &y, &s)) return NULL;
createSprite(std::string(menu_cstr), ti, si, x, y, s);
Py_INCREF(Py_None);
return Py_None;
}
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;
}
void McRFPy_API::createCaption(std::string menukey, std::string text, int fontsize, sf::Color textcolor) {
auto menu = menus[menukey];
menu->add_caption(text.c_str(), fontsize, textcolor);
}
void McRFPy_API::createButton(std::string menukey, int x, int y, int w, int h, sf::Color bgcolor, sf::Color textcolor, std::string caption, std::string action) {
auto menu = menus[menukey];
auto b = Button(x, y, w, h, bgcolor, textcolor, caption.c_str(), game->getFont(), action.c_str());
menu->add_button(b);
}
void McRFPy_API::createSprite(std::string menukey, int ti, int si, int x, int y, float scale) {
auto menu = menus[menukey];
auto s = IndexSprite(ti, si, x, y, scale);
menu->add_sprite(s);
}
int McRFPy_API::createTexture(std::string filename, int grid_size, int grid_width, int grid_height) {
sf::Texture t;
t.loadFromFile(filename.c_str());
t.setSmooth(false);
auto indextex = IndexTexture(t, grid_size, grid_width, grid_height);
game->textures.push_back(indextex);
return game->textures.size() - 1;
}

View File

@ -48,20 +48,28 @@ public:
// Jank Python Method Exposures
static PyObject* _createMenu(PyObject*, PyObject*); // creates a new menu object in McRFPy_API::menus
static PyObject* _listMenus(PyObject*, PyObject*);
//static PyObject* _createCaption(PyObject*, PyObject*); // calls menu.add_caption
static PyObject* _createCaption(PyObject*, PyObject*); // calls menu.add_caption
static PyObject* _createButton(PyObject*, PyObject*);
static PyObject* _createTexture(PyObject*, PyObject*);
static PyObject* _listTextures(PyObject*, PyObject*);
static PyObject* _createSprite(PyObject*, PyObject*);
// use _listMenus, probably will not implement
//static PyObject* _listCaptions(PyObject*, PyObject*);
//static PyObject* _createButton(PyObject*, PyObject*);
//static PyObject* _listButtons(PyObject*, PyObject*);
//static PyObject* _createEntity(PyObject*, PyObject*);
//static PyObject* _listEntities(PyObject*, PyObject*);
//static PyObject* _createGrid(PyObject*, PyObject*);
//static PyObject* _listGrids(PyObject*, PyObject*);
//static PyObject* _createSprite(PyObject*, PyObject*);
// Jank Functionality
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 createCaption(std::string menukey, std::string text, int fontsize, sf::Color textcolor);
static void createButton(std::string menukey, int x, int y, int w, int h, sf::Color bgcolor, sf::Color textcolor, std::string caption, std::string action);
static void createSprite(std::string menukey, int ti, int si, int x, int y, float scale);
static int createTexture(std::string filename, int grid_size, int grid_width, int grid_height);
//static void playSound(const char * filename);
//static void playMusic(const char * filename);

View File

@ -274,6 +274,12 @@ void UITestScene::sRender()
}
// Python API menus
for (auto pair: McRFPy_API::menus)
{
pair.second->render(game->getWindow());
}
// test Python sprite code
//McRFPy_API::executePyString("mcrfpy.drawSprite(123, 36, 10)");

37
src/scripts/test_ui.py Normal file
View File

@ -0,0 +1,37 @@
import mcrfpy
mcrfpy.createTexture("./assets/test_portraits.png", 32, 8, 8)
from random import choice, randint
box_colors = [
(0, 0, 192),
(0, 192, 0),
(192, 0, 0),
(192, 192, 0),
(0, 192, 192),
(192, 0, 192)
]
text_colors = [
(0, 0, 255),
(0, 255, 0),
(255, 0, 0),
(255, 255, 0),
(0, 255, 255),
(255, 0, 255)
]
test_x = 500
test_y = 10
for i in range(40):
ui_name = f"test{i}"
mcrfpy.createMenu(ui_name, test_x, test_y, 400, 200)
mcrfpy.createCaption(ui_name, "Hello There", 18, choice(text_colors))
mcrfpy.createButton(ui_name, 250, 20, 100, 50, choice(box_colors), (0, 0, 0), "asdf", "testaction")
mcrfpy.createSprite(ui_name, 0, randint(0, 3), 650, 60, 5.0)
test_x -= 50
test_y += 50
if (test_x <= 50):
test_x = 500
#print(test_x)