// // AniSceneWithUtils.cpp // SteveMaggieCpp // // Created by Katarzyna Kalinowska-Górska on 11.05.2017. // // #include #include "AniSceneWithUtils.h" #include "AniTouchInterceptingLayer.h" #include "AniGeometryUtils.h" #include "AniMiscUtils.h" #include "AniSoundsRepo.h" const std::string AniSceneWithUtils::BG_MUSIC_PLAYING_USER_DEFAULTS_KEY = "BG_MUSIC_PLAYING_USER_DEFAULTS_KEY"; bool AniSceneWithUtils::init() { if(!cocos2d::Scene::init()) { return false; } _currentDialogView = NULL; _currentLoadingView = NULL; _dialogDismissCallback = [](){}; _dialogPresentCallback = [](){}; _scenePresentCallback = [](){}; _sceneDismissCallback = [](){}; _backgroundMusicFilePath = ""; _touchOutsideDismissesDialog = true; return true; } void AniSceneWithUtils::onEnterTransitionDidFinish() { cocos2d::Scene::onEnterTransitionDidFinish(); _scenePresentCallback(); } void AniSceneWithUtils::onExit() { cocos2d::Scene::onExit(); _sceneDismissCallback(); } void AniSceneWithUtils::presentDialog(cocos2d::Node* dialogView, AnimationType presentAnimationType, AnimationType dismissAnimationType, const std::function presentCallback) { auto winSize = cocos2d::Director::getInstance()->getWinSize(); auto defaultDialogViewPosition = cocos2d::Point(winSize.width/2, winSize.height/2); auto defaultDialogViewAnchorPoint = cocos2d::Point(0.5, 0.5); this->presentDialog(dialogView, presentAnimationType, dismissAnimationType, defaultDialogViewPosition, defaultDialogViewAnchorPoint, presentCallback); } void AniSceneWithUtils::presentDialog(cocos2d::Node* dialogView, AnimationType presentAnimationType, AnimationType dismissAnimationType, cocos2d::Point dialogViewPosition, cocos2d::Point dialogViewAnchorPoint, const std::function presentCallback) { if(dialogView != NULL){ _dialogPresentCallback = presentCallback; this->dismissCurrentDialog(); this->_currentDialogView = dialogView; this->_dialogDismissAnimationType = dismissAnimationType; auto dialogOverlayView = cocos2d::Layer::create(); auto interceptingLayer = AniTouchInterceptingLayer::create(cocos2d::Color4B(0,0,0,200)); dialogOverlayView->setAnchorPoint(dialogViewAnchorPoint); dialogOverlayView->setPosition(dialogViewPosition); dialogOverlayView->setIgnoreAnchorPointForPosition(false); dialogOverlayView->addChild(interceptingLayer, 0); dialogOverlayView->addChild(dialogView, 1); this->addChild(dialogOverlayView); dialogView->setIgnoreAnchorPointForPosition(false); dialogView->setAnchorPoint(dialogViewAnchorPoint); dialogView->setPosition(dialogViewPosition); interceptingLayer->setOnTouchCallback([&](cocos2d::Touch* touch){ if(_touchOutsideDismissesDialog){ auto dialogViewBBToWorld = AniGeometryUtils::getBoundingBoxToWorld(_currentDialogView); auto touchPointLocation = touch->getLocationInView(); if(!dialogViewBBToWorld.containsPoint(touchPointLocation)){ this->dismissCurrentDialog(); } } }); if(presentAnimationType == AniSceneWithUtils::AnimationType::NONE){ _dialogPresentCallback(); } else { this->_animateDialogView(dialogOverlayView, presentAnimationType, _dialogPresentCallback); } } } void AniSceneWithUtils::dismissCurrentDialog() { this->dismissCurrentDialog(_dialogDismissCallback); } void AniSceneWithUtils::dismissCurrentDialog(const std::function dismissCallback) { if(this->_currentDialogView != NULL){ _dialogDismissCallback = dismissCallback; _fullDismissCallback = [&](){ if(this->_currentDialogView){ this->removeChild(this->_currentDialogView->getParent()); this->_currentDialogView = NULL; } _dialogDismissCallback(); }; if(_dialogDismissAnimationType == AniSceneWithUtils::AnimationType::NONE){ _fullDismissCallback(); } else { this->_animateDialogView(this->_currentDialogView->getParent(), this->_dialogDismissAnimationType, _fullDismissCallback); } } } void AniSceneWithUtils::presetDialogDismissCallback(const std::function dismissCallback) { _dialogDismissCallback = dismissCallback; } AniSceneWithUtils::AnimationType AniSceneWithUtils::animationTypeFromString(std::string animationType) { if(animationType == "SCALE_UP"){ return SCALE_UP; } else if(animationType == "SCALE_DOWN"){ return SCALE_DOWN; } else if(animationType == "FADE_OUT"){ return FADE_OUT; } else if(animationType == "FADE_IN"){ return FADE_IN; } return NONE; } // playing background music void AniSceneWithUtils::setBackgroundMusicFilePath(std::string musicPath) { _backgroundMusicFilePath = musicPath; } bool AniSceneWithUtils::isBackgroundMusicPlaying() { return AniSoundsRepo::isMusicPlaying(); // return CocosDenshion::SimpleAudioEngine::getInstance()->isBackgroundMusicPlaying(); } bool AniSceneWithUtils::playBackgroundMusic(bool onlyIfWasPlayingBefore) { bool playBgMusic = onlyIfWasPlayingBefore ? cocos2d::UserDefault::getInstance()->getBoolForKey(BG_MUSIC_PLAYING_USER_DEFAULTS_KEY.c_str(), true) : true; if(playBgMusic && !_backgroundMusicFilePath.empty()){ AniSoundsRepo::playMusic(_backgroundMusicFilePath); cocos2d::UserDefault::getInstance()->setBoolForKey(BG_MUSIC_PLAYING_USER_DEFAULTS_KEY.c_str(), true); return true; } cocos2d::UserDefault::getInstance()->setBoolForKey(BG_MUSIC_PLAYING_USER_DEFAULTS_KEY.c_str(), false); return false; } void AniSceneWithUtils::pauseBackgroundMusic() { AniSoundsRepo::pauseMusic(); } void AniSceneWithUtils::resumeBackgroundMusic() { AniSoundsRepo::resumeMusic(); } void AniSceneWithUtils::stopBackgroundMusic(bool savePreference) { AniSoundsRepo::stopMusic(); if(savePreference){ cocos2d::UserDefault::getInstance()->setBoolForKey(BG_MUSIC_PLAYING_USER_DEFAULTS_KEY.c_str(), false); } } void AniSceneWithUtils::setScenePresentCallback(std::function callback) { _scenePresentCallback = callback; } void AniSceneWithUtils::setSceneDismissCallback(std::function callback) { _sceneDismissCallback = callback; } /////////////////////////////////////// void AniSceneWithUtils::_animateDialogView(cocos2d::Node* dialogView, AnimationType animationType, const std::function& callback) { auto callbackAction = cocos2d::CallFunc::create([&](){ callback(); }); switch (animationType) { case SCALE_UP: dialogView->setScale(0); dialogView->runAction(cocos2d::Sequence::create(cocos2d::ScaleTo::create(0.2, 1), callbackAction, nullptr)); break; case SCALE_DOWN: dialogView->setScale(1); dialogView->runAction(cocos2d::Sequence::create(cocos2d::ScaleTo::create(0.2, 0), callbackAction, nullptr)); break; case FADE_OUT: dialogView->runAction(cocos2d::Sequence::create(cocos2d::FadeOut::create(AniMiscUtils::StandardAnimationTime), callbackAction, nullptr)); case FADE_IN: dialogView->setOpacity(0); dialogView->runAction(cocos2d::Sequence::create(cocos2d::FadeIn::create(AniMiscUtils::StandardAnimationTime), callbackAction, nullptr)); default: dialogView->runAction(callbackAction); break; } }