-
Notifications
You must be signed in to change notification settings - Fork 0
/
inputmanager.d
272 lines (242 loc) · 6.94 KB
/
inputmanager.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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
/// Input Manager
module jext.inputmanager;
//#unused
//#unused
//#unused
//#character adder
import std.c.stdio;
import jext.all;
/**
* Handles key presses, and navigating(sp) the text
*/
class InputManager {
public:
/// ctor with a LetterManager object
this( LetterManager letterManager ) {
this.letterManager = letterManager;
pos = letterManager.letters.length - 1;
}
/// Set the common base of LetterMan and this class
void setLetterBase( LetterBase letterBase ) {
this.letterBase = letterBase;
}
/// Main function for recieving key presses
dchar doInput() {
int c = 0;
c = readkey();
void directional() {
poll_input();
auto wait = false;
if ( key[ ALLEGRO_KEY_LCTRL ] || key[ ALLEGRO_KEY_RCTRL ] ) {
wait = true;
if ( key[ ALLEGRO_KEY_C ] && letterManager.count > 0 ) {
int i = 0;
for( i = letterManager.count() - 1;
i >= 0 && letterManager.letters[ i ].lock == false; --i )
{}
debug
mixin( traceLine( "/+ copy: +/ i" ) );
letterManager.copiedText = letterManager.getText()[ i + 1.. $ ];
}
if ( key[ ALLEGRO_KEY_V ] && letterManager.count > 0 ) {
int i = 0;
for( i = letterManager.count() - 1;
i >= 0 && letterManager.letters[ i ].lock == false; --i )
{}
debug
mixin( traceLine( "/+ paste: +/ i" ) );
letterManager.setText( letterManager.getText()[ 0 .. i + 1 ]
~ letterManager.copiedText );
foreach( index;
0 .. letterManager.count - letterManager.copiedText.length )
letterManager[ index ].lock = true;
pos = letterManager.count - 1;
}
if ( key[ ALLEGRO_KEY_LEFT ] ) {
if ( letterManager.letters[ pos ].lock != true ) {
int i = 0;
for( i = pos - 1;
i > -1 && letterManager.letters[ i ].letter != ' '
&& letterManager.letters[ i ].lock == false; --i )
{}
if ( pos > -1 )
pos = i;
}
}
if ( key[ ALLEGRO_KEY_RIGHT ] ) {
int i = 0;
for( i = pos + 1;
i < letterManager.letters.length &&
letterManager.letters[ i ].letter != ' ' ; ++i )
{}
if ( i < letterManager.letters.length )
pos = i;
else
pos = letterManager.letters.length - 1;
}
if ( key[ ALLEGRO_KEY_HOME ] ) {
int i = pos;
for( i = pos; i >= -1 && letterManager.letters[ i ].lock == false; --i )
{}
pos = i;
}
if ( key[ ALLEGRO_KEY_END ] )
pos = letterManager.letters.length - 1;
} else { // not control down
if ( key[ ALLEGRO_KEY_LEFT ] && letterManager.count > 0 ) {
--pos;
if ( pos == -2 )
pos = -1;
if ( letterManager.letters[ pos + 1 ].lock == true )
++pos;
wait = true;
}
if ( key[ ALLEGRO_KEY_RIGHT ] ) {
++pos;
if ( pos >= letterManager.letters.length )
--pos;
wait = true;
}
if ( key[ ALLEGRO_KEY_UP ] && letterManager.count > 0 ) {
with( letterManager ) {
int lastPos = pos;
int xpos = cast(int)letters[ pos ].xpos,
ypos = cast(int)letters[ pos ].ypos - g_height;
//int last = ypos; //#unused
auto bingo = false;
foreach( i, l; letters[ 0 .. pos ] ) {
if ( l.xpos == xpos && l.ypos == ypos ) {
pos = i;
bingo = true;
break;
}
} // foreach
if ( bingo == false ) {
for( int i = pos; i >= 0; --i )
if ( letters[ i ].letter == g_lf ) {
pos = i;
if ( pos != 0 )
--pos;
break;
}
}
if ( count > 0 && letters[ pos + 1 ].lock == true )
pos = lastPos;
} // with
wait = true;
} // key up
if ( key[ ALLEGRO_KEY_DOWN ] && letterManager.count > 0 ) {
with( letterManager ) {
int xpos = cast(int)letters[ pos ].xpos,
ypos = cast(int)letters[ pos ].ypos + g_height;
//int last = ypos; //#unused
auto bingo = false;
foreach( i, l; letters[ pos .. $ ] ) {
if ( l.xpos == xpos && l.ypos == ypos ) {
pos = pos + i;
bingo = true;
break;
}
} // foreach
if ( bingo == false ) {
for( int i = pos; i < letters.length; ++i ) {
if ( ( ypos + g_height == letters[i].ypos &&
letters[i].letter == g_lf )
|| i == letters.length - 1 ) {
pos = i;
if ( i != letters.length - 1 && pos - 1 > -1 )
--pos;
break;
}
}
}
} // with
wait = true;
} // key down
} // if not control pressed
if ( wait )
poll_input_wait();
}
directional();
if ( c ) {
auto doPut = false;
//#character adder
if ( chr( c ) >= 32 && ! tkey( c, ALLEGRO_KEY_DELETE ) ) {
doPut = true;
with( letterManager ) {
//insert letter
// pos = -1
// Bd press a -> aBc
// # #
//mixin( traceLine( "pos letters.length".split ) );
letters = letters[ 0 .. pos + 1 ] ~
new Letter( chr( c ) ) ~ letters[ pos + 1 .. $ ];
++pos;
placeLetters();
}
}
if ( tkey( c, ALLEGRO_KEY_ENTER ) || tkey( c, ALLEGRO_KEY_PAD_ENTER ) ) {
with( letterManager ) {
//letters = letters[ 0 .. pos + 1 ] ~ new Letter( g_cr ) ~ new Letter( g_lf ) ~ letters[ pos + 1 .. $ ];
letters = letters[ 0 .. pos + 1 ] ~
new Letter( g_lf ) ~
letters[ pos + 1 .. $ ];
pos += 1;
placeLetters();
}
}
if ( tkey( c, ALLEGRO_KEY_BACKSPACE ) && pos > -1
&& letterManager.letters[ pos ].lock == false ) {
doPut = true;
version( Terminal )
write( " \b" );
with( letterManager )
letters = letters[ 0 .. pos ] ~ letters[ pos + 1 .. $ ];
--pos;
letterManager.placeLetters();
}
//Suck - it sucks (letters that is)
if ( tkey( c, ALLEGRO_KEY_DELETE )
&& pos != letterManager.count - 1 ) {
// pos = 0
// a*Bc press del -> ac
// # #
with( letterManager )
letters = letters[ 0 .. pos + 1 ] ~ letters[ pos + 2 .. $ ],
placeLetters();
}
version( Terminal ) {
if ( doPut )
write( cast(char)c ~ "#\b" );
std.stdio.stdout.flush;
}
}
return chr( c ); //#unused
}
/// Draw cursor
void draw() {
double xpos;
double ypos;
if ( letterManager.letters.length > 0 && pos > -1 ) {
xpos = letterManager[ pos ].xpos;
ypos = letterManager[ pos ].ypos;
} else {
xpos = -g_width;
ypos = 0;
}
if ( xpos + g_width >= DISPLAY_W )
xpos = -g_width,
ypos += g_height;
al_draw_filled_rectangle(
letterManager.square.xpos + xpos + g_width + 1, letterManager.square.ypos + ypos,
letterManager.square.xpos + xpos + g_width * 2 + 1, letterManager.square.ypos + ypos + g_height,
al_map_rgba( 0, 0, 255, 128 ) );
}
@property ref auto letterBase() { return m_letterBase; } /// access Letter Base object
@property ref auto letterManager() { return m_letterManager; } /// access Letter Manager object
@property ref auto pos() { return m_pos; } /// access cursor position
private:
int m_pos;
LetterBase m_letterBase;
LetterManager m_letterManager;
}