-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinput.h
117 lines (91 loc) · 2.41 KB
/
input.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
#ifndef INPUT_H
#define INPUT_H
#define INPUT_KEY_HOLD 1
#define INPUT_KEY_FREE 0
#include "ff_vector2.h"
#include "ff_stb.h"
typedef enum MouseButton_
{
INPUT_MOUSE_LEFT = SDL_BUTTON(1),
INPUT_MOUSE_MIDDLE = SDL_BUTTON(2),
INPUT_MOUSE_RIGHT = SDL_BUTTON(3)
} MouseButton;
typedef union ActionFunction_
{
void (*default_func)(void* user_data);
void (*mouse_move_func)(void* user_data, Vector2f delta_mouse);
void (*mouse_wheel_func)(void* user_data, int wheel_direction);
} ActionFunction;
typedef enum ActionCode_
{
ACTION_QUIT,
ACTION_TOGGLE_CONSOLE,
ACTION_CONFIRM_CONSOLE,
ACTION_MOUSE_MOVE,
ACTION_MOUSE_DRAG_RIGHT,
ACTION_SCROLL_WHEEL,
ACTION_FORWARD,
ACTION_BACKWARD,
ACTION_STRAFE_RIGHT,
ACTION_STRAFE_LEFT,
ACTION_TURN_RIGHT,
ACTION_TURN_LEFT,
} ActionCode;
typedef enum ActionFlag_
{
ACT_FLAG_NO_FLAG = 0,
ACT_FLAG_CONTINUOUS
} ActionFlag;
typedef struct Action_
{
ActionCode action;
ActionFunction function;
void* user_data;
ActionFlag flags;
} Action;
typedef struct Input_
{
const uint8_t* is_scancode_down;
Action* registered_actions;
uint32_t registered_actions_size;
ActionCode* disabled_actions;
uint32_t disabled_actions_size;
ActionCode* enabled_actions;
uint32_t enabled_actions_size;
float mouse_sensitivity;
int mouse_x;
int mouse_y;
uint32_t mouse_buttons;
bool text_input_enabled;
char* text_input_destination;
uint32_t text_input_buffer_size;
uint32_t text_input_character_loc;
} Input;
extern Input input;
typedef struct ActionKeyPair_
{
uint32_t keycode;
ActionCode actioncode;
} ActionKeyPair;
void init_input();
void update_input();
void set_input_actions(Action* actions, const uint32_t number_of_actions);
void set_disabled_actions(ActionCode* actions, const uint32_t number_of_actions);
bool check_disabled_actions(ActionCode action);
void set_enabled_actions(ActionCode* actions, const uint32_t number_of_actions);
bool check_enabled_actions(ActionCode action);
void on_keydown(SDL_Keycode keycode, bool continuous);
void on_mouse_movement(const Vector2f amount);
void on_mouse_wheel(const int direction);
void on_text_input(SDL_Keycode keycode);
void start_text_input(char* text_destination, uint32_t buffer_size);
void end_text_input();
static inline uint32_t is_keyheld(SDL_Keycode key)
{
return input.is_scancode_down[SDL_GetScancodeFromKey(key)];
}
static inline bool is_mouse_held(MouseButton button)
{
return input.mouse_buttons & button;
}
#endif