diff options
| -rw-r--r-- | API.md | 26 | ||||
| -rw-r--r-- | ReiLua_API.lua | 6 | ||||
| -rw-r--r-- | docgen.lua | 5 | ||||
| -rw-r--r-- | examples/events/main.lua | 9 | ||||
| -rw-r--r-- | examples/resources/lib/utillib.lua | 4 | ||||
| -rw-r--r-- | include/lua_core.h | 3 | ||||
| -rw-r--r-- | include/state.h | 1 | ||||
| -rw-r--r-- | src/lua_core.c | 35 |
8 files changed, 85 insertions, 4 deletions
@@ -49,7 +49,7 @@ Some objects allocate memory that needs to be freed when object is no longer nee Arguments are stored in 'RL.arg' array. -## Types +## Structures Raylib structs in Lua @@ -333,6 +333,12 @@ Content of event table received by RL.event. --- +> { type RL.EVENT_JOYSTICK, int jid, int event } + + . + +--- + ## Globals - ConfigFlags > FLAG_VSYNC_HINT = 64 @@ -3391,6 +3397,18 @@ The key was held down until it repeated --- +> GLFW_CONNECTED = 262145 + +Joystick connected + +--- + +> GLFW_DISCONNECTED = 262146 + +Joystick disconnected + +--- + ## Globals - CBuffer > BUFFER_UNSIGNED_CHAR = 0 @@ -3511,6 +3529,12 @@ GLFW event cursor enter/leave --- +> EVENT_JOYSTICK = 11 + +GLFW event joystick + +--- + ## Core - Window-related functions diff --git a/ReiLua_API.lua b/ReiLua_API.lua index 7e93b23..de4bcbb 100644 --- a/ReiLua_API.lua +++ b/ReiLua_API.lua @@ -1161,6 +1161,10 @@ RL.GLFW_RELEASE=0 RL.GLFW_PRESS=1 ---The key was held down until it repeated RL.GLFW_REPEAT=2 +---Joystick connected +RL.GLFW_CONNECTED=262145 +---Joystick disconnected +RL.GLFW_DISCONNECTED=262146 -- Globals - CBuffer @@ -1208,6 +1212,8 @@ RL.EVENT_MOUSE_CURSOR_POS=8 RL.EVENT_MOUSE_SCROLL=9 ---GLFW event cursor enter/leave RL.EVENT_CURSOR_ENTER=10 +---GLFW event joystick +RL.EVENT_JOYSTICK=11 -- Core - Window-related functions ---Check if window has been initialized successfully @@ -132,9 +132,9 @@ apiFile:write( "\nSome objects allocate memory that needs to be freed when objec apiFile:write( "\n## Arguments\n" ) apiFile:write( "\nArguments are stored in 'RL.arg' array.\n" ) --- Types. +-- Structures. -apiFile:write( "\n## Types\n\ +apiFile:write( "\n## Structures\n\ Raylib structs in Lua\n\n---\n" ) apiFile:write( "\n> Vector2 = { 1.0, 1.0 } or { x = 1.0, y = 1.0 }\n\ @@ -251,6 +251,7 @@ apiFile:write( "\n> { type RL.EVENT_MOUSE_BUTTON, int button, int action, int mo apiFile:write( "\n> { type RL.EVENT_MOUSE_CURSOR_POS, number x, number y }\n\n Cursor Position Callback, runs on mouse move.\n\n---\n" ) apiFile:write( "\n> { type RL.EVENT_MOUSE_SCROLL, number xoffset, number yoffset }\n\n Srolling Callback, runs on mouse wheel.\n\n---\n" ) apiFile:write( "\n> { type RL.EVENT_CURSOR_ENTER, int enter }\n\n Cursor Enter Callback, cursor enters client area.\n\n---\n" ) +apiFile:write( "\n> { type RL.EVENT_JOYSTICK, int jid, int event }\n\n .\n\n---\n" ) if separate then apiFile:close() diff --git a/examples/events/main.lua b/examples/events/main.lua index 3a042f1..5643668 100644 --- a/examples/events/main.lua +++ b/examples/events/main.lua @@ -31,6 +31,8 @@ local function getEventType( event ) return "Mouse Scroll" elseif event.type == RL.EVENT_CURSOR_ENTER then return "Cursor Enter" + elseif event.type == RL.EVENT_JOYSTICK then + return "Joystick" end return "Unknown" @@ -90,6 +92,13 @@ function RL.event( event ) elseif event.type == RL.EVENT_CURSOR_ENTER then text = text.."enter: "..event.enter cursorIn = event.enter + elseif event.type == RL.EVENT_JOYSTICK then + text = text.."jid: "..event.jid.." event: "..event.event + if event.event == RL.GLFW_CONNECTED then + text = text.."\nConnected" + elseif event.event == RL.GLFW_DISCONNECTED then + text = text.."\nDisconnected" + end end end diff --git a/examples/resources/lib/utillib.lua b/examples/resources/lib/utillib.lua index 899f899..95184b0 100644 --- a/examples/resources/lib/utillib.lua +++ b/examples/resources/lib/utillib.lua @@ -166,4 +166,8 @@ function utillib.tableMove( t, src, len, dest ) table.move( copy, 1, len, dest, t ) end +function utillib.randomFloat( min, max ) + return min + math.random() * ( max - min ); +end + return utillib diff --git a/include/lua_core.h b/include/lua_core.h index a289cb0..431fc28 100644 --- a/include/lua_core.h +++ b/include/lua_core.h @@ -11,7 +11,8 @@ enum EventType { EVENT_MOUSE_BUTTON, EVENT_MOUSE_CURSOR_POS, EVENT_MOUSE_SCROLL, - EVENT_CURSOR_ENTER + EVENT_CURSOR_ENTER, + EVENT_JOYSTICK }; enum BufferType { diff --git a/include/state.h b/include/state.h index 1172086..a777f40 100644 --- a/include/state.h +++ b/include/state.h @@ -27,6 +27,7 @@ typedef struct { GLFWcursorposfun raylibMouseCursorPosCallback; GLFWscrollfun raylibMouseScrollCallback; GLFWcursorenterfun raylibCursorEnterCallback; + GLFWjoystickfun raylibJoystickCallback; } State; extern State *state; 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" ); |
