Tinkering with input
I want to move keyboard input defs to the Python API. I laid the groundwork for it today. From the JANKFILE: - working on API endpoint `_registerInputAction`. it will add "_py" as a suffix to the action string and register it along with other scene actions. - Adding public Scene methods. These are on the base class with default of return `false`. `bool Scene::registerActionInjected(int code, std::string name)` and `unregisterActionInjected` the PythonScene (and other scenes that support injected user input) can override this method, check existing registrations, and return `true` when succeeding. Also, upgraded to C++20 (g++ `c++2a`), mostly because I want to use map::contains.
This commit is contained in:
parent
d3826804a0
commit
d6446e18ea
10 changed files with 134 additions and 7 deletions
|
|
@ -8,7 +8,7 @@
|
|||
GameEngine::GameEngine()
|
||||
{
|
||||
font.loadFromFile("./assets/JetbrainsMono.ttf");
|
||||
window.create(sf::VideoMode(1024, 768), "McRogueFace - LGJ Submission by John McCardle");
|
||||
window.create(sf::VideoMode(1024, 768), "McRogueFace - r/RoguelikeDev Tutorial Run");
|
||||
visible = window.getDefaultView();
|
||||
window.setFramerateLimit(30);
|
||||
scene = "menu";
|
||||
|
|
|
|||
|
|
@ -45,6 +45,9 @@ static PyMethodDef mcrfpyMethods[] = {
|
|||
|
||||
{"registerPyAction", McRFPy_API::_registerPyAction, METH_VARARGS,
|
||||
"Register a callable Python object to correspond to an action string. (actionstr, callable)"},
|
||||
|
||||
{"registerInputAction", McRFPy_API::_registerInputAction, METH_VARARGS,
|
||||
"Register a SFML input code to correspond to an action string. (input_code, actionstr)"},
|
||||
|
||||
{"createGrid", McRFPy_API::_createGrid, METH_VARARGS,
|
||||
"create a new grid (title, grid_x, grid_y, grid_size, x, y, w, h). grid_x and grid_y are the width and height in squares. grid_size is the pixel w/h of sprites on the grid. x,y are the grid's screen position. w,h are the grid's screen size" },
|
||||
|
|
@ -536,6 +539,8 @@ PyObject* McRFPy_API::_registerPyAction(PyObject *self, PyObject *args)
|
|||
PyObject* callable;
|
||||
const char * actionstr;
|
||||
if (!PyArg_ParseTuple(args, "sO", &actionstr, &callable)) return NULL;
|
||||
//TODO: if the string already exists in the callbacks map,
|
||||
// decrease our reference count so it can potentially be garbage collected
|
||||
callbacks[std::string(actionstr)] = callable;
|
||||
Py_INCREF(callable);
|
||||
|
||||
|
|
@ -544,11 +549,35 @@ PyObject* McRFPy_API::_registerPyAction(PyObject *self, PyObject *args)
|
|||
return Py_None;
|
||||
}
|
||||
|
||||
PyObject* McRFPy_API::_registerInputAction(PyObject *self, PyObject *args)
|
||||
{
|
||||
int action_code;
|
||||
const char * actionstr;
|
||||
if (!PyArg_ParseTuple(args, "iz", &action_code, &actionstr)) return NULL;
|
||||
|
||||
bool success;
|
||||
|
||||
if (actionstr == NULL) { // Action provided is None, i.e. unregister
|
||||
success = game->currentScene()->unregisterActionInjected(action_code, std::string(actionstr) + "_py");
|
||||
} else {
|
||||
success = game->currentScene()->registerActionInjected(action_code, std::string(actionstr) + "_py");
|
||||
}
|
||||
|
||||
success ? Py_INCREF(Py_True) : Py_INCREF(Py_False);
|
||||
return success ? Py_True : Py_False;
|
||||
|
||||
}
|
||||
|
||||
void McRFPy_API::doAction(std::string actionstr) {
|
||||
// hard coded actions that require no registration
|
||||
//std::cout << "Calling Python Action: " << actionstr;
|
||||
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;
|
||||
if (callbacks.find(actionstr) == callbacks.end())
|
||||
{
|
||||
//std::cout << " (not found)" << std::endl;
|
||||
return;
|
||||
}
|
||||
//std::cout << " (" << PyUnicode_AsUTF8(PyObject_Repr(callbacks[actionstr])) << ")" << std::endl;
|
||||
PyObject_Call(callbacks[actionstr], PyTuple_New(0), NULL);
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -82,6 +82,7 @@ public:
|
|||
static PyObject* _createAnimation(PyObject*, PyObject*);
|
||||
|
||||
static PyObject* _registerPyAction(PyObject*, PyObject*);
|
||||
static PyObject* _registerInputAction(PyObject*, PyObject*);
|
||||
|
||||
static PyObject* _createSoundBuffer(PyObject*, PyObject*);
|
||||
static PyObject* _loadMusic(PyObject*, PyObject*);
|
||||
|
|
|
|||
|
|
@ -4,7 +4,7 @@
|
|||
MenuScene::MenuScene(GameEngine* g) : Scene(g)
|
||||
{
|
||||
text.setFont(game->getFont());
|
||||
text.setString("McRogueFace Engine - LGJ 2023 (Incomplete)");
|
||||
text.setString("McRogueFace Engine - r/RoguelikeDev Tutorial 2023");
|
||||
text.setCharacterSize(24);
|
||||
//std::cout << "MenuScene Initialized. " << game << std::endl;
|
||||
//std::cout << "Font: " << game->getFont().getInfo().family << std::endl;
|
||||
|
|
|
|||
|
|
@ -186,7 +186,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")) {
|
||||
if (ACTIONPY) {
|
||||
McRFPy_API::doAction(name);
|
||||
}
|
||||
else if (ACTIONONCE("click")) {
|
||||
// left click start
|
||||
//std::cout << "LClick started at (" << mousepos.x << ", " << mousepos.y << ")" << std::endl;
|
||||
dragstart = mousepos;
|
||||
|
|
@ -237,6 +240,15 @@ void PythonScene::doAction(std::string name, std::string type) {
|
|||
else if (ACTIONONCE("wait")) { McRFPy_API::player_input(+0, +0); }
|
||||
}
|
||||
|
||||
bool PythonScene::registerActionInjected(int code, std::string name) {
|
||||
return false;
|
||||
|
||||
}
|
||||
|
||||
bool PythonScene::unregisterActionInjected(int code, std::string name) {
|
||||
return false;
|
||||
}
|
||||
|
||||
void PythonScene::sRender() {
|
||||
game->getWindow().clear();
|
||||
|
||||
|
|
|
|||
|
|
@ -17,9 +17,13 @@ class PythonScene: public Scene
|
|||
void doZoom(sf::Vector2i, int);
|
||||
//std::list<Animation*> animations;
|
||||
void animate();
|
||||
std::map<std::string, bool> actionInjected;
|
||||
|
||||
public:
|
||||
PythonScene(GameEngine*, std::string);
|
||||
void update() override final;
|
||||
void doAction(std::string, std::string) override final;
|
||||
void sRender() override final;
|
||||
bool registerActionInjected(int, std::string) override final;
|
||||
bool unregisterActionInjected(int, std::string) override final;
|
||||
};
|
||||
|
|
|
|||
|
|
@ -23,3 +23,13 @@ std::string Scene::action(int code)
|
|||
{
|
||||
return actions[code];
|
||||
}
|
||||
|
||||
bool Scene::registerActionInjected(int code, std::string name)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
bool Scene::unregisterActionInjected(int code, std::string name)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
|
@ -4,6 +4,7 @@
|
|||
#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))
|
||||
#define ACTIONPY ((name.size() > 3 && name.compare(name.size() - 3, 3, "_py") == 0))
|
||||
|
||||
#include "Common.h"
|
||||
//#include "GameEngine.h"
|
||||
|
|
@ -32,5 +33,8 @@ public:
|
|||
bool hasAction(std::string);
|
||||
bool hasAction(int);
|
||||
std::string action(int);
|
||||
|
||||
virtual bool registerActionInjected(int, std::string);
|
||||
virtual bool unregisterActionInjected(int, std::string);
|
||||
|
||||
};
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue