WindowHandler.h 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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. /// returns true if current top window is a right-click popup
  36. bool isTopWindowPopup() const;
  37. /// removes given windows from the top and activates next
  38. void popWindow(std::shared_ptr<IShowActivatable> top);
  39. /// returns true if selected interface is on top
  40. bool isTopWindow(std::shared_ptr<IShowActivatable> window) const;
  41. bool isTopWindow(IShowActivatable * window) const;
  42. /// returns top window if it matches requested class
  43. template <typename T>
  44. std::shared_ptr<T> topWindow() const;
  45. /// should be called after frame has been rendered to screen
  46. void onFrameRendered();
  47. /// returns current number of windows in the stack
  48. size_t count() const;
  49. /// erases all currently existing windows from the stack
  50. void clear();
  51. /// returns all existing windows of selected type
  52. template <typename T>
  53. std::vector<std::shared_ptr<T>> findWindows() const;
  54. };
  55. template <typename T, typename ... Args>
  56. void WindowHandler::createAndPushWindow(Args && ... args)
  57. {
  58. auto newWindow = std::make_shared<T>(std::forward<Args>(args)...);
  59. pushWindow(newWindow);
  60. }
  61. template <typename T>
  62. std::vector<std::shared_ptr<T>> WindowHandler::findWindows() const
  63. {
  64. std::vector<std::shared_ptr<T>> result;
  65. for(const auto & window : windowsStack)
  66. {
  67. std::shared_ptr<T> casted = std::dynamic_pointer_cast<T>(window);
  68. if (casted)
  69. result.push_back(casted);
  70. }
  71. return result;
  72. }
  73. template <typename T>
  74. std::shared_ptr<T> WindowHandler::topWindow() const
  75. {
  76. return std::dynamic_pointer_cast<T>(topWindowImpl());
  77. }