-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinput_app.cpp
More file actions
294 lines (267 loc) · 6.03 KB
/
input_app.cpp
File metadata and controls
294 lines (267 loc) · 6.03 KB
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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
#include "input_app.h"
#include <system/platform.h>
#include <graphics/sprite_renderer.h>
#include <graphics/font.h>
#include <input/input_manager.h>
#include <input/keyboard.h>
#include <input/sony_controller_input_manager.h>
#include <input/touch_input_manager.h>
#include <system/debug_log.h>
static const char* key_names[] =
{
"0",
"1",
"2",
"3",
"4",
"5",
"6",
"7",
"8",
"9",
"A",
"B",
"C",
"D",
"E",
"F",
"G",
"H",
"I",
"J",
"K",
"L",
"M",
"N",
"O",
"P",
"Q",
"R",
"S",
"T",
"U",
"V",
"W",
"X",
"Y",
"Z",
"F1",
"F2",
"F3",
"F4",
"F5",
"F6",
"F7",
"F8",
"F9",
"F10",
"F11",
"F12",
"F13",
"F14",
"F15",
"NUMPAD0",
"NUMPAD1",
"NUMPAD2",
"NUMPAD3",
"NUMPAD4",
"NUMPAD5",
"NUMPAD6",
"NUMPAD7",
"NUMPAD8",
"NUMPAD9",
"NUMPADENTER",
"NUMPADSTAR",
"NUMPADEQUALS",
"NUMPADMINUS",
"NUMPADPLUS",
"NUMPADPERIOD",
"NUMPADSLASH",
"ESCAPE",
"TAB",
"LSHIFT",
"RSHIFT",
"LCONTROL",
"RCONTROL",
"BACKSPACE",
"RETURN",
"LALT",
"SPACE",
"CAPSLOCK",
"NUMLOCK",
"SCROLL",
"RALT",
"AT",
"COLON",
"UNDERLINE",
"MINUS",
"EQUALS",
"LBRACKET",
"RBRACKET",
"SEMICOLON",
"APOSTROPHE",
"GRAVE",
"BACKSLASH",
"COMMA",
"PERIOD",
"SLASH",
"UP",
"DOWN",
"LEFT",
"RIGHT",
"PAGEUP",
"PAGEDOWN"
};
InputApp::InputApp(gef::Platform& platform) :
Application(platform),
sprite_renderer_(NULL),
font_(NULL),
input_manager_(NULL),
active_touch_id_(-1)
{
}
void InputApp::Init()
{
sprite_renderer_ = gef::SpriteRenderer::Create(platform_);
input_manager_ = gef::InputManager::Create(platform_);
// make sure if there is a panel to detect touch input, then activate it
if (input_manager_ && input_manager_->touch_manager() && (input_manager_->touch_manager()->max_num_panels() > 0))
input_manager_->touch_manager()->EnablePanel(0);
InitFont();
}
void InputApp::CleanUp()
{
CleanUpFont();
delete input_manager_;
input_manager_ = NULL;
delete sprite_renderer_;
sprite_renderer_ = NULL;
}
bool InputApp::Update(float frame_time)
{
fps_ = 1.0f / frame_time;
if (input_manager_)
{
input_manager_->Update();
ProcessKeyboardInput();
ProcessControllerInput();
ProcessTouchInput();
}
return true;
}
void InputApp::Render()
{
sprite_renderer_->Begin();
DrawHUD();
sprite_renderer_->End();
}
void InputApp::InitFont()
{
font_ = new gef::Font(platform_);
font_->Load("comic_sans");
}
void InputApp::CleanUpFont()
{
delete font_;
font_ = NULL;
}
void InputApp::DrawHUD()
{
if(font_)
{
// if a touch is active lets draw some text
if (active_touch_id_ != -1)
{
font_->RenderText(sprite_renderer_, gef::Vector4(touch_position_.x, touch_position_.y, -0.9f), 1.0f, 0xffffffff, gef::TJ_LEFT, "(%.1f, %.1f)", touch_position_.x, touch_position_.y);
}
// display frame rate
font_->RenderText(sprite_renderer_, gef::Vector4(850.0f, 510.0f, -0.9f), 1.0f, 0xffffffff, gef::TJ_LEFT, "FPS: %.1f", fps_);
}
}
void InputApp::ProcessKeyboardInput()
{
const gef::Keyboard* keyboard = input_manager_->keyboard();
if (keyboard)
{
// go through all the keys and print out some debug text when a key is pressed or released
for (int key_num = 0; key_num < gef::Keyboard::NUM_KEY_CODES; ++key_num)
{
if (keyboard->IsKeyPressed((gef::Keyboard::KeyCode)key_num))
gef::DebugOut("%s pressed\n", key_names[key_num]);
else if (keyboard->IsKeyReleased((gef::Keyboard::KeyCode)key_num))
gef::DebugOut("%s released\n", key_names[key_num]);
}
}
}
void InputApp::ProcessControllerInput()
{
const gef::SonyControllerInputManager* controller_input = input_manager_->controller_input();
if (controller_input)
{
const gef::SonyController* controller = controller_input->GetController(0);
if (controller)
{
// check for button presses and print out some debug text
if (controller->buttons_pressed() & gef_SONY_CTRL_CROSS)
gef::DebugOut("CROSS pressed\n");
if (controller->buttons_pressed() & gef_SONY_CTRL_SQUARE)
gef::DebugOut("SQUARE pressed\n");
if (controller->buttons_pressed() & gef_SONY_CTRL_TRIANGLE)
gef::DebugOut("TRIANGLE pressed\n");
if (controller->buttons_pressed() & gef_SONY_CTRL_CIRCLE)
gef::DebugOut("CIRCLE pressed\n");
// print out some debug text when left stick is moved
if (controller->left_stick_x_axis() != 0.0f)
gef::DebugOut("left_stick_x_axis: %f\n", controller->left_stick_x_axis());
if (controller->left_stick_y_axis() != 0.0f)
gef::DebugOut("left_stick_y_axis: %f\n", controller->left_stick_y_axis());
// print out some debug text when left stick is moved
if (controller->right_stick_x_axis() != 0.0f)
gef::DebugOut("right_stick_x_axis: %f\n", controller->right_stick_x_axis());
if (controller->right_stick_y_axis() != 0.0f)
gef::DebugOut("right_stick_y_axis: %f\n", controller->right_stick_y_axis());
}
}
}
void InputApp::ProcessTouchInput()
{
const gef::TouchInputManager* touch_input = input_manager_->touch_manager();
if (touch_input && (touch_input->max_num_panels() > 0))
{
// get the active touches for this panel
const gef::TouchContainer& panel_touches = touch_input->touches(0);
// go through the touches
for (gef::ConstTouchIterator touch = panel_touches.begin(); touch != panel_touches.end(); ++touch)
{
// if active touch id is -1, then we are currently processing a touch
if (active_touch_id_ == -1)
{
// check for the start of a new touch
if (touch->type == gef::TT_NEW)
{
active_touch_id_ = touch->id;
// do any processing for a new touch here
// we're just going to record the position of the touch
touch_position_ = touch->position;
}
}
else if (active_touch_id_ == touch->id)
{
// we are processing touch data with a matching id to the one we are looking for
if (touch->type == gef::TT_ACTIVE)
{
// update an active touch here
// we're just going to record the position of the touch
touch_position_ = touch->position;
}
else if (touch->type == gef::TT_RELEASED)
{
// the touch we are tracking has been released
// perform any actions that need to happen when a touch is released here
// we're not doing anything here apart from resetting the active touch id
active_touch_id_ = -1;
}
}
}
}
}