-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathProjectView.cpp
424 lines (366 loc) · 12.7 KB
/
ProjectView.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
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
#include "ProjectView.h"
#include "Application/Model/Scale.h"
#include "Application/Persistency/PersistencyService.h"
#include "Application/Utils/randomnames.h"
#include "Application/Views/ModalDialogs/MessageBox.h"
#include "Application/Views/ModalDialogs/RenderProgressModal.h"
#include "BaseClasses/UIActionField.h"
#include "BaseClasses/UIIntVarField.h"
#include "BaseClasses/UIStaticField.h"
#include "BaseClasses/UITempoField.h"
#include "BaseClasses/View.h"
#include "BaseClasses/ViewEvent.h"
#include "Services/Midi/MidiService.h"
#include "System/System/System.h"
#ifdef PICOBUILD
#include "hardware/watchdog.h"
#include "pico/bootrom.h"
#endif
#include <nanoprintf.h>
static void LoadCallback(View &v, ModalView &dialog) {
if (dialog.GetReturnCode() == MBL_YES) {
ViewType vt = VT_SELECTPROJECT;
ViewEvent ve(VET_SWITCH_VIEW, &vt);
((ProjectView &)v).SetChanged();
((ProjectView &)v).NotifyObservers(&ve);
}
};
static void CreateNewProjectCallback(View &v, ModalView &dialog) {
if (dialog.GetReturnCode() == MBL_YES) {
PersistencyService::GetInstance()->SaveProjectState(UNNAMED_PROJECT_NAME);
// first clear out any existing "unnamed" project
PersistencyService::GetInstance()->PurgeUnnamedProject();
// now reboot!
watchdog_reboot(0, 0, 0);
}
};
#ifdef PICOBUILD
static void BootselCallback(View &v, ModalView &dialog) {
if (dialog.GetReturnCode() == MBL_YES) {
reset_usb_boot(0, 0);
}
};
#endif
static void SaveAsOverwriteCallback(View &v, ModalView &dialog) {
bool cancelOverwrite = dialog.GetReturnCode() == MBL_CANCEL;
if (cancelOverwrite) {
return;
}
PersistencyService *persist = PersistencyService::GetInstance();
const char *projName = ((ProjectView &)v).getProjectName().c_str();
const char *oldProjName = ((ProjectView &)v).getOldProjectName().c_str();
if (persist->Save(projName, oldProjName, true) != PERSIST_SAVED) {
Trace::Error("failed to save renamed project %s [old: %s]", projName,
oldProjName);
MessageBox *mb = new MessageBox(
((ProjectView &)v), "Failed to save project", MBBF_OK | MBBF_CANCEL);
((ProjectView &)v).DoModal(mb, SaveAsOverwriteCallback);
return;
}
if (persist->SaveProjectState(projName) != PERSIST_SAVED) {
Trace::Error("Failed to save project state");
} else {
Trace::Log("PROJECTVIEW-STATIC", "OVERWROTE:%s", projName);
((ProjectView &)v).clearSaveAsFlag(); // clear flag after saving
}
}
static void PurgeCallback(View &v, ModalView &dialog) {
((ProjectView &)v).OnPurgeInstruments(dialog.GetReturnCode() == MBL_YES);
};
static void RenderStopCallback(View &v, ModalView &dialog) {
// If the user clicked OK, stop the rendering
if (dialog.GetReturnCode() == MBL_OK) {
Player *player = Player::GetInstance();
if (player->IsRunning()) {
player->Stop();
// Show cancellation message
MessageBox *cancelDialog =
new MessageBox(((ProjectView &)v), "Rendering Stopped", MBBF_OK);
((ProjectView &)v).DoModal(cancelDialog);
}
}
}
ProjectView::ProjectView(GUIWindow &w, ViewData *data) : FieldView(w, data) {
lastClock_ = 0;
lastTick_ = 0;
project_ = data->project_;
GUIPoint position = GetAnchor();
Variable *v = project_->FindVariable(FourCC::VarTempo);
UITempoField *f =
new UITempoField(FourCC::ActionTempoChanged, position, *v,
"tempo: %d [%2.2x] ", MIN_TEMPO, MAX_TEMPO, 1, 10);
fieldList_.insert(fieldList_.end(), f);
f->AddObserver(*this);
tempoField_ = f;
v = project_->FindVariable(FourCC::VarMasterVolume);
position._y += 1;
UIIntVarField *f1 =
new UIIntVarField(position, *v, "master: %d", 10, 200, 1, 10);
fieldList_.insert(fieldList_.end(), f1);
v = project_->FindVariable(FourCC::VarTranspose);
position._y += 1;
UIIntVarField *f2 =
new UIIntVarField(position, *v, "transpose: %3.2d", -48, 48, 0x1, 0xC);
fieldList_.insert(fieldList_.end(), f2);
v = project_->FindVariable(FourCC::VarScale);
// if scale name is not found, set the default chromatic scale
if (v->GetInt() < 0) {
v->SetInt(0);
}
position._y += 1;
UIIntVarField *f3 =
new UIIntVarField(position, *v, "scale: %s", 0, numScales - 1, 1, 10);
fieldList_.insert(fieldList_.end(), f3);
position._y += 2;
UIActionField *a1 = new UIActionField(
"Compact Instruments", FourCC::ActionPurgeInstrument, position);
a1->AddObserver(*this);
fieldList_.insert(fieldList_.end(), a1);
position._y += 2;
// save existing fields horizontal alignment
int xalign = position._x;
v = project_->FindVariable(FourCC::VarProjectName);
auto label =
etl::make_string_with_capacity<MAX_UITEXTFIELD_LABEL_LENGTH>("project: ");
auto defaultName = etl::make_string_with_capacity<MAX_PROJECT_NAME_LENGTH>(
UNNAMED_PROJECT_NAME);
nameField_ = new UITextField<MAX_PROJECT_NAME_LENGTH>(
*v, position, label, FourCC::ActionProjectRename, defaultName);
nameField_->AddObserver(*this);
fieldList_.insert(fieldList_.end(), nameField_);
position._y += 1;
a1 = new UIActionField("Load", FourCC::ActionLoad, position);
a1->AddObserver(*this);
fieldList_.insert(fieldList_.end(), a1);
position._x += 5;
a1 = new UIActionField("Save", FourCC::ActionSave, position);
a1->AddObserver(*this);
fieldList_.insert(fieldList_.end(), a1);
position._x += 5;
a1 = new UIActionField("New", FourCC::ActionNewProject, position);
a1->AddObserver(*this);
fieldList_.insert(fieldList_.end(), a1);
position._x += 5;
a1 = new UIActionField("Random", FourCC::ActionRandomName, position);
a1->AddObserver(*this);
fieldList_.insert(fieldList_.end(), a1);
position._x = xalign;
// Add rendering action fields
position._y += 2;
// Add a static field as a label for the render actions
UIStaticField *renderLabel = new UIStaticField(position, "Render:");
fieldList_.insert(fieldList_.end(), renderLabel);
// Position the Mixdown action field to the right of the label
position._x += 8;
a1 = new UIActionField("Mixdown", FourCC::ActionRenderMixdown, position);
a1->AddObserver(*this);
fieldList_.insert(fieldList_.end(), a1);
position._x += 8;
a1 = new UIActionField("Stems", FourCC::ActionRenderStems, position);
a1->AddObserver(*this);
fieldList_.insert(fieldList_.end(), a1);
position._x = xalign;
}
ProjectView::~ProjectView() {}
void ProjectView::ProcessButtonMask(unsigned short mask, bool pressed) {
if (!pressed)
return;
FieldView::ProcessButtonMask(mask, pressed);
if (mask & EPBM_NAV) {
if (mask & EPBM_DOWN) {
ViewType vt = VT_SONG;
ViewEvent ve(VET_SWITCH_VIEW, &vt);
SetChanged();
NotifyObservers(&ve);
}
if (mask & EPBM_UP) {
ViewType vt = VT_DEVICE;
ViewEvent ve(VET_SWITCH_VIEW, &vt);
SetChanged();
NotifyObservers(&ve);
}
} else {
if (mask & EPBM_PLAY) {
Player *player = Player::GetInstance();
player->OnStartButton(PM_SONG, viewData_->songX_, false,
viewData_->songX_);
}
};
};
void ProjectView::DrawView() {
Clear();
GUITextProperties props;
GUIPoint pos = GetTitlePosition();
SetColor(CD_NORMAL);
// Draw title
const char *title = "Project ";
DrawString(pos._x, pos._y, title, props);
Variable *v = viewData_->project_->FindVariable(FourCC::VarProjectName);
etl::string<MAX_PROJECT_NAME_LENGTH> projectName = v->GetString();
DrawString(pos._x + strlen(title), pos._y, projectName.c_str(), props);
drawBattery(props);
FieldView::Redraw();
drawMap();
};
void ProjectView::Update(Observable &, I_ObservableData *data) {
if (!hasFocus_) {
return;
}
uintptr_t fourcc = (uintptr_t)data;
UIField *focus = GetFocus();
if (fourcc != FourCC::ActionTempoChanged) {
focus->ClearFocus();
focus->Draw(w_);
w_.Flush();
focus->SetFocus();
} else {
focus = tempoField_;
}
Player *player = Player::GetInstance();
switch (fourcc) {
case FourCC::ActionPurge:
project_->Purge();
break;
case FourCC::ActionPurgeInstrument: {
MessageBox *mb =
new MessageBox(*this, "Remove unused samples?", MBBF_YES | MBBF_NO);
DoModal(mb, PurgeCallback);
break;
}
case FourCC::ActionRandomName:
char name[12];
getRandomName(name);
printf("random:%s", name);
project_->SetProjectName(name);
saveAsFlag_ = true;
break;
case FourCC::ActionSave:
if (!player->IsRunning()) {
PersistencyService *persist = PersistencyService::GetInstance();
char projName[MAX_PROJECT_NAME_LENGTH];
project_->GetProjectName(projName);
if (saveAsFlag_) {
// first need to check if project with this name already exists
if (persist->Exists(projName)) {
Trace::Error("project already exists ask user to confirm overwrite");
MessageBox *mb = new MessageBox(*this, "Overwrite EXISTING project?",
MBBF_OK | MBBF_CANCEL);
DoModal(mb, SaveAsOverwriteCallback);
return;
}
if (persist->Save(projName, oldProjName_.c_str(), saveAsFlag_) !=
PERSIST_SAVED) {
Trace::Error("failed to save project state");
MessageBox *mb =
new MessageBox(*this, "Error saving Project", MBBF_OK);
DoModal(mb);
return;
}
clearSaveAsFlag();
} else {
if (persist->Save(projName, oldProjName_.c_str(), saveAsFlag_) !=
PERSIST_SAVED) {
Trace::Error("failed to save project state");
MessageBox *mb =
new MessageBox(*this, "Error saving Project", MBBF_OK);
DoModal(mb);
return;
}
}
// all good so now persist the new project name in project state
persist->SaveProjectState(projName);
} else {
MessageBox *mb = new MessageBox(*this, "Not while playing", MBBF_OK);
DoModal(mb);
}
break;
case FourCC::ActionProjectRename:
Trace::Log("PROJECTVIEW", "Project renamed! prev name:%s",
nameField_->GetString().c_str());
saveAsFlag_ = true;
break;
case FourCC::ActionLoad: {
if (!player->IsRunning()) {
MessageBox *mb = new MessageBox(*this, "Load song and lose changes?",
MBBF_YES | MBBF_NO);
DoModal(mb, LoadCallback);
} else {
MessageBox *mb = new MessageBox(*this, "Not while playing", MBBF_OK);
DoModal(mb);
}
break;
}
case FourCC::ActionNewProject: {
MessageBox *mb = new MessageBox(*this, "New project and lose changes?",
MBBF_YES | MBBF_NO);
DoModal(mb, CreateNewProjectCallback);
break;
}
#ifdef PICOBUILD
case FourCC::ActionBootSelect: {
if (!player->IsRunning()) {
MessageBox *mb =
new MessageBox(*this, "Reboot and lose changes?", MBBF_YES | MBBF_NO);
DoModal(mb, BootselCallback);
} else {
MessageBox *mb = new MessageBox(*this, "Not while playing", MBBF_OK);
DoModal(mb);
}
break;
}
#endif
case FourCC::ActionTempoChanged:
break;
case FourCC::ActionRenderMixdown:
if (!player->IsRunning()) {
// Start playback in rendering mode with MSM_FILE
player->Start(PM_SONG, true, MSM_FILE, true);
// Show a dialog with a Stop button during rendering
RenderProgressModal *renderDialog =
new RenderProgressModal(*this, "Rendering", "Press OK to stop");
DoModal(renderDialog, RenderStopCallback);
}
break;
case FourCC::ActionRenderStems:
if (!player->IsRunning()) {
// Start playback in rendering mode with MSM_FILESPLIT
player->Start(PM_SONG, true, MSM_FILESPLIT, true);
// Show a dialog with a Stop button during rendering
RenderProgressModal *renderDialog =
new RenderProgressModal(*this, "Stems Rendering", "Press OK to stop");
DoModal(renderDialog, RenderStopCallback);
}
break;
default:
NInvalid;
break;
};
focus->Draw(w_);
isDirty_ = true;
};
void ProjectView::OnPurgeInstruments(bool removeFromDisk) {
project_->PurgeInstruments(removeFromDisk);
};
void ProjectView::OnQuit() {
ViewEvent ve(VET_QUIT_APP);
SetChanged();
NotifyObservers(&ve);
};
void ProjectView::OnFocus() {
// only store current project name for use in a "save as" operation if it's
// not already been modified by the user pending a save which is indicated
// by the saveAsFlag_ flag being set
if (!saveAsFlag_) {
oldProjName_ = getProjectName();
}
}
/// Updates the animation by redrawing the battery gauge on every clock tick
/// (~1Hz). This occurs even when playback is not active and there is no user
/// cursor navigation.
void ProjectView::AnimationUpdate() {
// redraw batt gauge on every clock tick (~1Hz) even when not playing
// and not redrawing due to user cursor navigation
GUITextProperties props;
drawBattery(props);
w_.Flush();
};