twn_input: singleton rework, twn_control.h and fixes

This commit is contained in:
2024-10-08 10:12:30 +03:00
parent aef3f6444e
commit 0ede612bec
17 changed files with 677 additions and 647 deletions

View File

@ -1,5 +1,5 @@
#ifndef INPUT_INTERNAL_API_H
#define INPUT_INTERNAL_API_H
#ifndef TWN_INPUT_C_H
#define TWN_INPUT_C_H
#include "twn_input.h"
#include "twn_vec.h"
@ -15,6 +15,15 @@ union ButtonCode {
};
typedef enum ButtonSource {
BUTTON_SOURCE_NOT_SET,
BUTTON_SOURCE_KEYBOARD_PHYSICAL,
BUTTON_SOURCE_KEYBOARD_CHARACTER,
BUTTON_SOURCE_GAMEPAD,
BUTTON_SOURCE_MOUSE,
} ButtonSource;
/* an input to which an action is bound */
/* it is not limited to literal buttons */
typedef struct Button {
@ -23,6 +32,45 @@ typedef struct Button {
} Button;
/* represents the collective state of a group of buttons */
/* that is, changes in the states of any of the bound buttons will affect it */
typedef struct Action {
size_t num_bindings;
/* if you bind more than the number set in the configuration file */
/* it forgets the first Button to add the new one at the end */
Button *bindings;
Vec2 position; /* set if applicable, e.g. mouse click */
bool is_pressed;
bool just_changed;
} Action;
typedef struct ActionHashItem {
char *key;
Action value;
} ActionHashItem;
typedef struct InputState {
const uint8_t *keyboard_state; /* array of booleans indexed by scancode */
ActionHashItem *action_hash;
Vec2i mouse_window_position;
Vec2i mouse_relative_position;
uint32_t mouse_state; /* SDL mouse button bitmask */
ButtonSource last_active_source;
bool is_anything_just_pressed;
bool mouse_captured;
} InputState;
void input_state_init(InputState *input);
void input_state_deinit(InputState *input);
void input_state_update(InputState *input);
void input_reset_state(InputState *input);
#endif