WindowHandler.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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. void WindowHandler::popWindows(int howMany)
  44. {
  45. if(!howMany)
  46. return; //senseless but who knows...
  47. assert(windowsStack.size() >= howMany);
  48. windowsStack.back()->deactivate();
  49. for(int i = 0; i < howMany; i++)
  50. {
  51. disposed.push_back(windowsStack.back());
  52. windowsStack.pop_back();
  53. }
  54. if(!windowsStack.empty())
  55. {
  56. windowsStack.back()->activate();
  57. totalRedraw();
  58. }
  59. GH.fakeMouseMove();
  60. }
  61. std::shared_ptr<IShowActivatable> WindowHandler::topWindowImpl() const
  62. {
  63. if(windowsStack.empty())
  64. return nullptr;
  65. return windowsStack.back();
  66. }
  67. bool WindowHandler::isTopWindow(std::shared_ptr<IShowActivatable> window) const
  68. {
  69. assert(window != nullptr);
  70. return !windowsStack.empty() && windowsStack.back() == window;
  71. }
  72. bool WindowHandler::isTopWindow(IShowActivatable * window) const
  73. {
  74. assert(window != nullptr);
  75. return !windowsStack.empty() && windowsStack.back().get() == window;
  76. }
  77. void WindowHandler::totalRedraw()
  78. {
  79. CSDL_Ext::fillSurface(screen2, Colors::BLACK);
  80. Canvas target = Canvas::createFromSurface(screen2);
  81. for(auto & elem : windowsStack)
  82. elem->showAll(target);
  83. CSDL_Ext::blitAt(screen2, 0, 0, screen);
  84. }
  85. void WindowHandler::simpleRedraw()
  86. {
  87. //update only top interface and draw background
  88. if(windowsStack.size() > 1)
  89. CSDL_Ext::blitAt(screen2, 0, 0, screen); //blit background
  90. Canvas target = Canvas::createFromSurface(screen);
  91. if(!windowsStack.empty())
  92. windowsStack.back()->show(target); //blit active interface/window
  93. }
  94. void WindowHandler::onScreenResize()
  95. {
  96. for(const auto & entry : windowsStack)
  97. entry->onScreenResize();
  98. totalRedraw();
  99. }
  100. void WindowHandler::onFrameRendered()
  101. {
  102. disposed.clear();
  103. }
  104. size_t WindowHandler::count() const
  105. {
  106. return windowsStack.size();
  107. }
  108. void WindowHandler::clear()
  109. {
  110. if(!windowsStack.empty())
  111. windowsStack.back()->deactivate();
  112. windowsStack.clear();
  113. disposed.clear();
  114. }