-
Notifications
You must be signed in to change notification settings - Fork 4
/
input.h
97 lines (77 loc) · 3.08 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
#pragma once
#include <functional>
#include <SDL_assert.h>
#include "magic_enum.h"
#include "raw_input.h"
#include "../src/input_conf.h"
// Action-based input
struct Input {
static const int kMaxPlayers;
static vec GetAnalog(int player, AnalogInput k) {
SDL_assert(player < kMaxPlayers);
return analog_states[player][int(k)];
}
static bool IsPressed(int player, GameKeys k) {
SDL_assert(player < kMaxPlayers);
return (action_states[player][int(k)] == PRESSED || action_states[player][int(k)] == JUST_PRESSED);
}
static bool IsPressedAnyPlayer(GameKeys k) {
for (int i = 0; i < kMaxPlayers; i++) {
if (IsPressed(i, k)) return true;
}
return false;
}
static bool IsJustPressedAnyPlayer(GameKeys k) {
for (int i = 0; i < kMaxPlayers; i++) {
if (IsJustPressed(i, k)) return true;
}
return false;
}
static bool IsReleased(int player, GameKeys k) {
SDL_assert(player < kMaxPlayers);
return (action_states[player][int(k)] == RELEASED || action_states[player][int(k)] == JUST_RELEASED);
}
// True for one frame after the action is pressed
static bool IsJustPressed(int player, GameKeys k) {
SDL_assert(player < kMaxPlayers);
return (action_states[player][int(k)] == JUST_PRESSED);
}
// True for `interval` time after the key is pressed
static bool IsJustPressed(int player, GameKeys k, float interval) {
SDL_assert(player < kMaxPlayers);
return action_states[player][int(k)] == JUST_PRESSED || (action_states[player][int(k)] == PRESSED && action_times[player][int(k)] < interval);
}
// True for one frame after the action is pressed
static bool IsJustReleased(int player, GameKeys k) {
SDL_assert(player < kMaxPlayers);
return (action_states[player][int(k)] == JUST_RELEASED);
}
// True for `interval` time after the key is pressed
static bool IsJustReleased(int player, GameKeys k, float interval) {
SDL_assert(player < kMaxPlayers);
return action_states[player][int(k)] == JUST_RELEASED || (action_states[player][int(k)] == RELEASED && action_times[player][int(k)] < interval);
}
// Consume a just pressed event so the next call to IsJustPressed for that action returns false
static void ConsumeJustPressed(int player, GameKeys k) {
SDL_assert(player < kMaxPlayers);
action_states[player][int(k)] = PRESSED;
action_times[player][int(k)] += 1000.f;
}
// Consume a just released event so the next call to IsJustReleased for that action returns false
static void ConsumeJustReleased(int player, GameKeys k) {
SDL_assert(player < kMaxPlayers);
action_states[player][int(k)] = RELEASED;
action_times[player][int(k)] += 1000.f;
}
// Called from main.cpp
static void Update(float dt);
static void Init();
static void IgnoreInput(bool enable);
private:
static void MapGameKeys();
static std::function<bool(int)> action_mapping[magic_enum::enum_count<GameKeys>()];
static std::function<vec(int)> analog_mapping[magic_enum::enum_count<AnalogInput>()];
static KeyStates action_states[][magic_enum::enum_count<GameKeys>()];
static float action_times[][magic_enum::enum_count<GameKeys>()];
static vec analog_states[][magic_enum::enum_count<AnalogInput>()];
};