Pan/Zoom grids, Python basic generation template provided
This commit is contained in:
parent
a53ae29467
commit
6dbf8a5119
|
@ -238,6 +238,7 @@ void Grid::render(sf::RenderWindow & window)
|
|||
}
|
||||
|
||||
// grid lines for testing & validation
|
||||
/*
|
||||
sf::Vertex line[] =
|
||||
{
|
||||
sf::Vertex(sf::Vector2f(0, 0), sf::Color::Red),
|
||||
|
@ -254,7 +255,7 @@ void Grid::render(sf::RenderWindow & window)
|
|||
};
|
||||
|
||||
renderTexture.draw(lineb, 2, sf::Lines);
|
||||
|
||||
*/
|
||||
|
||||
// render to window
|
||||
renderTexture.display();
|
||||
|
|
|
@ -43,6 +43,9 @@ static PyMethodDef mcrfpyMethods[] = {
|
|||
{"listGrids", McRFPy_API::_listGrids, METH_VARARGS,
|
||||
"return grid objects and all points" },
|
||||
|
||||
{"modGrid", McRFPy_API::_modGrid, METH_VARARGS,
|
||||
"call with a Grid object to update all fields"},
|
||||
|
||||
{NULL, NULL, 0, NULL}
|
||||
};
|
||||
|
||||
|
@ -489,6 +492,8 @@ PyObject* McRFPy_API::_registerPyAction(PyObject *self, PyObject *args)
|
|||
}
|
||||
|
||||
void McRFPy_API::doAction(std::string actionstr) {
|
||||
// hard coded actions that require no registration
|
||||
if (!actionstr.compare("startrepl")) return McRFPy_API::REPL();
|
||||
if (callbacks.find(actionstr) == callbacks.end()) return;
|
||||
//std::cout << "Calling: " << PyUnicode_AsUTF8(PyObject_Repr(callbacks[actionstr])) << std::endl;
|
||||
PyObject_Call(callbacks[actionstr], PyTuple_New(0), NULL);
|
||||
|
@ -558,3 +563,55 @@ PyObject* McRFPy_API::_listGrids(PyObject*, PyObject*) {
|
|||
}
|
||||
return gridlist;
|
||||
}
|
||||
|
||||
PyObject* McRFPy_API::_modGrid(PyObject* self, PyObject* args) {
|
||||
PyObject* o;
|
||||
if (!PyArg_ParseTuple(args, "O", &o)) return NULL;
|
||||
//std::cout << PyUnicode_AsUTF8(PyObject_Repr(o)) << std::endl;
|
||||
std::string title = PyUnicode_AsUTF8(PyObject_GetAttrString(o, "title"));
|
||||
int grid_x = PyLong_AsLong(PyObject_GetAttrString(o, "grid_x"));
|
||||
int grid_y = PyLong_AsLong(PyObject_GetAttrString(o, "grid_y"));
|
||||
int grid_size = PyLong_AsLong(PyObject_GetAttrString(o, "grid_size"));
|
||||
int x = PyLong_AsLong(PyObject_GetAttrString(o, "x"));
|
||||
int y = PyLong_AsLong(PyObject_GetAttrString(o, "y"));
|
||||
int w = PyLong_AsLong(PyObject_GetAttrString(o, "w"));
|
||||
int h = PyLong_AsLong(PyObject_GetAttrString(o, "h"));
|
||||
bool visible = PyObject_IsTrue(PyObject_GetAttrString(o, "visible"));
|
||||
|
||||
auto grid = grids[title];
|
||||
if (grid == NULL) return NULL;
|
||||
grid->box.setPosition(sf::Vector2f(x, y));
|
||||
grid->box.setSize(sf::Vector2f(w, h));
|
||||
grid->visible = visible;
|
||||
|
||||
//iterate over gridpoints
|
||||
PyObject* gpointlist = PyObject_GetAttrString(o, "points");
|
||||
//std::cout << PyUnicode_AsUTF8(PyObject_Repr(gpointlist)) << std::endl;
|
||||
for (int i = 0; i < grid->points.size(); i++) {
|
||||
PyObject* gpointobj = PyList_GetItem(gpointlist, i);
|
||||
PyObject* colortuple = PyObject_GetAttrString(gpointobj, "color");
|
||||
grid->points[i].color =
|
||||
sf::Color(
|
||||
PyLong_AsLong(PyTuple_GetItem(colortuple, 0)),
|
||||
PyLong_AsLong(PyTuple_GetItem(colortuple, 1)),
|
||||
PyLong_AsLong(PyTuple_GetItem(colortuple, 2))
|
||||
);
|
||||
grid->points[i].walkable = PyObject_IsTrue(PyObject_GetAttrString(gpointobj, "walkable"));
|
||||
grid->points[i].tilesprite = PyLong_AsLong(PyObject_GetAttrString(gpointobj, "tilesprite"));
|
||||
grid->points[i].transparent = PyObject_IsTrue(PyObject_GetAttrString(gpointobj, "transparent"));
|
||||
grid->points[i].visible = PyObject_IsTrue(PyObject_GetAttrString(gpointobj, "visible"));
|
||||
grid->points[i].discovered = PyObject_IsTrue(PyObject_GetAttrString(gpointobj, "discovered"));
|
||||
PyObject* overlaycolortuple = PyObject_GetAttrString(gpointobj, "color_overlay");
|
||||
grid->points[i].color_overlay =
|
||||
sf::Color(
|
||||
PyLong_AsLong(PyTuple_GetItem(overlaycolortuple, 0)),
|
||||
PyLong_AsLong(PyTuple_GetItem(overlaycolortuple, 1)),
|
||||
PyLong_AsLong(PyTuple_GetItem(overlaycolortuple, 2))
|
||||
);
|
||||
grid->points[i].tile_overlay = PyLong_AsLong(PyObject_GetAttrString(gpointobj, "tile_overlay"));
|
||||
grid->points[i].uisprite = PyLong_AsLong(PyObject_GetAttrString(gpointobj, "uisprite"));
|
||||
}
|
||||
|
||||
Py_INCREF(Py_None);
|
||||
return Py_None;
|
||||
}
|
||||
|
|
|
@ -67,6 +67,7 @@ public:
|
|||
|
||||
static PyObject* _createGrid(PyObject*, PyObject*);
|
||||
static PyObject* _listGrids(PyObject*, PyObject*);
|
||||
static PyObject* _modGrid(PyObject*, PyObject*);
|
||||
|
||||
static PyObject* _registerPyAction(PyObject*, PyObject*);
|
||||
|
||||
|
|
|
@ -6,7 +6,7 @@ PythonScene::PythonScene(GameEngine* g, std::string pymodule)
|
|||
: Scene(g) {
|
||||
// mouse events
|
||||
registerAction(ActionCode::MOUSEBUTTON + sf::Mouse::Left, "click");
|
||||
registerAction(ActionCode::MOUSEBUTTON + sf::Mouse::Left, "rclick");
|
||||
registerAction(ActionCode::MOUSEBUTTON + sf::Mouse::Right, "rclick");
|
||||
registerAction(ActionCode::MOUSEWHEEL + ActionCode::WHEEL_DEL, "wheel_up");
|
||||
registerAction(ActionCode::MOUSEWHEEL + ActionCode::WHEEL_NEG + ActionCode::WHEEL_DEL, "wheel_down");
|
||||
|
||||
|
@ -35,6 +35,7 @@ PythonScene::PythonScene(GameEngine* g, std::string pymodule)
|
|||
registerAction(0, "event");
|
||||
|
||||
dragging = false;
|
||||
drag_grid = NULL;
|
||||
|
||||
// import pymodule and call start()
|
||||
McRFPy_API::executePyString("import " + pymodule);
|
||||
|
@ -47,11 +48,13 @@ void PythonScene::update() {
|
|||
// check if left click is still down & mouse has moved
|
||||
// continue the drag motion
|
||||
if (dragging && drag_grid) {
|
||||
//std::cout << "Compute dragging" << std::endl;
|
||||
auto mousepos = sf::Mouse::getPosition(game->getWindow());
|
||||
auto dx = mousepos.x - mouseprev.x,
|
||||
dy = mousepos.y - mouseprev.y;
|
||||
auto dx = mouseprev.x - mousepos.x,
|
||||
dy = mouseprev.y - mousepos.y;
|
||||
drag_grid->center_x += (dx / drag_grid->zoom);
|
||||
drag_grid->center_y += (dy / drag_grid->zoom);
|
||||
mouseprev = mousepos;
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -104,8 +107,10 @@ void PythonScene::doZoom(sf::Vector2i mousepos, int value) {
|
|||
|
||||
void PythonScene::doAction(std::string name, std::string type) {
|
||||
auto mousepos = sf::Mouse::getPosition(game->getWindow());
|
||||
//std::cout << "name: " << name << ", type: " << type << std::endl;
|
||||
if (ACTIONONCE("click")) {
|
||||
// left click start
|
||||
//std::cout << "LClick started at (" << mousepos.x << ", " << mousepos.y << ")" << std::endl;
|
||||
dragstart = mousepos;
|
||||
mouseprev = mousepos;
|
||||
dragging = true;
|
||||
|
@ -120,9 +125,11 @@ void PythonScene::doAction(std::string name, std::string type) {
|
|||
}
|
||||
else if (ACTIONAFTER("click")) {
|
||||
// left click end
|
||||
//std::cout << "LClick ended at (" << mousepos.x << ", " << mousepos.y << ")" << std::endl;
|
||||
// if click ended without starting a drag event, try lclick?
|
||||
if (dragstart == mousepos) {
|
||||
// mouse did not move, do click
|
||||
//std::cout << "(did not move)" << std::endl;
|
||||
doLClick(mousepos);
|
||||
}
|
||||
dragging = false;
|
||||
|
|
|
@ -3,7 +3,7 @@
|
|||
// macros for scene input
|
||||
#define ACTION(X, Y) (name.compare(X) == 0 && type.compare(Y) == 0)
|
||||
#define ACTIONONCE(X) ((name.compare(X) == 0 && type.compare("start") == 0 && !actionState[name]))
|
||||
#define ACTIONAFTER(X) ((name.compare(X) == 0 && type.compare("end") == 0 && actionState[name]))
|
||||
#define ACTIONAFTER(X) ((name.compare(X) == 0 && type.compare("end") == 0))
|
||||
|
||||
#include "Common.h"
|
||||
//#include "GameEngine.h"
|
||||
|
|
|
@ -1,4 +1,57 @@
|
|||
import UIMenu
|
||||
import Grid
|
||||
import mcrfpy
|
||||
from random import randint
|
||||
print("TestScene imported")
|
||||
BLACK = (0, 0, 0)
|
||||
WHITE = (255, 255, 255)
|
||||
RED, GREEN, BLUE = (255, 0, 0), (0, 255, 0), (0, 0, 255)
|
||||
DARKRED, DARKGREEN, DARKBLUE = (192, 0, 0), (0, 192, 0), (0, 0, 192)
|
||||
|
||||
class TestScene:
|
||||
def __init__(self, ui_name = "demobox1", grid_name = "demogrid"):
|
||||
# Texture & Sound Loading
|
||||
mcrfpy.createTexture("./assets/test_portraits.png", 32, 8, 8) #0 - portraits
|
||||
self.ui_name = ui_name
|
||||
self.grid_name = grid_name
|
||||
|
||||
# Create dialog UI
|
||||
mcrfpy.createMenu(ui_name, 20, 520, 500, 200)
|
||||
mcrfpy.createCaption(ui_name, "Hello There", 18, BLACK)
|
||||
mcrfpy.createCaption(ui_name, "", 18, BLACK)
|
||||
mcrfpy.createButton(ui_name, 250, 20, 100, 50, DARKBLUE, (0, 0, 0), "clicky", "testaction")
|
||||
mcrfpy.createButton(ui_name, 250, 20, 100, 50, DARKRED, (0, 0, 0), "REPL", "startrepl")
|
||||
mcrfpy.createButton(ui_name, 250, 20, 100, 50, DARKRED, (0, 0, 0), "map gen", "gridgen")
|
||||
mcrfpy.createSprite(ui_name, 0, randint(0, 3), 300, 20, 5.0)
|
||||
self.menus = mcrfpy.listMenus()
|
||||
self.menus[0].visible = True
|
||||
mcrfpy.modMenu(self.menus[0])
|
||||
|
||||
# Button behavior
|
||||
self.clicks = 0
|
||||
mcrfpy.registerPyAction("testaction", self.click)
|
||||
mcrfpy.registerPyAction("gridgen", self.gridgen)
|
||||
|
||||
# create grid (title, gx, gy, gs, x, y, w, h)
|
||||
mcrfpy.createGrid(grid_name, 20, 20, 16, 20, 20, 800, 500)
|
||||
self.grids = mcrfpy.listGrids()
|
||||
|
||||
def click(self):
|
||||
self.clicks += 1
|
||||
self.menus[0].captions[1].text = f"Clicks: {self.clicks}"
|
||||
self.menus[0].sprites[0].sprite_index = randint(0, 3)
|
||||
mcrfpy.modMenu(self.menus[0])
|
||||
|
||||
def gridgen(self):
|
||||
print(f"[Python] modifying {len(self.grids[0].points)} grid points")
|
||||
for p in self.grids[0].points:
|
||||
p.color = (randint(0, 255), randint(64, 192), 0)
|
||||
print("[Python] Modifying:")
|
||||
self.grids[0].visible = True
|
||||
mcrfpy.modGrid(self.grids[0])
|
||||
scene = None
|
||||
def start():
|
||||
global scene
|
||||
print("TestScene.start called")
|
||||
scene = TestScene()
|
||||
|
||||
|
|
Loading…
Reference in New Issue