-
Notifications
You must be signed in to change notification settings - Fork 1
/
keyboard.cpp
226 lines (202 loc) · 3.98 KB
/
keyboard.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
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
#include "mem_check.h"
#include "keyboard.h"
#include "system.h"
#include <curses.h>
KEYBOARD :: KEYBOARD ()
{
sw=49;
s=50;
se=51;
w=52;
e=54;
nw=55;
n=56;
ne=57;
get='g';
quit='Q';
wait=53;
escape=27;
backspace=8;
readmore=' ';
readmore2='\n';
inc_fire=']';
dec_fire='[';
inventory='i';
reload='r';
options='!';
fire='f';
nearest='n';
help='?';
messagebuffer='M';
char_info='@';
look='l';
open='o';
close='c';
_throw='t';
save='S';
up='<';
down='>';
rest='R';
attack='A';
exchange='e';
activate_armor='\'';
activate_weapon=';';
explore='E';
travel='X';
show_visible='L';
}
bool KEYBOARD :: yes_no()
{
int character;
while (1)
{
character=wait_for_key();
if (character=='y' || character=='Y')
return true;
if (character=='n' || character=='N' || character==escape)
return false;
}
}
int KEYBOARD :: getkey()
{
return getkeyvalue();
}
int KEYBOARD :: ungetkey(int character)
{
return ungetch(character);
}
int KEYBOARD :: wait_for_key()
{
int character;
character=KEYBOARD_NONE;
while( character==KEYBOARD_NONE)
character=getkeyvalue();
return character;
}
string KEYBOARD :: get_name(int posx, int posy, int size)
{
char character;
char temp[2];
string imie;
string letters="abcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
temp[1]='\0';
show_cursor();
print_character(posx-1,posy,' ');
set_cursor(posx,posy);
while(1)
{
character=getkeyvalue();
if (character!=KEYBOARD_NONE)
{
temp[0] = character;
if (character==backspace && imie.size()>0)
{
imie.resize(imie.size()-1);
imie += " ";
print_text(posx,posy,imie);
imie.resize(imie.size()-1);
print_text(posx,posy,imie);
myrefresh();
}
if (character==readmore2)
break;
if (imie.size()<size && letters.find(temp)!=-1)
{
imie += temp;
print_text(posx,posy,imie);
myrefresh();
}
}
}
if (imie.size()==0)
imie = "Unnamed";
hide_cursor();
return imie;
}
int KEYBOARD :: get_value(int posx, int posy, int size)
{
char character;
char temp[2];
string value;
string letters="0123456789";
temp[1]='\0';
show_cursor();
print_character(posx-1,posy,' ');
set_cursor(posx,posy);
while(1)
{
character=getkeyvalue();
if (character!=KEYBOARD_NONE)
{
temp[0] = character;
if (character==backspace && value.size()>0)
{
value.resize(value.size()-1);
value += " ";
print_text(posx,posy,value);
value.resize(value.size()-1);
print_text(posx,posy,value);
myrefresh();
}
if (character==readmore2)
break;
if (value.size()<size && letters.find(temp)!=-1)
{
value += temp;
print_text(posx,posy,value);
myrefresh();
}
}
}
if (value.size()==0)
return -1;
char *stopstring;
int l = strtol( value.c_str(), &stopstring, 10 );
hide_cursor();
return l;
}
// Pobiera direction i zwraca *dx z przedzialu (-1,1) i tak samo *dy;
int KEYBOARD :: get_direction(int &dx, int &dy)
{
int key;
dx=0;
dy=0;
key=this->getkey();
if (key==this->n)
{
dy--;
}
else if (key==this->ne)
{
dy--;
dx++;
}
else if (key==this->e)
{
dx++;
}
else if (key==this->se)
{
dx++;
dy++;
}
else if (key==this->s)
{
dy++;
}
else if (key==this->sw)
{
dy++;
dx--;
}
else if (key==this->w)
{
dx--;
}
else if (key==this->nw)
{
dx--;
dy--;
}
return key;
}