Keyboard initial functionality commit

This commit is contained in:
John McCardle 2024-03-07 08:13:37 -05:00
parent 9587218b28
commit 9d728ee902
6 changed files with 154 additions and 47 deletions

View File

@ -31,4 +31,116 @@ public:
if (a & WHEEL_NEG) factor = -1;
return (a & WHEEL_DEL) * factor;
}
static std::string key_str(sf::Keyboard::Key& keycode)
{
switch(keycode)
{
case sf::Keyboard::Key::Unknown: return "Unknown"; break;
case sf::Keyboard::Key::A: return "A"; break;
case sf::Keyboard::Key::B: return "B"; break;
case sf::Keyboard::Key::C: return "C"; break;
case sf::Keyboard::Key::D: return "D"; break;
case sf::Keyboard::Key::E: return "E"; break;
case sf::Keyboard::Key::F: return "F"; break;
case sf::Keyboard::Key::G: return "G"; break;
case sf::Keyboard::Key::H: return "H"; break;
case sf::Keyboard::Key::I: return "I"; break;
case sf::Keyboard::Key::J: return "J"; break;
case sf::Keyboard::Key::K: return "K"; break;
case sf::Keyboard::Key::L: return "L"; break;
case sf::Keyboard::Key::M: return "M"; break;
case sf::Keyboard::Key::N: return "N"; break;
case sf::Keyboard::Key::O: return "O"; break;
case sf::Keyboard::Key::P: return "P"; break;
case sf::Keyboard::Key::Q: return "Q"; break;
case sf::Keyboard::Key::R: return "R"; break;
case sf::Keyboard::Key::S: return "S"; break;
case sf::Keyboard::Key::T: return "T"; break;
case sf::Keyboard::Key::U: return "U"; break;
case sf::Keyboard::Key::V: return "V"; break;
case sf::Keyboard::Key::W: return "W"; break;
case sf::Keyboard::Key::X: return "X"; break;
case sf::Keyboard::Key::Y: return "Y"; break;
case sf::Keyboard::Key::Z: return "Z"; break;
case sf::Keyboard::Key::Num0: return "Num0"; break;
case sf::Keyboard::Key::Num1: return "Num1"; break;
case sf::Keyboard::Key::Num2: return "Num2"; break;
case sf::Keyboard::Key::Num3: return "Num3"; break;
case sf::Keyboard::Key::Num4: return "Num4"; break;
case sf::Keyboard::Key::Num5: return "Num5"; break;
case sf::Keyboard::Key::Num6: return "Num6"; break;
case sf::Keyboard::Key::Num7: return "Num7"; break;
case sf::Keyboard::Key::Num8: return "Num8"; break;
case sf::Keyboard::Key::Num9: return "Num9"; break;
case sf::Keyboard::Key::Escape: return "Escape"; break;
case sf::Keyboard::Key::LControl: return "LControl"; break;
case sf::Keyboard::Key::LShift: return "LShift"; break;
case sf::Keyboard::Key::LAlt: return "LAlt"; break;
case sf::Keyboard::Key::LSystem: return "LSystem"; break;
case sf::Keyboard::Key::RControl: return "RControl"; break;
case sf::Keyboard::Key::RShift: return "RShift"; break;
case sf::Keyboard::Key::RAlt: return "RAlt"; break;
case sf::Keyboard::Key::RSystem: return "RSystem"; break;
case sf::Keyboard::Key::Menu: return "Menu"; break;
case sf::Keyboard::Key::LBracket: return "LBracket"; break;
case sf::Keyboard::Key::RBracket: return "RBracket"; break;
case sf::Keyboard::Key::Semicolon: return "Semicolon"; break;
case sf::Keyboard::Key::Comma: return "Comma"; break;
case sf::Keyboard::Key::Period: return "Period"; break;
case sf::Keyboard::Key::Apostrophe: return "Apostrophe"; break;
case sf::Keyboard::Key::Slash: return "Slash"; break;
case sf::Keyboard::Key::Backslash: return "Backslash"; break;
case sf::Keyboard::Key::Grave: return "Grave"; break;
case sf::Keyboard::Key::Equal: return "Equal"; break;
case sf::Keyboard::Key::Hyphen: return "Hyphen"; break;
case sf::Keyboard::Key::Space: return "Space"; break;
case sf::Keyboard::Key::Enter: return "Enter"; break;
case sf::Keyboard::Key::Backspace: return "Backspace"; break;
case sf::Keyboard::Key::Tab: return "Tab"; break;
case sf::Keyboard::Key::PageUp: return "PageUp"; break;
case sf::Keyboard::Key::PageDown: return "PageDown"; break;
case sf::Keyboard::Key::End: return "End"; break;
case sf::Keyboard::Key::Home: return "Home"; break;
case sf::Keyboard::Key::Insert: return "Insert"; break;
case sf::Keyboard::Key::Delete: return "Delete"; break;
case sf::Keyboard::Key::Add: return "Add"; break;
case sf::Keyboard::Key::Subtract: return "Subtract"; break;
case sf::Keyboard::Key::Multiply: return "Multiply"; break;
case sf::Keyboard::Key::Divide: return "Divide"; break;
case sf::Keyboard::Key::Left: return "Left"; break;
case sf::Keyboard::Key::Right: return "Right"; break;
case sf::Keyboard::Key::Up: return "Up"; break;
case sf::Keyboard::Key::Down: return "Down"; break;
case sf::Keyboard::Key::Numpad0: return "Numpad0"; break;
case sf::Keyboard::Key::Numpad1: return "Numpad1"; break;
case sf::Keyboard::Key::Numpad2: return "Numpad2"; break;
case sf::Keyboard::Key::Numpad3: return "Numpad3"; break;
case sf::Keyboard::Key::Numpad4: return "Numpad4"; break;
case sf::Keyboard::Key::Numpad5: return "Numpad5"; break;
case sf::Keyboard::Key::Numpad6: return "Numpad6"; break;
case sf::Keyboard::Key::Numpad7: return "Numpad7"; break;
case sf::Keyboard::Key::Numpad8: return "Numpad8"; break;
case sf::Keyboard::Key::Numpad9: return "Numpad9"; break;
case sf::Keyboard::Key::F1: return "F1"; break;
case sf::Keyboard::Key::F2: return "F2"; break;
case sf::Keyboard::Key::F3: return "F3"; break;
case sf::Keyboard::Key::F4: return "F4"; break;
case sf::Keyboard::Key::F5: return "F5"; break;
case sf::Keyboard::Key::F6: return "F6"; break;
case sf::Keyboard::Key::F7: return "F7"; break;
case sf::Keyboard::Key::F8: return "F8"; break;
case sf::Keyboard::Key::F9: return "F9"; break;
case sf::Keyboard::Key::F10: return "F10"; break;
case sf::Keyboard::Key::F11: return "F11"; break;
case sf::Keyboard::Key::F12: return "F12"; break;
case sf::Keyboard::Key::F13: return "F13"; break;
case sf::Keyboard::Key::F14: return "F14"; break;
case sf::Keyboard::Key::F15: return "F15"; break;
case sf::Keyboard::Key::Pause: return "Pause"; break;
default:
return "Any";
break;
}
}
};

View File

@ -119,6 +119,11 @@ void GameEngine::sUserInput()
std::string name = currentScene()->action(actionCode);
currentScene()->doAction(name, actionType);
}
else if (currentScene()->key_callable != NULL && currentScene()->key_callable != Py_None)
{
PyObject* args = Py_BuildValue("(ss)", ActionCode::key_str(event.key.code).c_str(), actionType.c_str());
PyObject_Call(currentScene()->key_callable, args, NULL);
}
else
{
//std::cout << "[GameEngine] Action not registered for input: " << actionCode << ": " << actionType << std::endl;

View File

@ -5,44 +5,12 @@
#include "UI.h"
#include "Resources.h"
// static class members...?
//std::map<std::string, UIMenu*> McRFPy_API::menus;
//std::map<std::string, Grid*> McRFPy_API::grids;
std::map<std::string, PyObject*> McRFPy_API::callbacks;
//std::list<Animation*> McRFPy_API::animations;
std::vector<sf::SoundBuffer> McRFPy_API::soundbuffers;
sf::Music McRFPy_API::music;
sf::Sound McRFPy_API::sfx;
//std::string McRFPy_API::input_mode;
//int McRFPy_API::turn_number;
//std::string McRFPy_API::active_grid;
//bool McRFPy_API::do_camfollow;
//EntityManager McRFPy_API::entities;
static PyMethodDef mcrfpyMethods[] = {
/*
{"createMenu", McRFPy_API::_createMenu, METH_VARARGS,
"Create a new uimenu (name_str, x, y, w, h)"},
{"listMenus", McRFPy_API::_listMenus, METH_VARARGS,
"return a list of existing menus"},
{"modMenu", McRFPy_API::_modMenu, METH_VARARGS,
"call with a UIMenu object to update all fields"},
{"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"},
*/
{"registerPyAction", McRFPy_API::_registerPyAction, METH_VARARGS,
"Register a callable Python object to correspond to an action string. (actionstr, callable)"},
@ -56,25 +24,13 @@ static PyMethodDef mcrfpyMethods[] = {
{"playSound", McRFPy_API::_playSound, METH_VARARGS, "(int)"},
{"getMusicVolume", McRFPy_API::_getMusicVolume, METH_VARARGS, ""},
{"getSoundVolume", McRFPy_API::_getSoundVolume, METH_VARARGS, ""},
/*
{"unlockPlayerInput", McRFPy_API::_unlockPlayerInput, METH_VARARGS, ""},
{"lockPlayerInput", McRFPy_API::_lockPlayerInput, METH_VARARGS, ""},
{"requestGridTarget", McRFPy_API::_requestGridTarget, METH_VARARGS, ""},
{"activeGrid", McRFPy_API::_activeGrid, METH_VARARGS, ""},
{"setActiveGrid", McRFPy_API::_setActiveGrid, METH_VARARGS, ""},
{"inputMode", McRFPy_API::_inputMode, METH_VARARGS, ""},
{"turnNumber", McRFPy_API::_turnNumber, METH_VARARGS, ""},
{"createEntity", McRFPy_API::_createEntity, METH_VARARGS, ""},
//{"listEntities", McRFPy_API::_listEntities, METH_VARARGS, ""},
{"refreshFov", McRFPy_API::_refreshFov, METH_VARARGS, ""},
{"camFollow", McRFPy_API::_camFollow, METH_VARARGS, ""},
*/
{"sceneUI", McRFPy_API::_sceneUI, METH_VARARGS, "sceneUI(scene) - Returns a list of UI elements"},
{"currentScene", McRFPy_API::_currentScene, METH_VARARGS, "currentScene() - Current scene's name. Returns a string"},
{"setScene", McRFPy_API::_setScene, METH_VARARGS, "setScene(scene) - transition to a different scene"},
{"createScene", McRFPy_API::_createScene, METH_VARARGS, "createScene(scene) - create a new blank scene with given name"},
{"keypressScene", McRFPy_API::_keypressScene, METH_VARARGS, "keypressScene(callable) - assign a callable object to the current scene receive keypress events"},
{NULL, NULL, 0, NULL}
};
@ -481,3 +437,16 @@ PyObject* McRFPy_API::_createScene(PyObject* self, PyObject* args) {
Py_INCREF(Py_None);
return Py_None;
}
PyObject* McRFPy_API::_keypressScene(PyObject* self, PyObject* args) {
PyObject* callable;
if (!PyArg_ParseTuple(args, "O", &callable)) return NULL;
if (game->currentScene()->key_callable != NULL and game->currentScene()->key_callable != Py_None)
{
Py_DECREF(game->currentScene()->key_callable);
}
Py_INCREF(callable);
game->currentScene()->key_callable = callable;
Py_INCREF(Py_None);
return Py_None;
}

View File

@ -73,7 +73,7 @@ public:
static PyObject* _setScene(PyObject*, PyObject*);
static PyObject* _currentScene(PyObject*, PyObject*);
static PyObject* _createScene(PyObject*, PyObject*);
static PyObject* _keypressScene(PyObject*, PyObject*);
// accept keyboard input from scene
static sf::Vector2i cursor_position;
static void player_input(int, int);

View File

@ -39,3 +39,21 @@ bool Scene::unregisterActionInjected(int code, std::string name)
{
return false;
}
void Scene::key_register(PyObject* callable)
{
if (key_callable)
{
// decrement reference before overwriting
Py_DECREF(key_callable);
}
key_callable = callable;
Py_INCREF(key_callable);
}
void Scene::key_unregister()
{
if (key_callable == NULL) return;
Py_DECREF(key_callable);
key_callable = NULL;
}

View File

@ -41,4 +41,7 @@ public:
std::shared_ptr<std::vector<std::shared_ptr<UIDrawable>>> ui_elements;
PyObject* key_callable;
void key_register(PyObject*);
void key_unregister();
};