WindowHandler.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. /*
  2. * WindowHandler.cpp, part of VCMI engine
  3. *
  4. * Authors: listed in file AUTHORS in main folder
  5. *
  6. * License: GNU General Public License v2.0 or later
  7. * Full text of license available in license.txt file, in main folder
  8. *
  9. */
  10. #include "StdInc.h"
  11. #include "WindowHandler.h"
  12. #include "GameEngine.h"
  13. #include "CIntObject.h"
  14. #include "CursorHandler.h"
  15. #include "../render/Canvas.h"
  16. #include "../render/IScreenHandler.h"
  17. #include "../render/Colors.h"
  18. void WindowHandler::popWindow(std::shared_ptr<IShowActivatable> top)
  19. {
  20. if (windowsStack.back() != top)
  21. throw std::runtime_error("Attempt to pop non-top window from stack!");
  22. top->deactivate();
  23. disposed.push_back(top);
  24. windowsStack.pop_back();
  25. if(!windowsStack.empty())
  26. windowsStack.back()->activate();
  27. totalRedraw();
  28. }
  29. void WindowHandler::pushWindow(std::shared_ptr<IShowActivatable> newInt)
  30. {
  31. if (newInt == nullptr)
  32. throw std::runtime_error("Attempt to push null window onto windows stack!");
  33. if (vstd::contains(windowsStack, newInt))
  34. throw std::runtime_error("Attempt to add already existing window to stack!");
  35. if(!windowsStack.empty())
  36. windowsStack.back()->deactivate();
  37. windowsStack.push_back(newInt);
  38. ENGINE->cursor().set(Cursor::Map::POINTER);
  39. newInt->activate();
  40. totalRedraw();
  41. }
  42. bool WindowHandler::isTopWindowPopup() const
  43. {
  44. if (windowsStack.empty())
  45. return false;
  46. return windowsStack.back()->isPopupWindow();
  47. }
  48. void WindowHandler::popWindows(int howMany)
  49. {
  50. if(!howMany)
  51. return; //senseless but who knows...
  52. assert(windowsStack.size() >= howMany);
  53. windowsStack.back()->deactivate();
  54. for(int i = 0; i < howMany; i++)
  55. {
  56. disposed.push_back(windowsStack.back());
  57. windowsStack.pop_back();
  58. }
  59. if(!windowsStack.empty())
  60. {
  61. windowsStack.back()->activate();
  62. totalRedraw();
  63. }
  64. ENGINE->fakeMouseMove();
  65. }
  66. std::shared_ptr<IShowActivatable> WindowHandler::topWindowImpl() const
  67. {
  68. if(windowsStack.empty())
  69. return nullptr;
  70. return windowsStack.back();
  71. }
  72. bool WindowHandler::isTopWindow(std::shared_ptr<IShowActivatable> window) const
  73. {
  74. assert(window != nullptr);
  75. return !windowsStack.empty() && windowsStack.back() == window;
  76. }
  77. bool WindowHandler::isTopWindow(IShowActivatable * window) const
  78. {
  79. assert(window != nullptr);
  80. return !windowsStack.empty() && windowsStack.back().get() == window;
  81. }
  82. void WindowHandler::totalRedraw()
  83. {
  84. totalRedrawRequested = true;
  85. }
  86. void WindowHandler::totalRedrawImpl()
  87. {
  88. logGlobal->debug("totalRedraw requested!");
  89. Canvas target = ENGINE->screenHandler().getScreenCanvas();
  90. for(auto & elem : windowsStack)
  91. elem->showAll(target);
  92. }
  93. void WindowHandler::simpleRedraw()
  94. {
  95. if (totalRedrawRequested)
  96. totalRedrawImpl();
  97. else
  98. simpleRedrawImpl();
  99. totalRedrawRequested = false;
  100. }
  101. void WindowHandler::simpleRedrawImpl()
  102. {
  103. Canvas target = ENGINE->screenHandler().getScreenCanvas();
  104. if(!windowsStack.empty())
  105. windowsStack.back()->show(target); //blit active interface/window
  106. }
  107. void WindowHandler::onScreenResize()
  108. {
  109. for(const auto & entry : windowsStack)
  110. entry->onScreenResize();
  111. totalRedraw();
  112. }
  113. void WindowHandler::onFrameRendered()
  114. {
  115. disposed.clear();
  116. }
  117. size_t WindowHandler::count() const
  118. {
  119. return windowsStack.size();
  120. }
  121. void WindowHandler::clear()
  122. {
  123. if(!windowsStack.empty())
  124. windowsStack.back()->deactivate();
  125. windowsStack.clear();
  126. disposed.clear();
  127. }