-
Notifications
You must be signed in to change notification settings - Fork 0
/
input.cpp
121 lines (114 loc) · 3.37 KB
/
input.cpp
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
#include "share.hpp"
#include "input.hpp"
#include "imgui-sfml/imgui-SFML.h"
#include "imgui/imgui.h"
#include "game.hpp"
#include <iostream>
typedef struct{
Input::KeyState m_keyState;
Input::KeyType m_KeyType;
long m_bind;
bool m_pressed;
bool m_released;
}Key;
static Key a_Keys[Input::GameKeys::GAME_KEYS_COUNT];
static Key* key;
static sf::Event sf_event;
void handleInput()
{
resetKeyBools();
while (sf_window.pollEvent(sf_event))
{
ImGui::SFML::ProcessEvent(sf_window, sf_event);
if (sf_event.type == sf::Event::Closed)
{
pgame->close();
}
if (sf_event.type == sf::Event::KeyPressed)
{
for (int x = 0; x < Input::GAME_KEYS_COUNT; x++)
{
if (a_Keys[x].m_KeyType != Input::KEYBOARD)continue;
if (a_Keys[x].m_bind != sf_event.key.scancode)continue;
a_Keys[x].m_pressed = true;
a_Keys[x].m_keyState = Input::DOWN;
}
}
if (sf_event.type == sf::Event::KeyReleased)
{
for (int x = 0; x < Input::GAME_KEYS_COUNT; x++)
{
if (a_Keys[x].m_KeyType != Input::KEYBOARD)continue;
if (a_Keys[x].m_bind != sf_event.key.scancode)continue;
a_Keys[x].m_released = true;
a_Keys[x].m_keyState = Input::UP;
}
}
if (sf_event.type == sf::Event::MouseButtonPressed)
{
for (int x = 0; x < Input::GAME_KEYS_COUNT; x++)
{
if (a_Keys[x].m_KeyType != Input::MOUSE)continue;
if (a_Keys[x].m_bind != sf_event.mouseButton.button)continue;
a_Keys[x].m_pressed = true;
a_Keys[x].m_keyState = Input::DOWN;
}
}
if (sf_event.type == sf::Event::MouseButtonReleased)
{
for (int x = 0; x < Input::GAME_KEYS_COUNT; x++)
{
if (a_Keys[x].m_KeyType != Input::MOUSE)continue;
if (a_Keys[x].m_bind != sf_event.mouseButton.button)continue;
a_Keys[x].m_released = true;
a_Keys[x].m_keyState = Input::UP;
}
}
if (sf_event.type == sf::Event::EventType::MouseWheelScrolled)
{
for (int x = 0; x < Input::GAME_KEYS_COUNT; x++)
{
if (a_Keys[x].m_KeyType != Input::MOUSE_WHEEL)continue;
if (a_Keys[x].m_bind != sf_event.mouseWheelScroll.delta)continue;
a_Keys[x].m_pressed = true;
}
}
}
}
void setKeyBind(Input::GameKeys key, long sf_Scancode, Input::KeyType KeyType)
{
a_Keys[key].m_bind = sf_Scancode;
a_Keys[key].m_KeyType = KeyType;
}
Input::KeyState &getKeyState(Input::GameKeys key)
{
return a_Keys[key].m_keyState;
}
long &getKeyBind(Input::GameKeys key)
{
return a_Keys[key].m_bind;
}
Input::KeyType &getKeyType(Input::GameKeys key)
{
return a_Keys[key].m_KeyType;
}
bool isKeyPressed(Input::GameKeys key)
{
return a_Keys[key].m_pressed;
}
bool isKeyReLeased(Input::GameKeys key)
{
return a_Keys[key].m_released;
}
bool isKeyDown(Input::GameKeys key)
{
return a_Keys[key].m_keyState == Input::DOWN;
}
static void resetKeyBools()
{
for (int x = 0; x < Input::GAME_KEYS_COUNT; x++)
{
a_Keys[x].m_pressed = false;
a_Keys[x].m_released = false;
}
}