-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCInput.cpp
184 lines (162 loc) · 4.5 KB
/
CInput.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
/*
Copyright 2016, Michael R. Hoopmann, Institute for Systems Biology
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
#include "CInput.h"
CInput::CInput(){
int i;
for(i=0;i<128;i++) keyState[i]=false;
for(i=0;i<3;i++) buttonState[i]=false;
lastButton=0;
keyShift = false;
}
void CInput::add(char c, bool key){
if(key){
keyBuffer.push_back(c);
if (keyBuffer.size() == 256) keyBuffer.pop_front();
} else {
cursorBuffer.push_back(c);
if(cursorBuffer.size()==256) cursorBuffer.pop_front();
}
}
void CInput::clear(){
keyBuffer.clear();
cursorBuffer.clear();
}
char CInput::getBuffer(bool key){
if(key){
if (keyBuffer.size() > 0){
char c = keyBuffer[0];
keyBuffer.pop_front();
return c;
}
return 0;
}
if(cursorBuffer.size() > 0){
char c = cursorBuffer[0];
cursorBuffer.pop_front();
return c;
}
return 0;
}
bool CInput::getButtonState(int k){
return buttonState[k];
}
bool CInput::getKeyState(int k){
return keyState[k];
}
bool CInput::isPressed(int k){
if(keyState[k]) return true;
else return false;
}
bool CInput::isReleased(int k){
if(keyState[k]) return false;
else return true;
}
int CInput::mouseAction(){
//printf("mouseAction %d\n",lastButton);
int i=lastButton;
if(lastButton>0) lastButton=0;
return i;
}
void CInput::processEvent(SDL_Event& e){
if( e.type == SDL_KEYDOWN ) setKey(e.key.keysym.sym,true);
else if( e.type == SDL_KEYUP ) setKey(e.key.keysym.sym,false);
else if( e.type == SDL_MOUSEBUTTONDOWN ) setButton(e.button.button,true);
else if( e.type == SDL_MOUSEBUTTONUP ) setButton(e.button.button,false);
}
void CInput::setButton(Uint8 k, bool b){
switch(k){
case SDL_BUTTON_LEFT:
buttonState[0]=b;
if(!b) lastButton=1;
break;
case SDL_BUTTON_RIGHT:
buttonState[1]=b;
if(!b) lastButton=2;
break;
case SDL_BUTTON_MIDDLE:
buttonState[2]=b;
if(!b) lastButton=3;
break;
default:
break;
}
}
void CInput::setKey(SDL_Keycode k, bool b){
//Set key states first
switch (k){
case SDLK_BACKSPACE: keyState[KEY_BACKSPACE] = b; break;
case SDLK_RETURN: keyState[KEY_ENTER] = b; break;
case SDLK_0:
case SDLK_KP_0: keyState[KEY_0] = b; break;
case SDLK_1:
case SDLK_KP_1: keyState[KEY_1] = b; break;
case SDLK_2:
case SDLK_KP_2: keyState[KEY_2] = b; break;
case SDLK_3:
case SDLK_KP_3: keyState[KEY_3] = b; break;
case SDLK_4:
case SDLK_KP_4: keyState[KEY_4] = b; break;
case SDLK_5:
case SDLK_KP_5: keyState[KEY_5] = b; break;
case SDLK_6:
case SDLK_KP_6: keyState[KEY_6] = b; break;
case SDLK_7:
case SDLK_KP_7: keyState[KEY_7] = b; break;
case SDLK_8:
case SDLK_KP_8: keyState[KEY_8] = b; break;
case SDLK_9:
case SDLK_KP_9: keyState[KEY_9] = b; break;
case SDLK_q: keyState[KEY_Q] = b; break;
case SDLK_DELETE: keyState[KEY_DELETE] = b; break;
case SDLK_UP: keyState[KEY_UP] = b; break;
case SDLK_DOWN: keyState[KEY_DOWN] = b; break;
case SDLK_LEFT:
keyState[KEY_LEFT] = b;
if(b) add(1, false);
break;
case SDLK_RIGHT:
keyState[KEY_RIGHT] = b;
if(b) add(2, false);
break;
case SDLK_CAPSLOCK:
keyState[KEY_CAPS] = b;
if (keyState[KEY_CAPS]) keyShift = !keyShift;
break;
case SDLK_LSHIFT:
keyState[KEY_LSHIFT] = b;
if (keyState[KEY_LSHIFT] || keyState[KEY_RSHIFT]) keyShift = true;
else keyShift = false;
if (keyState[KEY_CAPS]) keyShift = !keyShift;
break;
case SDLK_RSHIFT:
keyState[KEY_RSHIFT] = b;
if (keyState[KEY_LSHIFT] || keyState[KEY_RSHIFT]) keyShift = true;
else keyShift = false;
if (keyState[KEY_CAPS]) keyShift = !keyShift;
break;
default: break;
}
//Buffer keypress if necessary
if (b && k<128) {
if (keyShift && k > 96 && k < 123) add((char)(k - 32));
else add((char)k);
}
if(b && k>=SDLK_KP_1 && k<=SDLK_KP_0){
if(k==SDLK_KP_0) add(48);
else add((char)k-SDLK_KP_1+49);
}
}
size_t CInput::size(bool key){
if(key) return keyBuffer.size();
return cursorBuffer.size();
}