AniParentScene.cpp
18 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
//
// AniParentScene.cpp
// SteveMaggieCpp
//
// Created by Katarzyna Kalinowska-Górska on 16.05.2017.
//
//
#include <stdio.h>
#include "AniParentScene.h"
#include "AniLayoutParser.h"
#include "AniSimpleButton.h"
#include "AniTwoStateButton.h"
#include "AniMathUtils.h"
#include "AniStringUtils.h"
#include "AniResourceUtilities.h"
#include "AniMiscUtils.h"
#include "AniTOSAcceptPopupView.h"
#include "AniLevelPickerLayer.h"
#include "AniSoundsRepo.h"
#include "HelloWorldScene.h"
AniParentScene* AniParentScene::create(std::string layoutFilePath, std::string scenarioFilePath)
{
AniParentScene * scene = new (std::nothrow) AniParentScene();
if(scene && scene->initWithConfigurationFiles(layoutFilePath, scenarioFilePath))
{
scene->autorelease();
return scene;
}
CC_SAFE_DELETE(scene);
return nullptr;
}
bool AniParentScene::initWithConfigurationFiles(std::string layoutFilePath, std::string scenarioFilePath)
{
if(!AniSceneWithUtils::init())
{
return false;
}
_layoutFilePath = layoutFilePath;
_scenarioFilePath = scenarioFilePath;
_loadFromAssets = false;
_layers.clear();
_objects.clear();
_alwaysSkipDemo = false;
_layoutLoaded = false;
// _AniScenarioHandler = new AniScenarioHandler(this);
// _AniScenarioObjects.insert({"AniScenarioHandler", _AniScenarioHandler});
_userTaskInteractionStarted = false;
_currentTask = "";
_rightObjectTouchedAlready = false; //TODO these should probably be in the WorksheetScene
_isHandlingTouches = false;
this->clearTouchHandlers();
this->resetLastUserTouchTimes();
return true;
}
void AniParentScene::reloadLayoutClean()
{
this->removeAllChildren();
_layers.clear();
_objects.clear();
_AniScenarioObjects.clear();
// _AniScenarioObjects.insert({"AniScenarioHandler", _AniScenarioHandler});
_userTaskInteractionStarted = false;
_currentTask = "";
_rightObjectTouchedAlready = false; //TODO these should probably be in the WorksheetScene
_isHandlingTouches = false;
this->clearTouchHandlers();
this->resetLastUserTouchTimes();
this->loadLayout(true);
}
void AniParentScene::loadLayout(bool forceLoad)
{
if(!this->_layoutLoaded || forceLoad){
_layoutLoaded = false;
if (_loadFromAssets && cocos2d::FileUtils::getInstance()->isFileExist(this->_layoutFilePath)) {
auto& AniLayoutParser = AniLayoutParser::getInstance();
AniLayoutParser.loadLayoutFromJSONFile(this->_layoutFilePath, this);
this->_layoutLoaded = true;
}
else if(!_loadFromAssets){
auto& AniLayoutParser = AniLayoutParser::getInstance();
AniLayoutParser.loadLayoutFromDownloadedJSONFile(this->_layoutFilePath, this);
this->_layoutLoaded = true;
}
}
}
void AniParentScene::loadScenario(bool skipDemoActions)
{
// auto fileUtils = cocos2d::FileUtils::getInstance();
// if (fileUtils->isFileExist(this->_scenarioFilePath)) {
// std::string jsonString = fileUtils->getStringFromFile(this->_scenarioFilePath);
// _AniScenarioHandler->loadScenarioFromJSONString(jsonString, skipDemoActions);
// }
}
void AniParentScene::setLoadFromAssets(bool loadFromAssets)
{
_loadFromAssets = loadFromAssets;
}
// overrides
void AniParentScene::onEnter()
{
this->configureTouchHandlers();
this->configureTransitionListeners();
AniSceneWithUtils::onEnter();
// auto playing = this->playBackgroundMusic(true);
// this->adjustBgMusicButton(playing);
}
void AniParentScene::onExit()
{
AniSceneWithUtils::onExit();
// if(_AniScenarioHandler){
// _AniScenarioHandler->end();
//// delete _AniScenarioHandler; //TODO do it somewhere when the scene gets dealloced
// }
// this->stopBackgroundMusic(false);
}
void AniParentScene::configureTransitionListeners() //TODO check if it works. remove somewhere later the vent listeners? leaks?
{
_eventDispatcher->addCustomEventListener("game_on_hide", CC_CALLBACK_1(AniParentScene::onGameHide, this));
_eventDispatcher->addCustomEventListener("game_on_show", CC_CALLBACK_1(AniParentScene::onGameShow, this));
}
void AniParentScene::onGameHide(cocos2d::EventCustom* event)
{
// _AniScenarioHandler->pauseScenario();
}
void AniParentScene::onGameShow(cocos2d::EventCustom* event)
{
// _AniScenarioHandler->resumeScenario();
}
// object config & handlers
AniScenarioObject* AniParentScene::getObjectByName(std::string objectName)
{
if(_objects.find(objectName) == _objects.end()){
if(_AniScenarioObjects.find(objectName) == _AniScenarioObjects.end()){
return NULL;
}
return _AniScenarioObjects.at(objectName);
}
return dynamic_cast<AniScenarioObject*>(_objects.at(objectName));
}
void AniParentScene::addNewObject(std::string objectName, AniScenarioObject* newObject)
{
_AniScenarioObjects.insert({objectName, newObject});
}
void AniParentScene::setupBackgroundMusic(std::string backgroundMusicPath)
{
_backgroundMusicFilePath = backgroundMusicPath;
}
bool AniParentScene::touchHandlerForWidget(std::string objectName, cocos2d::ui::Widget::TouchEventType touchEventType)
{
if(objectName == "backButton" && touchEventType == cocos2d::ui::Widget::TouchEventType::ENDED){
// _AniScenarioHandler->pauseScenario();
// _AniScenarioHandler->stopAllActions();
AniSoundsRepo::stopAllSounds();
cocos2d::Director::getInstance()->end();
_sceneDismissCallback();
return true;
} /*else if(objectName == "backgroundMusicButton" && touchEventType == cocos2d::ui::Widget::TouchEventType::ENDED){
// auto bgMusicButton = dynamic_cast<AniValueStorage*>(_objects[objectName]);
// if(this->isBackgroundMusicPlaying()){
// this->stopBackgroundMusic(true);
//
// if(bgMusicButton->isActive()){
// bgMusicButton->setActive(false);
// }
//
// } else {
// this->playBackgroundMusic(false);
//
// if(!bgMusicButton->isActive()){
// bgMusicButton->setActive(true);
// }
// }
}*/
return false;
}
void AniParentScene::adjustBgMusicButton(bool bgMusicPlaying)
{
if(_objects.find("backgroundMusicButton") != _objects.end()){
auto bgMusicButton = dynamic_cast<AniTwoStateButton*>(_objects["backgroundMusicButton"]);
if(bgMusicPlaying){
if(!bgMusicButton->isActive()){
bgMusicButton->setActive(true);
}
} else {
if(bgMusicButton->isActive()){
bgMusicButton->setActive(false);
}
}
}
}
void AniParentScene::disableButton(const std::string& buttonName)
{
auto object = this->getObjectByName(buttonName);
auto button = dynamic_cast<AniSimpleButton*>(object);
if(button){
button->setEnabled(false);
if(button->getOpacity() != 0){
button->setOpacity(128);
for(auto child : button->getChildren()){
child->setOpacity(128);
}
}
}
}
void AniParentScene::enableButton(const std::string& buttonName)
{
auto object = this->getObjectByName(buttonName);
auto button = dynamic_cast<AniSimpleButton*>(object);
if(button){
button->setEnabled(true);
button->setOpacity(255);
for(auto child : button->getChildren()){
child->setOpacity(255);
}
}
}
// fast forward button convenience
void AniParentScene::disableFastForwardButton()
{
this->disableButton("fastForwardButton");
}
void AniParentScene::enableFastForwardButton()
{
this->enableButton("fastForwardButton");
}
// touch handlers
void AniParentScene::configureTouchHandlers()
{
auto touchListener = cocos2d::EventListenerTouchOneByOne::create();
touchListener->onTouchBegan = [&](cocos2d::Touch* touch, cocos2d::Event* event) -> bool{
_isHandlingTouches = true;
for(auto handler : _touchBeganHandlers){
handler.second(touch, event);
}
_isHandlingTouches = false;
this->filterTouchHandlers();
return true;
};
touchListener->onTouchMoved = [&](cocos2d::Touch* touch, cocos2d::Event* event){
_isHandlingTouches = true;
_lastUserTouchMovedTime = cocos2d::utils::getTimeInMilliseconds();
for(auto handler : _touchMovedHandlers){
handler.second(touch, event);
}
_isHandlingTouches = false;
this->filterTouchHandlers();
};
touchListener->onTouchEnded = [&](cocos2d::Touch* touch, cocos2d::Event* event){
_isHandlingTouches = true;
_lastUserTouchEndedTime = cocos2d::utils::getTimeInMilliseconds();
for(auto handler : _touchEndedHandlers){
handler.second(touch, event);
}
_isHandlingTouches = false;
this->filterTouchHandlers();
};
_eventDispatcher->addEventListenerWithSceneGraphPriority(touchListener, this);
}
void AniParentScene::filterTouchHandlers()
{
for(auto pair : _touchHandlersToRemove){
this->removeTouchHandler(pair.first, pair.second);
}
_touchHandlersToRemove.clear();
}
void AniParentScene::clearTouchHandlers()
{
_touchBeganHandlers.clear();
_touchMovedHandlers.clear();
_touchEndedHandlers.clear();
}
void AniParentScene::addTouchHandler(std::string key, TouchHandlerFunction touchHandler, TouchHandlerType touchType)
{
auto handlersArray = this->pickTouchHandlersContainer(touchType);
if(handlersArray){
handlersArray->insert({key, touchHandler});
}
}
void AniParentScene::removeTouchHandler(std::string touchHandlerKey, TouchHandlerType touchHandlerType)
{
if(!_isHandlingTouches){
auto handlersArray = this->pickTouchHandlersContainer(touchHandlerType);
if(handlersArray){
handlersArray->erase(touchHandlerKey);
}
}
else {
_touchHandlersToRemove.insert({touchHandlerKey, touchHandlerType});
}
}
std::map<std::string,TouchHandlerFunction>* AniParentScene::pickTouchHandlersContainer(TouchHandlerType touchHandlerType)
{
std::map<std::string,TouchHandlerFunction>* handlersArray = NULL;
switch(touchHandlerType){
case TouchHandlerType::TOUCHES_BEGAN:
handlersArray = &_touchBeganHandlers;
break;
case TouchHandlerType::TOUCHES_MOVED:
handlersArray = &_touchMovedHandlers;
break;
case TouchHandlerType::TOUCHES_ENDED:
handlersArray = &_touchEndedHandlers;
break;
default:
break;
}
return handlersArray;
}
long long AniParentScene::getLastScreenTouchTime()
{
if(_lastUserTouchEndedTime != -1 && _lastUserTouchMovedTime != -1){
return MAX(_lastUserTouchEndedTime, _lastUserTouchMovedTime);
} else {
return _lastUserTouchMovedTime; // if moved is null, ended must be null as well
}
}
void AniParentScene::resetLastUserTouchTimes()
{
_lastUserTouchEndedTime = -1;
_lastUserTouchMovedTime = -1;
}
void AniParentScene::setAlwaysSkipDemo(bool alwaysSkipDemo)
{
_alwaysSkipDemo = alwaysSkipDemo;
}
// layout parse delegate
void AniParentScene::addLayer(cocos2d::Layer* layer)
{
_layers.push_back(layer);
}
void AniParentScene::addObject(std::string objectName, cocos2d::Node* object)
{
_objects.insert({objectName, object});
}
void AniParentScene::addAniScenarioObject(std::string objectName, AniScenarioObject* object)
{
_AniScenarioObjects.insert({objectName, object});
}
// AniScenarioObject
void AniParentScene::setProperty(std::string propertyName, const rapidjson::Value& newValue, ActionParseDelegate* parseDelegate)
{
static std::string demoState = "demoState";
static std::string userTaskInteractionStarted = "userTaskInteractionStarted";
static std::string currentTask = "currentTask";
static std::string rightObjectTouchedAlready = "rightObjectTouchedAlready";
if (propertyName == demoState) {
// _AniScenarioHandler->setProperty(propertyName, newValue, parseDelegate);
}
else if(propertyName == userTaskInteractionStarted){
_userTaskInteractionStarted = newValue.GetBool();
}
else if(propertyName.find(userTaskInteractionStarted) == 0){
std::string additionalName = propertyName.substr(userTaskInteractionStarted.size());
_userTaskInteractionStartedMap[additionalName] = newValue.GetBool();
}
else if(propertyName == currentTask){
_currentTask = newValue.GetString();
}
else if(propertyName == rightObjectTouchedAlready){
_rightObjectTouchedAlready = newValue.GetBool();
}
}
void AniParentScene::callFunctionByName(std::string methodName, const rapidjson::Value* arguments, ActionParseDelegate* parseDelegate, std::function<void()> callback)
{
if(methodName == "disableFastForwardButton"){
this->disableFastForwardButton();
}
else if(methodName == "clearTouchHandlers"){
this->clearTouchHandlers();
}
}
std::string AniParentScene::getPropertyAsString(std::string propertyName)
{
static std::string userTaskInteractionStarted = "userTaskInteractionStarted";
if(propertyName == userTaskInteractionStarted){
return _userTaskInteractionStarted ? "true" : "false";
}
else if(propertyName.find(userTaskInteractionStarted) != std::string::npos){
auto tokens = AniStringUtils::splitString(propertyName, userTaskInteractionStarted);
if(tokens.size() > 0){
auto key = tokens[tokens.size()-1];
if(_userTaskInteractionStartedMap.find(key) != _userTaskInteractionStartedMap.end()){
return _userTaskInteractionStartedMap.at(key) ? "true" : "false";
}
}
}
else if(propertyName == "rightObjectTouchedAlready"){
return _rightObjectTouchedAlready ? "true" : "false";
}
return "NULL";
}
void AniParentScene::addNewNodeToObjectLayer(cocos2d::Node* newNode){
if(_layers.size() > 0){
_layers[_layers.size()-1]->addChild(newNode);
}
}
void AniParentScene::showTOSAcceptPopup(std::function<void()> onAccept){
if(AniMiscUtils::wasTOSAccepted()){
// gameState->tosAcceptMenuShown = false;
onAccept();
} else {
// gameState->tosAcceptMenuShown = true;
auto tosPopup = AniTOSAcceptPopupView::create(std::bind([&](std::function<void()>f){
AniMiscUtils::saveTOSAccepted();
// gameState->tosAcceptMenuShown = false;
f();}, onAccept));
addTouchBlockingLayer(tosPopup);
tosPopup->setLocalZOrder(500);
#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
auto keyboardListener = cocos2d::EventListenerKeyboard::create();
keyboardListener->onKeyReleased = std::bind([&](cocos2d::EventKeyboard::KeyCode keyCode, cocos2d::Event* event, AniTOSAcceptPopupView* n){
if(keyCode == cocos2d::EventKeyboard::KeyCode::KEY_BACK){
if(AniMiscUtils::isNodeVisible(n)){
if (n->isShowingParentalGate()) {
n->hideParentalGate();
} else {
cocos2d::Director::getInstance()->end();
//auto scene = HelloWorld::createScene();
//cocos2d::Director::getInstance()->replaceScene(scene);
}
}
}
}, std::placeholders::_1, std::placeholders::_2, tosPopup);
_eventDispatcher->addEventListenerWithSceneGraphPriority(keyboardListener, tosPopup);
#endif
}
}
void AniParentScene::showAniLevelPickerLayer(std::function<void()> onLayerDismissed, std::function<void(int)> onLevelChanged) {
isShowingLevelPicker = true;
auto AniLevelPickerLayer = AniLevelPickerLayer::create(getBoundingBox().size.width,
getBoundingBox().size.height);
addTouchBlockingLayer(AniLevelPickerLayer);
AniLevelPickerLayer->setLocalZOrder(400);
AniLevelPickerLayer->addOnGoPressedCallback(onLayerDismissed);
AniLevelPickerLayer->setOnLevelChangedCallback(onLevelChanged);
#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
auto keyboardListener = cocos2d::EventListenerKeyboard::create();
keyboardListener->onKeyReleased = std::bind(
[&](cocos2d::EventKeyboard::KeyCode keyCode, cocos2d::Event *event, cocos2d::Node *n) {
if (keyCode == cocos2d::EventKeyboard::KeyCode::KEY_BACK) {
if (AniMiscUtils::isNodeVisible(n)) {
cocos2d::Director::getInstance()->end();
// auto scene = HelloWorld::createScene();
// cocos2d::Director::getInstance()->replaceScene(scene);
}
}
}, std::placeholders::_1, std::placeholders::_2, AniLevelPickerLayer);
_eventDispatcher->addEventListenerWithSceneGraphPriority(keyboardListener, AniLevelPickerLayer);
#endif
}
void AniParentScene::addTouchBlockingLayer(cocos2d::Node* layer){
addChild(layer);
auto touchListener = cocos2d::EventListenerTouchOneByOne::create();
touchListener->setSwallowTouches(true);
touchListener->onTouchBegan = std::bind([&](cocos2d::Touch* touch, cocos2d::Event* event, cocos2d::Node* n){
return AniMiscUtils::isNodeVisible(n);
}, std::placeholders::_1, std::placeholders::_2, layer);
_eventDispatcher->addEventListenerWithSceneGraphPriority(touchListener, layer);
}
void AniParentScene::repeatPickLevelPrompt(){
static int DelaySecs = 15;
runAction(cocos2d::Sequence::create(cocos2d::DelayTime::create(DelaySecs),
cocos2d::CallFunc::create([&](){
if(isShowingLevelPicker){
AniSoundsRepo::playSound(AniSoundsRepo::pickLevelSound().filePath);
repeatPickLevelPrompt();
}
}),
nullptr));
}