WindowHandler.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. /*
  2. * WindowHandler.h, 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. #pragma once
  11. class IShowActivatable;
  12. class WindowHandler
  13. {
  14. /// list of windows. front = bottom-most (background), back = top-most (foreground)
  15. /// (includes adventure map, window windows, all kind of active dialogs, and so on)
  16. std::vector<std::shared_ptr<IShowActivatable>> windowsStack;
  17. /// Temporary list of recently popped windows
  18. std::vector<std::shared_ptr<IShowActivatable>> disposed;
  19. bool totalRedrawRequested = false;
  20. /// returns top windows
  21. std::shared_ptr<IShowActivatable> topWindowImpl() const;
  22. /// forces total redraw (using showAll), sets a flag, method gets called at the end of the rendering
  23. void totalRedrawImpl();
  24. /// update only top windows and draw background from buffer, sets a flag, method gets called at the end of the rendering
  25. void simpleRedrawImpl();
  26. public:
  27. /// forces total redraw (using showAll), sets a flag, method gets called at the end of the rendering
  28. void totalRedraw();
  29. /// update only top windows and draw background from buffer, sets a flag, method gets called at the end of the rendering
  30. void simpleRedraw();
  31. /// called whenever user selects different resolution, requiring to center/resize all windows
  32. void onScreenResize();
  33. /// deactivate old top windows, activates this one and pushes to the top
  34. void pushWindow(std::shared_ptr<IShowActivatable> newInt);
  35. /// creates window of class T and pushes it to the top
  36. template <typename T, typename ... Args>
  37. void createAndPushWindow(Args && ... args);
  38. /// pops one or more windows - deactivates top, deletes and removes given number of windows, activates new front
  39. void popWindows(int howMany);
  40. /// returns true if current top window is a right-click popup
  41. bool isTopWindowPopup() const;
  42. /// removes given windows from the top and activates next
  43. void popWindow(std::shared_ptr<IShowActivatable> top);
  44. /// returns true if selected interface is on top
  45. bool isTopWindow(std::shared_ptr<IShowActivatable> window) const;
  46. bool isTopWindow(IShowActivatable * window) const;
  47. /// returns top window if it matches requested class
  48. template <typename T>
  49. std::shared_ptr<T> topWindow() const;
  50. /// should be called after frame has been rendered to screen
  51. void onFrameRendered();
  52. /// returns current number of windows in the stack
  53. size_t count() const;
  54. /// erases all currently existing windows from the stack
  55. void clear();
  56. /// returns all existing windows of selected type
  57. template <typename T>
  58. std::vector<std::shared_ptr<T>> findWindows() const;
  59. };
  60. template <typename T, typename ... Args>
  61. void WindowHandler::createAndPushWindow(Args && ... args)
  62. {
  63. auto newWindow = std::make_shared<T>(std::forward<Args>(args)...);
  64. pushWindow(newWindow);
  65. }
  66. template <typename T>
  67. std::vector<std::shared_ptr<T>> WindowHandler::findWindows() const
  68. {
  69. std::vector<std::shared_ptr<T>> result;
  70. for(const auto & window : windowsStack)
  71. {
  72. std::shared_ptr<T> casted = std::dynamic_pointer_cast<T>(window);
  73. if (casted)
  74. result.push_back(casted);
  75. }
  76. return result;
  77. }
  78. template <typename T>
  79. std::shared_ptr<T> WindowHandler::topWindow() const
  80. {
  81. return std::dynamic_pointer_cast<T>(topWindowImpl());
  82. }