WindowHandler.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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 "CGuiHandler.h"
  13. #include "CIntObject.h"
  14. #include "CursorHandler.h"
  15. #include "../CMT.h"
  16. #include "../CGameInfo.h"
  17. #include "../render/Canvas.h"
  18. #include "../render/Colors.h"
  19. #include "../renderSDL/SDL_Extensions.h"
  20. void WindowHandler::popWindow(std::shared_ptr<IShowActivatable> top)
  21. {
  22. if (windowsStack.back() != top)
  23. throw std::runtime_error("Attempt to pop non-top window from stack!");
  24. top->deactivate();
  25. disposed.push_back(top);
  26. windowsStack.pop_back();
  27. if(!windowsStack.empty())
  28. windowsStack.back()->activate();
  29. totalRedraw();
  30. }
  31. void WindowHandler::pushWindow(std::shared_ptr<IShowActivatable> newInt)
  32. {
  33. if (newInt == nullptr)
  34. throw std::runtime_error("Attempt to push null window onto windows stack!");
  35. if (vstd::contains(windowsStack, newInt))
  36. throw std::runtime_error("Attempt to add already existing window to stack!");
  37. //a new interface will be present, we'll need to use buffer surface (unless it's advmapint that will alter screenBuf on activate anyway)
  38. screenBuf = screen2;
  39. if(!windowsStack.empty())
  40. windowsStack.back()->deactivate();
  41. windowsStack.push_back(newInt);
  42. CCS->curh->set(Cursor::Map::POINTER);
  43. newInt->activate();
  44. totalRedraw();
  45. }
  46. bool WindowHandler::isTopWindowPopup() const
  47. {
  48. if (windowsStack.empty())
  49. return false;
  50. return windowsStack.back()->isPopupWindow();
  51. }
  52. void WindowHandler::popWindows(int howMany)
  53. {
  54. if(!howMany)
  55. return; //senseless but who knows...
  56. assert(windowsStack.size() >= howMany);
  57. windowsStack.back()->deactivate();
  58. for(int i = 0; i < howMany; i++)
  59. {
  60. disposed.push_back(windowsStack.back());
  61. windowsStack.pop_back();
  62. }
  63. if(!windowsStack.empty())
  64. {
  65. windowsStack.back()->activate();
  66. totalRedraw();
  67. }
  68. GH.fakeMouseMove();
  69. }
  70. std::shared_ptr<IShowActivatable> WindowHandler::topWindowImpl() const
  71. {
  72. if(windowsStack.empty())
  73. return nullptr;
  74. return windowsStack.back();
  75. }
  76. bool WindowHandler::isTopWindow(std::shared_ptr<IShowActivatable> window) const
  77. {
  78. assert(window != nullptr);
  79. return !windowsStack.empty() && windowsStack.back() == window;
  80. }
  81. bool WindowHandler::isTopWindow(IShowActivatable * window) const
  82. {
  83. assert(window != nullptr);
  84. return !windowsStack.empty() && windowsStack.back().get() == window;
  85. }
  86. void WindowHandler::totalRedraw()
  87. {
  88. totalRedrawRequested = true;
  89. }
  90. void WindowHandler::totalRedrawImpl()
  91. {
  92. logGlobal->debug("totalRedraw requested!");
  93. Canvas target = Canvas::createFromSurface(screen2);
  94. for(auto & elem : windowsStack)
  95. elem->showAll(target);
  96. CSDL_Ext::blitAt(screen2, 0, 0, screen);
  97. }
  98. void WindowHandler::simpleRedraw()
  99. {
  100. if (totalRedrawRequested)
  101. totalRedrawImpl();
  102. else
  103. simpleRedrawImpl();
  104. totalRedrawRequested = false;
  105. }
  106. void WindowHandler::simpleRedrawImpl()
  107. {
  108. //update only top interface and draw background
  109. if(windowsStack.size() > 1)
  110. CSDL_Ext::blitAt(screen2, 0, 0, screen); //blit background
  111. Canvas target = Canvas::createFromSurface(screen);
  112. if(!windowsStack.empty())
  113. windowsStack.back()->show(target); //blit active interface/window
  114. }
  115. void WindowHandler::onScreenResize()
  116. {
  117. for(const auto & entry : windowsStack)
  118. entry->onScreenResize();
  119. totalRedraw();
  120. }
  121. void WindowHandler::onFrameRendered()
  122. {
  123. disposed.clear();
  124. }
  125. size_t WindowHandler::count() const
  126. {
  127. return windowsStack.size();
  128. }
  129. void WindowHandler::clear()
  130. {
  131. if(!windowsStack.empty())
  132. windowsStack.back()->deactivate();
  133. windowsStack.clear();
  134. disposed.clear();
  135. }