WindowHandler.cpp 3.5 KB

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