-
Notifications
You must be signed in to change notification settings - Fork 2
/
control_handler.cpp
210 lines (194 loc) · 4.46 KB
/
control_handler.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
#include "control_handler.h"
#include "system.h"
#include "texture_manager.h"
fr::ControlHandler *fr::ControlHandler::m_instance = 0;
void fr::ControlHandler::init()
{
quit = false;
has_keyboard = false;
has_touch = false;
mouse_pos.x = 0;
mouse_pos.y = 0;
input_text = "";
is_text_input = false;
}
void fr::ControlHandler::clear()
{
}
bool fr::ControlHandler::IsKeyDown(SDL_Scancode k)
{
if (has_keyboard)
{
return key_state[k];
}
return false;
}
std::string fr::ControlHandler::HearInputText()
{
return input_text;
}
bool fr::ControlHandler::IsTextInput()
{
return is_text_input;
}
bool fr::ControlHandler::IsBackspaceDown()
{
return is_backspace_down;
}
void fr::ControlHandler::SwitchTextInput()
{
if (is_text_input)
{
SDL_StopTextInput();
is_text_input = false;
}
else
{
SDL_StartTextInput();
is_text_input = true;
}
}
bool fr::ControlHandler::IsMouseButtonDown(MouseButton k)
{
return mouse_state[k];
}
void fr::ControlHandler::GetMousePos(int &x, int &y)
{
x = mouse_pos.x / System::instance()->GetScale();
y = mouse_pos.y / System::instance()->GetScale();
}
int fr::ControlHandler::GetFingerCount()
{
return finger_state.size();
}
fr::Finger fr::ControlHandler::GetFinger(int index)
{
return finger_state[index];
}
bool fr::ControlHandler::IsQuit()
{
return quit;
}
void fr::ControlHandler::update()
{
is_backspace_down = false;
input_text = "";
for (std::vector<Finger>::iterator iter = finger_state.begin(); iter != finger_state.end();)
{
if (iter->released)
{
finger_state.erase(iter);
iter = finger_state.begin();
}
else
{
iter++;
}
}
while (SDL_PollEvent(&e))
{
switch (e.type)
{
case SDL_QUIT:
quit = true;
break;
case SDL_MOUSEMOTION:
mouse_pos.x = e.motion.x;
mouse_pos.y = e.motion.y;
break;
case SDL_MOUSEBUTTONDOWN:
if (e.button.button == SDL_BUTTON_LEFT)
{
mouse_state[MOUSEBUTTON_LEFT] = true;
}
if (e.button.button == SDL_BUTTON_MIDDLE)
{
mouse_state[MOUSEBUTTON_MIDDLE] = true;
}
if (e.button.button == SDL_BUTTON_RIGHT)
{
mouse_state[MOUSEBUTTON_RIGHT] = true;
}
break;
case SDL_MOUSEBUTTONUP:
if (e.button.button == SDL_BUTTON_LEFT)
{
mouse_state[MOUSEBUTTON_LEFT] = false;
}
if (e.button.button == SDL_BUTTON_MIDDLE)
{
mouse_state[MOUSEBUTTON_MIDDLE] = false;
}
if (e.button.button == SDL_BUTTON_RIGHT)
{
mouse_state[MOUSEBUTTON_RIGHT] = false;
}
break;
case SDL_KEYDOWN:
has_keyboard = true;
key_state = SDL_GetKeyboardState(0);
if (key_state[SDL_SCANCODE_ESCAPE])
{
quit = true;
}
if( e.key.keysym.sym == SDLK_BACKSPACE)
{
is_backspace_down = true;
}
break;
case SDL_TEXTINPUT:
input_text += e.text.text;
break;
case SDL_FINGERDOWN:
{
has_touch = true;
Finger new_finger;
new_finger.x = e.tfinger.x * System::instance()->GetWindowWidth();
new_finger.y = e.tfinger.y * System::instance()->GetWindowHeigh();
new_finger.startx = e.tfinger.x * System::instance()->GetWindowWidth();
new_finger.starty = e.tfinger.y * System::instance()->GetWindowHeigh();
new_finger.dx = e.tfinger.dx * System::instance()->GetWindowWidth();
new_finger.dy = e.tfinger.dy * System::instance()->GetWindowHeigh();
new_finger.moved = false;
new_finger.released = false;
new_finger.id = e.tfinger.fingerId;
finger_state.push_back(new_finger);
}
case SDL_FINGERMOTION:
{
FingerID load_id = e.tfinger.fingerId;
for (int i = 0; i < finger_state.size(); i++)
{
if (load_id == finger_state[i].id)
{
finger_state[i].x = e.tfinger.x * System::instance()->GetWindowWidth();
finger_state[i].y = e.tfinger.y * System::instance()->GetWindowHeigh();
finger_state[i].dx = e.tfinger.dx * System::instance()->GetWindowWidth();
finger_state[i].dy = e.tfinger.dy * System::instance()->GetWindowHeigh();
if (finger_state[i].dx != 0 || finger_state[i].dy != 0)
//檢測手指是否移動
{
finger_state[i].moved = true;
}
}
}
}
break;
case SDL_FINGERUP:
{
FingerID load_id = e.tfinger.fingerId;
for (int i = 0; i < finger_state.size(); i++)
{
if (load_id == finger_state[i].id)
{
finger_state[i].released = true;
}
}
}
break;
case SDL_WINDOWEVENT:
System::instance()->RefreshWindowSize();
break;
} //switch (e.type)
} //while (SDL_PollEvent(&e))
} //void fr::ControlHandler::update()