Condense event code a bit;

This commit is contained in:
bjorn 2018-07-26 13:40:15 -07:00
parent 693766cf3b
commit 156c0cde5d
8 changed files with 50 additions and 111 deletions

View File

@ -33,39 +33,28 @@ static int nextEvent(lua_State* L) {
return 2; return 2;
case EVENT_FOCUS: case EVENT_FOCUS:
lua_pushboolean(L, event.data.focus.focused);
return 2;
case EVENT_MOUNT: case EVENT_MOUNT:
lua_pushboolean(L, event.data.mount.mounted); lua_pushboolean(L, event.data.boolean.value);
return 2; return 2;
#ifndef EMSCRIPTEN #ifndef EMSCRIPTEN
case EVENT_THREAD_ERROR: case EVENT_THREAD_ERROR:
luax_pushobject(L, event.data.threaderror.thread); luax_pushobject(L, event.data.thread.thread);
lua_pushstring(L, event.data.threaderror.error); lua_pushstring(L, event.data.thread.error);
free((void*) event.data.threaderror.error); free((void*) event.data.thread.error);
return 3; return 3;
#endif #endif
case EVENT_CONTROLLER_ADDED: case EVENT_CONTROLLER_ADDED:
luax_pushobject(L, event.data.controlleradded.controller);
lovrRelease(event.data.controlleradded.controller);
return 2;
case EVENT_CONTROLLER_REMOVED: case EVENT_CONTROLLER_REMOVED:
luax_pushobject(L, event.data.controllerremoved.controller); luax_pushobject(L, event.data.controller.controller);
lovrRelease(event.data.controlleradded.controller); lovrRelease(event.data.controller.controller);
return 2; return 2;
case EVENT_CONTROLLER_PRESSED: case EVENT_CONTROLLER_PRESSED:
luax_pushobject(L, event.data.controllerpressed.controller);
lua_pushstring(L, ControllerButtons[event.data.controllerpressed.button]);
return 3;
case EVENT_CONTROLLER_RELEASED: case EVENT_CONTROLLER_RELEASED:
luax_pushobject(L, event.data.controllerreleased.controller); luax_pushobject(L, event.data.controller.controller);
lua_pushstring(L, ControllerButtons[event.data.controllerpressed.button]); lua_pushstring(L, ControllerButtons[event.data.controller.button]);
return 3; return 3;
default: default:
@ -114,11 +103,8 @@ int l_lovrEventPush(lua_State* L) {
break; break;
case EVENT_FOCUS: case EVENT_FOCUS:
data.focus.focused = lua_toboolean(L, 2);
break;
case EVENT_MOUNT: case EVENT_MOUNT:
data.mount.mounted = lua_toboolean(L, 2); data.boolean.value = lua_toboolean(L, 2);
break; break;
#ifdef EMSCRIPTEN #ifdef EMSCRIPTEN
@ -126,27 +112,20 @@ int l_lovrEventPush(lua_State* L) {
break; break;
#else #else
case EVENT_THREAD_ERROR: case EVENT_THREAD_ERROR:
data.threaderror.thread = luax_checktype(L, 2, Thread); data.thread.thread = luax_checktype(L, 2, Thread);
data.threaderror.error = luaL_checkstring(L, 3); data.thread.error = luaL_checkstring(L, 3);
break; break;
#endif #endif
case EVENT_CONTROLLER_ADDED: case EVENT_CONTROLLER_ADDED:
data.controlleradded.controller = luax_checktype(L, 2, Controller);
break;
case EVENT_CONTROLLER_REMOVED: case EVENT_CONTROLLER_REMOVED:
data.controllerremoved.controller = luax_checktype(L, 2, Controller); data.controller.controller = luax_checktype(L, 2, Controller);
break; break;
case EVENT_CONTROLLER_PRESSED: case EVENT_CONTROLLER_PRESSED:
data.controllerpressed.controller = luax_checktype(L, 2, Controller);
data.controllerpressed.button = luaL_checkoption(L, 3, NULL, ControllerButtons);
break;
case EVENT_CONTROLLER_RELEASED: case EVENT_CONTROLLER_RELEASED:
data.controllerreleased.controller = luax_checktype(L, 2, Controller); data.controller.controller = luax_checktype(L, 2, Controller);
data.controllerreleased.button = luaL_checkoption(L, 3, NULL, ControllerButtons); data.controller.button = luaL_checkoption(L, 3, NULL, ControllerButtons);
break; break;
} }

View File

@ -27,11 +27,10 @@ static int threadRunner(void* data) {
lovrRelease(thread); lovrRelease(thread);
if (thread->error) { if (thread->error) {
Event event; lovrEventPush((Event) {
event.type = EVENT_THREAD_ERROR; .type = EVENT_THREAD_ERROR,
event.data.threaderror.thread = thread; .data.thread = { thread, strdup(thread->error) }
event.data.threaderror.error = strdup(thread->error); });
lovrEventPush(event);
lua_close(L); lua_close(L);
return 1; return 1;
} }

View File

@ -24,49 +24,28 @@ typedef struct {
} QuitEvent; } QuitEvent;
typedef struct { typedef struct {
bool focused; bool value;
} FocusEvent; } BoolEvent;
typedef struct {
bool mounted;
} MountEvent;
#ifndef EMSCRIPTEN #ifndef EMSCRIPTEN
typedef struct { typedef struct {
Thread* thread; Thread* thread;
const char* error; const char* error;
} ThreadErrorEvent; } ThreadEvent;
#endif #endif
typedef struct {
Controller* controller;
} ControllerAddedEvent;
typedef struct {
Controller* controller;
} ControllerRemovedEvent;
typedef struct { typedef struct {
Controller* controller; Controller* controller;
ControllerButton button; ControllerButton button;
} ControllerPressedEvent; } ControllerEvent;
typedef struct {
Controller* controller;
ControllerButton button;
} ControllerReleasedEvent;
typedef union { typedef union {
QuitEvent quit; QuitEvent quit;
FocusEvent focus; BoolEvent boolean;
MountEvent mount;
#ifndef EMSCRIPTEN #ifndef EMSCRIPTEN
ThreadErrorEvent threaderror; ThreadEvent thread;
#endif #endif
ControllerAddedEvent controlleradded; ControllerEvent controller;
ControllerRemovedEvent controllerremoved;
ControllerPressedEvent controllerpressed;
ControllerReleasedEvent controllerreleased;
} EventData; } EventData;
typedef struct { typedef struct {

View File

@ -15,7 +15,7 @@ static GraphicsState state;
static void onCloseWindow(GLFWwindow* window) { static void onCloseWindow(GLFWwindow* window) {
if (window == state.window) { if (window == state.window) {
lovrEventPush((Event) { .type = EVENT_QUIT, .data = { .quit = { false, 0 } } }); lovrEventPush((Event) { .type = EVENT_QUIT, .data.quit = { false, 0 } });
} }
} }

View File

@ -92,21 +92,14 @@ static void onMouseButton(GLFWwindow* window, int button, int action, int mods)
return; return;
} }
Event event;
Controller* controller; Controller* controller;
int i; int i;
vec_foreach(&state.controllers, controller, i) { vec_foreach(&state.controllers, controller, i) {
if (button == GLFW_MOUSE_BUTTON_RIGHT) { if (button == GLFW_MOUSE_BUTTON_RIGHT) {
if (action == GLFW_PRESS) { lovrEventPush((Event) {
event.type = EVENT_CONTROLLER_PRESSED; .type = action == GLFW_PRESS ? EVENT_CONTROLLER_PRESSED : EVENT_CONTROLLER_RELEASED,
event.data.controllerpressed.controller = controller; .data.controller = { controller, CONTROLLER_BUTTON_TRIGGER }
event.data.controllerpressed.button = CONTROLLER_BUTTON_TRIGGER; });
} else {
event.type = EVENT_CONTROLLER_RELEASED;
event.data.controllerreleased.controller = controller;
event.data.controllerreleased.button = CONTROLLER_BUTTON_TRIGGER;
}
lovrEventPush(event);
} }
} }
} }

View File

@ -86,11 +86,11 @@ static void openvrRefreshControllers() {
Controller* controller; int i; Controller* controller; int i;
vec_foreach_rev(&state.controllers, controller, i) { vec_foreach_rev(&state.controllers, controller, i) {
if (controller->id != controllerIds[0] && controller->id != controllerIds[1]) { if (controller->id != controllerIds[0] && controller->id != controllerIds[1]) {
EventType type = EVENT_CONTROLLER_REMOVED;
EventData data = { .controllerremoved = { controller } };
Event event = { .type = type, .data = data };
lovrRetain(controller); lovrRetain(controller);
lovrEventPush(event); lovrEventPush((Event) {
.type = EVENT_CONTROLLER_REMOVED,
.data.controller = { controller, 0 }
});
vec_splice(&state.controllers, i, 1); vec_splice(&state.controllers, i, 1);
lovrRelease(controller); lovrRelease(controller);
} }
@ -101,11 +101,11 @@ static void openvrRefreshControllers() {
if ((int) controllerIds[i] != -1) { if ((int) controllerIds[i] != -1) {
controller = openvrAddController(controllerIds[i]); controller = openvrAddController(controllerIds[i]);
if (!controller) continue; if (!controller) continue;
EventType type = EVENT_CONTROLLER_ADDED;
EventData data = { .controlleradded = { controller } };
Event event = { .type = type, .data = data };
lovrRetain(controller); lovrRetain(controller);
lovrEventPush(event); lovrEventPush((Event) {
.type = EVENT_CONTROLLER_ADDED,
.data.controller = { controller, 0 }
});
} }
} }
} }
@ -188,8 +188,7 @@ static void openvrPoll() {
bool isPress = vrEvent.eventType == EVREventType_VREvent_ButtonPress; bool isPress = vrEvent.eventType == EVREventType_VREvent_ButtonPress;
if (vrEvent.trackedDeviceIndex == state.headsetIndex && vrEvent.data.controller.button == EVRButtonId_k_EButton_ProximitySensor) { if (vrEvent.trackedDeviceIndex == state.headsetIndex && vrEvent.data.controller.button == EVRButtonId_k_EButton_ProximitySensor) {
Event event = { .type = EVENT_MOUNT, .data = { .mount = { isPress } } }; lovrEventPush((Event) { .type = EVENT_MOUNT, .data.boolean = { isPress } });
lovrEventPush(event);
break; break;
} }
@ -197,15 +196,10 @@ static void openvrPoll() {
Controller* controller = state.controllers.data[i]; Controller* controller = state.controllers.data[i];
if (controller->id == vrEvent.trackedDeviceIndex) { if (controller->id == vrEvent.trackedDeviceIndex) {
ControllerButton button = getButton(vrEvent.data.controller.button, openvrControllerGetHand(controller)); ControllerButton button = getButton(vrEvent.data.controller.button, openvrControllerGetHand(controller));
if (isPress) { lovrEventPush((Event) {
EventData data = { .controllerpressed = { controller, button } }; .type = isPress ? EVENT_CONTROLLER_PRESSED : EVENT_CONTROLLER_RELEASED,
Event event = { .type = EVENT_CONTROLLER_PRESSED, .data = data }; .data.controller = { controller, button }
lovrEventPush(event); });
} else {
EventData data = { .controllerreleased = { controller, button } };
Event event = { .type = EVENT_CONTROLLER_RELEASED, .data = data };
lovrEventPush(event);
}
break; break;
} }
} }
@ -215,9 +209,7 @@ static void openvrPoll() {
case EVREventType_VREvent_InputFocusCaptured: case EVREventType_VREvent_InputFocusCaptured:
case EVREventType_VREvent_InputFocusReleased: { case EVREventType_VREvent_InputFocusReleased: {
bool isFocused = vrEvent.eventType == EVREventType_VREvent_InputFocusReleased; bool isFocused = vrEvent.eventType == EVREventType_VREvent_InputFocusReleased;
EventData data = { .focus = { isFocused } }; lovrEventPush((Event) { .type = EVENT_FOCUS, .data.boolean = { isFocused } });
Event event = { .type = EVENT_FOCUS, .data = data };
lovrEventPush(event);
break; break;
} }

View File

@ -45,7 +45,7 @@ static void onControllerAdded(uint32_t id) {
lovrRetain(controller); lovrRetain(controller);
lovrEventPush((Event) { lovrEventPush((Event) {
.type = EVENT_CONTROLLER_ADDED, .type = EVENT_CONTROLLER_ADDED,
.data = { .controlleradded = { controller } } .data.controller = { controller, 0 }
}); });
} }
@ -56,7 +56,7 @@ static void onControllerRemoved(uint32_t id) {
lovrRetain(controller); lovrRetain(controller);
lovrEventPush((Event) { lovrEventPush((Event) {
.type = EVENT_CONTROLLER_REMOVED, .type = EVENT_CONTROLLER_REMOVED,
.data = { .controllerremoved = { controller } } .data.controller = { controller, 0 }
}); });
vec_splice(&state.controllers, i, 1); vec_splice(&state.controllers, i, 1);
lovrRelease(controller); lovrRelease(controller);
@ -68,21 +68,21 @@ static void onControllerRemoved(uint32_t id) {
static void onControllerPressed(uint32_t id, ControllerButton button) { static void onControllerPressed(uint32_t id, ControllerButton button) {
lovrEventPush((Event) { lovrEventPush((Event) {
.type = EVENT_CONTROLLER_PRESSED, .type = EVENT_CONTROLLER_PRESSED,
.data = { .controllerpressed = { state.controllers.data[id], button } } .data.controller = { state.controllers.data[id], button }
}); });
} }
static void onControllerReleased(uint32_t id, ControllerButton button) { static void onControllerReleased(uint32_t id, ControllerButton button) {
lovrEventPush((Event) { lovrEventPush((Event) {
.type = EVENT_CONTROLLER_RELEASED, .type = EVENT_CONTROLLER_RELEASED,
.data = { .controllerreleased = { state.controllers.data[id], button } } .data.controller = { state.controllers.data[id], button }
}); });
} }
static void onMountChanged(bool mounted) { static void onMountChanged(bool mounted) {
lovrEventPush((Event) { lovrEventPush((Event) {
.type = EVENT_MOUNT, .type = EVENT_MOUNT,
.data = { .mount = { mounted } } .data.boolean = { mounted }
}); });
} }

View File

@ -131,10 +131,7 @@ bool lovrRun(int argc, char** argv, int* status) {
} }
void lovrQuit(int status) { void lovrQuit(int status) {
EventType type = EVENT_QUIT; lovrEventPush((Event) { .type = EVENT_QUIT, .data.quit = { false, status } });
EventData data = { .quit = { false, status } };
Event event = { .type = type, .data = data };
lovrEventPush(event);
} }
const char* lovrGetOS() { const char* lovrGetOS() {