Revised Keyboard input, currentDir

Changed KeyboardMask to HeldKeys function.
Changed filesystem::current_path to QDir::setCurrent to avoid use of 'c_str'...
This commit is contained in:
NPO 2024-11-05 13:50:57 -05:00
parent e3e31b8e60
commit 839f73321c
6 changed files with 31 additions and 47 deletions

View File

@ -146,7 +146,7 @@ public:
void setJoystick(int id);
int getJoystickID() { return joystickID; }
SDL_Joystick* getJoystick() { return joystick; }
std::vector<int> heldKeys;
std::vector<int> keyStrokes;
private:
static int lastSep(const std::string& path);
@ -277,7 +277,6 @@ public:
bool doAudioSync;
melonDS::u32 getInputMask(){return inputMask;}
std::map<melonDS::u8,bool> KeyboardMask; //For Lua Scripts
private:
std::unique_ptr<melonDS::Savestate> backupState;

View File

@ -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)

View File

@ -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

View File

@ -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);

View File

@ -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))]

View File

@ -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 = ""