-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathModuleInput.cpp
More file actions
228 lines (187 loc) · 4.59 KB
/
Copy pathModuleInput.cpp
File metadata and controls
228 lines (187 loc) · 4.59 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
#include "Globals.h"
#include "Application.h"
#include "ModuleModelLoader.h"
#include "ModuleInput.h"
#include "ModuleWindow.h"
#include "ModuleCamera.h"
#include "SDL/SDL.h"
#include "Imgui/imgui.h"
#include "Imgui/imgui_impl_sdl.h"
#include "Imgui/imgui_impl_opengl3.h"
#include "GL/glew.h"
#include <assert.h>
#define MAX_KEYS 300
ModuleInput::ModuleInput()
{
keyboard = new KeyState[MAX_KEYS];
memset(keyboard, KEY_IDLE, sizeof(KeyState) * MAX_KEYS);
}
// Destructor
ModuleInput::~ModuleInput()
{}
// Called before render is available
bool ModuleInput::Init()
{
LOG("Init SDL input event system");
bool ret = true;
SDL_Init(0);
if(SDL_InitSubSystem(SDL_INIT_EVENTS) < 0)
{
LOG("SDL_EVENTS could not initialize! SDL_Error: %s\n", SDL_GetError());
ret = false;
}
return ret;
}
update_status ModuleInput::PreUpdate()
{
mouse_motion = { 0, 0 };
memset(windowEvents, false, WE_COUNT * sizeof(bool));
mouse_wheel = 0;
const Uint8* keys = SDL_GetKeyboardState(NULL);
for (int i = 0; i < MAX_KEYS; ++i)
{
if (keys[i] == 1)
{
if (keyboard[i] == KEY_IDLE)
keyboard[i] = KEY_DOWN;
else
keyboard[i] = KEY_REPEAT;
}
else
{
if (keyboard[i] == KEY_REPEAT || keyboard[i] == KEY_DOWN)
keyboard[i] = KEY_UP;
else
keyboard[i] = KEY_IDLE;
}
}
for (int i = 0; i < NUM_MOUSE_BUTTONS; ++i)
{
if (mouse_buttons[i] == KEY_DOWN)
mouse_buttons[i] = KEY_REPEAT;
if (mouse_buttons[i] == KEY_UP)
mouse_buttons[i] = KEY_IDLE;
}
SDL_PumpEvents();
SDL_Event event;
while (SDL_PollEvent(&event))
{
ImGui_ImplSDL2_ProcessEvent(&event);
switch (event.type)
{
case SDL_QUIT:
windowEvents[WE_QUIT] = true;
break;
case SDL_WINDOWEVENT:
if (event.window.event == SDL_WINDOWEVENT_RESIZED || event.window.event == SDL_WINDOWEVENT_SIZE_CHANGED)
App->window->Resize(event.window.data1, event.window.data2);
switch (event.window.event)
{
//case SDL_WINDOWEVENT_LEAVE:
case SDL_WINDOWEVENT_HIDDEN:
case SDL_WINDOWEVENT_MINIMIZED:
case SDL_WINDOWEVENT_FOCUS_LOST:
windowEvents[WE_HIDE] = true;
break;
//case SDL_WINDOWEVENT_ENTER:
case SDL_WINDOWEVENT_SHOWN:
case SDL_WINDOWEVENT_FOCUS_GAINED:
case SDL_WINDOWEVENT_MAXIMIZED:
case SDL_WINDOWEVENT_RESTORED:
windowEvents[WE_SHOW] = true;
break;
default:
break;
}
break;
case SDL_MOUSEBUTTONDOWN:
mouse_buttons[event.button.button - 1] = KEY_DOWN;
break;
case SDL_MOUSEBUTTONUP:
mouse_buttons[event.button.button - 1] = KEY_UP;
break;
case SDL_MOUSEMOTION:
mouse_motion.x = (float)event.motion.xrel;
mouse_motion.y = (float)event.motion.yrel;
mouse.x = (float)event.motion.x;
mouse.y = (float)event.motion.y;
break;
case SDL_MOUSEWHEEL:
if (event.wheel.y > 0) // scroll up
{
mouse_wheel = event.wheel.y;
App->camera->Zoom(true);
}
else if (event.wheel.y < 0) // scroll down
{
mouse_wheel = event.wheel.y;
App->camera->Zoom(false);
}
break;
case (SDL_DROPFILE):
// In case if dropped file
dropped_filedir = event.drop.file;
// Shows directory of dropped file
SDL_ShowSimpleMessageBox(
SDL_MESSAGEBOX_INFORMATION,
"File dropped on window",
dropped_filedir,
App->window->window
);
DropModelFile(dropped_filedir);
SDL_free(dropped_filedir); // Free dropped_filedir memory
break;
default:
break;
}
}
if (GetWindowEvent(EventWindow::WE_QUIT) == true || GetKey(SDL_SCANCODE_ESCAPE) == KEY_DOWN)
return UPDATE_STOP;
return UPDATE_CONTINUE;
}
// Called every draw update
update_status ModuleInput::Update()
{
return UPDATE_CONTINUE;
}
// Called before quitting
bool ModuleInput::CleanUp()
{
delete[] keyboard;
LOG("Quitting SDL input event subsystem");
SDL_QuitSubSystem(SDL_INIT_EVENTS);
return true;
}
bool ModuleInput::GetWindowEvent(EventWindow ev) const
{
return windowEvents[ev];
}
const fPoint& ModuleInput::GetMousePosition() const
{
return mouse;
}
const int ModuleInput::GetMouseWheel() const
{
return mouse_wheel;
}
void ModuleInput::DropModelFile(char * myDropped_filedir)
{
LOG("Checking if extension of dropped file is correct.");
assert(dropped_filedir != NULL);
std::string fileExt(myDropped_filedir);
std::string filedir(myDropped_filedir);
std::size_t dotFound = fileExt.find_last_of(".");
fileExt.erase(0, dotFound + 1);
if(fileExt == "fbx" || fileExt == "FBX")
{
LOG("File is .fbx: Loading model.");
//App->modelLoader->loadModel(dropped_filedir);
return;
}
LOG("File is not an .fbx. File cannot be loaded.");
return;
}
const fPoint& ModuleInput::GetMouseMotion() const
{
return mouse_motion;
}