2021-11-30 21:29:18 -06:00
|
|
|
#include "input.h"
|
2022-08-26 09:19:17 -05:00
|
|
|
|
2022-12-14 13:01:42 -06:00
|
|
|
#include <stdio.h>
|
|
|
|
#include "script.h"
|
2022-08-26 09:19:17 -05:00
|
|
|
#include "stb_ds.h"
|
2022-12-16 11:54:05 -06:00
|
|
|
#include "log.h"
|
2023-02-19 11:16:35 -06:00
|
|
|
#include "ffi.h"
|
2021-11-30 21:29:18 -06:00
|
|
|
|
|
|
|
int32_t mouseWheelX = 0;
|
|
|
|
int32_t mouseWheelY = 0;
|
|
|
|
float deltaT = 0;
|
2022-12-16 11:54:05 -06:00
|
|
|
|
2023-02-02 17:52:15 -06:00
|
|
|
cpVect mouse_pos = {0,0};
|
|
|
|
cpVect mouse_delta = {0,0};
|
2022-02-06 10:14:57 -06:00
|
|
|
|
2022-08-26 09:19:17 -05:00
|
|
|
static int *downkeys = NULL;
|
2022-08-12 14:03:56 -05:00
|
|
|
|
2022-12-16 11:54:05 -06:00
|
|
|
static int mquit = 0;
|
|
|
|
|
2023-04-19 15:16:35 -05:00
|
|
|
static struct callee pawn_callee;
|
2022-12-19 18:15:38 -06:00
|
|
|
|
2023-04-19 15:16:35 -05:00
|
|
|
void register_pawn(struct callee c)
|
|
|
|
{
|
|
|
|
pawn_callee = c;
|
2022-12-19 18:15:38 -06:00
|
|
|
}
|
|
|
|
|
2023-02-03 13:41:53 -06:00
|
|
|
void add_downkey(int key) {
|
|
|
|
for (int i = 0; i < arrlen(downkeys); i++)
|
|
|
|
if (downkeys[i] == key) return;
|
|
|
|
|
|
|
|
arrput(downkeys, key);
|
|
|
|
}
|
|
|
|
|
|
|
|
void rm_downkey(int key) {
|
|
|
|
for (int i = 0; i < arrlen(downkeys); i++)
|
|
|
|
if (downkeys[i] == key) {
|
|
|
|
arrdelswap(downkeys, i);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-06 10:14:57 -06:00
|
|
|
static void cursor_pos_cb(GLFWwindow *w, double xpos, double ypos)
|
|
|
|
{
|
2023-02-02 17:52:15 -06:00
|
|
|
mouse_delta.x = xpos - mouse_pos.x;
|
|
|
|
mouse_delta.y = ypos - mouse_pos.y;
|
|
|
|
|
|
|
|
mouse_pos.x = xpos;
|
|
|
|
mouse_pos.y = ypos;
|
|
|
|
|
2023-04-19 15:16:35 -05:00
|
|
|
JSValue argv[2];
|
|
|
|
argv[0] = JS_NewString(js, "input_mouse_pos");
|
|
|
|
argv[1] = vec2js(mouse_pos);
|
|
|
|
script_callee(pawn_callee, 2, argv);
|
|
|
|
JS_FreeValue(js, argv[0]);
|
|
|
|
JS_FreeValue(js, argv[1]);
|
2022-02-06 10:14:57 -06:00
|
|
|
}
|
|
|
|
|
2023-02-13 08:30:35 -06:00
|
|
|
static void pawn_call_keydown(int key)
|
|
|
|
{
|
2023-04-19 15:16:35 -05:00
|
|
|
JSValue argv[2];
|
|
|
|
argv[0] = JS_NewString(js, "input_num_pressed");
|
|
|
|
argv[1] = JS_NewInt32(js, key);
|
|
|
|
script_callee(pawn_callee, 2, argv);
|
|
|
|
JS_FreeValue(js, argv[0]);
|
|
|
|
JS_FreeValue(js, argv[1]);
|
2023-02-13 08:30:35 -06:00
|
|
|
}
|
|
|
|
|
2022-02-06 10:14:57 -06:00
|
|
|
static void scroll_cb(GLFWwindow *w, double xoffset, double yoffset)
|
|
|
|
{
|
|
|
|
mouseWheelY = yoffset;
|
|
|
|
mouseWheelX = xoffset;
|
|
|
|
}
|
|
|
|
|
2023-02-02 17:52:15 -06:00
|
|
|
static void mb_cb(GLFWwindow *w, int button, int action, int mods)
|
|
|
|
{
|
|
|
|
const char *act = NULL;
|
|
|
|
const char *btn = NULL;
|
|
|
|
switch (action) {
|
|
|
|
case GLFW_PRESS:
|
|
|
|
act = "pressed";
|
2023-02-03 13:41:53 -06:00
|
|
|
add_downkey(button);
|
2023-02-02 17:52:15 -06:00
|
|
|
break;
|
|
|
|
|
|
|
|
case GLFW_RELEASE:
|
|
|
|
act = "released";
|
2023-02-03 13:41:53 -06:00
|
|
|
rm_downkey(button);
|
2023-02-08 15:30:12 -06:00
|
|
|
call_input_signal("input_any_released");
|
2023-02-02 17:52:15 -06:00
|
|
|
break;
|
|
|
|
|
|
|
|
case GLFW_REPEAT:
|
|
|
|
act = "repeat";
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2023-02-03 13:41:53 -06:00
|
|
|
btn = keyname_extd(button, button);
|
2023-02-02 17:52:15 -06:00
|
|
|
|
|
|
|
if (!act || !btn) {
|
|
|
|
YughError("Tried to call a mouse action that doesn't exist.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
char keystr[50] = {'\0'};
|
|
|
|
snprintf(keystr, 50, "input_%s_%s", btn, act);
|
|
|
|
call_input_signal(keystr);
|
|
|
|
}
|
|
|
|
|
2023-02-04 22:53:54 -06:00
|
|
|
void set_mouse_mode(int mousemode)
|
|
|
|
{
|
|
|
|
glfwSetInputMode(mainwin->window, GLFW_CURSOR, mousemode);
|
|
|
|
}
|
|
|
|
|
2023-02-13 13:35:01 -06:00
|
|
|
void char_cb(GLFWwindow *w, unsigned int codepoint)
|
|
|
|
{
|
2023-04-19 15:16:35 -05:00
|
|
|
static char out[2] = {0};
|
|
|
|
static JSValue argv[2];
|
|
|
|
|
|
|
|
out[0] = (char)codepoint;
|
|
|
|
argv[0] = JS_NewString(js, "input_text");
|
|
|
|
argv[1] = JS_NewString(js, out);
|
|
|
|
script_callee(pawn_callee, 2, argv);
|
|
|
|
|
|
|
|
JS_FreeValue(js, argv[0]);
|
|
|
|
JS_FreeValue(js, argv[1]);
|
2023-02-13 13:35:01 -06:00
|
|
|
}
|
|
|
|
|
2023-03-10 13:13:48 -06:00
|
|
|
static GLFWcharfun nukechar;
|
|
|
|
|
2022-02-06 10:14:57 -06:00
|
|
|
void input_init()
|
|
|
|
{
|
|
|
|
glfwSetCursorPosCallback(mainwin->window, cursor_pos_cb);
|
|
|
|
glfwSetScrollCallback(mainwin->window, scroll_cb);
|
2023-02-02 17:52:15 -06:00
|
|
|
glfwSetMouseButtonCallback(mainwin->window, mb_cb);
|
2023-03-10 13:13:48 -06:00
|
|
|
nukechar = glfwSetCharCallback(mainwin->window, char_cb);
|
|
|
|
}
|
|
|
|
|
|
|
|
void input_to_nuke()
|
|
|
|
{
|
|
|
|
glfwSetCharCallback(mainwin->window, nukechar);
|
|
|
|
}
|
|
|
|
|
|
|
|
void input_to_game()
|
|
|
|
{
|
|
|
|
glfwSetCharCallback(mainwin->window, char_cb);
|
2022-08-12 14:03:56 -05:00
|
|
|
}
|
|
|
|
|
2022-12-20 08:16:26 -06:00
|
|
|
void call_input_signal(char *signal) {
|
2023-04-19 15:16:35 -05:00
|
|
|
JSValue s = JS_NewString(js, signal);
|
|
|
|
JS_Call(js, pawn_callee.fn, pawn_callee.obj, 1, &s);
|
|
|
|
JS_FreeValue(js, s);
|
2022-12-20 08:16:26 -06:00
|
|
|
}
|
|
|
|
|
2022-12-22 16:58:06 -06:00
|
|
|
const char *keyname_extd(int key, int scancode) {
|
|
|
|
char keybuf[50];
|
2023-02-08 15:30:12 -06:00
|
|
|
const char *kkey = NULL;
|
2022-12-22 16:58:06 -06:00
|
|
|
|
|
|
|
if (key > 289 && key < 302) {
|
2023-02-28 09:40:53 -06:00
|
|
|
switch(key) {
|
|
|
|
case 290:
|
|
|
|
return "f1";
|
|
|
|
case 291:
|
|
|
|
return "f2";
|
|
|
|
case 292:
|
|
|
|
return "f3";
|
|
|
|
case 293:
|
|
|
|
return "f4";
|
|
|
|
case 294:
|
|
|
|
return "f5";
|
|
|
|
case 295:
|
|
|
|
return "f6";
|
|
|
|
case 296:
|
|
|
|
return "f7";
|
|
|
|
case 297:
|
|
|
|
return "f8";
|
|
|
|
case 298:
|
|
|
|
return "f9";
|
|
|
|
case 299:
|
|
|
|
return "f10";
|
|
|
|
case 300:
|
|
|
|
return "f11";
|
|
|
|
case 301:
|
|
|
|
return "f12";
|
|
|
|
}
|
2022-12-16 11:54:05 -06:00
|
|
|
} else {
|
|
|
|
switch(key) {
|
|
|
|
case GLFW_KEY_ENTER:
|
|
|
|
kkey = "enter";
|
|
|
|
break;
|
|
|
|
|
|
|
|
case GLFW_KEY_ESCAPE:
|
|
|
|
kkey = "escape";
|
|
|
|
break;
|
|
|
|
|
2023-02-06 16:41:47 -06:00
|
|
|
case GLFW_KEY_DELETE:
|
|
|
|
kkey = "delete";
|
|
|
|
break;
|
|
|
|
|
|
|
|
case GLFW_KEY_INSERT:
|
|
|
|
kkey = "insert";
|
|
|
|
break;
|
|
|
|
|
2022-12-16 11:54:05 -06:00
|
|
|
case GLFW_KEY_TAB:
|
|
|
|
kkey = "tab";
|
|
|
|
break;
|
|
|
|
|
|
|
|
case GLFW_KEY_RIGHT:
|
|
|
|
kkey = "right";
|
|
|
|
break;
|
|
|
|
|
|
|
|
case GLFW_KEY_LEFT:
|
|
|
|
kkey = "left";
|
|
|
|
break;
|
|
|
|
|
|
|
|
case GLFW_KEY_UP:
|
|
|
|
kkey = "up";
|
|
|
|
break;
|
|
|
|
|
|
|
|
case GLFW_KEY_DOWN:
|
|
|
|
kkey = "down";
|
|
|
|
break;
|
2022-12-16 13:29:50 -06:00
|
|
|
|
|
|
|
case GLFW_KEY_LEFT_SHIFT:
|
|
|
|
kkey = "lshift";
|
|
|
|
break;
|
|
|
|
|
|
|
|
case GLFW_KEY_RIGHT_SHIFT:
|
|
|
|
kkey = "rshift";
|
|
|
|
break;
|
|
|
|
|
|
|
|
case GLFW_KEY_LEFT_CONTROL:
|
|
|
|
kkey = "lctrl";
|
|
|
|
break;
|
|
|
|
|
|
|
|
case GLFW_KEY_LEFT_ALT:
|
|
|
|
kkey = "lalt";
|
|
|
|
break;
|
|
|
|
|
|
|
|
case GLFW_KEY_RIGHT_CONTROL:
|
|
|
|
kkey = "rctrl";
|
|
|
|
break;
|
|
|
|
|
|
|
|
case GLFW_KEY_RIGHT_ALT:
|
|
|
|
kkey= "ralt";
|
|
|
|
break;
|
2023-01-16 17:18:09 -06:00
|
|
|
|
|
|
|
case GLFW_KEY_SPACE:
|
|
|
|
kkey = "space";
|
|
|
|
break;
|
2023-02-03 13:41:53 -06:00
|
|
|
|
|
|
|
case GLFW_MOUSE_BUTTON_RIGHT:
|
|
|
|
kkey = "rmouse";
|
|
|
|
break;
|
|
|
|
|
|
|
|
case GLFW_MOUSE_BUTTON_LEFT:
|
|
|
|
kkey = "lmouse";
|
|
|
|
break;
|
|
|
|
|
|
|
|
case GLFW_MOUSE_BUTTON_MIDDLE:
|
|
|
|
kkey = "mmouse";
|
|
|
|
break;
|
2023-02-08 15:30:12 -06:00
|
|
|
|
|
|
|
case GLFW_KEY_KP_ADD:
|
|
|
|
kkey = "plus";
|
|
|
|
break;
|
|
|
|
|
|
|
|
case GLFW_KEY_KP_SUBTRACT:
|
|
|
|
kkey = "minus";
|
|
|
|
break;
|
2023-02-10 14:31:59 -06:00
|
|
|
case GLFW_KEY_GRAVE_ACCENT:
|
|
|
|
kkey = "backtick";
|
|
|
|
break;
|
|
|
|
|
|
|
|
case GLFW_KEY_LEFT_BRACKET:
|
|
|
|
kkey = "lbracket";
|
|
|
|
break;
|
|
|
|
|
|
|
|
case GLFW_KEY_RIGHT_BRACKET:
|
|
|
|
kkey = "rbracket";
|
|
|
|
break;
|
2023-02-13 13:35:01 -06:00
|
|
|
|
|
|
|
case GLFW_KEY_BACKSPACE:
|
|
|
|
kkey = "backspace";
|
|
|
|
break;
|
2022-12-16 13:29:50 -06:00
|
|
|
}
|
|
|
|
|
2022-12-22 16:58:06 -06:00
|
|
|
if (kkey) return kkey;
|
|
|
|
|
2022-12-16 11:54:05 -06:00
|
|
|
}
|
|
|
|
|
2023-02-08 15:30:12 -06:00
|
|
|
kkey = glfwGetKeyName(key, scancode);
|
|
|
|
|
|
|
|
if (kkey) return kkey;
|
|
|
|
|
2022-12-22 16:58:06 -06:00
|
|
|
return "NULL";
|
|
|
|
}
|
2022-12-16 11:54:05 -06:00
|
|
|
|
2022-12-22 16:58:06 -06:00
|
|
|
void call_input_down(int *key) {
|
|
|
|
char keystr[50] = {'\0'};
|
|
|
|
snprintf(keystr, 50, "input_%s_down", keyname_extd(*key, 0));
|
|
|
|
call_input_signal(keystr);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* This is called once every frame - or more if we want it more! */
|
|
|
|
void input_poll(double wait)
|
|
|
|
{
|
2023-02-02 17:52:15 -06:00
|
|
|
mouse_delta = cpvzero;
|
2022-12-22 16:58:06 -06:00
|
|
|
mouseWheelX = 0;
|
|
|
|
mouseWheelY = 0;
|
|
|
|
|
|
|
|
glfwWaitEventsTimeout(wait);
|
|
|
|
|
|
|
|
for (int i = 0; i < arrlen(downkeys); i++)
|
|
|
|
call_input_down(&downkeys[i]);
|
|
|
|
}
|
|
|
|
|
2023-02-13 08:30:35 -06:00
|
|
|
int key_is_num(int key) {
|
|
|
|
return key <= 57 && key >= 48;
|
|
|
|
}
|
|
|
|
|
2022-12-22 16:58:06 -06:00
|
|
|
void win_key_callback(GLFWwindow *w, int key, int scancode, int action, int mods)
|
|
|
|
{
|
|
|
|
char keystr[50] = {'\0'};
|
2023-02-28 09:40:53 -06:00
|
|
|
char kkey[50] = {'\0'};
|
|
|
|
snprintf(kkey, 50, "%s", keyname_extd(key,scancode));
|
2022-12-16 11:54:05 -06:00
|
|
|
|
2022-08-12 14:03:56 -05:00
|
|
|
switch (action) {
|
|
|
|
case GLFW_PRESS:
|
2022-12-22 16:58:06 -06:00
|
|
|
snprintf(keystr, 50, "input_%s_pressed", kkey);
|
2023-03-17 10:25:35 -05:00
|
|
|
call_input_signal(keystr);
|
|
|
|
snprintf(keystr,50,"input_%s_pressrep", kkey);
|
|
|
|
call_input_signal(keystr);
|
2023-02-03 13:41:53 -06:00
|
|
|
add_downkey(key);
|
2023-02-10 14:31:59 -06:00
|
|
|
call_input_signal("input_any_pressed");
|
2023-02-13 08:30:35 -06:00
|
|
|
|
|
|
|
if (key_is_num(key)) {
|
|
|
|
pawn_call_keydown(key-48);
|
|
|
|
}
|
|
|
|
|
2022-08-12 14:03:56 -05:00
|
|
|
break;
|
|
|
|
|
|
|
|
case GLFW_RELEASE:
|
2022-12-22 16:58:06 -06:00
|
|
|
snprintf(keystr, 50, "input_%s_released", kkey);
|
2023-03-17 10:25:35 -05:00
|
|
|
call_input_signal(keystr);
|
2023-02-03 13:41:53 -06:00
|
|
|
rm_downkey(key);
|
2023-02-08 15:30:12 -06:00
|
|
|
call_input_signal("input_any_released");
|
2022-08-12 14:03:56 -05:00
|
|
|
break;
|
|
|
|
|
|
|
|
case GLFW_REPEAT:
|
2022-12-22 16:58:06 -06:00
|
|
|
snprintf(keystr, 50, "input_%s_rep", kkey);
|
2023-03-17 10:25:35 -05:00
|
|
|
call_input_signal(keystr);
|
|
|
|
snprintf(keystr,50,"input_%s_pressrep", kkey);
|
|
|
|
call_input_signal(keystr);
|
2022-08-12 14:03:56 -05:00
|
|
|
break;
|
|
|
|
}
|
2022-02-06 10:14:57 -06:00
|
|
|
}
|
2021-11-30 21:29:18 -06:00
|
|
|
|
2022-02-06 10:14:57 -06:00
|
|
|
void cursor_hide()
|
|
|
|
{
|
2022-12-13 12:32:36 -06:00
|
|
|
glfwSetInputMode(mainwin->window, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
|
2022-02-06 10:14:57 -06:00
|
|
|
}
|
2021-11-30 21:29:18 -06:00
|
|
|
|
2022-02-06 10:14:57 -06:00
|
|
|
void cursor_show()
|
|
|
|
{
|
|
|
|
glfwSetInputMode(mainwin->window, GLFW_CURSOR, GLFW_CURSOR_NORMAL);
|
|
|
|
}
|
2021-11-30 21:29:18 -06:00
|
|
|
|
2022-02-06 10:14:57 -06:00
|
|
|
int action_down(int scancode)
|
|
|
|
{
|
2022-08-26 09:19:17 -05:00
|
|
|
for (int i = 0; i < arrlen(downkeys); i++) {
|
|
|
|
if (downkeys[i] == scancode)
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
2021-11-30 21:29:18 -06:00
|
|
|
}
|
2022-08-12 14:03:56 -05:00
|
|
|
|
|
|
|
int action_up(int scancode)
|
|
|
|
{
|
2022-08-26 09:19:17 -05:00
|
|
|
int found = 0;
|
|
|
|
for (int i = 0; i < arrlen(downkeys); i++) {
|
|
|
|
if (downkeys[i] == scancode) {
|
|
|
|
found = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return !found;
|
2022-11-25 07:12:31 -06:00
|
|
|
}
|
2022-12-16 11:54:05 -06:00
|
|
|
|
|
|
|
int want_quit() {
|
|
|
|
return mquit;
|
|
|
|
}
|
|
|
|
|
|
|
|
void quit() {
|
|
|
|
YughInfo("Exiting game.");
|
|
|
|
mquit = 1;
|
2022-12-16 13:29:50 -06:00
|
|
|
}
|