WindowHandler.h 2.9 KB

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