-
Notifications
You must be signed in to change notification settings - Fork 0
/
output.d
195 lines (178 loc) · 4.68 KB
/
output.d
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
module sdl.event.handlers.keyboard;
import precompiler.implementations.aa;
private import sdl.loader;
private import std.algorithm, std.conv, std.datetime.stopwatch;
private import error.handler;
private import util.time;
/**
* Key handler
*/
abstract class _Key
{
/**
* Reference for the handler
*/
KeyboardHandler* keyboard;
/**
* Getting metadata about the key code
*/
KeyMetadata meta;
/**
* Only assigned by the KeyboardHandler
* invoke rebind() for changing current key
*/
int keyCode;
abstract void onDown();
abstract void onUp();
/**
* Params:
* newKeycode = New button to be assigned
*/
final void rebind(SDL_Keycode newKeycode)
{
if(newKeycode != keyCode)
ErrorHandler.assertErrorMessage(keyboard.rebind(this, newKeycode), "Key Rebind error", "Error rebinding key'" ~ newKeycode.to!char);
}
}
private class KeyMetadata
{
float lastDownTime, downTimeStamp;
float lastUpTime, upTimeStamp;
int keyCode;
bool isPressed = false;
this(int key)
{
this.keyCode = key;
}
this(SDL_Keycode key)
{
this.keyCode = key;
}
private void stampDownTime()
{
downTimeStamp = Time.getCurrentTime();
}
private void stampUpTime()
{
upTimeStamp = Time.getCurrentTime();
}
public float getDowntimeDuration()
{
if(isPressed)
return (Time.getCurrentTime() - downTimeStamp) / 1000;
return 0;
}
public float getUpTimeDuration()
{
if(!isPressed)
return (Time.getCurrentTime() - upTimeStamp) / 1000;
return 0;
}
private void setPressed(bool press)
{
if(press != isPressed)
{
if(isPressed)
{
lastDownTime = getDowntimeDuration();
stampUpTime();
}
else
{
lastUpTime = getUpTimeDuration();
stampDownTime();
}
isPressed = press;
}
}
}
/**
* Keyboard wrapper
*/
class KeyboardHandler
{
private:
listeners = map!(_Key[], [int])
listenersCount = map!(int, [int])
KeyMetadata[256] metadatas;
public:
int[256] pressedKeys;
this()
{
import std.stdio;
for(int i = 0; i < 256; i++)
metadatas[i] = new KeyMetadata(i);
pressedKeys[] = -1;
}
/**
* Will take care of not let memory fragmentation happen by switching places with current index and last
* Params:
* k = Key object reference
* kCode = New key code
* Returns: Rebinded was succesful
*/
bool rebind(_Key k, SDL_Keycode kCode)
{
_Key[] currentListener = listeners[k.keyCode];
int currentCount = listenersCount[k.keyCode];
int index = cast(int)countUntil(currentListener, k);
if(index != -1)
{
swapAt(currentListener, index, currentCount - 1);
currentListener[currentCount-1] = null;
listenersCount[k.keyCode]--;
addKeyListener(kCode, k);
}
return index != -1;
}
/**
* Will make the key being enqueued in the keyboard event distribution
* Params:
* key = Keycode for being assigned with the Key object
* k = Key object reference
*/
void addKeyListener(SDL_Keycode key, _Key k)
{
if((key in listeners) == null)
{
aaSet(listeners, key, []);
aaGet(listeners, key).reserve(4);
aaSet(listenersCount, key, 0);
}
if(aaGet(listeners, key).length != aaGet(listenersCount, key))
aaGet(listeners, key)[$ - 1] = k;
else
aaGet(listeners, key)~= k;
k.keyCode = key;
k.meta = metadatas[cast(ubyte)key];
aaGet(listenersCount, key)++;
}
void handleKeyUp(SDL_Keycode key)
{
if((key in listeners) != null)
{
metadatas[cast(ubyte)key].setPressed(false);
_Key[] keyListeners = aaGet(listeners, key);
immutable int len = aaGet(listenersCount, key);
for(int i = 0; i < len; i++)
keyListeners[i].onUp();
}
}
void handleKeyDown(SDL_Keycode key)
{
if((key in listeners) != null)
{
metadatas[cast(ubyte)key].setPressed(true);
_Key[] keyListeners = aaGet(listeners, key);
immutable int len = aaGet(listenersCount, key);
for(int i = 0; i < len; i++)
keyListeners[i].onDown();
}
}
void update()
{
foreach(key; pressedKeys)
{
}
}
}