-
Notifications
You must be signed in to change notification settings - Fork 0
/
Input.h
93 lines (87 loc) · 1.56 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
#ifndef INPUT_H
#define INPUT_H
#include "Constants.h"
#include "Geometry.h"
class Mouse {
friend class Input;
private:
int clickCount[256];
bool pressed[256];
void KeyDown(int key) {
pressed[key] = true;
clickCount[key]++;
}
void KeyUp(int key) {
pressed[key] = false;
}
public:
Matrix3x3 transform;
Vector2D position;
int x, y;
void SetPos(int x, int y) {
this->x = x;
this->y = y;
position = transform(Vector2D(x, y, 1));
}
Mouse() {
memset(clickCount, 0, sizeof(clickCount));
memset(pressed, 0, sizeof(pressed));
transform[0][0] = transform[1][1] = transform[2][2] = 1.0;
}
void Set(int code, bool state) {
if (pressed[code] != state)
if (!state) KeyUp(code);
else KeyDown(code);
}
bool IsPressed(int key) {
return pressed[key];
}
bool NeedProcess(int key) {
if (clickCount[key]) {
clickCount[key]--;
return true;
}
return false;
}
};
struct Keyboard {
friend class Input;
private:
bool pressed[512];
int clickCount[512];
void KeyDown(int key) {
pressed[key] = true;
clickCount[key]++;
}
void KeyUp(int key) {
pressed[key] = false;
}
public:
void Set(int code, bool state) {
if (pressed[code] != state)
if (!state) KeyUp(code);
else KeyDown(code);
}
Keyboard() {
memset(pressed, 0, sizeof(pressed));
memset(clickCount, 0, sizeof(clickCount));
}
bool NeedProcess(int key) {
if (clickCount[key]) {
clickCount[key]--;
return true;
}
return false;
}
bool IsPressed(int key) {
return pressed[key];
}
};
class Input
{
public:
Mouse mouse;
Keyboard keyboard;
void Update();
};
#endif