ToyScenarioHandler.cpp
23.1 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
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
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
//
// ToyScenarioHandler.cpp
// SteveMaggieCpp
//
// Created by Katarzyna Kalinowska-Górska on 23.05.2017.
//
//
#include <stdio.h>
#include "ToyScenarioHandler.h"
#include "ToySoundsRepo.h"
#include "ToyJSONParseUtils.h"
#include "ToyActionParser.h"
#include "ToyParentScene.h"
#include "ToyRepeatedActionScheduler.h"
#include "ToyMathUtils.h"
#include "ToyResourceUtilities.h"
#include "ToyValueStorage.h"
#include "ToyTimeIndicatorInterface.h"
#include "ToyRatePromptHandler.h"
const std::string ToyScenarioHandler::DEMO_STATE_UNINITIALIZED = "UNINITIALIZED";
const std::string ToyScenarioHandler::DEMO_STATE_PLAYING = "PLAYING";
const std::string ToyScenarioHandler::DEMO_STATE_FINISHED = "FINISHED";
const std::string ToyScenarioHandler::DEMO_STATE_NEVER_PLAYED = "NEVER_PLAYED";
ToyScenarioHandler::ToyScenarioHandler(ToyParentScene* scene)
{
_scene = scene;
_soundsResFolder = "";
_altSoundsResFolder = "";
_cachedJSONData = NULL;
_cachedJSONString = "";
static int valueStorageKeyNumber = 0;
valueStorageKeyNumber = (valueStorageKeyNumber + 1)%100;
_valueStorageContainerName = "ScenarioHandlerValues" + std::to_string(valueStorageKeyNumber);
_shouldSkipLayoutReloadOnFastForward = false;
this->clearSetup();
}
ToyScenarioHandler::~ToyScenarioHandler()
{
this->end();
}
void ToyScenarioHandler::clearSetup()
{
_scene->clearTouchHandlers();
_scene->resetLastUserTouchTimes();
_lastActionIndex = -1;
_currentCompletingActionTags.clear();
_scenarioPaused = false;
_scenarioBarred = false;
_scenarioFinished = false;
_predefinedActions.clear();
_loopActionCounters.clear();
_storedSoundIds.clear();
_demoState = DEMO_STATE_UNINITIALIZED;
this->clearAllScheduledActions();
for(auto pair : _predefinedActions){
delete pair.second;
}
_predefinedActions.clear();
for(auto action : _actionsSequence){
delete action;
}
_actionsSequence.clear();
//TODO --> cleanup
}
void ToyScenarioHandler::end() //TODO: clear steup?
{
this->clearSetup();
if(_cachedJSONData){
delete _cachedJSONData;
_cachedJSONData = nullptr;
}
_cachedJSONString = "";
}
void ToyScenarioHandler::loadScenarioFromJSONString(std::string jsonString, bool skipDemoActions)
{
_cachedJSONString = jsonString;
rapidjson::Document doc;
doc.Parse(jsonString.c_str());
this->loadScenarioFromJSONObject(doc, skipDemoActions);
}
// handling scenario and actions
void ToyScenarioHandler::loadScenarioFromJSONObject(const rapidjson::Value& jsonObject, bool skipDemoActions, bool isReplay)
{
rapidjson::Document* copy = new rapidjson::Document(); //in case we pass _cachedJSONData as jsonObject
copy->CopyFrom(jsonObject, copy->GetAllocator());
if(_cachedJSONData){
delete _cachedJSONData;
}
_cachedJSONData = copy;
for(auto pair : _predefinedActions){
delete pair.second;
}
_predefinedActions.clear();
_soundsResFolder = (*_cachedJSONData)["soundResFolder"]["path"].GetString();
_soundsResFolder = ToyResourceUtilities::getInstance().getFullPathForDownloadedFile(_soundsResFolder, false);
_altSoundsResFolder = "";
if(ToyJSONParseUtils::hasMemberString(((*_cachedJSONData)["soundResFolder"]), "alternativePath")){
_altSoundsResFolder = (*_cachedJSONData)["soundResFolder"]["alternativePath"].GetString();
_altSoundsResFolder = ToyResourceUtilities::getInstance().getFullPathForDownloadedFile(_altSoundsResFolder, false);
}
if(ToyJSONParseUtils::hasMemberObject((*_cachedJSONData), "predefinedActions")){
const auto& predefinedActionsData = (*_cachedJSONData)["predefinedActions"];
for(const auto& pair : predefinedActionsData.GetObject()){
rapidjson::Value* val = new rapidjson::Value();
val->CopyFrom(pair.value, _cachedJSONData->GetAllocator()); //!!!TODO crashes? should crash on using predefined aciton probably?
std::string name = pair.name.GetString();
_predefinedActions.insert({name, val});
}
}
for(auto action : _actionsSequence){
delete action;
}
_actionsSequence.clear();
int nDemoActions = 0;
auto actionsArrayJSON = (*_cachedJSONData)["actions"].GetArray();
for(int i = 0; i < actionsArrayJSON.Size(); ++i){
if(ToyJSONParseUtils::checkMemberBool(actionsArrayJSON[i], "demoAction", true)){
++nDemoActions;
if(skipDemoActions){
continue;
}
} else if(!skipDemoActions && ToyJSONParseUtils::checkMemberBool(actionsArrayJSON[i], "doNotPlayAfterDemo", true)){
continue;
} else if(!isReplay && ToyJSONParseUtils::checkMemberBool(actionsArrayJSON[i],"onReplayOnly", true)){
continue;
}
if(actionsArrayJSON[i]["actionType"] == "randomizedOrderActionSets"){
auto& actionSets = actionsArrayJSON[i]["actionSets"];
std::vector<const rapidjson::Value*> actionSetsVector;
// for(const auto& actionSet : actionSets.GetArray()){
// actionSetsVector.push_back(&actionSet);
// }
// ToyMathUtils::shuffleArray(actionSetsVector); // crashes randomly when this is run. Probably because of the behaviour of the copy constructor of rapidjson::Value objects.
std::vector<int> shuffleHelperArray; // an int array, on the other hand, can be shuffled
shuffleHelperArray.reserve(actionSets.Size());
for(int i = 0; i < actionSets.Size(); ++i){
shuffleHelperArray.push_back(i);
}
ToyMathUtils::shuffleArray(shuffleHelperArray);
auto actionSetsArray = actionSets.GetArray();
for(int i = 0; i < shuffleHelperArray.size(); ++i){
int j = shuffleHelperArray[i];
actionSetsVector.push_back(&actionSetsArray[j]);
}
for(int j = 0; j < actionSetsVector.size(); ++j){
auto actions = actionSetsVector[j]->GetArray();
for(int k = 0; k < actions.Size(); ++k){
this->prepareAction(&actions[k]);
}
}
} else {
this->prepareAction(&actionsArrayJSON[i]);
}
}
if(nDemoActions == 0){
_scene->disableFastForwardButton();
}
this->trimActions(_actionsSequence);
this->mergeSequences(_actionsSequence);
this->preloadSoundEffects(_actionsSequence);
}
void ToyScenarioHandler::prepareAction(const rapidjson::Value* action)
{
std::string actionType = (*action)["actionType"].GetString();
if(actionType == "callPredefinedAction"){
rapidjson::Value* predefinedAction = new rapidjson::Value();
predefinedAction->CopyFrom(*_predefinedActions[(*action)["actionId"].GetString()], _cachedJSONData->GetAllocator());
_actionsSequence.push_back(predefinedAction);
} else if(actionType == "callRandomPredefinedAction"){
auto actionIdsJSON = (*action)["actionIds"].GetArray();
auto randIndex = ToyMathUtils::getRandomInt(0,actionIdsJSON.Size() - 1);
rapidjson::Value* predefinedAction = new rapidjson::Value();
predefinedAction->CopyFrom(*_predefinedActions[actionIdsJSON[randIndex].GetString()], _cachedJSONData->GetAllocator());
_actionsSequence.push_back(predefinedAction);
} else {
rapidjson::Value* actionCopy = new rapidjson::Value();
actionCopy->CopyFrom(*action, _cachedJSONData->GetAllocator());
_actionsSequence.push_back(actionCopy);
}
};
void ToyScenarioHandler::trimActions(std::vector<rapidjson::Value*>& actions)
{
int i = 0;
std::vector<int> actionsToTrim;
while(i < actions.size()){
if(ToyJSONParseUtils::hasMemberInt(*actions[i], "trimPreviousActions")){
auto trimBy = (*actions[i])["trimPreviousActions"].GetInt();
for(int j = MAX(0, i - trimBy); j < i; ++j){
if(std::find(actionsToTrim.begin(), actionsToTrim.end(), j) == actionsToTrim.end()){
actionsToTrim.push_back(j);
}
}
}
++i;
}
for(int k = (int)actionsToTrim.size()-1; k >= 0; --k){
actions.erase(actions.begin() + actionsToTrim[k], actions.begin() + actionsToTrim[k] + 1);
}
}
void ToyScenarioHandler::mergeSequences(std::vector<rapidjson::Value*>& actions)
{
int i = (int)actions.size()-1;
while(i > 0){
if(ToyJSONParseUtils::checkMemberString(*actions[i], "actionType", "sequence") && ToyJSONParseUtils::checkMemberString(*actions[i-1], "actionType", "sequence")){
if(ToyJSONParseUtils::checkMemberInt(*actions[i], "trimPreviousSequence", 1)){
actions.erase(actions.begin() + i - 1, actions.begin() + i);
} else { //TODO powinno dzialac ale jak nie to to tez trzeba odkomentowac i poprawic
// rapidjson::Value newVal;
// newVal.CopyFrom(*actions[i-1], _cachedJSONData->GetAllocator());
// actions[i-1] = &newVal;
// auto arr = (*actions[i])["actions"].GetArray();
// for(int j = 0; j < arr.Size(); ++j){
// arr.PushBack((*actions[i])["actions"].GetArray()[j]);
// }
//
// actions.erase(actions.begin() + i, actions.begin() + i + 1);
}
}
--i;
}
}
void ToyScenarioHandler::reloadScenario(bool skipDemo, bool isReplay)
{
if(_cachedJSONData){
// if(_cachedJSONString != ""){
// this->loadScenarioFromJSONString(_cachedJSONString, skipDemo);
this->loadScenarioFromJSONObject(*_cachedJSONData, skipDemo, isReplay);
}
}
// scenario
void ToyScenarioHandler::runScenario()
{
if(_scenarioPaused){
return;
}
if(_scenarioFinished || (_lastActionIndex >= (int)_actionsSequence.size()-1)){
ToyActionParser::getInstance().cleanUp(this);
return;
}
auto i = _lastActionIndex+1;
auto currentAction = _actionsSequence[i];
// std::string actionType = (*currentAction)["actionType"].GetString();
// if(actionType == "wait"){
// if(i < _actionsSequence.size()-1){
// auto nextAction = _actionsSequence[i+1];
// if(ToyJSONParseUtils::hasMemberBool(*nextAction, "ignoreWaitBefore") && (*nextAction)["ignoreWaitBefore"].GetBool() == true){
// // skip action
// _lastActionIndex = i;
// if(i >= _actionsSequence.size()-1){
// _scenarioFinished = true;
// }
// this->runScenario();
// }
// }
// }
// log("action index: "+i+" action type: "+currentAction.actionType);
auto parsedAction = ToyActionParser::getInstance().parseJSONAction(*currentAction, this);
_lastActionIndex = i;
if(i >= _actionsSequence.size()-1){
_scenarioFinished = true;
}
if(parsedAction == NULL){
this->runScenario();
} else {
this->runAction(parsedAction);
}
}
void ToyScenarioHandler::pauseScenario()
{
_scenarioPaused = true;
}
void ToyScenarioHandler::resumeScenario()
{
_scenarioPaused = false;
}
void ToyScenarioHandler::resetScenario(bool skipDemo, bool reloadLayout)
{
this->pauseScenario();
this->stopAllActions();
this->clearAllScheduledActions();
_scene->unscheduleAllCallbacks();
ToySoundsRepo::stopAllSounds();
ToyActionParser::getInstance().cleanUp(this);
this->clearSetup();
if(reloadLayout){
_scene->reloadLayoutClean();
}
// _scene->removeAllChildren(); //change all references to scene to more liberal i.e. e.g. here leave these concrete lines to the scene and just put "onResetScenario" or something
// _scene->loadLayout(true); //TODO memory leaks?
if(skipDemo){
_scene->disableFastForwardButton();
} else {
skipDemo = _scene->_alwaysSkipDemo;
}
this->reloadScenario(skipDemo, skipDemo && reloadLayout); // we hit replay if we skip the demo and do not reload layuot. TODO: make it more flexible. the detcetion, that is./ maybe 3rd param in the resetScenario func.
this->runScenario();
}
// sound
void ToyScenarioHandler::preloadSoundEffects(std::vector<rapidjson::Value*> actions)
{
for (auto actionObject : actions) {
this->preloadSoundEffects(*actionObject);
}
}
void ToyScenarioHandler::preloadSoundEffects(const rapidjson::Value& actionObject)
{
if(actionObject.IsObject()){
if(ToyJSONParseUtils::hasMemberArray(actionObject, "actions")){
for(const auto& value : actionObject["actions"].GetArray()){
this->preloadSoundEffects(value);
}
}
else if(ToyJSONParseUtils::checkMemberString(actionObject, "actionType", "playSound") && ToyJSONParseUtils::checkMemberInt(actionObject, "needsPreload", 1)){
if(ToyJSONParseUtils::hasMemberString(actionObject, "sound")){
auto soundFilename = actionObject["sound"].GetString();
auto soundPath = ToyJSONParseUtils::checkMemberBool(actionObject, "useAlternativePath", true) ? _altSoundsResFolder + soundFilename : _soundsResFolder + soundFilename;
ToySoundsRepo::preloadSoundEffect(soundPath);
// CocosDenshion::SimpleAudioEngine::getInstance()->preloadEffect(soundPath.c_str());
}
}
}
}
void ToyScenarioHandler::stopAllSoundEffects()
{
ToySoundsRepo::stopAllSounds();
// CocosDenshion::SimpleAudioEngine::getInstance()->stopAllEffects();
}
//debug
//void ToyScenarioHandler::jumpToLastAction : function(){
//
// this.lastActionIndex = this.actionsSequence.length - 2;
// this.runScenario();
//
// },
// ActionParseDelegate
ToyScenarioObject* ToyScenarioHandler::getObjectByName (std::string objectName)
{
return _scene->getObjectByName(objectName);
}
ToyScenarioObject* ToyScenarioHandler::getDefaultObjectForAction(std::string actionType)
{
return _scene;
}
ToyScenarioObject* ToyScenarioHandler::getDefaultObjectForConditionCheck()
{
return _scene;
}
void ToyScenarioHandler::addNewObject(std::string objectName, ToyScenarioObject* newObject)
{
_scene->addNewObject(objectName, newObject);
}
std::string ToyScenarioHandler::getDefaultSoundsPath()
{
return _soundsResFolder;
}
std::string ToyScenarioHandler::getAlternativeSoundsPath()
{
return _altSoundsResFolder;
}
std::string ToyScenarioHandler::getToyValueStorageContainerName()
{
return _valueStorageContainerName;
}
void ToyScenarioHandler::runAction(cocos2d::Action* action)
{
_scene->runAction(action);
}
void ToyScenarioHandler::actionFinished(const rapidjson::Value& jsonActionObject)
{
this->runScenario();
}
void ToyScenarioHandler::runInstantly(std::function<void()> actionFunction)
{
actionFunction();
}
void ToyScenarioHandler::runCompletingAction(cocos2d::Action* action)
{
_currentCompletingActionTags.push_back(action->getTag());
_scene->runAction(action);
}
void ToyScenarioHandler::cancelAllCompletingActions()
{
for(const auto& actionTag : _currentCompletingActionTags){
_scene->stopActionByTag(actionTag);
}
_currentCompletingActionTags.clear();
}
void ToyScenarioHandler::schedule(std::function<void(float)> callback, std::string key, float delay)
{
_scene->schedule(callback, delay, key);
}
void ToyScenarioHandler::scheduleOnce(const std::function<void(float)> callback, float delay, std::string key)
{
_scene->scheduleOnce(callback, delay, key);
}
void ToyScenarioHandler::unschedule(std::string key)
{
_scene->unschedule(key);
}
void ToyScenarioHandler::newTouchHandler(std::string key, TouchHandlerFunction touchHandler, TouchHandlerType touchType)
{
_scene->addTouchHandler(key, touchHandler, touchType);
}
void ToyScenarioHandler::removeTouchHandler(std::string key, TouchHandlerType touchHandlerType)
{
_scene->removeTouchHandler(key, touchHandlerType);
}
long long ToyScenarioHandler::getLastScreenTouchTime()
{
return _scene->getLastScreenTouchTime();
}
int ToyScenarioHandler::getLoopActionCounter(std::string loopId)
{
if(_loopActionCounters.find(loopId) == _loopActionCounters.end()){
return -1;
}
return _loopActionCounters[loopId];
}
int ToyScenarioHandler::addNewLoopActionCounter(std::string loopId, int timesRepeated)
{
_loopActionCounters[loopId] = timesRepeated;
return _loopActionCounters[loopId];
}
int ToyScenarioHandler::decrementLoopActionCounter(std::string loopId)
{
_loopActionCounters[loopId] = _loopActionCounters[loopId] - 1;
return _loopActionCounters[loopId];
}
void ToyScenarioHandler::deleteLoopActionCounter(std::string loopId)
{
_loopActionCounters.erase(loopId);
}
void ToyScenarioHandler::setLastActionIndex(int index)
{
_lastActionIndex = index;
}
void ToyScenarioHandler::storeSoundId(std::string soundPath, unsigned int newSoundId)
{
_storedSoundIds[soundPath] = newSoundId;
}
unsigned int ToyScenarioHandler::removeStoredSoundId(std::string soundPath)
{
if(_storedSoundIds.find(soundPath) != _storedSoundIds.end()){
auto soundId = _storedSoundIds[soundPath];
_storedSoundIds.erase(soundPath);
return soundId;
}
return -1;
}
//addiitonal convenience methods
void ToyScenarioHandler::stopAllActions()
{
_scene->stopAllActions();
}
void ToyScenarioHandler::clearAllScheduledActions()
{
ToyRepeatedActionScheduler::getInstance().clearAllScheduledActions(this);
}
// ToyScenarioObject
// ToyScenarioObject
void ToyScenarioHandler::setProperty(std::string propertyName, const rapidjson::Value& newValue, ActionParseDelegate* parseDelegate)
{
if (propertyName == "demoState") {
auto value = newValue.GetString();
if(DEMO_STATE_PLAYING == value){
_demoState = DEMO_STATE_PLAYING;
}
else if(DEMO_STATE_FINISHED == value){
_demoState = DEMO_STATE_FINISHED;
}
else if(DEMO_STATE_UNINITIALIZED == value){
_demoState = DEMO_STATE_UNINITIALIZED;
}
else if(DEMO_STATE_NEVER_PLAYED == value){
_demoState = DEMO_STATE_NEVER_PLAYED;
}
} else if(propertyName == "shouldSkipLayoutReloadOnFastForward"){
_shouldSkipLayoutReloadOnFastForward = newValue.GetBool();
}
}
void ToyScenarioHandler::callFunctionByName(std::string methodName, const rapidjson::Value* arguments, ActionParseDelegate* parseDelegate, std::function<void()> callback)
{
if(methodName == "clearAllScheduledActions"){
this->clearAllScheduledActions();
} else if(methodName == "stopActionByTag"){
if((*arguments).IsArray()) {
const auto ¶ms = (*arguments).GetArray();
const auto &tag = params[0].GetInt();
_scene->stopActionByTag(tag);
}
} else if(methodName == "dismiss"){
cocos2d::Director::getInstance()->popScene();
} else if(methodName == "startTimer"){
if((*arguments).IsArray()){
const auto& params = (*arguments).GetArray();
const auto& totalTime = params[0].GetFloat();
const auto& step = params[1].GetFloat();
const auto& actionsOnProgressStep = &(params[2]); //params[1];//.GetArray();
const auto& actionsOnTimeUp = &(params[3]);
const auto& finishScenarioOnTimeUp = params[4].GetBool();
const auto& timeIndicatorName = params[5].GetString();
startTimer(totalTime, step, actionsOnProgressStep, actionsOnTimeUp, finishScenarioOnTimeUp, timeIndicatorName);
}
} else if(methodName == "stopTimer"){
stopTimer();
} else if(methodName == "pauseTimer"){
pauseTimer();
} else if(methodName == "resumeTimer"){
resumeTimer();
} else if(methodName == "countUpRateMePromptCounter"){
countUpRateMePromptCounter();
}
}
void ToyScenarioHandler::startTimer(float pTimeToFinish, float step, const rapidjson::Value* actionsOnProgressStep, const rapidjson::Value* actionsOnTimeUp, bool finishScenarioAfterTimeUp, std::string timeIndicatorObjectName){
_timerRunning = true;
_timerStep = step;
_totalTimerTime = pTimeToFinish;
_timePassed = 0;
_timeSlider = dynamic_cast<ToyTimeIndicatorInterface*>(_scene->getObjectByName(timeIndicatorObjectName));
if (_timeSlider != nullptr) {
_timeSlider->setProgress(0);
_timeSlider->startTimeAnimation(pTimeToFinish);
}
_actionsOnTimerUp.clear();
auto array = actionsOnTimeUp->GetArray();
for(int i = 0; i < array.Size(); ++i){
const rapidjson::Value& action = array[i];
rapidjson::Value* actionCopy = new rapidjson::Value();
actionCopy->CopyFrom(action, _cachedJSONData->GetAllocator());
_actionsOnTimerUp.push_back(actionCopy);
}
this->schedule([&](float timeElapsed){
_timePassed += timeElapsed;
// if (_timeSlider != nullptr) {
// _timeSlider->setProgressFrac(_timePassed / _totalTimerTime);
// }
if(_timePassed >= _totalTimerTime){
stopTimer();
pauseScenario();
stopAllActions();
clearAllScheduledActions();
_scene->unscheduleAllCallbacks(); //TODO: this doesn't work here. We still have one waitingFunction trying to be scheduled after this in the ToyRepeatedActionScheduler!!!
ToySoundsRepo::stopAllSounds();
// if (finishScenarioAfterTimeUp) {
clearSetup();
// }
simpleCopyActionsToMainSequence(_actionsOnTimerUp);
runScenario();
} /*else {
//TODO execute on timer progress actions
}*/
}, "timerStep", step);
}
void ToyScenarioHandler::stopTimer(){
if (_timeSlider != nullptr) {
_timeSlider->stopTimeAnimation();
}
_timerRunning = false;
unschedule("timerStep");
//// if(timeout){
// simpleCopyActionsToMainSequence(_actionsOnTimerUp);
//// } /*else {
// simpleCopyActionsToMainSequence(_actionsOnTimerStopped);
// }*/
}
void ToyScenarioHandler::pauseTimer(){
_timerRunning = false;
if (_timeSlider != nullptr) {
_timeSlider->stopTimeAnimation();
}
unschedule("timerStep");
}
void ToyScenarioHandler::resumeTimer(){
if(_timerRunning) return;
if (_timeSlider != nullptr) {
_timeSlider->startTimeAnimation(_totalTimerTime - _timePassed);
}
this->schedule([&](float timeElapsed){
_timePassed += timeElapsed;
if(_timePassed >= _totalTimerTime){
stopTimer();
clearSetup();
simpleCopyActionsToMainSequence(_actionsOnTimerUp);
runScenario();
}
}, "timerStep", _timerStep);
}
// review prompt
void ToyScenarioHandler::countUpRateMePromptCounter(){
ToyRatePromptHandler::countUp();
}
void ToyScenarioHandler::simpleCopyActionsToMainSequence(std::vector<rapidjson::Value*> actions){
for(int i = 0; i < actions.size(); ++i){
this->prepareAction(actions[i]);
}
}