123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142 |
- //
- // RUPopupMgr.cpp
- // red_utils
- //
- // Created by ZhengSong on 2023/6/9.
- //
- #include "RUPopupMgr.h"
- #include "RUBasePopup.h"
- #include "RUPopupDelegate.h"
- NS_RU_BEGIN
- static PopupMgr* _instance = nullptr;
- PopupMgr* PopupMgr::getInstance() {
- if (_instance == nullptr) {
- _instance = new PopupMgr();
- }
- return _instance;
- }
- void PopupMgr::openPopup(BasePopup* popup)
- {
- //重复插入?
- if (_existWindowName(popup->_getWindowName())) {
- CCASSERT(false, "wtf");
- }
- _popupLayer(popup);
-
- }
- void PopupMgr::openPopupByQueue(BasePopup* popup)
- {
- //重复插入?
- if (_existWindowName(popup->_getWindowName())) {
- CCASSERT(false, "wtf");
- }
- _waiting.push_back(popup);
- //进行优先级排序,优先级相同根据插入时间排序
- auto compareByPriority = [](const BasePopup* p1, const BasePopup* p2)
- {
- return p1->_getPriority() > p2->_getPriority();
- };
- std::stable_sort(_waiting.begin(), _waiting.end(), compareByPriority);
- if(_running.empty()) {
- BasePopup* popup = _waiting.at(_waiting.size() - 1);
- _popupLayer(popup);
- _waiting.erase(_waiting.end() - 1);
- }
- }
- void PopupMgr::_popupLayer(BasePopup* popup)
- {
- if(_delegate) {
- const std::string& popupName = popup->_getWindowName();
- _delegate->onStartPopup(popupName);
- }
- _running.push_back(popup);
- popup->_open();
- }
- void PopupMgr::closePopup(std::string popupName)
- {
- if(_running.empty()) {
- return;
- }
- _isClosing = true;
- for (BasePopup* popupLayer : _running) {
- if (popupLayer->_getWindowName() == popupName) {
- popupLayer->_close();
- }
- }
- }
- void PopupMgr::popupLayerClosed(std::string popupName)
- {
- _isClosing = false;
- for (auto iter = _running.begin(); iter != _running.end(); iter++) {
- if ((*iter)->_getWindowName() == popupName) {
- (*iter)->_execCloseCb();
- _running.erase(iter);
- break;
- }
- }
- if(_delegate) {
- _delegate->onEndPopup(popupName);
- }
- if(_running.empty() == true && _waiting.empty() == false) {
- //打开下一个弹窗
- BasePopup* popup = _waiting.at(_waiting.size() - 1);
- _waiting.erase(_waiting.end() - 1);
- _popupLayer(popup);
- }
- }
- void PopupMgr::forcePopup(BasePopup* popup) {
- //如果当前是正在关闭状态,不能强制关闭当前
- if(_isClosing) {
- return;
- }
- //清空队列
- for (auto runningPop : _running) {
- if(_delegate) {
- _delegate->onEndPopup(runningPop->_getWindowName());
- }
- }
- _running.clear();
- _waiting.clear();
- _popupLayer(popup);
- }
- void PopupMgr::setPopupDelegate(PopupDelegate* delegate) {
- _delegate = delegate;
- }
- void PopupMgr::checkPopupByDestruct(BasePopup* popup)
- {
- auto iter = std::find(_running.begin(), _running.end(), popup);
- if(iter != _running.end()) {
- _running.erase(iter);
- }
- auto iter1 = std::find(_waiting.begin(), _waiting.end(), popup);
- if(iter1 != _waiting.end()) {
- _waiting.erase(iter1);
- }
- }
- bool PopupMgr::_existWindowName(std::string popupName)
- {
- for (auto iter = _running.begin(); iter != _running.end(); iter++) {
- if ((*iter)->_getWindowName() == popupName) {
- return true;
- }
- }
- for (auto iter = _waiting.begin(); iter != _waiting.end(); iter++) {
- if ((*iter)->_getWindowName() == popupName) {
- return true;
- }
- }
- return false;
- }
- NS_RU_END
|