Add action mapping

This commit is contained in:
2023-04-25 21:59:12 +00:00
parent dc1fda6611
commit 02707a9ada
5 changed files with 115 additions and 22 deletions

View File

@@ -53,6 +53,11 @@ JSValue int2js(int i)
return JS_NewInt64(js, i);
}
JSValue str2js(const char *c)
{
return JS_NewString(js, c);
}
double js2number(JSValue v)
{
double g;
@@ -979,6 +984,10 @@ JSValue duk_register(JSContext *js, JSValueConst this, int argc, JSValueConst *a
case 7:
register_pawn(c);
break;
case 8:
register_gamepad(c);
break;
}
return JS_NULL;

View File

@@ -16,5 +16,6 @@ struct color js2color(JSValue v);
double js2number(JSValue v);
JSValue num2js(double g);
JSValue int2js(int i);
JSValue str2js(const char *c);
#endif

View File

@@ -5,6 +5,7 @@
#include "stb_ds.h"
#include "log.h"
#include "ffi.h"
#include "time.h"
int32_t mouseWheelX = 0;
int32_t mouseWheelY = 0;
@@ -24,12 +25,18 @@ static struct joystick *joysticks = NULL;
static int mquit = 0;
static struct callee pawn_callee;
static struct callee gamepad_callee;
void register_pawn(struct callee c)
{
pawn_callee = c;
}
void register_gamepad(struct callee c)
{
gamepad_callee = c;
}
void add_downkey(int key) {
for (int i = 0; i < arrlen(downkeys); i++)
if (downkeys[i] == key) return;
@@ -372,17 +379,48 @@ void input_poll(double wait)
GLFWgamepadstate state;
if (!glfwGetGamepadState(joysticks[i].id, &state)) continue;
JSValue argv[3];
argv[0] = int2js(joysticks[i].id);
char inputstr[50];
for (int b = 0; b < 15; b++) {
argv[1] = str2js(gamepad2str(b));
if (state.buttons[b]) {
if (!joysticks[i].state.buttons[b])
YughWarn("Pressed button %s.", gamepad2str(b));
argv[2] = int2js(0);
script_callee(gamepad_callee,3,argv);
if (!joysticks[i].state.buttons[b]) {
argv[2] = int2js(1);
script_callee(gamepad_callee,3,argv);
}
}
else if (!state.buttons[b] && joysticks[i].state.buttons[b]) {
YughWarn("Released button %s.", gamepad2str(b));
argv[2] = int2js(2);
script_callee(gamepad_callee,3,argv);
}
}
argv[1] = str2js("axis_ljoy");
cpVect v;
v.x = state.axes[0];
v.y = -state.axes[1];
argv[2] = vec2js(v);
script_callee(gamepad_callee,3,argv);
argv[1] = str2js("axis_rjoy");
v.x = state.axes[2];
v.y = -state.axes[3];
argv[2] = vec2js(v);
script_callee(gamepad_callee,3,argv);
argv[1] = str2js("axis_ltrigger");
argv[2] = num2js((state.axes[4]+1)/2);
script_callee(gamepad_callee,3,argv);
argv[1] = str2js("axis_rtrigger");
argv[2] = num2js((state.axes[5]+1)/2);
script_callee(gamepad_callee,3,argv);
joysticks[i].state = state;
}
}

View File

@@ -26,6 +26,7 @@ const char *keyname_extd(int key, int scancode);
int action_down(int scancode);
void register_pawn(struct callee c);
void register_gamepad(struct callee c);
int want_quit();
void quit();