mirror of https://github.com/bjornbytes/lovr.git
Add keypressed and keyreleased events; rm t.hotkeys;
This commit is contained in:
parent
c20c5508b4
commit
cb4281b889
|
@ -12,12 +12,29 @@ StringEntry EventTypes[] = {
|
|||
[EVENT_RESTART] = ENTRY("restart"),
|
||||
[EVENT_FOCUS] = ENTRY("focus"),
|
||||
[EVENT_RESIZE] = ENTRY("resize"),
|
||||
[EVENT_KEYPRESSED] = ENTRY("keypressed"),
|
||||
[EVENT_KEYRELEASED] = ENTRY("keyreleased"),
|
||||
#ifdef LOVR_ENABLE_THREAD
|
||||
[EVENT_THREAD_ERROR] = ENTRY("threaderror"),
|
||||
#endif
|
||||
{ 0 }
|
||||
};
|
||||
|
||||
StringEntry KeyCodes[] = {
|
||||
[KEY_W] = ENTRY("w"),
|
||||
[KEY_A] = ENTRY("a"),
|
||||
[KEY_S] = ENTRY("s"),
|
||||
[KEY_D] = ENTRY("d"),
|
||||
[KEY_Q] = ENTRY("q"),
|
||||
[KEY_E] = ENTRY("e"),
|
||||
[KEY_UP] = ENTRY("up"),
|
||||
[KEY_DOWN] = ENTRY("down"),
|
||||
[KEY_LEFT] = ENTRY("left"),
|
||||
[KEY_RIGHT] = ENTRY("right"),
|
||||
[KEY_ESCAPE] = ENTRY("escape"),
|
||||
[KEY_F5] = ENTRY("f5")
|
||||
};
|
||||
|
||||
static LOVR_THREAD_LOCAL int pollRef;
|
||||
|
||||
void luax_checkvariant(lua_State* L, int index, Variant* variant) {
|
||||
|
@ -112,6 +129,11 @@ static int nextEvent(lua_State* L) {
|
|||
lua_pushinteger(L, event.data.resize.height);
|
||||
return 3;
|
||||
|
||||
case EVENT_KEYPRESSED:
|
||||
case EVENT_KEYRELEASED:
|
||||
luax_pushenum(L, KeyCodes, event.data.key.code);
|
||||
return 2;
|
||||
|
||||
#ifdef LOVR_ENABLE_THREAD
|
||||
case EVENT_THREAD_ERROR:
|
||||
luax_pushtype(L, Thread, event.data.thread.thread);
|
||||
|
@ -133,18 +155,6 @@ static int nextEvent(lua_State* L) {
|
|||
}
|
||||
}
|
||||
|
||||
static void hotkeyHandler(KeyCode key, ButtonAction action) {
|
||||
if (action != BUTTON_PRESSED) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (key == KEY_ESCAPE) {
|
||||
lovrEventPush((Event) { .type = EVENT_QUIT, .data.quit.exitCode = 0 });
|
||||
} else if (key == KEY_F5) {
|
||||
lovrEventPush((Event) { .type = EVENT_RESTART });
|
||||
}
|
||||
}
|
||||
|
||||
static int l_lovrEventClear(lua_State* L) {
|
||||
lovrEventClear();
|
||||
return 0;
|
||||
|
@ -213,11 +223,5 @@ int luaopen_lovr_event(lua_State* L) {
|
|||
luax_atexit(L, lovrEventDestroy);
|
||||
}
|
||||
|
||||
luax_pushconf(L);
|
||||
lua_getfield(L, -1, "hotkeys");
|
||||
if (lua_toboolean(L, -1)) {
|
||||
lovrPlatformOnKeyboardEvent(hotkeyHandler);
|
||||
}
|
||||
lua_pop(L, 2);
|
||||
return 1;
|
||||
}
|
||||
|
|
|
@ -12,6 +12,13 @@ static struct {
|
|||
size_t head;
|
||||
} state;
|
||||
|
||||
static void onKeyboardEvent(KeyCode key, ButtonAction action) {
|
||||
lovrEventPush((Event) {
|
||||
.type = action == BUTTON_PRESSED ? EVENT_KEYPRESSED : EVENT_KEYRELEASED,
|
||||
.data.key.code = key
|
||||
});
|
||||
}
|
||||
|
||||
void lovrVariantDestroy(Variant* variant) {
|
||||
switch (variant->type) {
|
||||
case TYPE_STRING: free(variant->value.string); return;
|
||||
|
@ -23,12 +30,14 @@ void lovrVariantDestroy(Variant* variant) {
|
|||
bool lovrEventInit() {
|
||||
if (state.initialized) return false;
|
||||
arr_init(&state.events);
|
||||
lovrPlatformOnKeyboardEvent(onKeyboardEvent);
|
||||
return state.initialized = true;
|
||||
}
|
||||
|
||||
void lovrEventDestroy() {
|
||||
if (!state.initialized) return;
|
||||
arr_free(&state.events);
|
||||
lovrPlatformOnKeyboardEvent(NULL);
|
||||
memset(&state, 0, sizeof(state));
|
||||
}
|
||||
|
||||
|
|
|
@ -12,6 +12,8 @@ typedef enum {
|
|||
EVENT_RESTART,
|
||||
EVENT_FOCUS,
|
||||
EVENT_RESIZE,
|
||||
EVENT_KEYPRESSED,
|
||||
EVENT_KEYRELEASED,
|
||||
EVENT_CUSTOM,
|
||||
#ifdef LOVR_ENABLE_THREAD
|
||||
EVENT_THREAD_ERROR,
|
||||
|
@ -55,6 +57,10 @@ typedef struct {
|
|||
uint32_t height;
|
||||
} ResizeEvent;
|
||||
|
||||
typedef struct {
|
||||
int code;
|
||||
} KeyEvent;
|
||||
|
||||
typedef struct {
|
||||
struct Thread* thread;
|
||||
char* error;
|
||||
|
@ -70,6 +76,7 @@ typedef union {
|
|||
QuitEvent quit;
|
||||
BoolEvent boolean;
|
||||
ResizeEvent resize;
|
||||
KeyEvent key;
|
||||
ThreadEvent thread;
|
||||
CustomEvent custom;
|
||||
} EventData;
|
||||
|
|
|
@ -84,7 +84,6 @@ function lovr.boot()
|
|||
version = '0.13.0',
|
||||
identity = 'default',
|
||||
saveprecedence = true,
|
||||
hotkeys = true,
|
||||
modules = {
|
||||
audio = true,
|
||||
data = true,
|
||||
|
|
Loading…
Reference in New Issue