Joystick event.

This commit is contained in:
jussi
2023-11-18 17:11:36 +02:00
parent 5c8324d25f
commit d19864947b
8 changed files with 85 additions and 4 deletions

26
API.md
View File

@@ -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. Arguments are stored in 'RL.arg' array.
## Types ## Structures
Raylib structs in Lua 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 ## Globals - ConfigFlags
> FLAG_VSYNC_HINT = 64 > 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 ## Globals - CBuffer
> BUFFER_UNSIGNED_CHAR = 0 > BUFFER_UNSIGNED_CHAR = 0
@@ -3511,6 +3529,12 @@ GLFW event cursor enter/leave
--- ---
> EVENT_JOYSTICK = 11
GLFW event joystick
---
## Core - Window-related functions ## Core - Window-related functions

View File

@@ -1161,6 +1161,10 @@ RL.GLFW_RELEASE=0
RL.GLFW_PRESS=1 RL.GLFW_PRESS=1
---The key was held down until it repeated ---The key was held down until it repeated
RL.GLFW_REPEAT=2 RL.GLFW_REPEAT=2
---Joystick connected
RL.GLFW_CONNECTED=262145
---Joystick disconnected
RL.GLFW_DISCONNECTED=262146
-- Globals - CBuffer -- Globals - CBuffer
@@ -1208,6 +1212,8 @@ RL.EVENT_MOUSE_CURSOR_POS=8
RL.EVENT_MOUSE_SCROLL=9 RL.EVENT_MOUSE_SCROLL=9
---GLFW event cursor enter/leave ---GLFW event cursor enter/leave
RL.EVENT_CURSOR_ENTER=10 RL.EVENT_CURSOR_ENTER=10
---GLFW event joystick
RL.EVENT_JOYSTICK=11
-- Core - Window-related functions -- Core - Window-related functions
---Check if window has been initialized successfully ---Check if window has been initialized successfully

View File

@@ -132,9 +132,9 @@ apiFile:write( "\nSome objects allocate memory that needs to be freed when objec
apiFile:write( "\n## Arguments\n" ) apiFile:write( "\n## Arguments\n" )
apiFile:write( "\nArguments are stored in 'RL.arg' array.\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" ) Raylib structs in Lua\n\n---\n" )
apiFile:write( "\n> Vector2 = { 1.0, 1.0 } or { x = 1.0, y = 1.0 }\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_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_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_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 if separate then
apiFile:close() apiFile:close()

View File

@@ -31,6 +31,8 @@ local function getEventType( event )
return "Mouse Scroll" return "Mouse Scroll"
elseif event.type == RL.EVENT_CURSOR_ENTER then elseif event.type == RL.EVENT_CURSOR_ENTER then
return "Cursor Enter" return "Cursor Enter"
elseif event.type == RL.EVENT_JOYSTICK then
return "Joystick"
end end
return "Unknown" return "Unknown"
@@ -90,6 +92,13 @@ function RL.event( event )
elseif event.type == RL.EVENT_CURSOR_ENTER then elseif event.type == RL.EVENT_CURSOR_ENTER then
text = text.."enter: "..event.enter text = text.."enter: "..event.enter
cursorIn = 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
end end

View File

@@ -166,4 +166,8 @@ function utillib.tableMove( t, src, len, dest )
table.move( copy, 1, len, dest, t ) table.move( copy, 1, len, dest, t )
end end
function utillib.randomFloat( min, max )
return min + math.random() * ( max - min );
end
return utillib return utillib

View File

@@ -11,7 +11,8 @@ enum EventType {
EVENT_MOUSE_BUTTON, EVENT_MOUSE_BUTTON,
EVENT_MOUSE_CURSOR_POS, EVENT_MOUSE_CURSOR_POS,
EVENT_MOUSE_SCROLL, EVENT_MOUSE_SCROLL,
EVENT_CURSOR_ENTER EVENT_CURSOR_ENTER,
EVENT_JOYSTICK
}; };
enum BufferType { enum BufferType {

View File

@@ -27,6 +27,7 @@ typedef struct {
GLFWcursorposfun raylibMouseCursorPosCallback; GLFWcursorposfun raylibMouseCursorPosCallback;
GLFWscrollfun raylibMouseScrollCallback; GLFWscrollfun raylibMouseScrollCallback;
GLFWcursorenterfun raylibCursorEnterCallback; GLFWcursorenterfun raylibCursorEnterCallback;
GLFWjoystickfun raylibJoystickCallback;
} State; } State;
extern State *state; extern State *state;

View File

@@ -885,6 +885,8 @@ static void defineGlobals() {
assignGlobalInt( GLFW_RELEASE, "GLFW_RELEASE" ); // The key or mouse button was released 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_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_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 */ /* CBuffer Data Types */
assignGlobalInt( BUFFER_UNSIGNED_CHAR, "BUFFER_UNSIGNED_CHAR" ); // C type unsigned char assignGlobalInt( BUFFER_UNSIGNED_CHAR, "BUFFER_UNSIGNED_CHAR" ); // C type unsigned char
assignGlobalInt( BUFFER_UNSIGNED_SHORT, "BUFFER_UNSIGNED_SHORT" ); // C type unsigned short 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_CURSOR_POS, "EVENT_MOUSE_CURSOR_POS" ); // GLFW event cursor position
assignGlobalInt( EVENT_MOUSE_SCROLL, "EVENT_MOUSE_SCROLL" ); // GLFW event mouse scroll 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_CURSOR_ENTER, "EVENT_CURSOR_ENTER" ); // GLFW event cursor enter/leave
assignGlobalInt( EVENT_JOYSTICK, "EVENT_JOYSTICK" ); // GLFW event joystick
/*DOC_END*/ /*DOC_END*/
lua_pop( L, -1 ); lua_pop( L, -1 );
@@ -1286,6 +1289,37 @@ static void cursorEnterInputEvent( GLFWwindow* window, int enter ) {
lua_pop( L, -1 ); 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 ) { bool luaInit( int argn, const char **argc ) {
state->luaState = luaL_newstate(); state->luaState = luaL_newstate();
lua_State *L = state->luaState; lua_State *L = state->luaState;
@@ -1399,6 +1433,7 @@ bool luaCallMain() {
state->raylibMouseCursorPosCallback = glfwSetCursorPosCallback( GetWindowHandle(), mouseCursorPosInputEvent ); state->raylibMouseCursorPosCallback = glfwSetCursorPosCallback( GetWindowHandle(), mouseCursorPosInputEvent );
state->raylibMouseScrollCallback = glfwSetScrollCallback( GetWindowHandle(), mouseScrollInputEvent ); state->raylibMouseScrollCallback = glfwSetScrollCallback( GetWindowHandle(), mouseScrollInputEvent );
state->raylibCursorEnterCallback = glfwSetCursorEnterCallback( GetWindowHandle(), cursorEnterInputEvent ); state->raylibCursorEnterCallback = glfwSetCursorEnterCallback( GetWindowHandle(), cursorEnterInputEvent );
state->raylibJoystickCallback = glfwSetJoystickCallback( joystickInputEvent );
lua_getglobal( L, "RL" ); lua_getglobal( L, "RL" );
lua_getfield( L, -1, "init" ); lua_getfield( L, -1, "init" );