RUPopupMgr.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. //
  2. // RUPopupMgr.cpp
  3. // red_utils
  4. //
  5. // Created by ZhengSong on 2023/6/9.
  6. //
  7. #include "RUPopupMgr.h"
  8. #include "RUBasePopup.h"
  9. #include "RUPopupDelegate.h"
  10. NS_RU_BEGIN
  11. static PopupMgr* _instance = nullptr;
  12. PopupMgr* PopupMgr::getInstance() {
  13. if (_instance == nullptr) {
  14. _instance = new PopupMgr();
  15. }
  16. return _instance;
  17. }
  18. void PopupMgr::openPopup(BasePopup* popup)
  19. {
  20. //重复插入?
  21. if (_existWindowName(popup->_getWindowName())) {
  22. CCASSERT(false, "wtf");
  23. }
  24. _popupLayer(popup);
  25. }
  26. void PopupMgr::openPopupByQueue(BasePopup* popup)
  27. {
  28. //重复插入?
  29. if (_existWindowName(popup->_getWindowName())) {
  30. CCASSERT(false, "wtf");
  31. }
  32. _waiting.push_back(popup);
  33. //进行优先级排序,优先级相同根据插入时间排序
  34. auto compareByPriority = [](const BasePopup* p1, const BasePopup* p2)
  35. {
  36. return p1->_getPriority() > p2->_getPriority();
  37. };
  38. std::stable_sort(_waiting.begin(), _waiting.end(), compareByPriority);
  39. if(_running.empty()) {
  40. BasePopup* popup = _waiting.at(_waiting.size() - 1);
  41. _popupLayer(popup);
  42. _waiting.erase(_waiting.end() - 1);
  43. }
  44. }
  45. void PopupMgr::_popupLayer(BasePopup* popup)
  46. {
  47. if(_delegate) {
  48. const std::string& popupName = popup->_getWindowName();
  49. _delegate->onStartPopup(popupName);
  50. }
  51. _running.push_back(popup);
  52. popup->_open();
  53. }
  54. void PopupMgr::closePopup(std::string popupName)
  55. {
  56. if(_running.empty()) {
  57. return;
  58. }
  59. _isClosing = true;
  60. for (BasePopup* popupLayer : _running) {
  61. if (popupLayer->_getWindowName() == popupName) {
  62. popupLayer->_close();
  63. }
  64. }
  65. }
  66. void PopupMgr::popupLayerClosed(std::string popupName)
  67. {
  68. _isClosing = false;
  69. for (auto iter = _running.begin(); iter != _running.end(); iter++) {
  70. if ((*iter)->_getWindowName() == popupName) {
  71. (*iter)->_execCloseCb();
  72. _running.erase(iter);
  73. break;
  74. }
  75. }
  76. if(_delegate) {
  77. _delegate->onEndPopup(popupName);
  78. }
  79. if(_running.empty() == true && _waiting.empty() == false) {
  80. //打开下一个弹窗
  81. BasePopup* popup = _waiting.at(_waiting.size() - 1);
  82. _waiting.erase(_waiting.end() - 1);
  83. _popupLayer(popup);
  84. }
  85. }
  86. void PopupMgr::forcePopup(BasePopup* popup) {
  87. //如果当前是正在关闭状态,不能强制关闭当前
  88. if(_isClosing) {
  89. return;
  90. }
  91. //清空队列
  92. for (auto runningPop : _running) {
  93. if(_delegate) {
  94. _delegate->onEndPopup(runningPop->_getWindowName());
  95. }
  96. }
  97. _running.clear();
  98. _waiting.clear();
  99. _popupLayer(popup);
  100. }
  101. void PopupMgr::setPopupDelegate(PopupDelegate* delegate) {
  102. _delegate = delegate;
  103. }
  104. void PopupMgr::checkPopupByDestruct(BasePopup* popup)
  105. {
  106. auto iter = std::find(_running.begin(), _running.end(), popup);
  107. if(iter != _running.end()) {
  108. _running.erase(iter);
  109. }
  110. auto iter1 = std::find(_waiting.begin(), _waiting.end(), popup);
  111. if(iter1 != _waiting.end()) {
  112. _waiting.erase(iter1);
  113. }
  114. }
  115. bool PopupMgr::_existWindowName(std::string popupName)
  116. {
  117. for (auto iter = _running.begin(); iter != _running.end(); iter++) {
  118. if ((*iter)->_getWindowName() == popupName) {
  119. return true;
  120. }
  121. }
  122. for (auto iter = _waiting.begin(); iter != _waiting.end(); iter++) {
  123. if ((*iter)->_getWindowName() == popupName) {
  124. return true;
  125. }
  126. }
  127. return false;
  128. }
  129. NS_RU_END