Add isKeyDown

This commit is contained in:
Josip Miskovic 2022-08-01 21:51:56 +02:00 committed by Bjorn
parent 50b321fb64
commit 31ca502034
3 changed files with 19 additions and 4 deletions

View File

@ -114,10 +114,17 @@ static int l_lovrSystemRequestPermission(lua_State* L) {
return 0;
}
static int l_lovrSystemIsKeyDown(lua_State* L) {
os_key key = luax_checkenum(L, 1, KeyboardKey, NULL);
lua_pushboolean(L, lovrSystemIsKeyDown(key));
return 1;
}
static const luaL_Reg lovrSystem[] = {
{ "getOS", l_lovrSystemGetOS },
{ "getCoreCount", l_lovrSystemGetCoreCount },
{ "requestPermission", l_lovrSystemRequestPermission },
{ "isKeyDown", l_lovrSystemIsKeyDown },
{ NULL, NULL }
};

View File

@ -4,7 +4,13 @@
#include "util.h"
#include <string.h>
static struct {
bool initialized;
bool pressedKeys[KEY_COUNT];
} state;
static void onKeyboardEvent(os_button_action action, os_key key, uint32_t scancode, bool repeat) {
state.pressedKeys[key] = (action == BUTTON_PRESSED);
lovrEventPush((Event) {
.type = action == BUTTON_PRESSED ? EVENT_KEYPRESSED : EVENT_KEYRELEASED,
.data.key.code = key,
@ -30,10 +36,6 @@ static void onPermissionEvent(os_permission permission, bool granted) {
lovrEventPush(event);
}
static struct {
bool initialized;
} state;
bool lovrSystemInit() {
if (state.initialized) return false;
os_on_key(onKeyboardEvent);
@ -59,6 +61,10 @@ uint32_t lovrSystemGetCoreCount() {
return os_get_core_count();
}
bool lovrSystemIsKeyDown(os_key key) {
return state.pressedKeys[key];
}
void lovrSystemRequestPermission(Permission permission) {
os_request_permission((os_permission) permission);
}

View File

@ -1,5 +1,6 @@
#include <stdbool.h>
#include <stdint.h>
#include "core/os.h"
#pragma once
@ -12,3 +13,4 @@ void lovrSystemDestroy(void);
const char* lovrSystemGetOS(void);
uint32_t lovrSystemGetCoreCount(void);
void lovrSystemRequestPermission(Permission permission);
bool lovrSystemIsKeyDown(os_key key);