94 lines
2.2 KiB
C
94 lines
2.2 KiB
C
/**
|
|
* Copyright (c) 2025 Dominic Masters
|
|
*
|
|
* This software is released under the MIT License.
|
|
* https://opensource.org/licenses/MIT
|
|
*/
|
|
|
|
#pragma once
|
|
#include "script/scriptcontext.h"
|
|
#include "input/input.h"
|
|
#include "script/scriptstruct.h"
|
|
|
|
int moduleInputBind(lua_State *L) {
|
|
assertNotNull(L, "Lua state cannot be NULL");
|
|
|
|
// Requires action and button.
|
|
if(!lua_isstring(L, 1)) {
|
|
luaL_error(L, "inputBind: Expected button name as first argument");
|
|
return 0;
|
|
}
|
|
|
|
// Expect action ID
|
|
if(!lua_isinteger(L, 2)) {
|
|
luaL_error(L, "inputBind: Expected action ID as second argument");
|
|
return 0;
|
|
}
|
|
|
|
const char_t *strBtn = lua_tostring(L, 1);
|
|
const inputaction_t action = (inputaction_t)lua_tointeger(L, 2);
|
|
|
|
// Validate action
|
|
if(action < INPUT_ACTION_NULL || action >= INPUT_ACTION_COUNT) {
|
|
luaL_error(L, "inputBind: Invalid action ID %d", (int)action);
|
|
return 0;
|
|
}
|
|
|
|
// Get button by name
|
|
inputbutton_t btn = inputButtonGetByName(strBtn);
|
|
if(btn.type == INPUT_BUTTON_TYPE_NONE) {
|
|
printf("inputBind: Unknown button name '%s'\n", strBtn);
|
|
return 0;
|
|
}
|
|
|
|
inputBind(btn, action);
|
|
return 0;
|
|
}
|
|
|
|
void moduleInputEventGetter(
|
|
const scriptcontext_t *context,
|
|
const char_t *key,
|
|
const void *structPtr,
|
|
scriptvalue_t *outValue
|
|
) {
|
|
if(stringCompare(key, "action") == 0) {
|
|
outValue->type = SCRIPT_VALUE_TYPE_INT;
|
|
outValue->value.intValue = ((const inputevent_t*)structPtr)->action;
|
|
return;
|
|
}
|
|
}
|
|
|
|
void moduleInput(scriptcontext_t *context) {
|
|
assertNotNull(context, "Script context cannot be NULL");
|
|
|
|
// Setup enums.
|
|
scriptContextExec(context, INPUT_ACTION_SCRIPT);
|
|
|
|
// Input values.
|
|
scriptContextExec(context,
|
|
#if INPUT_KEYBOARD == 1
|
|
"INPUT_KEYBOARD = true\n"
|
|
#endif
|
|
#if INPUT_GAMEPAD == 1
|
|
"INPUT_GAMEPAD = true\n"
|
|
#endif
|
|
#if INPUT_SDL2 == 1
|
|
"INPUT_SDL2 = true\n"
|
|
#endif
|
|
);
|
|
|
|
// Script structure
|
|
scriptStructRegister(
|
|
context,
|
|
"input_mt",
|
|
&moduleInputEventGetter,
|
|
NULL
|
|
);
|
|
|
|
// Events
|
|
scriptContextRegPointer(context,"INPUT_EVENT_PRESSED",&INPUT.eventPressed);
|
|
scriptContextRegPointer(context,"INPUT_EVENT_RELEASED",&INPUT.eventReleased);
|
|
|
|
// Bind methods
|
|
scriptContextRegFunc(context, "inputBind", moduleInputBind);
|
|
} |