2024-09-16 06:07:01 +00:00
|
|
|
#ifndef TWN_INPUT_H
|
|
|
|
#define TWN_INPUT_H
|
2024-07-08 00:44:20 +00:00
|
|
|
|
2024-09-16 06:07:01 +00:00
|
|
|
#include "twn_config.h"
|
|
|
|
#include "twn_vec.h"
|
2024-08-26 21:33:37 +00:00
|
|
|
#include "twn_engine_api.h"
|
2024-07-08 00:44:20 +00:00
|
|
|
|
|
|
|
#include <SDL2/SDL.h>
|
|
|
|
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <stdbool.h>
|
|
|
|
|
|
|
|
|
2024-09-23 17:43:16 +00:00
|
|
|
typedef enum ButtonSource {
|
2024-07-08 00:44:20 +00:00
|
|
|
BUTTON_SOURCE_NOT_SET,
|
|
|
|
BUTTON_SOURCE_KEYBOARD_PHYSICAL,
|
|
|
|
BUTTON_SOURCE_KEYBOARD_CHARACTER,
|
|
|
|
BUTTON_SOURCE_GAMEPAD,
|
|
|
|
BUTTON_SOURCE_MOUSE,
|
2024-09-23 17:43:16 +00:00
|
|
|
} ButtonSource;
|
2024-07-08 00:44:20 +00:00
|
|
|
|
|
|
|
|
2024-09-23 17:43:16 +00:00
|
|
|
union ButtonCode {
|
2024-07-08 00:44:20 +00:00
|
|
|
SDL_Scancode scancode;
|
|
|
|
SDL_Keycode keycode;
|
|
|
|
SDL_GameControllerButton gamepad_button;
|
|
|
|
uint8_t mouse_button; /* SDL_BUTTON_ enum */
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/* an input to which an action is bound */
|
|
|
|
/* it is not limited to literal buttons */
|
2024-09-23 17:43:16 +00:00
|
|
|
typedef struct Button {
|
|
|
|
enum ButtonSource source;
|
|
|
|
union ButtonCode code;
|
|
|
|
} Button;
|
2024-07-08 00:44:20 +00:00
|
|
|
|
|
|
|
|
|
|
|
/* represents the collective state of a group of buttons */
|
|
|
|
/* that is, changes in the states of any of the bound buttons will affect it */
|
2024-09-23 17:43:16 +00:00
|
|
|
typedef struct Action {
|
2024-07-08 00:44:20 +00:00
|
|
|
size_t num_bindings;
|
2024-07-30 21:05:28 +00:00
|
|
|
|
2024-10-01 00:13:58 +00:00
|
|
|
/* if you bind more than the number set in the configuration file */
|
2024-09-23 17:43:16 +00:00
|
|
|
/* it forgets the first Button to add the new one at the end */
|
2024-10-01 00:13:58 +00:00
|
|
|
Button *bindings;
|
2024-07-30 21:05:28 +00:00
|
|
|
|
2024-09-23 17:43:16 +00:00
|
|
|
Vec2 position; /* set if applicable, e.g. mouse click */
|
2024-07-08 00:44:20 +00:00
|
|
|
bool is_pressed;
|
|
|
|
bool just_changed;
|
2024-09-23 17:43:16 +00:00
|
|
|
} Action;
|
2024-07-08 00:44:20 +00:00
|
|
|
|
|
|
|
|
2024-09-23 17:43:16 +00:00
|
|
|
typedef struct ActionHashItem {
|
2024-07-08 00:44:20 +00:00
|
|
|
char *key;
|
2024-09-23 17:43:16 +00:00
|
|
|
Action value;
|
|
|
|
} ActionHashItem;
|
2024-07-08 00:44:20 +00:00
|
|
|
|
|
|
|
|
2024-09-23 17:43:16 +00:00
|
|
|
typedef struct InputState {
|
|
|
|
ActionHashItem *action_hash;
|
2024-07-08 00:44:20 +00:00
|
|
|
const uint8_t *keyboard_state; /* array of booleans indexed by scancode */
|
|
|
|
uint32_t mouse_state; /* SDL mouse button bitmask */
|
2024-09-23 17:43:16 +00:00
|
|
|
Vec2i mouse_window_position;
|
|
|
|
Vec2i mouse_relative_position;
|
|
|
|
ButtonSource last_active_source;
|
2024-07-08 00:44:20 +00:00
|
|
|
bool is_anything_just_pressed;
|
2024-09-23 17:43:16 +00:00
|
|
|
} InputState;
|
2024-07-08 00:44:20 +00:00
|
|
|
|
|
|
|
|
2024-09-23 17:43:16 +00:00
|
|
|
TWN_API void input_state_init(InputState *input);
|
|
|
|
TWN_API void input_state_deinit(InputState *input);
|
|
|
|
TWN_API void input_state_update(InputState *input);
|
2024-07-08 00:44:20 +00:00
|
|
|
|
2024-09-23 17:43:16 +00:00
|
|
|
TWN_API void input_bind_action_scancode(InputState *input,
|
2024-07-08 00:44:20 +00:00
|
|
|
char *action_name,
|
|
|
|
SDL_Scancode scancode);
|
2024-09-23 17:43:16 +00:00
|
|
|
TWN_API void input_unbind_action_scancode(InputState *input,
|
2024-07-08 00:44:20 +00:00
|
|
|
char *action_name,
|
|
|
|
SDL_Scancode scancode);
|
2024-09-23 17:43:16 +00:00
|
|
|
TWN_API void input_bind_action_mouse(InputState *input,
|
2024-07-08 00:44:20 +00:00
|
|
|
char *action_name,
|
|
|
|
uint8_t mouse_button);
|
2024-09-23 17:43:16 +00:00
|
|
|
TWN_API void input_unbind_action_mouse(InputState *input,
|
2024-07-08 00:44:20 +00:00
|
|
|
char *action_name,
|
|
|
|
uint8_t mouse_button);
|
|
|
|
|
2024-09-23 17:43:16 +00:00
|
|
|
TWN_API void input_add_action(InputState *input, char *action_name);
|
|
|
|
TWN_API void input_delete_action(InputState *input, char *action_name);
|
2024-07-08 00:44:20 +00:00
|
|
|
|
2024-09-23 17:43:16 +00:00
|
|
|
TWN_API bool input_is_action_pressed(InputState *input, char *action_name);
|
|
|
|
TWN_API bool input_is_action_just_pressed(InputState *input, char *action_name);
|
|
|
|
TWN_API bool input_is_action_just_released(InputState *input, char *action_name);
|
2024-07-30 21:05:28 +00:00
|
|
|
|
2024-09-23 17:43:16 +00:00
|
|
|
TWN_API Vec2 input_get_action_position(InputState *input, char *action_name);
|
2024-07-08 00:44:20 +00:00
|
|
|
|
2024-09-23 17:43:16 +00:00
|
|
|
TWN_API void input_set_mouse_captured(InputState *input, bool value);
|
|
|
|
TWN_API bool input_is_mouse_captured(InputState *input);
|
2024-07-08 00:44:20 +00:00
|
|
|
|
|
|
|
#endif
|