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

@@ -4,87 +4,26 @@
#include "twn_config.h"
#include "twn_types.h"
#include "twn_engine_api.h"
#include "twn_scancode.h"
#include "twn_control.h"
#include <stdint.h>
#include <stdbool.h>
#include <stddef.h>
typedef enum ButtonSource {
BUTTON_SOURCE_NOT_SET,
BUTTON_SOURCE_KEYBOARD_PHYSICAL,
BUTTON_SOURCE_KEYBOARD_CHARACTER,
BUTTON_SOURCE_GAMEPAD,
BUTTON_SOURCE_MOUSE,
} ButtonSource;
TWN_API void input_bind_action_control(const char *action_name, Control control);
TWN_API void input_unbind_action_control(const char *action_name, Control control);
/* internal */
typedef struct Button Button;
TWN_API void input_add_action(const char *action_name);
TWN_API void input_delete_action(const char *action_name);
TWN_API bool input_is_action_pressed(const char *action_name);
TWN_API bool input_is_action_just_pressed(const char *action_name);
TWN_API bool input_is_action_just_released(const char *action_name);
/* 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;
TWN_API Vec2 input_get_action_position(const char *action_name);
/* 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;
/* TODO: don't assume SDL button mask */
typedef struct InputState {
uint32_t mouse_state; /* SDL mouse button bitmask */
Vec2i mouse_window_position;
Vec2i mouse_relative_position;
ButtonSource last_active_source;
bool is_anything_just_pressed;
/* engine state */
ActionHashItem *action_hash;
const uint8_t *keyboard_state; /* array of booleans indexed by scancode */
} InputState;
TWN_API void input_state_init(InputState *input);
TWN_API void input_state_deinit(InputState *input);
TWN_API void input_state_update(InputState *input);
TWN_API void input_bind_action_scancode(InputState *input,
char *action_name,
Scancode scancode);
TWN_API void input_unbind_action_scancode(InputState *input,
char *action_name,
Scancode scancode);
TWN_API void input_bind_action_mouse(InputState *input,
char *action_name,
uint8_t mouse_button);
TWN_API void input_unbind_action_mouse(InputState *input,
char *action_name,
uint8_t mouse_button);
TWN_API void input_add_action(InputState *input, char *action_name);
TWN_API void input_delete_action(InputState *input, char *action_name);
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);
TWN_API Vec2 input_get_action_position(InputState *input, char *action_name);
TWN_API void input_set_mouse_captured(InputState *input, bool value);
TWN_API bool input_is_mouse_captured(InputState *input);
TWN_API void input_set_mouse_captured(bool enabled);
TWN_API bool input_is_mouse_captured(void);
#endif