/
input_manager.h
167 lines (143 loc) · 4.58 KB
/
input_manager.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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
// ____________________________
// ██▀▀█▀▀██▀▀▀▀▀▀▀█▀▀█ │ ▄▄▄▄ ▄▄
// ██ ▀ █▄ ▀██▄ ▀ ▄█ ▄▀▀ █ │ ██ ██▀▄ ██▀▄ ██ █ ██▀
// █ █ █ ▀▀ ▄█ █ █ ▀▄█ █▄ │ ▄██▄ ██ █ ██▀ ▀█▄█ ▀█▄
// ▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀────────┘ ▀▀
// by Guillaume 'Aoineko' Blanchard under CC BY-SA license
//─────────────────────────────────────────────────────────────────────────────
// Advanced input manager
//─────────────────────────────────────────────────────────────────────────────
#pragma once
#include "core.h"
#include "system_port.h"
#include "input.h"
//=============================================================================
// DEFINES
//=============================================================================
// Device ID
enum IPM_DEVICE
{
IPM_DEVICE_JOYSTICK_1 = 0,
IPM_DEVICE_JOYSTICK_2,
IPM_DEVICE_KEYBOARD_1,
IPM_DEVICE_KEYBOARD_2,
//---------------------------------
IPM_DEVICE_MAX,
IPM_DEVICE_ANY,
};
// Device input ID
enum IPM_INPUT
{
IPM_INPUT_STICK = 0,
IPM_INPUT_BUTTON_A,
IPM_INPUT_BUTTON_B,
//---------------------------------
IPM_INPUT_MAX,
IPM_INPUT_ANY,
};
// Device event ID
enum IPM_EVENT
{
IPM_EVENT_CLICK = 0,
IPM_EVENT_HOLD,
IPM_EVENT_DOUBLE_CLICK,
IPM_EVENT_DOUBLE_CLICK_HOLD,
IPM_EVENT_RELEASE,
//---------------------------------
IPM_EVENT_MAX,
IPM_EVENT_ANY,
IPM_EVENT_NONE,
};
// State flag
#define IPM_STATE_OFF 0x00
#define IPM_STATE_PRESS 0x01
#define IPM_STATE_ON 0x02
#define IPM_STATE_RELEASE 0x03
#define IPM_STATE_PRESSMASK 0x03
#define IPM_STATE_HOLD 0x10
#define IPM_STATE_HOLDING 0x20
#define IPM_STATE_HOLDMASK 0x30
#define IPM_STATE_DOUBLE 0x80
#define IPM_EVENT_TAB_SIZE 16
// Callback function prototypes
typedef void (*IPM_cb)(u8 joy, u8 in, u8 evt);
typedef u8 (*IPM_check)(u8 joy, u8 in);
// IPM event structure
typedef struct
{
u8 Device;
u8 Input;
u8 Event;
IPM_cb Callback;
} IPM_Event;
// IPM process structure
typedef struct
{
u8 CurrentStatus;
u8 PreviousStatus;
u8 State[IPM_INPUT_MAX];
u8 Timer[IPM_INPUT_MAX];
} IPM_Process;
// IPM key set structure
typedef struct
{
u8 Up;
u8 Right;
u8 Down;
u8 Left;
u8 TriggerA;
u8 TriggerB;
} IPM_KeySet;
// IPM configuration structure
typedef struct
{
u8 DeviceSupport[IPM_DEVICE_MAX];
u8 HoldTimer;
u8 DoubleClickTimer;
IPM_KeySet KeySet[2];
} IPM_Config;
// IPM internal data structure
typedef struct
{
IPM_Process Process[IPM_DEVICE_MAX];
IPM_Event Events[IPM_EVENT_TAB_SIZE];
u8 EventsNum;
IPM_check Checker[IPM_EVENT_MAX];
IPM_Config Config;
} IPM_Data;
// Manager internal variables
extern IPM_Data g_IPM;
//=============================================================================
// FUNCTIONS
//=============================================================================
// Function: IPM_Initialize
// Initialize input manager
void IPM_Initialize(IPM_Config* config);
// Function: IPM_SetTimer
// Initialize input manager
inline void IPM_SetTimer(u8 doubleClk, u8 hold)
{
g_IPM.Config.DoubleClickTimer = doubleClk;
g_IPM.Config.HoldTimer = hold;
}
// Function: IPM_Update
// Update device manager
void IPM_Update();
// Function: IPM_RegisterEvent
// Register a callback to a given device manager's event
bool IPM_RegisterEvent(u8 joy, u8 input, u8 event, IPM_cb cb);
// Function: IPM_GetStatus
// Get current device status
inline u8 IPM_GetStatus(u8 dev) { return g_IPM.Process[dev].CurrentStatus; }
// Function: IPM_GetStickDirection
// Get current direction of the given device
u8 IPM_GetStickDirection(u8 joy);
// Function: IPM_GetInputState
// Get current device state
inline u8 IPM_GetInputState(u8 dev, u8 input) { return g_IPM.Process[dev].State[input]; }
// Function: IPM_GetInputTimer
// Get current device state timer
inline u8 IPM_GetInputTimer(u8 dev, u8 input) { return g_IPM.Process[dev].Timer[input]; }
// Function: IPM_GetEventName
// Get event name
const c8* IPM_GetEventName(u8 ev);