2024-09-16 13:17:00 +00:00
|
|
|
#include "twn_input_c.h"
|
|
|
|
#include "twn_util.h"
|
2024-10-08 07:12:30 +00:00
|
|
|
#include "twn_control.h"
|
2024-10-01 00:13:58 +00:00
|
|
|
#include "twn_engine_context_c.h"
|
2024-07-08 00:44:20 +00:00
|
|
|
|
|
|
|
#include <SDL2/SDL.h>
|
|
|
|
#include <stb_ds.h>
|
|
|
|
|
|
|
|
#include <stdbool.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
|
|
|
|
2024-09-23 17:43:16 +00:00
|
|
|
static void update_action_pressed_state(InputState *input, Action *action) {
|
2024-10-01 00:13:58 +00:00
|
|
|
for (size_t i = 0; i < (uint64_t)ctx.keybind_slots; ++i) {
|
2024-07-08 00:44:20 +00:00
|
|
|
switch (action->bindings[i].source) {
|
|
|
|
case BUTTON_SOURCE_NOT_SET:
|
|
|
|
break;
|
|
|
|
case BUTTON_SOURCE_KEYBOARD_PHYSICAL:
|
|
|
|
/* not pressed */
|
|
|
|
if (input->keyboard_state[action->bindings[i].code.scancode] == 0) {
|
|
|
|
action->just_changed = action->is_pressed;
|
|
|
|
action->is_pressed = false;
|
|
|
|
}
|
|
|
|
/* pressed */
|
|
|
|
else {
|
|
|
|
action->just_changed = !action->is_pressed;
|
|
|
|
action->is_pressed = true;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case BUTTON_SOURCE_MOUSE:
|
|
|
|
/* not pressed */
|
|
|
|
if ((input->mouse_state & action->bindings[i].code.mouse_button) == 0) {
|
|
|
|
action->just_changed = action->is_pressed;
|
|
|
|
action->is_pressed = false;
|
|
|
|
}
|
|
|
|
/* pressed */
|
|
|
|
else {
|
|
|
|
action->just_changed = !action->is_pressed;
|
|
|
|
action->is_pressed = true;
|
2024-07-12 18:10:25 +00:00
|
|
|
action->position.x = (float)input->mouse_window_position.x;
|
2024-10-08 07:12:30 +00:00
|
|
|
action->position.y = (float)input->mouse_window_position.y;
|
2024-07-08 00:44:20 +00:00
|
|
|
|
2024-07-12 18:10:25 +00:00
|
|
|
/* TODO: */
|
2024-07-08 00:44:20 +00:00
|
|
|
/*
|
|
|
|
* SDL_RenderWindowToLogical will turn window mouse
|
|
|
|
* coords into a position inside the logical render
|
|
|
|
* area. this has to be done to get an accurate point
|
|
|
|
* that can actually be used in game logic
|
|
|
|
*/
|
2024-07-12 18:10:25 +00:00
|
|
|
// SDL_RenderWindowToLogical(input->renderer,
|
|
|
|
// input->mouse_window_position.x,
|
|
|
|
// input->mouse_window_position.y,
|
|
|
|
// &action->position.x,
|
|
|
|
// &action->position.y);
|
2024-07-08 00:44:20 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2024-09-23 17:43:16 +00:00
|
|
|
static void input_bind_code_to_action(InputState *input,
|
2024-10-08 07:12:30 +00:00
|
|
|
char const *action_name,
|
2024-09-23 17:43:16 +00:00
|
|
|
ButtonSource source,
|
|
|
|
union ButtonCode code)
|
2024-07-08 00:44:20 +00:00
|
|
|
{
|
2024-09-23 17:43:16 +00:00
|
|
|
ActionHashItem *action_item = shgetp_null(input->action_hash, action_name);
|
2024-07-08 00:44:20 +00:00
|
|
|
if (action_item == NULL) {
|
|
|
|
log_warn("(%s) Action \"%s\" does not exist.", __func__, action_name);
|
|
|
|
return;
|
|
|
|
}
|
2024-09-23 17:43:16 +00:00
|
|
|
Action *action = &action_item->value;
|
2024-07-08 00:44:20 +00:00
|
|
|
|
|
|
|
/* check every binding to make sure this code isn't already bound */
|
2024-10-01 00:13:58 +00:00
|
|
|
for (size_t i = 0; i < (uint64_t)ctx.keybind_slots; ++i) {
|
2024-09-23 17:43:16 +00:00
|
|
|
Button *binding = &action->bindings[i];
|
2024-07-08 00:44:20 +00:00
|
|
|
|
|
|
|
if (binding->source != source)
|
|
|
|
break;
|
|
|
|
|
|
|
|
bool is_already_bound = false;
|
|
|
|
switch (binding->source) {
|
|
|
|
case BUTTON_SOURCE_NOT_SET:
|
|
|
|
break;
|
|
|
|
case BUTTON_SOURCE_KEYBOARD_PHYSICAL:
|
|
|
|
is_already_bound = binding->code.scancode == code.scancode;
|
|
|
|
break;
|
|
|
|
case BUTTON_SOURCE_KEYBOARD_CHARACTER:
|
|
|
|
is_already_bound = binding->code.keycode == code.keycode;
|
|
|
|
break;
|
|
|
|
case BUTTON_SOURCE_GAMEPAD:
|
|
|
|
is_already_bound = binding->code.gamepad_button == code.gamepad_button;
|
|
|
|
break;
|
|
|
|
case BUTTON_SOURCE_MOUSE:
|
|
|
|
is_already_bound = binding->code.mouse_button == code.mouse_button;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (is_already_bound) {
|
|
|
|
log_warn("(%s) Code already bound to action \"%s\".", __func__, action_name);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* if we're at max bindings, forget the first element and shift the rest */
|
2024-10-01 00:13:58 +00:00
|
|
|
if (action->num_bindings == (uint64_t)ctx.keybind_slots) {
|
2024-07-08 00:44:20 +00:00
|
|
|
--action->num_bindings;
|
|
|
|
size_t shifted_size = (sizeof action->bindings) - (sizeof action->bindings[0]);
|
2024-09-25 22:42:34 +00:00
|
|
|
SDL_memmove(action->bindings, action->bindings + 1, shifted_size);
|
2024-07-08 00:44:20 +00:00
|
|
|
}
|
|
|
|
|
2024-09-23 17:43:16 +00:00
|
|
|
action->bindings[action->num_bindings++] = (Button) {
|
2024-07-08 00:44:20 +00:00
|
|
|
.source = source,
|
|
|
|
.code = code,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2024-09-23 17:43:16 +00:00
|
|
|
static void input_unbind_code_from_action(InputState *input,
|
2024-10-08 07:12:30 +00:00
|
|
|
char const *action_name,
|
2024-09-23 17:43:16 +00:00
|
|
|
ButtonSource source,
|
|
|
|
union ButtonCode code)
|
2024-07-08 00:44:20 +00:00
|
|
|
{
|
2024-09-23 17:43:16 +00:00
|
|
|
ActionHashItem *action_item = shgetp_null(input->action_hash, action_name);
|
2024-07-08 00:44:20 +00:00
|
|
|
if (action_item == NULL) {
|
|
|
|
log_warn("(%s) Action \"%s\" does not exist.", __func__, action_name);
|
|
|
|
return;
|
|
|
|
}
|
2024-09-23 17:43:16 +00:00
|
|
|
Action *action = &action_item->value;
|
2024-07-08 00:44:20 +00:00
|
|
|
|
|
|
|
/* check every binding to make sure this code is bound */
|
|
|
|
size_t index = 0;
|
|
|
|
bool is_bound = false;
|
2024-10-01 00:13:58 +00:00
|
|
|
for (index = 0; index < (uint64_t)ctx.keybind_slots; ++index) {
|
2024-09-23 17:43:16 +00:00
|
|
|
Button *binding = &action->bindings[index];
|
2024-07-08 00:44:20 +00:00
|
|
|
|
|
|
|
if (binding->source != source)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
switch (binding->source) {
|
|
|
|
case BUTTON_SOURCE_NOT_SET:
|
|
|
|
break;
|
|
|
|
case BUTTON_SOURCE_KEYBOARD_PHYSICAL:
|
|
|
|
is_bound = binding->code.scancode == code.scancode;
|
|
|
|
break;
|
|
|
|
case BUTTON_SOURCE_KEYBOARD_CHARACTER:
|
|
|
|
is_bound = binding->code.keycode == code.keycode;
|
|
|
|
break;
|
|
|
|
case BUTTON_SOURCE_GAMEPAD:
|
|
|
|
is_bound = binding->code.gamepad_button == code.gamepad_button;
|
|
|
|
break;
|
|
|
|
case BUTTON_SOURCE_MOUSE:
|
|
|
|
is_bound = binding->code.mouse_button == code.mouse_button;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2024-09-20 15:39:37 +00:00
|
|
|
/* stop early, this won't change */
|
2024-07-08 00:44:20 +00:00
|
|
|
if (is_bound)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!is_bound) {
|
|
|
|
log_warn("(%s) Code is not bound to action \"%s\".", __func__, action_name);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* remove the element to unbind and shift the rest so there isn't a gap */
|
2024-09-20 15:39:37 +00:00
|
|
|
size_t elements_after_index = action->num_bindings - index;
|
|
|
|
size_t shifted_size = elements_after_index * sizeof action->bindings[0];
|
2024-09-25 22:42:34 +00:00
|
|
|
SDL_memmove(action->bindings + index, action->bindings + index + 1, shifted_size);
|
2024-09-20 15:39:37 +00:00
|
|
|
--action->num_bindings;
|
2024-07-08 00:44:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2024-09-23 17:43:16 +00:00
|
|
|
void input_state_init(InputState *input) {
|
2024-07-08 00:44:20 +00:00
|
|
|
sh_new_strdup(input->action_hash);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2024-09-23 17:43:16 +00:00
|
|
|
void input_state_deinit(InputState *input) {
|
2024-10-01 10:34:58 +00:00
|
|
|
input_reset_state(input);
|
2024-07-08 00:44:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2024-09-23 17:43:16 +00:00
|
|
|
void input_state_update(InputState *input) {
|
2024-07-08 00:44:20 +00:00
|
|
|
input->keyboard_state = SDL_GetKeyboardState(NULL);
|
|
|
|
input->mouse_state = SDL_GetMouseState(&input->mouse_window_position.x,
|
|
|
|
&input->mouse_window_position.y);
|
|
|
|
|
2024-07-30 21:05:28 +00:00
|
|
|
SDL_GetRelativeMouseState(&input->mouse_relative_position.x,
|
|
|
|
&input->mouse_relative_position.y);
|
|
|
|
|
2024-10-12 17:24:47 +00:00
|
|
|
ctx.game.mouse_position = input->mouse_window_position;
|
|
|
|
ctx.game.mouse_movement = input->mouse_relative_position;
|
2024-10-08 07:12:30 +00:00
|
|
|
|
2024-07-08 00:44:20 +00:00
|
|
|
for (size_t i = 0; i < shlenu(input->action_hash); ++i) {
|
2024-09-23 17:43:16 +00:00
|
|
|
Action *action = &input->action_hash[i].value;
|
2024-07-08 00:44:20 +00:00
|
|
|
update_action_pressed_state(input, action);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2024-10-08 07:12:30 +00:00
|
|
|
void input_bind_action_control(char const *action_name,
|
|
|
|
Control control)
|
2024-07-08 00:44:20 +00:00
|
|
|
{
|
2024-10-08 07:12:30 +00:00
|
|
|
SDL_assert_always(action_name);
|
2024-07-08 00:44:20 +00:00
|
|
|
|
2024-10-08 07:12:30 +00:00
|
|
|
if (CONTROL_SCANCODE_START <= control && control < CONTROL_SCANCODE_LIMIT)
|
|
|
|
input_bind_code_to_action(&ctx.input,
|
2024-07-08 00:44:20 +00:00
|
|
|
action_name,
|
|
|
|
BUTTON_SOURCE_KEYBOARD_PHYSICAL,
|
2024-10-08 07:12:30 +00:00
|
|
|
(union ButtonCode) { .scancode = (SDL_Scancode)control });
|
2024-07-08 00:44:20 +00:00
|
|
|
|
2024-10-08 07:12:30 +00:00
|
|
|
else if (CONTROL_MOUSECODE_START <= control && control < CONTROL_MOUSECODE_LIMIT) {
|
|
|
|
uint8_t const mouse_button = (uint8_t)(control - CONTROL_MOUSECODE_START);
|
|
|
|
input_bind_code_to_action(&ctx.input,
|
|
|
|
action_name,
|
|
|
|
BUTTON_SOURCE_MOUSE,
|
|
|
|
(union ButtonCode) { .mouse_button = SDL_BUTTON(mouse_button)});
|
|
|
|
} else
|
|
|
|
log_warn("(%s) Invalid control value given: %i.", __func__, control);
|
2024-07-08 00:44:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2024-10-08 07:12:30 +00:00
|
|
|
void input_unbind_action_control(char const *action_name,
|
|
|
|
Control control)
|
2024-07-08 00:44:20 +00:00
|
|
|
{
|
2024-10-08 07:12:30 +00:00
|
|
|
SDL_assert_always(action_name);
|
|
|
|
|
|
|
|
if (CONTROL_SCANCODE_START <= control && control < CONTROL_SCANCODE_LIMIT)
|
|
|
|
input_unbind_code_from_action(&ctx.input,
|
|
|
|
action_name,
|
|
|
|
BUTTON_SOURCE_KEYBOARD_PHYSICAL,
|
|
|
|
(union ButtonCode) { .scancode = (SDL_Scancode)control });
|
|
|
|
|
|
|
|
else if (CONTROL_MOUSECODE_START <= control && control < CONTROL_MOUSECODE_LIMIT) {
|
|
|
|
uint8_t const mouse_button = (uint8_t)(control - CONTROL_MOUSECODE_START);
|
|
|
|
input_unbind_code_from_action(&ctx.input,
|
|
|
|
action_name,
|
|
|
|
BUTTON_SOURCE_MOUSE,
|
|
|
|
(union ButtonCode) { .mouse_button = SDL_BUTTON(mouse_button)});
|
|
|
|
} else
|
|
|
|
log_warn("(%s) Invalid control value given: %i.", __func__, control);
|
2024-07-08 00:44:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2024-10-08 07:12:30 +00:00
|
|
|
void input_add_action(char const *action_name) {
|
|
|
|
SDL_assert_always(action_name);
|
|
|
|
|
|
|
|
if (shgeti(ctx.input.action_hash, action_name) >= 0) {
|
2024-07-08 00:44:20 +00:00
|
|
|
log_warn("(%s) Action \"%s\" is already registered.", __func__, action_name);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-10-01 00:13:58 +00:00
|
|
|
Action new_action = { 0 };
|
|
|
|
new_action.bindings = ccalloc(ctx.keybind_slots, sizeof *new_action.bindings);
|
2024-10-08 07:12:30 +00:00
|
|
|
shput(ctx.input.action_hash, action_name, new_action);
|
2024-07-08 00:44:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2024-10-08 07:12:30 +00:00
|
|
|
void input_delete_action(char const *action_name) {
|
|
|
|
SDL_assert_always(action_name);
|
|
|
|
|
|
|
|
ActionHashItem *action = shgetp_null(ctx.input.action_hash, action_name);
|
2024-10-01 00:13:58 +00:00
|
|
|
if (action == NULL) {
|
2024-07-08 00:44:20 +00:00
|
|
|
log_warn("(%s) Action \"%s\" is not registered.", __func__, action_name);
|
2024-10-01 00:13:58 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
SDL_free(action->value.bindings);
|
2024-10-08 07:12:30 +00:00
|
|
|
shdel(ctx.input.action_hash, action_name);
|
2024-07-08 00:44:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2024-10-08 07:12:30 +00:00
|
|
|
bool input_is_action_pressed(char const *action_name) {
|
|
|
|
SDL_assert_always(action_name);
|
|
|
|
|
|
|
|
ActionHashItem *action = shgetp_null(ctx.input.action_hash, action_name);
|
2024-07-08 00:44:20 +00:00
|
|
|
if (action == NULL) {
|
|
|
|
log_warn("(%s) Action \"%s\" does not exist.", __func__, action_name);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return action->value.is_pressed;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2024-10-08 07:12:30 +00:00
|
|
|
bool input_is_action_just_pressed(char const *action_name) {
|
|
|
|
SDL_assert_always(action_name);
|
|
|
|
|
|
|
|
ActionHashItem *action = shgetp_null(ctx.input.action_hash, action_name);
|
2024-07-08 00:44:20 +00:00
|
|
|
if (action == NULL) {
|
|
|
|
log_warn("(%s) Action \"%s\" does not exist.", __func__, action_name);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return action->value.is_pressed && action->value.just_changed;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2024-10-08 07:12:30 +00:00
|
|
|
bool input_is_action_just_released(char const *action_name) {
|
|
|
|
SDL_assert_always(action_name);
|
|
|
|
|
|
|
|
ActionHashItem *action = shgetp_null(ctx.input.action_hash, action_name);
|
2024-07-08 00:44:20 +00:00
|
|
|
if (action == NULL) {
|
|
|
|
log_warn("(%s) Action \"%s\" does not exist.", __func__, action_name);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return !action->value.is_pressed && action->value.just_changed;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2024-10-08 07:12:30 +00:00
|
|
|
Vec2 input_get_action_position(char const *action_name) {
|
|
|
|
SDL_assert_always(action_name);
|
|
|
|
|
|
|
|
ActionHashItem *action = shgetp_null(ctx.input.action_hash, action_name);
|
2024-07-08 00:44:20 +00:00
|
|
|
if (action == NULL) {
|
|
|
|
log_warn("(%s) Action \"%s\" does not exist.", __func__, action_name);
|
2024-09-23 17:43:16 +00:00
|
|
|
return (Vec2) { 0 };
|
2024-07-08 00:44:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return action->value.position;
|
|
|
|
}
|
2024-07-30 21:05:28 +00:00
|
|
|
|
|
|
|
|
2024-10-08 07:12:30 +00:00
|
|
|
void input_set_mouse_captured(bool enabled) {
|
|
|
|
if (SDL_SetRelativeMouseMode(enabled) != 0)
|
|
|
|
log_warn("(%s) Mouse capture isn't supported.", __func__);
|
2024-07-30 21:05:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2024-10-08 07:12:30 +00:00
|
|
|
bool input_is_mouse_captured(void) {
|
2024-07-30 21:05:28 +00:00
|
|
|
return SDL_GetRelativeMouseMode();
|
|
|
|
}
|
2024-08-21 13:55:34 +00:00
|
|
|
|
|
|
|
|
2024-09-23 17:43:16 +00:00
|
|
|
void input_reset_state(InputState *input) {
|
2024-10-01 00:13:58 +00:00
|
|
|
for (size_t i = 0; i < shlenu(input->action_hash); ++i) {
|
|
|
|
Action *action = &input->action_hash[i].value;
|
|
|
|
SDL_free(action->bindings);
|
|
|
|
}
|
|
|
|
|
2024-08-21 13:55:34 +00:00
|
|
|
stbds_shfree(input->action_hash);
|
|
|
|
}
|