From 839f73321cad9fc386e77885571cfb4a4afb50a8 Mon Sep 17 00:00:00 2001 From: NPO <77460082+NPO-197@users.noreply.github.com> Date: Tue, 5 Nov 2024 13:50:57 -0500 Subject: [PATCH] Revised Keyboard input, currentDir Changed KeyboardMask to HeldKeys function. Changed filesystem::current_path to QDir::setCurrent to avoid use of 'c_str'... --- src/frontend/qt_sdl/EmuInstance.h | 3 +-- src/frontend/qt_sdl/EmuInstanceInput.cpp | 15 ++------------- src/frontend/qt_sdl/LuaMain.cpp | 22 +++++++++++----------- src/frontend/qt_sdl/Screen.cpp | 14 ++++++-------- tools/LuaScripts/LuaScriptTest.lua | 2 +- tools/LuaScripts/Lua_Docs.md | 22 ++++++++++------------ 6 files changed, 31 insertions(+), 47 deletions(-) diff --git a/src/frontend/qt_sdl/EmuInstance.h b/src/frontend/qt_sdl/EmuInstance.h index 748af451..60264cb4 100644 --- a/src/frontend/qt_sdl/EmuInstance.h +++ b/src/frontend/qt_sdl/EmuInstance.h @@ -146,7 +146,7 @@ public: void setJoystick(int id); int getJoystickID() { return joystickID; } SDL_Joystick* getJoystick() { return joystick; } - + std::vector heldKeys; std::vector keyStrokes; private: static int lastSep(const std::string& path); @@ -277,7 +277,6 @@ public: bool doAudioSync; melonDS::u32 getInputMask(){return inputMask;} - std::map KeyboardMask; //For Lua Scripts private: std::unique_ptr backupState; diff --git a/src/frontend/qt_sdl/EmuInstanceInput.cpp b/src/frontend/qt_sdl/EmuInstanceInput.cpp index 9f48ea7f..fdc47063 100644 --- a/src/frontend/qt_sdl/EmuInstanceInput.cpp +++ b/src/frontend/qt_sdl/EmuInstanceInput.cpp @@ -74,9 +74,6 @@ void EmuInstance::inputInit() hotkeyMask = 0; lastHotkeyMask = 0; - for (int i=0;i<256;i++) - KeyboardMask[i]=false; - joystick = nullptr; controller = nullptr; hasRumble = false; @@ -224,11 +221,7 @@ int getEventKeyVal(QKeyEvent* event) void EmuInstance::onKeyPress(QKeyEvent* event) { - if (event->key()<256) - KeyboardMask[event->key()]=true; - if ((event->key()&(1<<24))!=0 && (event->key()&0xff)<'A') //Special Keys - KeyboardMask[0xff - (event->key()&0xff)]=true; - + heldKeys.push_back(event->key()); int keyHK = getEventKeyVal(event); int keyKP = keyHK; keyStrokes.push_back(keyHK); @@ -246,11 +239,7 @@ void EmuInstance::onKeyPress(QKeyEvent* event) void EmuInstance::onKeyRelease(QKeyEvent* event) { - if (event->key()<256) - KeyboardMask[event->key()]=false; - if ((event->key()&(1<<24))!=0 && (event->key()&0xff)<'A')//Special keys - KeyboardMask[0xff - (event->key()&0xff)]=false; - + heldKeys.erase(std::find(heldKeys.begin(),heldKeys.end(),event->key())); int keyHK = getEventKeyVal(event); int keyKP = keyHK; if (event->modifiers() != Qt::KeypadModifier) diff --git a/src/frontend/qt_sdl/LuaMain.cpp b/src/frontend/qt_sdl/LuaMain.cpp index f64c4999..b5ec7cbb 100644 --- a/src/frontend/qt_sdl/LuaMain.cpp +++ b/src/frontend/qt_sdl/LuaMain.cpp @@ -128,17 +128,17 @@ void LuaBundle::createLuaState() overlays->clear(); flagNewLua = false; luaState = nullptr; - std::string fileName = luaDialog->currentScript.fileName().toStdString(); - std::string filedir = luaDialog->currentScript.dir().path().toStdString(); + QByteArray fileName = luaDialog->currentScript.fileName().toLocal8Bit(); + QString filedir = luaDialog->currentScript.dir().path(); lua_State* L = luaL_newstate(); LuaBundle* pBundle = this; std::memcpy(lua_getextraspace(L), &pBundle, sizeof(LuaBundle*)); //Write a pointer to this LuaBundle into the extra space of the new lua_State luaL_openlibs(L); for (LuaFunction* function : definedLuaFunctions) lua_register(L,function->name,function->cfunction); - std::filesystem::current_path(filedir.c_str()); + QDir::setCurrent(filedir); lua_sethook(L,&luaHookFunction,LUA_MASKCOUNT,MELON_LUA_HOOK_INSTRUCTION_COUNT); - if (luaL_dofile(L,&fileName[0])==LUA_OK) + if (luaL_dofile(L,fileName.data())==LUA_OK) { luaState = L; } @@ -412,18 +412,18 @@ int Lua_getMouse(lua_State* L) } AddLuaFunction(Lua_getMouse,GetMouse); -int Lua_KeyboardMask(lua_State* L) +int Lua_HeldKeys(lua_State* L) { LuaBundle* bundle = get_bundle(L); - lua_createtable(L,0,256); - for (int i=0;i<256;i++) + lua_createtable(L,0,bundle->getEmuInstance()->heldKeys.size()); + for (int key : bundle->getEmuInstance()->heldKeys) { - lua_pushboolean(L,bundle->getEmuInstance()->KeyboardMask[i]); - lua_seti(L,-2,i); + lua_pushboolean(L,true); + lua_seti(L,-2,key); } - return 1;//returns table of 256 booleans describing the current state of the keyboard. + return 1;//returns table of currently held keys. } -AddLuaFunction(Lua_KeyboardMask,KeyboardMask); +AddLuaFunction(Lua_HeldKeys,HeldKeys); /*-------------------------------------------------------------------------------------------------- Front-end lua function definitions diff --git a/src/frontend/qt_sdl/Screen.cpp b/src/frontend/qt_sdl/Screen.cpp index 077774d7..53f08864 100644 --- a/src/frontend/qt_sdl/Screen.cpp +++ b/src/frontend/qt_sdl/Screen.cpp @@ -1025,19 +1025,17 @@ void ScreenPanelGL::drawOverlays(int screenType,int screen) glTexSubImage2D(GL_TEXTURE_2D,0,0,0,overlay.rectangle.width(),overlay.rectangle.height(),GL_RGBA,GL_UNSIGNED_BYTE,overlay.displayBuffer->bits()); overlay.flipped = false; } + + glBindTexture(GL_TEXTURE_2D, overlay.GLTexture); + glUniform2f(overlayPosULoc,overlay.rectangle.left(),overlay.rectangle.top()); + glUniform2f(overlaySizeULoc,overlay.rectangle.width(),overlay.rectangle.height()); + if(screenType == canvasTarget_OSD) // OSD gets drawn differently then top or bottom screen target { - glBindTexture(GL_TEXTURE_2D, overlay.GLTexture); - glUniform2i(osdPosULoc,overlay.rectangle.left(),overlay.rectangle.top()); - glUniform2i(osdSizeULoc,overlay.rectangle.width(),overlay.rectangle.height()); glDrawArrays(GL_TRIANGLES, 0, 2*3); continue; } - glBindTexture(GL_TEXTURE_2D, overlay.GLTexture); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); - glUniform2f(overlayPosULoc,overlay.rectangle.left(),overlay.rectangle.top()); - glUniform2f(overlaySizeULoc,overlay.rectangle.width(),overlay.rectangle.height()); + glUniform1i(overlayScreenTypeULoc, screenType); glUniformMatrix2x3fv(overlayTransformULoc, 1, GL_TRUE,screenMatrix[screen]); glDrawArrays(GL_TRIANGLES,screenType == 0 ? 0 : 2*3, 2*3); diff --git a/tools/LuaScripts/LuaScriptTest.lua b/tools/LuaScripts/LuaScriptTest.lua index 4a0b9ff9..93c52211 100644 --- a/tools/LuaScripts/LuaScriptTest.lua +++ b/tools/LuaScripts/LuaScriptTest.lua @@ -140,7 +140,7 @@ end function KeyHeld(keyStr) -- Only need to update mask once per frame if updateFlags.KeyboardCheck == nil then - mask = KeyboardMask() + mask = HeldKeys() updateFlags.KeyboardCheck = true end return mask[string.byte(keyStr:sub(1,1))] diff --git a/tools/LuaScripts/Lua_Docs.md b/tools/LuaScripts/Lua_Docs.md index ba61552c..6fef3045 100644 --- a/tools/LuaScripts/Lua_Docs.md +++ b/tools/LuaScripts/Lua_Docs.md @@ -86,28 +86,26 @@ end end ``` -`tKeyMask KeyboardMask()` -- Returns a lua table of 256 bool values representing the current state of the keyboard. -- Most Keys are ascii -- TODO: Improve support for non-US Keyboards +`tKeyMask HeldKeys()` +- Returns a lua table containing the value "true" for each held key and "nil" for any key not currently held. +- check the Qt docs for a list of key codes: https://doc.qt.io/qt-6/qt.html#Key-enum - Example ```Lua - --Print id of key being pressed - for k,v in pairs(KeyboardMask()) do - if v then - print("KeyPressed:"..k) - end + --Loop over all currently held keys + for k,_ in pairs(HeldKeys()) do + print("KeyPressed:"..k) end --Check if the "Q" key is currently pressed - if KeyboardMask()[string.byte("Q")] then + if HeldKeys()[string.byte("Q")] then print("\"Q\" Pressed!") end ``` `tKeyStrokes Keys()` - Returns a lua table of all keys pressed since the last time `Keys()` was called... -- DIFFERENT from `KeyboardMask()` -- Mostly used for detecting special non-US Keyboard keys, or for faster typing/higher poll rate +- DIFFERENT from `HeldKeys()` +- This function keeps proper track of the order of keys typed, and if the same key was pressed multiple times since the last check. +- Mostly used for cases that need faster typing/higher poll rate... otherwise use HeldKeys() for most use cases. - Example ```Lua typed = ""