-
Notifications
You must be signed in to change notification settings - Fork 0
/
App.cpp
executable file
·374 lines (321 loc) · 8.26 KB
/
App.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
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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
#include "App.h"
#include <nanogui/label.h>
#include <nanogui/layout.h>
#include "scenarios/BirdScenario.h"
#include "scenarios/BipedScenario.h"
const double gFPS = 30;
const cApp::eScene gDefaultScene = cApp::eSceneCurve;
cApp::cApp(int w, int h, const std::string& title) : nanogui::Screen(Eigen::Vector2i(w, h), title)
{
mGUIWindow = nullptr;
mSceneCombo = nullptr;
mPlayButton = nullptr;
mPlaybackSlider = nullptr;
mPrevTime = 0;
mEnableAnimation = true;
}
cApp::~cApp()
{
mScenario.reset();
}
void cApp::Init()
{
cDrawUtil::InitDrawUtil();
BuildScenario(gDefaultScene);
BuildGUI();
}
bool cApp::keyboardEvent(int key, int scancode, int action, int modifiers) {
if (Screen::keyboardEvent(key, scancode, action, modifiers))
return true;
if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS) {
setVisible(false);
return true;
}
return false;
}
bool cApp::mouseButtonEvent(const Eigen::Vector2i &p, int button, bool down, int modifiers)
{
if (nanogui::Screen::mouseButtonEvent(p, button, down, modifiers))
{
return true;
}
if (mScenario != nullptr)
{
return mScenario->MouseButtonEvent(p, button, down, modifiers);
}
return false;
}
bool cApp::mouseMotionEvent(const Eigen::Vector2i &p, const Eigen::Vector2i &rel, int button, int modifiers)
{
if (nanogui::Screen::mouseMotionEvent(p, rel, button, modifiers))
{
return true;
}
if (mScenario != nullptr)
{
return mScenario->MouseMotionEvent(p, rel, button, modifiers);
}
return false;
}
bool cApp::scrollEvent(const Eigen::Vector2i &p, const Eigen::Vector2f &rel)
{
if (nanogui::Screen::scrollEvent(p, rel))
{
return true;
}
if (mScenario != nullptr)
{
return mScenario->ScrollEvent(p, rel);
}
return false;
}
void cApp::draw(NVGcontext *ctx)
{
//Draw the user interface
Screen::draw(ctx);
}
void cApp::drawContents()
{
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LEQUAL);
Update();
DrawScenario();
}
bool cApp::resizeEvent(const Eigen::Vector2i& size)
{
bool val = nanogui::Screen::resizeEvent(size);
if (mScenario != nullptr)
{
mScenario->Resize(size);
}
return val;
}
double cApp::GetFPS() const
{
return gFPS;
}
void cApp::BuildScenario(eScene scene)
{
mScenario.reset();
switch (scene)
{
case eSceneCurve:
mScenario = std::unique_ptr<cScenario>(new cBirdScenario());
break;
case eSceneCharacter:
mScenario = std::unique_ptr<cScenario>(new cBipedScenario());
break;
default:
assert(false); // unsupported scene
break;
}
mScenario->Resize(mSize);
mScenario->Init();
}
void cApp::UpdateScenario(double time)
{
double time_elapsed = time - mPrevTime;
double time_step = 1 / GetFPS();
time_elapsed = std::min(time_elapsed, time_step);
StepScenario(time_elapsed);
mPrevTime = time;
}
void cApp::StepScenario(double time_elapsed)
{
if (mScenario != nullptr)
{
mScenario->Update(time_elapsed);
}
}
void cApp::Update()
{
if (EnableAnimation())
{
// Advances animation
UpdateScenario(glfwGetTime());
}
UpdateGUI();
}
void cApp::DrawScenario()
{
if (mScenario != nullptr)
{
mScenario->Draw();
}
}
bool cApp::EnableAnimation() const
{
bool enable = mEnableAnimation;
if (mPlayButton != nullptr)
{
enable &= mPlayButton->pushed();
}
return enable;
}
void cApp::ClearGUI()
{
if (mGUIWindow != nullptr)
{
removeChild(mGUIWindow);
}
}
void cApp::BuildGUI()
{
ClearGUI();
// Build GUI panel that will contain the interface
mGUIWindow = new nanogui::Window(this, "Scene Control");
mGUIWindow->setPosition(nanogui::Vector2i(0, 0));
mGUIWindow->setLayout(new nanogui::GroupLayout());
// Add combo box to choose between difference scenes
new nanogui::Label(mGUIWindow, "Scene", "sans-bold");
mSceneCombo = new nanogui::ComboBox(mGUIWindow, {"Bird", "Biped"});
tComboCallback scene_combo_callback = std::bind(&cApp::SceneComboCallback, this, std::placeholders::_1);
mSceneCombo->setCallback(scene_combo_callback);
mSceneCombo->setSelectedIndex(gDefaultScene);
// Add combo box to pick between different parameter files
new nanogui::Label(mGUIWindow, "Param File", "sans-bold");
mParamFileCombo = new nanogui::ComboBox(mGUIWindow);
tComboCallback param_file_combo_callback = std::bind(&cApp::ParamFileComboCallback, this, std::placeholders::_1);
mParamFileCombo->setCallback(param_file_combo_callback);
UpdateParamFileCombo();
// Add panel for playback control
new nanogui::Label(mGUIWindow, "Playback", "sans-bold");
Widget* playback = new Widget(mGUIWindow);
playback->setLayout(new nanogui::BoxLayout(nanogui::Orientation::Horizontal,
nanogui::Alignment::Middle, 0, 6));
// Step backward button
auto step_backward = new nanogui::Button(playback, "", ENTYPO_ICON_FB);
step_backward->setCallback(std::bind(&cApp::StepBackwardCallback, this));
// Play/Pause button
mPlayButton = new nanogui::Button(playback, "", ENTYPO_ICON_PAUS);
mPlayButton->setFlags(nanogui::Button::ToggleButton);
mPlayButton->setPushed(true);
mPlayButton->setChangeCallback(std::bind(&cApp::TogglePlayCallback, this, std::placeholders::_1));
// Step forward button
auto step_forward = new nanogui::Button(playback, "", ENTYPO_ICON_FF);
step_forward->setCallback(std::bind(&cApp::StepForwardCallback, this));
// Reload scene button
auto reload = new nanogui::Button(playback, "", ENTYPO_ICON_CCW);
reload->setCallback(std::bind(&cApp::Reload, this));
// Slider to show/control progress of the playback
mPlaybackSlider = new nanogui::Slider(mGUIWindow);
mPlaybackSlider->setCallback(std::bind(&cApp::PlaybackSliderCallback, this, std::placeholders::_1));
mPlaybackSlider->setFinalCallback(std::bind(&cApp::PlaybackSliderFinalCallback, this, std::placeholders::_1));
// After all GUI has been built, call refresh to reorganize everything
RefreshGUI();
}
void cApp::UpdateGUI()
{
if (mScenario != nullptr)
{
double progress = mScenario->GetPlaybackProgress();
mPlaybackSlider->setValue(static_cast<float>(progress));
}
}
void cApp::RefreshGUI()
{
performLayout();
}
void cApp::UpdateParamFileCombo()
{
// update parameter files depending on which scene is active
const auto& param_files = mScenario->GetParamFiles();
std::vector<std::string> short_param_files;
BuildShortFileNames(param_files, short_param_files);
mParamFileCombo->setItems(param_files, short_param_files);
mParamFileCombo->setSelectedIndex(0);
}
cApp::eScene cApp::GetCurrScene() const
{
eScene scene = gDefaultScene;
if (mSceneCombo != nullptr)
{
scene = static_cast<eScene>(mSceneCombo->selectedIndex());
}
return scene;
}
const std::string& cApp::GetCurrParamFile() const
{
int i = mParamFileCombo->selectedIndex();
const auto& items = mParamFileCombo->items();
const std::string& param_file = items[i];
return param_file;
}
void cApp::SceneComboCallback(int i)
{
eScene scene = static_cast<eScene>(i);
BuildScenario(scene);
UpdateParamFileCombo();
RefreshGUI();
}
void cApp::ParamFileComboCallback(int i)
{
const std::string& param_file = GetCurrParamFile();
mScenario->LoadParams(param_file);
RefreshGUI();
}
void cApp::StepBackwardCallback()
{
mPlayButton->setPushed(false);
TogglePlayCallback(false);
StepScenario(-1 / GetFPS());
}
void cApp::StepForwardCallback()
{
mPlayButton->setPushed(false);
TogglePlayCallback(false);
StepScenario(1 / GetFPS());
}
void cApp::TogglePlayCallback(bool pushed)
{
if (pushed)
{
mPlayButton->setIcon(ENTYPO_ICON_PAUS);
}
else
{
mPlayButton->setIcon(ENTYPO_ICON_PLAY);
}
}
void cApp::PlaybackSliderCallback(double val)
{
if (val >= 1)
{
val = 0.999999;
}
mScenario->SetPlaybackProgress(val);
StepScenario(0);
mEnableAnimation = false;
}
void cApp::PlaybackSliderFinalCallback(double val)
{
PlaybackSliderCallback(val);
mEnableAnimation = true;
}
void cApp::Reload()
{
BuildScenario(GetCurrScene());
mScenario->LoadParams(GetCurrParamFile());
}
void cApp::BuildShortFileNames(const std::vector<std::string>& files, std::vector<std::string>& out_names) const
{
// shorten the filepaths for display in the GUI
out_names.clear();
std::vector<std::string> short_names;
for (size_t f = 0; f < files.size(); ++f)
{
const std::string& curr_file = files[f];
int idx = 0;
for (int i = static_cast<int>(curr_file.size()) - 1; i >= 0; --i)
{
char curr_char = curr_file[i];
if (curr_char == '\\' || curr_char == '/')
{
idx = i + 1;
break;
}
}
std::string filename = curr_file.substr(idx, curr_file.size() - idx);
out_names.push_back(filename);
}
}