diff options
| author | jussi | 2023-11-18 17:11:36 +0200 |
|---|---|---|
| committer | jussi | 2023-11-18 17:11:36 +0200 |
| commit | d19864947b6e8ea1049a56ca3d965691c51b4108 (patch) | |
| tree | 5bd46e570b673ece6ef697848516d5d2401f093a /src/lua_core.c | |
| parent | 5c8324d25f5f3500d5258f599afc710352611513 (diff) | |
| download | reilua-enhanced-d19864947b6e8ea1049a56ca3d965691c51b4108.tar.gz reilua-enhanced-d19864947b6e8ea1049a56ca3d965691c51b4108.tar.bz2 reilua-enhanced-d19864947b6e8ea1049a56ca3d965691c51b4108.zip | |
Joystick event.
Diffstat (limited to 'src/lua_core.c')
| -rw-r--r-- | src/lua_core.c | 35 |
1 files changed, 35 insertions, 0 deletions
diff --git a/src/lua_core.c b/src/lua_core.c index ebe0a42..2825b16 100644 --- a/src/lua_core.c +++ b/src/lua_core.c @@ -885,6 +885,8 @@ static void defineGlobals() { assignGlobalInt( GLFW_RELEASE, "GLFW_RELEASE" ); // The key or mouse button was released assignGlobalInt( GLFW_PRESS, "GLFW_PRESS" ); // The key or mouse button was pressed assignGlobalInt( GLFW_REPEAT, "GLFW_REPEAT" ); // The key was held down until it repeated + assignGlobalInt( GLFW_CONNECTED, "GLFW_CONNECTED" ); // Joystick connected + assignGlobalInt( GLFW_DISCONNECTED, "GLFW_DISCONNECTED" ); // Joystick disconnected /* CBuffer Data Types */ assignGlobalInt( BUFFER_UNSIGNED_CHAR, "BUFFER_UNSIGNED_CHAR" ); // C type unsigned char assignGlobalInt( BUFFER_UNSIGNED_SHORT, "BUFFER_UNSIGNED_SHORT" ); // C type unsigned short @@ -907,6 +909,7 @@ static void defineGlobals() { assignGlobalInt( EVENT_MOUSE_CURSOR_POS, "EVENT_MOUSE_CURSOR_POS" ); // GLFW event cursor position assignGlobalInt( EVENT_MOUSE_SCROLL, "EVENT_MOUSE_SCROLL" ); // GLFW event mouse scroll assignGlobalInt( EVENT_CURSOR_ENTER, "EVENT_CURSOR_ENTER" ); // GLFW event cursor enter/leave + assignGlobalInt( EVENT_JOYSTICK, "EVENT_JOYSTICK" ); // GLFW event joystick /*DOC_END*/ lua_pop( L, -1 ); @@ -1286,6 +1289,37 @@ static void cursorEnterInputEvent( GLFWwindow* window, int enter ) { lua_pop( L, -1 ); } +static void joystickInputEvent( int jid, int event ) { + /* Pass through to raylib callback. */ + if ( state->raylibJoystickCallback != NULL ) { + state->raylibJoystickCallback( jid, event ); + } + + lua_State *L = state->luaState; + + lua_pushcfunction( L, luaTraceback ); + int tracebackidx = lua_gettop( L ); + + lua_getglobal( L, "RL" ); + lua_getfield( L, -1, "event" ); + + if ( lua_isfunction( L, -1 ) ) { + lua_createtable( L, 3, 0 ); + lua_pushinteger( L, EVENT_JOYSTICK ); + lua_setfield( L, -2, "type" ); + lua_pushinteger( L, jid ); + lua_setfield( L, -2, "jid" ); + lua_pushinteger( L, event ); + lua_setfield( L, -2, "event" ); + + if ( lua_pcall( L, 1, 0, tracebackidx ) != 0 ) { + TraceLog( LOG_ERROR, "Lua error: %s", lua_tostring( L, -1 ) ); + state->run = false; + } + } + lua_pop( L, -1 ); +} + bool luaInit( int argn, const char **argc ) { state->luaState = luaL_newstate(); lua_State *L = state->luaState; @@ -1399,6 +1433,7 @@ bool luaCallMain() { state->raylibMouseCursorPosCallback = glfwSetCursorPosCallback( GetWindowHandle(), mouseCursorPosInputEvent ); state->raylibMouseScrollCallback = glfwSetScrollCallback( GetWindowHandle(), mouseScrollInputEvent ); state->raylibCursorEnterCallback = glfwSetCursorEnterCallback( GetWindowHandle(), cursorEnterInputEvent ); + state->raylibJoystickCallback = glfwSetJoystickCallback( joystickInputEvent ); lua_getglobal( L, "RL" ); lua_getfield( L, -1, "init" ); |
