WindowHandler.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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. Canvas target = Canvas::createFromSurface(screen2);
  91. for(auto & elem : windowsStack)
  92. elem->showAll(target);
  93. CSDL_Ext::blitAt(screen2, 0, 0, screen);
  94. }
  95. void WindowHandler::simpleRedraw()
  96. {
  97. if (totalRedrawRequested)
  98. totalRedrawImpl();
  99. else
  100. simpleRedrawImpl();
  101. totalRedrawRequested = false;
  102. }
  103. void WindowHandler::simpleRedrawImpl()
  104. {
  105. //update only top interface and draw background
  106. if(windowsStack.size() > 1)
  107. CSDL_Ext::blitAt(screen2, 0, 0, screen); //blit background
  108. Canvas target = Canvas::createFromSurface(screen);
  109. if(!windowsStack.empty())
  110. windowsStack.back()->show(target); //blit active interface/window
  111. }
  112. void WindowHandler::onScreenResize()
  113. {
  114. for(const auto & entry : windowsStack)
  115. entry->onScreenResize();
  116. totalRedraw();
  117. }
  118. void WindowHandler::onFrameRendered()
  119. {
  120. disposed.clear();
  121. }
  122. size_t WindowHandler::count() const
  123. {
  124. return windowsStack.size();
  125. }
  126. void WindowHandler::clear()
  127. {
  128. if(!windowsStack.empty())
  129. windowsStack.back()->deactivate();
  130. windowsStack.clear();
  131. disposed.clear();
  132. }