ToyBlockingActionParser.cpp
39.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
//
// ToyBlockingActionParser.cpp
// SteveMaggieCpp
//
// Created by Katarzyna Kalinowska-Górska on 31.05.2017.
//
//
#include <stdio.h>
#include "ToyBlockingActionParser.h"
#include "ToyJSONParseUtils.h"
#include "TouchHandlerTypes.h"
#include "ToyGeometryUtils.h"
#include "ToyDragAndDropHandler.h"
#include "ToySimpleValue.h"
#include "ToyChangingSprite.h"
#include "ToyLayoutObject.h"
#include "ToySimpleValue.h"
#include "ToyMathUtils.h"
#include "ToyValueStorage.h"
#include "ToyPaletteNode.h"
#include "ToyContainerSprite.h"
#include "ToySoundsRepo.h"
// main parse function
cocos2d::Action* ToyBlockingActionParser::parseJSONAction(const rapidjson::Value& jsonActionObject, ActionParseDelegate* parseDelegate, bool notifyDelegateWhenFinished)
{
cocos2d::Action* parsedAction = NULL;
auto blocking = ToyJSONParseUtils::checkMemberBool(jsonActionObject, "dontBlock", true);
if(!blocking){
notifyDelegateWhenFinished = true;
}
// log("ToyBlockingActionParser: parsing action of type "+jsonActionObject.actionType);
if(ToyJSONParseUtils::hasMemberString(jsonActionObject, "actionType")){
std::string actionType = jsonActionObject["actionType"].GetString();
if(actionType == "callFunctionWithBlocking"){
parsedAction = this->parseCallFunctionWithBlockingAction(jsonActionObject, parseDelegate, notifyDelegateWhenFinished);
}
else if(actionType == "waitForTouch"){
parsedAction = this->parseWaitForTouchAction(jsonActionObject, parseDelegate, notifyDelegateWhenFinished);
}
else if(actionType == "waitForPaletteComponent"){
parsedAction = this->parseWaitForPaletteComponentAction(jsonActionObject, parseDelegate, notifyDelegateWhenFinished);
}
else if(actionType == "waitForObjectUnlock"){
parsedAction = this->parseWaitForObjectUnlockAction(jsonActionObject, parseDelegate, notifyDelegateWhenFinished);
}
else if(actionType == "waitForDragAndDrop"){
parsedAction = this->parseWaitForDragAndDropAction(jsonActionObject, parseDelegate, notifyDelegateWhenFinished);
}
}
return parsedAction;
}
// the function's last parameter has to be the callback
cocos2d::Action* ToyBlockingActionParser::parseCallFunctionWithBlockingAction(const rapidjson::Value& jsonActionObject, ActionParseDelegate* parseDelegate, bool notifyDelegateWhenFinished)
{
auto storedValueKey = ToyValueStorage::getInstance().storeValue(jsonActionObject, parseDelegate->getToyValueStorageContainerName());
auto actionFunction = [&](std::string pStoredValueKey, ActionParseDelegate* pParseDelegate, bool pNotifyDelegate){
auto storedValue = ToyValueStorage::getInstance().getStoredValue(pStoredValueKey, pParseDelegate->getToyValueStorageContainerName());
if(ToyActionParser::getInstance().checkLateCondition(*storedValue, pParseDelegate) == false){
return;
}
auto object = ToyActionParser::getInstance().prepareObjectsForAction(*storedValue, pParseDelegate)[0];
auto callback = [&](std::string p2StoredValueKey, ActionParseDelegate* p2ParseDelegate, bool p2NotifyDelegate){
auto storedValue2 = ToyValueStorage::getInstance().getStoredValue(p2StoredValueKey, p2ParseDelegate->getToyValueStorageContainerName());
if(p2NotifyDelegate){
p2ParseDelegate->actionFinished(*storedValue2);
}
ToyValueStorage::getInstance().removeStoredValue(p2StoredValueKey, p2ParseDelegate->getToyValueStorageContainerName());
};
const rapidjson::Value* params = NULL;
if(ToyJSONParseUtils::hasMemberArray(*storedValue, "parameters")){
params = &((*storedValue)["parameters"]);
}
object->callFunctionByName((*storedValue)["functionName"].GetString(), params, pParseDelegate, std::bind(callback, pStoredValueKey, pParseDelegate, pNotifyDelegate));
};
return ToyActionParser::getInstance().embedFunctionInAction(std::bind(actionFunction, storedValueKey, parseDelegate, notifyDelegateWhenFinished), jsonActionObject, parseDelegate, false);
}
// functions for parsing differenct actions
cocos2d::Action* ToyBlockingActionParser::parseWaitForTouchAction(const rapidjson::Value& jsonActionObject, ActionParseDelegate* parseDelegate, bool notifyDelegateWhenFinished)
{
static const std::string TouchHandlerFunctionKey = "WaitForTouchActionTouchesBeganHandler";
auto storedValueKey = ToyValueStorage::getInstance().storeValue(jsonActionObject, parseDelegate->getToyValueStorageContainerName());
auto actionFunction = [&](std::string pStoredValueKey, ActionParseDelegate* pParseDelegate, bool pNotifyDelegate){
auto storedValue = ToyValueStorage::getInstance().getStoredValue(pStoredValueKey, pParseDelegate->getToyValueStorageContainerName());
TouchHandlerFunction newTouchBeganHandler = std::bind([&](cocos2d::Touch* touch, cocos2d::Event* event, std::string p2StoredValueKey, ActionParseDelegate* p2ParseDelegate, bool p2NotifyDelegate){
auto storedValue2 = ToyValueStorage::getInstance().getStoredValue(p2StoredValueKey, p2ParseDelegate->getToyValueStorageContainerName());
auto objectsToTouch = ToyActionParser::getInstance().parseObjectOrObjects(*storedValue2, "rightObjectName", "rightObjectNames", p2ParseDelegate);
auto wrongObjects = ToyActionParser::getInstance().parseObjectOrObjects(*storedValue2, "wrongObjectName", "wrongObjectNames", p2ParseDelegate);
int touchedObjectsCount = 0;
static std::string SubContainer = "WaitForTouchActionTouchedFlags"; //TODO: container name from the delegate
for(int i = 0; i < objectsToTouch.size(); ++i){
auto objectToTouch = objectsToTouch[i];
auto objAsNode = dynamic_cast<cocos2d::Node*>(objectToTouch);
auto touchedPtr = ToyValueStorage::getInstance().getStoredToySimpleValue(SubContainer+objectToTouch->getObjectName(), p2ParseDelegate->getToyValueStorageContainerName());
if(!touchedPtr){
ToySimpleValue temp{false};
touchedPtr = &temp;
ToyValueStorage::getInstance().storeToySimpleValueWithKey(touchedPtr, SubContainer+objectToTouch->getObjectName(), p2ParseDelegate->getToyValueStorageContainerName());
}
if(!touchedPtr->getBoolValue() && ToyGeometryUtils::getBoundingBoxToWorld(objAsNode).containsPoint(touch->getLocation())){
if(ToyJSONParseUtils::hasMemberArray(*storedValue2, "rightActions")){
ToyActionParser::getInstance().parseAndRunActions((*storedValue2)["rightActions"], p2ParseDelegate, false);
}
if(ToyJSONParseUtils::hasMemberArray(*storedValue2, "rightActionsForObjects")){
ToyActionParser::getInstance().parseAndRunActions((*storedValue2)["rightActionsForObjects"].GetArray()[i], p2ParseDelegate, false);
}
touchedPtr->setBoolValue(true);
if(++touchedObjectsCount == objectsToTouch.size()){
if(ToyJSONParseUtils::checkMemberBool(*storedValue2, "dontBlock", false)){
p2ParseDelegate->removeTouchHandler(TouchHandlerFunctionKey, TOUCHES_BEGAN);
p2ParseDelegate->cancelAllCompletingActions();
if(p2NotifyDelegate){
p2ParseDelegate->actionFinished(*storedValue2);
}
ToyValueStorage::getInstance().removeStoredValue(pStoredValueKey, p2ParseDelegate->getToyValueStorageContainerName());
ToyValueStorage::getInstance().clearStoredData(p2ParseDelegate->getToyValueStorageContainerName());
} else {
//to enable picking the objects again
touchedObjectsCount = 0;
for(auto object : objectsToTouch){
ToySimpleValue* touchedPtr2 = ToyValueStorage::getInstance().getStoredToySimpleValue(SubContainer + object->getObjectName(), p2ParseDelegate->getToyValueStorageContainerName());
if(touchedPtr2){
touchedPtr2->setBoolValue(false);
} else {
ToySimpleValue temp2{false};
ToyValueStorage::getInstance().storeToySimpleValueWithKey(&temp2, SubContainer + objectToTouch->getObjectName(), p2ParseDelegate->getToyValueStorageContainerName());
}
}
}
}
return true;
}
}
if(ToyJSONParseUtils::hasMemberArray(*storedValue2, "wrongActions")){
for(auto wrongObject : wrongObjects){
auto objAsNode = dynamic_cast<cocos2d::Node*>(wrongObject);
if(ToyGeometryUtils::getBoundingBoxToWorld(objAsNode).containsPoint(touch->getLocation())){
ToyActionParser::getInstance().parseAndRunActions((*storedValue2)["wrongActions"], p2ParseDelegate, false);
break;
}
}
}
return false;
}, std::placeholders::_1, std::placeholders::_2, pStoredValueKey, pParseDelegate, pNotifyDelegate);
pParseDelegate->newTouchHandler(TouchHandlerFunctionKey, newTouchBeganHandler, TOUCHES_BEGAN);
this->runCompletingActions(*storedValue, pParseDelegate);
};
auto notify = ToyJSONParseUtils::checkMemberBool(jsonActionObject, "dontBlock", true);
return ToyActionParser::getInstance().embedFunctionInAction(std::bind(actionFunction, storedValueKey, parseDelegate, notifyDelegateWhenFinished), jsonActionObject, parseDelegate, notify); //todo there is some ambiguity here... dont notify now or later? will have to solve it somehow
}
cocos2d::Action* ToyBlockingActionParser::parseWaitForPaletteComponentAction(const rapidjson::Value& jsonActionObject, ActionParseDelegate* parseDelegate, bool notifyDelegateWhenFinished)
{
auto storedValueKey = ToyValueStorage::getInstance().storeValue(jsonActionObject, parseDelegate->getToyValueStorageContainerName());
auto actionFunction = [&](std::string pStoredValueKey, ActionParseDelegate* pParseDelegate, bool pNotifyDelegate){
auto storedValue = ToyValueStorage::getInstance().getStoredValue(pStoredValueKey, pParseDelegate->getToyValueStorageContainerName());
auto objectsToTouch = ToyActionParser::getInstance().parseObjectOrObjects(*storedValue, "rightObjectName", "rightObjectNames", pParseDelegate);
auto paletteNode = dynamic_cast<ToyPaletteNode*>(dynamic_cast<cocos2d::Node*>(objectsToTouch[0])->getParent());
std::function<void(ToyPaletteNode *, ToyPaletteComponentSprite *, bool, bool)> callback = std::bind([&](ToyPaletteNode * pToyPaletteNode, ToyPaletteComponentSprite * paletteComponentSprite, bool chosen, bool pressed, std::string pStoredValueKey2, ActionParseDelegate* pParseDelegate2, bool pNotifyDelegate2){
if(pressed){
bool wasRightObject = false;
auto storedValue2 = ToyValueStorage::getInstance().getStoredValue(pStoredValueKey2, pParseDelegate2->getToyValueStorageContainerName());
auto rightObjects = ToyActionParser::getInstance().parseObjectOrObjects(*storedValue2, "rightObjectName", "rightObjectNames", pParseDelegate2);
for(ToyScenarioObject* rightObject : rightObjects){
ToyPaletteComponentSprite* rightToyPaletteComponentSprite = dynamic_cast<ToyPaletteComponentSprite*>(rightObject);
if(paletteComponentSprite->objectName == rightToyPaletteComponentSprite->objectName){
if(ToyJSONParseUtils::hasMemberArray(*storedValue2, "rightActions")){
ToyActionParser::getInstance().parseAndRunActions((*storedValue2)["rightActions"], pParseDelegate2, false);
}
wasRightObject = true;
if(ToyJSONParseUtils::checkMemberBool(*storedValue2, "dontBlock", false)){
pToyPaletteNode->setOnPaletteStateChangedCallback([](ToyPaletteNode*, ToyPaletteComponentSprite*, bool, bool){});
pParseDelegate2->cancelAllCompletingActions();
if(pNotifyDelegate2){
pParseDelegate2->actionFinished(*storedValue2);
}
ToyValueStorage::getInstance().removeStoredValue(pStoredValueKey2, pParseDelegate2->getToyValueStorageContainerName());
}
break;
}
}
if(!wasRightObject && ToyJSONParseUtils::hasMemberArray(*storedValue2, "wrongActions")){
auto wrongObjects = ToyActionParser::getInstance().parseObjectOrObjects(*storedValue2, "wrongObjectName", "wrongObjectNames", pParseDelegate2);
for(auto wrongObject : wrongObjects){
auto wrongToyPaletteComponentSprite = dynamic_cast<ToyPaletteComponentSprite*>(wrongObject);
if(paletteComponentSprite->objectName == wrongToyPaletteComponentSprite->objectName){
ToyActionParser::getInstance().parseAndRunActions((*storedValue2)["wrongActions"], pParseDelegate2, false);
break;
}
}
}
}
}, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3, std::placeholders::_4,pStoredValueKey, pParseDelegate, pNotifyDelegate);
paletteNode->setOnPaletteStateChangedCallback(callback);
this->runCompletingActions(*storedValue, pParseDelegate);
};
auto notify = ToyJSONParseUtils::checkMemberBool(jsonActionObject, "dontBlock", true);
return ToyActionParser::getInstance().embedFunctionInAction(std::bind(actionFunction, storedValueKey, parseDelegate, notifyDelegateWhenFinished), jsonActionObject, parseDelegate, notify); //todo there is some ambiguity here... dont notify now or later? will have to solve it somehow
}
cocos2d::Action* ToyBlockingActionParser::parseWaitForDragAndDropAction(const rapidjson::Value& jsonActionObject, ActionParseDelegate* parseDelegate, bool notifyDelegateWhenFinished)
{
auto storedValueKey = ToyValueStorage::getInstance().storeValue(jsonActionObject, parseDelegate->getToyValueStorageContainerName());
auto actionFunction = [&](std::string pStoredValueKey, ActionParseDelegate* pParseDelegate, bool pNotifyDelegate){
auto pJsonActionObject = ToyValueStorage::getInstance().getStoredValue(pStoredValueKey, pParseDelegate->getToyValueStorageContainerName());
auto objectsToDrag = ToyActionParser::getInstance().parseObjectOrObjects(*pJsonActionObject, "draggedObjectName", "draggedObjectNames", pParseDelegate);
auto extendPercent = 0;
if(ToyJSONParseUtils::hasMemberFloat(*pJsonActionObject, "draggedObjectGrabAreaExtendByPercent")){
extendPercent = (*pJsonActionObject)["draggedObjectGrabAreaExtendByPercent"].GetFloat();
}
auto objectsToDragExtendPercentValues = std::vector<float>(objectsToDrag.size(), extendPercent);
std::vector<ToyScenarioObject*> wrongDraggedObjects = ToyActionParser::getInstance().parseObjectOrObjects(*pJsonActionObject, "wrongDraggedObjectName", "wrongDraggedObjectNames", pParseDelegate);
ToyScenarioObject* destObject = nullptr;
std::vector<ToyScenarioObject*> wrongDstObjects;
int indexForConditionedCompletingActions = 0;
if(ToyJSONParseUtils::hasMemberObject(*pJsonActionObject, "randomDestinationObjectNameFrom")){
auto detailsObject = (*pJsonActionObject)["randomDestinationObjectNameFrom"].GetObject();
auto allDstObjects = ToyActionParser::getInstance().parseObjects(detailsObject["namesList"], pParseDelegate);
int index = -1;
while(destObject == nullptr){
index = ToyMathUtils::getRandomInt(0, allDstObjects.size()-1);
if(!dynamic_cast<ToyContainerSprite*>(allDstObjects[index])->hasContent()){//TODO parse the condition first
destObject = allDstObjects[index];
break;
}
}
for(int i = 0; i < index; ++i){
wrongDstObjects.push_back(allDstObjects[i]);
}
for(int i = index+1; i < allDstObjects.size(); ++i){
wrongDstObjects.push_back(allDstObjects[i]);
}
auto destObjSoundPath = ToyJSONParseUtils::parseStringArray(detailsObject["correspSoundsList"])[index];
ToySoundsRepo::playSound(pParseDelegate->getDefaultSoundsPath() + destObjSoundPath);
if(ToyJSONParseUtils::hasMemberBool(detailsObject, "determineConditionedCompletingActions")){// && //detailsObject["determineConditionedCompletingActions"].GetBool() == true){
indexForConditionedCompletingActions = index;
}
} else {
destObject = ToyActionParser::getInstance().parseObject((*pJsonActionObject)["destinationObjectName"].GetString(), pParseDelegate);
wrongDstObjects = ToyActionParser::getInstance().parseObjectOrObjects(*pJsonActionObject, "wrongDestinationObjectName", "wrongDestinationObjectNames", pParseDelegate);
}
auto dndHandler = new ToyDragAndDropHandler(ToyActionParser::convertToNodeArray(objectsToDrag), ToyActionParser::convertToNode(destObject), ToyActionParser::convertToNodeArray(wrongDraggedObjects), ToyActionParser::convertToNodeArray(wrongDstObjects));
dndHandler->setObjectToDragExtendPercentValues(objectsToDragExtendPercentValues);
if(ToyJSONParseUtils::checkMemberBool(*pJsonActionObject, "moreAccurateDestinationCheck", true)){
dndHandler->setAccurateDestinationCheck(true);
} else if(ToyJSONParseUtils::checkMemberBool(*pJsonActionObject, "fingerOnlyDestinationCheck", true)){
dndHandler->setFingerOnlyDestinationCheck(true);
}
if(ToyJSONParseUtils::hasMemberArray(*pJsonActionObject, "startDraggingCorrectObjectActions")){
dndHandler->_startDraggingCorrectObjectCallback = std::bind([&](cocos2d::Node* object, std::string p2StoredValueKey, ActionParseDelegate* p2ParseDelegate){
auto p2JsonActionObject = ToyValueStorage::getInstance().getStoredValue(p2StoredValueKey, p2ParseDelegate->getToyValueStorageContainerName());
ToyActionParser::getInstance().parseAndRunActions((*p2JsonActionObject)["startDraggingCorrectObjectActions"], p2ParseDelegate, false);
}, std::placeholders::_1, pStoredValueKey, pParseDelegate);
}
// if(ToyJSONParseUtils::hasMemberArray(*pJsonActionObject, "startDraggingConcreteObjectCallbacks")){
//
// auto allCallbacks = (*pJsonActionObject)["startDraggingConcreteObjectCallbacks"].GetArray();
//
// for(int i = 0; i < allCallbacks.Size(); ++i){
//
// std::string objectName = allCallbacks[i]["objectName"].GetString();
// auto callback = std::bind([&](cocos2d::Node* wrongDraggedObject,std::string p2StoredValueKey, ActionParseDelegate* p2ParseDelegate){
// auto p2JsonActionObject = ToyValueStorage::getInstance().getStoredValue(p2StoredValueKey, p2ParseDelegate->getToyValueStorageContainerName());
// ToyActionParser::getInstance().parseAndRunActions((*p2JsonActionObject)["actions"], p2ParseDelegate, false);
// }, std::placeholders::_1, pStoredValueKey, pParseDelegate);
// dndHandler->_startDraggingConcreteObjectCallbacks.insert({objectName, callback});
// }
// }
std::function<void()> successCallback = std::bind([&](std::string p2StoredValueKey, ActionParseDelegate* p2ParseDelegate){
auto p2JsonActionObject = ToyValueStorage::getInstance().getStoredValue(p2StoredValueKey, p2ParseDelegate->getToyValueStorageContainerName());
ToyActionParser::getInstance().parseAndRunActions((*p2JsonActionObject)["rightActions"], p2ParseDelegate, false);
},pStoredValueKey, pParseDelegate);
if(ToyJSONParseUtils::hasMemberArray(*pJsonActionObject, "rightActions")){
dndHandler->_successCallback = std::bind([&](cocos2d::Node* destinationObject, cocos2d::Node* rightObject, std::function<void()> pSuccessCallback){
if(rightObject){
pSuccessCallback();
}
}, std::placeholders::_1, std::placeholders::_2, successCallback);
}
auto newTBH = dndHandler->prepareTouchBeganHandler();
auto newTMH = dndHandler->prepareTouchMovedHandler();
auto newTEH = dndHandler->prepareTouchEndedHandler();
static const std::string tbhKey = "dragNDropHandlerTBH";
static const std::string tmhKey = "dragNDropHandlerTMH";
static const std::string tehKey = "dragNDropHandlerTEH";
pParseDelegate->newTouchHandler(tbhKey, newTBH, TOUCHES_BEGAN);
pParseDelegate->newTouchHandler(tmhKey, newTMH, TOUCHES_MOVED);
pParseDelegate->newTouchHandler(tehKey, newTEH, TOUCHES_ENDED);
std::function<void(cocos2d::Node*, cocos2d::Node*)> finalSuccessCallback = std::bind([&](cocos2d::Node* destinationObject, cocos2d::Node* rightObject, std::string p2StoredValueKey, ActionParseDelegate* p2ParseDelegate, bool p2NotifyDelegate, ToyDragAndDropHandler* pDndHandler){
auto p2JsonActionObject = ToyValueStorage::getInstance().getStoredValue(p2StoredValueKey, p2ParseDelegate->getToyValueStorageContainerName());
p2ParseDelegate->removeTouchHandler(tbhKey, TOUCHES_BEGAN);
p2ParseDelegate->removeTouchHandler(tmhKey, TOUCHES_MOVED); //TODO what about static strings? it's probably ok...
p2ParseDelegate->removeTouchHandler(tehKey, TOUCHES_ENDED);
p2ParseDelegate->cancelAllCompletingActions();
if(p2NotifyDelegate){
p2ParseDelegate->actionFinished(*p2JsonActionObject);
}
delete pDndHandler;
ToyValueStorage::getInstance().removeStoredValue(p2StoredValueKey, p2ParseDelegate->getToyValueStorageContainerName());
}, std::placeholders::_1, std::placeholders::_2, pStoredValueKey, pParseDelegate, pNotifyDelegate, dndHandler);
if(ToyJSONParseUtils::checkMemberBool(*pJsonActionObject, "requireFullDestinationColourIn", true)){
dndHandler->_allowMultipleDrag = true;
auto dstObjAsToyColourableSprite = dynamic_cast<ToyColourableSprite*>(destObject);
std::function<void()> callbackToStore = std::bind(finalSuccessCallback, nullptr, nullptr);
auto storedCallbackKey = ToyValueStorage::getInstance().storeFunction(callbackToStore, pParseDelegate->getToyValueStorageContainerName());
dstObjAsToyColourableSprite->_autofillCallback = std::bind([&](ToyDragAndDropHandler* pDndHandler, std::string pStoredCallbackKey, ActionParseDelegate* p2ParseDelegate){
pDndHandler->draggedObjectMoveBack(false);
ToyValueStorage::getInstance().runAndRemoveStoredFunction(pStoredCallbackKey, p2ParseDelegate->getToyValueStorageContainerName());
}, dndHandler, storedCallbackKey, pParseDelegate);
} else {
dndHandler->_finalSuccessCallback = finalSuccessCallback;
}
if(ToyJSONParseUtils::checkMemberBool(*pJsonActionObject, "wrongDraggedObjectAllowDrag", true)){
dndHandler->_allowDraggingWrongObjects = true;
}
if(ToyJSONParseUtils::hasMemberArray(*pJsonActionObject, "wrongDraggedObjectActions")){
auto wrongObjectCallback = std::bind([&](cocos2d::Node* wrongDraggedObject,std::string p2StoredValueKey, ActionParseDelegate* p2ParseDelegate){
auto p2JsonActionObject = ToyValueStorage::getInstance().getStoredValue(p2StoredValueKey, p2ParseDelegate->getToyValueStorageContainerName());
ToyActionParser::getInstance().parseAndRunActions((*p2JsonActionObject)["wrongDraggedObjectActions"], p2ParseDelegate, false);
}, std::placeholders::_1, pStoredValueKey, pParseDelegate);
if(dndHandler->_allowDraggingWrongObjects){
dndHandler->_draggedWrongObjectCallback = wrongObjectCallback;
} else {
dndHandler->_startDraggingWrongObjectCallback = wrongObjectCallback;
}
}
if(ToyJSONParseUtils::hasMemberArray(*pJsonActionObject, "wrongDestinationObjectActions")){
dndHandler->_wrongDestinationObjectCallback = std::bind([&](cocos2d::Node* wrongDestObject,std::string p2StoredValueKey, ActionParseDelegate* p2ParseDelegate){
auto p2JsonActionObject = ToyValueStorage::getInstance().getStoredValue(p2StoredValueKey, p2ParseDelegate->getToyValueStorageContainerName());
ToyActionParser::getInstance().parseAndRunActions((*p2JsonActionObject)["wrongDestinationObjectActions"], p2ParseDelegate, false);
}, std::placeholders::_1, pStoredValueKey, pParseDelegate);
}
if(ToyJSONParseUtils::hasMemberArray(*pJsonActionObject, "draggedObjectEnterCorrectDestinationActions")){
dndHandler->_draggedObjectEnterCorrectDestinationCallback = std::bind([&](cocos2d::Node* draggedObject, cocos2d::Node* destinationObject,std::string p2StoredValueKey, ActionParseDelegate* p2ParseDelegate){
auto p2JsonActionObject = ToyValueStorage::getInstance().getStoredValue(p2StoredValueKey, p2ParseDelegate->getToyValueStorageContainerName());
ToyActionParser::getInstance().parseAndRunActions((*p2JsonActionObject)["draggedObjectEnterCorrectDestinationActions"], p2ParseDelegate, false);
}, std::placeholders::_1, std::placeholders::_2, pStoredValueKey, pParseDelegate);
}
if(ToyJSONParseUtils::hasMemberArray(*pJsonActionObject, "draggedObjectEnterWrongDestinationActions")){
dndHandler->_draggedObjectEnterWrongDestinationCallback = std::bind([&](cocos2d::Node* draggedObject, cocos2d::Node* destinationObject,std::string p2StoredValueKey, ActionParseDelegate* p2ParseDelegate){
auto p2JsonActionObject = ToyValueStorage::getInstance().getStoredValue(p2StoredValueKey, p2ParseDelegate->getToyValueStorageContainerName());
ToyActionParser::getInstance().parseAndRunActions((*p2JsonActionObject)["draggedObjectEnterWrongDestinationActions"], p2ParseDelegate, false);
}, std::placeholders::_1, std::placeholders::_2, pStoredValueKey, pParseDelegate);
}
if(ToyJSONParseUtils::hasMemberArray(*pJsonActionObject, "dropTooSoonActions")){
dndHandler->_dropTooSoonCallback = std::bind([&](std::string p2StoredValueKey, ActionParseDelegate* p2ParseDelegate){
auto p2JsonActionObject = ToyValueStorage::getInstance().getStoredValue(p2StoredValueKey, p2ParseDelegate->getToyValueStorageContainerName());
ToyActionParser::getInstance().parseAndRunActions((*p2JsonActionObject)["dropTooSoonActions"], p2ParseDelegate, false);
}, pStoredValueKey, pParseDelegate);
}
auto destObjectAsToyLayoutObject = dynamic_cast<ToyLayoutObject*>(destObject);
if(destObjectAsToyLayoutObject->className == "ChangingSprite"){
dndHandler->_objectDragNDropSuccessBehaviour = [&](cocos2d::Node* destinationObject, cocos2d::Node* draggedObject, cocos2d::Point startingObjectPosition){
draggedObject->setOpacity(0);
dynamic_cast<ToyChangingSprite*>(destinationObject)->switchSprites();
};
} else if(destObjectAsToyLayoutObject->className == "ContainerSprite"){
dndHandler->_objectDragNDropSuccessBehaviour = [&](cocos2d::Node* destinationObject, cocos2d::Node* draggedObject, cocos2d::Point startingObjectPosition){
dynamic_cast<ToyContainerSprite*>(destinationObject)->addContentNode(draggedObject);
};
} else if(destObjectAsToyLayoutObject->className == "ColourableSprite"){
if(ToyJSONParseUtils::checkMemberBool(*pJsonActionObject, "requireFullDestinationColourIn", true)){
dndHandler->_objectDragNDropSuccessBehaviour = [&](cocos2d::Node* destinationObject, cocos2d::Node* draggedObject, cocos2d::Point startingObjectPosition){
dynamic_cast<ToyColourableSprite*>(destinationObject)->_colouringEnabled = false;
};
} else {
dndHandler->_objectDragNDropSuccessBehaviour = std::bind([&](cocos2d::Node* destinationObject, cocos2d::Node* draggedObject, cocos2d::Point startingObjectPosition, std::function<void()> pSuccessCallback){
auto dstColSprite = dynamic_cast<ToyColourableSprite*>(destinationObject);
dstColSprite->_autofillCallback = pSuccessCallback;
dstColSprite->startAutofillAnimation();
draggedObject->runAction(cocos2d::MoveTo::create(0.2, startingObjectPosition));
}, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3, successCallback);
}
} else {
dndHandler->_objectDragNDropSuccessBehaviour = std::bind([&](cocos2d::Node* destinationObject, cocos2d::Node* draggedObject, cocos2d::Point startingObjectPosition, std::string p2ToyValueStorageKey, ActionParseDelegate* p2ParseDelegate){
auto p2JsonActionObject = ToyValueStorage::getInstance().getStoredValue(p2ToyValueStorageKey, p2ParseDelegate->getToyValueStorageContainerName());
if(ToyJSONParseUtils::checkMemberBool(*p2JsonActionObject, "draggedObjectShouldDisappear", true)){
draggedObject->setOpacity(0);
} else {
cocos2d::Point draggedObjectFinalPosition = cocos2d::Point(-1, -1);
if(ToyJSONParseUtils::hasMemberPoint(*p2JsonActionObject, "draggedObjectFinalPosition")){
draggedObjectFinalPosition = ToyJSONParseUtils::getMemberPoint(*p2JsonActionObject, "draggedObjectFinalPosition");
} else if(ToyJSONParseUtils::hasMemberString(*p2JsonActionObject, "draggedObjectFinalPosition")){
auto positionValue = ToyActionParser::getInstance().parseObject((*p2JsonActionObject)["draggedObjectFinalPosition"].GetString(), p2ParseDelegate);
auto castPositionValue = dynamic_cast<ToySimpleValue*>(positionValue);
draggedObjectFinalPosition = castPositionValue->getPointValue();
}
if(draggedObjectFinalPosition.x != -1){
draggedObject->runAction(cocos2d::MoveTo::create(0.2, draggedObjectFinalPosition));
}
}
}, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3, pStoredValueKey, pParseDelegate);
}
this->runCompletingActions(*pJsonActionObject, pParseDelegate, indexForConditionedCompletingActions);
};
auto notify = ToyJSONParseUtils::checkMemberBool(jsonActionObject, "dontBlock", true);
return ToyActionParser::getInstance().embedFunctionInAction(std::bind(actionFunction, storedValueKey, parseDelegate, notifyDelegateWhenFinished), jsonActionObject, parseDelegate, notify);
}
cocos2d::Action* ToyBlockingActionParser::parseWaitForObjectUnlockAction(const rapidjson::Value& jsonActionObject, ActionParseDelegate* parseDelegate, bool notifyDelegateWhenFinished)
{
// if(jsonActionObject.objectName == null){
// log("ToyBlockingActionParser: parseWaitForObjectUnlockAction: objectName must not be null.");
// return null;
// }
auto unlockingObject = parseDelegate->getObjectByName(jsonActionObject["objectName"].GetString());
// if(unlockingObject == null){
// log("ToyBlockingActionParser: parseWaitForObjectUnlockAction: no object with given objectName. Unlocking object must not be null.");
// return null;
// }
std::function<void()> actionFunction = [&](){};
auto className = unlockingObject->getClassName();
if(className == "ColourableSprite"){
actionFunction = this->parseWaitForObjectUnlockActionFunction_ToyColourableSprite(dynamic_cast<ToyColourableSprite*>(unlockingObject), jsonActionObject, parseDelegate, notifyDelegateWhenFinished);
}
// else {
// log("ToyBlockingActionParser: parseWaitForObjectUnlockAction: cannot determine object type. Unlocking object must have the classNAme specified.");
// }
auto notify = ToyJSONParseUtils::checkMemberBool(jsonActionObject, "dontBlock", true);
return ToyActionParser::getInstance().embedFunctionInAction(actionFunction, jsonActionObject, parseDelegate, notify);
}
std::function<void()> ToyBlockingActionParser::parseWaitForObjectUnlockActionFunction_ToyColourableSprite(ToyColourableSprite* unlockingObject, const rapidjson::Value& jsonActionObject, ActionParseDelegate* parseDelegate, bool notifyDelegateWhenFinished)
{
auto storedValueKey = ToyValueStorage::getInstance().storeValue(jsonActionObject, parseDelegate->getToyValueStorageContainerName());
auto actionFunction = [&](std::string pStoredValueKey, ActionParseDelegate* pParseDelegate, bool pNotifyDelegate, ToyColourableSprite* pUnlockingObject){
auto storedValue = ToyValueStorage::getInstance().getStoredValue(pStoredValueKey, pParseDelegate->getToyValueStorageContainerName());
auto newTouchBeganHandler = std::bind([&](cocos2d::Touch* touch, cocos2d::Event* event, std::string p2StoredValueKey, ActionParseDelegate* p2ParseDelegate, ToyColourableSprite* p2UnlockingObject){
auto storedValue2 = ToyValueStorage::getInstance().getStoredValue(p2StoredValueKey, p2ParseDelegate->getToyValueStorageContainerName());
if(!ToyGeometryUtils::getBoundingBoxToWorld(p2UnlockingObject).containsPoint(touch->getLocation())){
if(ToyJSONParseUtils::hasMemberArray(*storedValue2, "wrongActions")){
auto wrongObjects = ToyActionParser::getInstance().parseObjectOrObjects(*storedValue2, "wrongObjectName", "wrongObjectNames", p2ParseDelegate);
auto wrongNodes = ToyActionParser::getInstance().convertToNodeArray(wrongObjects);
for(int j = 0; j < wrongNodes.size(); ++j){
if(ToyGeometryUtils::getBoundingBoxToWorld(wrongNodes[j]).containsPoint(touch->getLocation())){
ToyActionParser::getInstance().parseAndRunActions((*storedValue2)["wrongActions"], p2ParseDelegate, false);
break;
}
}
}
} else if(ToyJSONParseUtils::hasMemberArray(*storedValue2, "rightActions")) {
ToyActionParser::getInstance().parseAndRunActions((*storedValue2)["rightActions"], p2ParseDelegate, false);
}
return false;
}, std::placeholders::_1, std::placeholders::_2, pStoredValueKey, pParseDelegate, pUnlockingObject);
static const std::string nthKey = "waitForToyColourableSpriteTBH";
pParseDelegate->newTouchHandler(nthKey, newTouchBeganHandler, TOUCHES_BEGAN);
pUnlockingObject->_autofillCallback = std::bind([&](std::string p2StoredValueKey, ActionParseDelegate* p2ParseDelegate, bool p2NotifyDelegate){
auto storedValue2 = ToyValueStorage::getInstance().getStoredValue(p2StoredValueKey, p2ParseDelegate->getToyValueStorageContainerName());
p2ParseDelegate->removeTouchHandler(nthKey, TOUCHES_BEGAN); //TODO what about nth key?
p2ParseDelegate->cancelAllCompletingActions();
if(p2NotifyDelegate){
p2ParseDelegate->actionFinished(*storedValue2);
}
ToyValueStorage::getInstance().removeStoredValue(p2StoredValueKey, p2ParseDelegate->getToyValueStorageContainerName());
}, pStoredValueKey, pParseDelegate, pNotifyDelegate);
this->runCompletingActions(*storedValue, pParseDelegate);
};
return std::bind(actionFunction, storedValueKey, parseDelegate, notifyDelegateWhenFinished, unlockingObject);
}
// helper functions
//TODO descriptions
void ToyBlockingActionParser::runCompletingActions(const rapidjson::Value& jsonActionObject, ActionParseDelegate* parseDelegate, int indexForConditionedActions)
{
if(ToyJSONParseUtils::hasMemberArray(jsonActionObject, "completingActions")){
auto randomTagBase = ToyMathUtils::getRandomInt(0,10000);
const auto& array = jsonActionObject["completingActions"];
for(int i = 0; i < array.Size(); ++i){
auto action = ToyActionParser::getInstance().parseJSONAction(array[i], parseDelegate, false);
if(action){
action->setTag(randomTagBase+i);
parseDelegate->runCompletingAction(action);
}
}
} else if(ToyJSONParseUtils::hasMemberArray(jsonActionObject, "conditionedCompletingActions")){
auto randomTagBase = ToyMathUtils::getRandomInt(0,10000);
const auto& array = jsonActionObject["conditionedCompletingActions"].GetArray()[indexForConditionedActions];
for(int i = 0; i < array.Size(); ++i){
auto action = ToyActionParser::getInstance().parseJSONAction(array[i], parseDelegate, false);
if(action){
action->setTag(randomTagBase+i);
parseDelegate->runCompletingAction(action);
}
}
}
}