CGuiHandler.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /*
  2. * CGuiHandler.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. VCMI_LIB_NAMESPACE_BEGIN
  12. template <typename T> struct CondSh;
  13. class Point;
  14. class Rect;
  15. VCMI_LIB_NAMESPACE_END
  16. enum class MouseButton;
  17. class ShortcutHandler;
  18. class FramerateManager;
  19. class IStatusBar;
  20. class CIntObject;
  21. class IUpdateable;
  22. class IShowActivatable;
  23. class IRenderHandler;
  24. class IScreenHandler;
  25. class WindowHandler;
  26. class EventDispatcher;
  27. class InputHandler;
  28. // Handles GUI logic and drawing
  29. class CGuiHandler
  30. {
  31. private:
  32. /// Fake no-op version status bar, for use in windows that have no status bar
  33. std::shared_ptr<IStatusBar> fakeStatusBar;
  34. /// Status bar of current window, if any. Uses weak_ptr to allow potential hanging reference after owned window has been deleted
  35. std::weak_ptr<IStatusBar> currentStatusBar;
  36. std::unique_ptr<ShortcutHandler> shortcutsHandlerInstance;
  37. std::unique_ptr<WindowHandler> windowHandlerInstance;
  38. std::unique_ptr<IScreenHandler> screenHandlerInstance;
  39. std::unique_ptr<IRenderHandler> renderHandlerInstance;
  40. std::unique_ptr<FramerateManager> framerateManagerInstance;
  41. std::unique_ptr<EventDispatcher> eventDispatcherInstance;
  42. std::unique_ptr<InputHandler> inputHandlerInstance;
  43. public:
  44. boost::mutex interfaceMutex;
  45. /// returns current position of mouse cursor, relative to vcmi window
  46. const Point & getCursorPosition() const;
  47. ShortcutHandler & shortcuts();
  48. FramerateManager & framerate();
  49. EventDispatcher & events();
  50. InputHandler & input();
  51. /// Returns current logical screen dimensions
  52. /// May not match size of window if user has UI scaling different from 100%
  53. Point screenDimensions() const;
  54. /// returns true if chosen keyboard key is currently pressed down
  55. bool isKeyboardAltDown() const;
  56. bool isKeyboardCtrlDown() const;
  57. bool isKeyboardShiftDown() const;
  58. void startTextInput(const Rect & where);
  59. void stopTextInput();
  60. IScreenHandler & screenHandler();
  61. IRenderHandler & renderHandler();
  62. WindowHandler & windows();
  63. /// Returns currently active status bar. Guaranteed to be non-null
  64. std::shared_ptr<IStatusBar> statusbar();
  65. /// Set currently active status bar
  66. void setStatusbar(std::shared_ptr<IStatusBar>);
  67. IUpdateable *curInt;
  68. ui8 defActionsDef; //default auto actions
  69. bool captureChildren; //all newly created objects will get their parents from stack and will be added to parents children list
  70. std::list<CIntObject *> createdObj; //stack of objs being created
  71. CGuiHandler();
  72. ~CGuiHandler();
  73. void init();
  74. void renderFrame();
  75. /// called whenever user selects different resolution, requiring to center/resize all windows
  76. void onScreenResize();
  77. void handleEvents(); //takes events from queue and calls interested objects
  78. void fakeMouseMove();
  79. void drawFPSCounter(); // draws the FPS to the upper left corner of the screen
  80. bool amIGuiThread();
  81. /// Calls provided functor in main thread on next execution frame
  82. void dispatchMainThread(const std::function<void()> & functor);
  83. };
  84. extern CGuiHandler GH; //global gui handler
  85. struct SObjectConstruction
  86. {
  87. CIntObject *myObj;
  88. SObjectConstruction(CIntObject *obj);
  89. ~SObjectConstruction();
  90. };
  91. struct SSetCaptureState
  92. {
  93. bool previousCapture;
  94. ui8 prevActions;
  95. SSetCaptureState(bool allow, ui8 actions);
  96. ~SSetCaptureState();
  97. };
  98. #define OBJ_CONSTRUCTION SObjectConstruction obj__i(this)
  99. #define OBJ_CONSTRUCTION_TARGETED(obj) SObjectConstruction obj__i(obj)
  100. #define OBJECT_CONSTRUCTION_CAPTURING(actions) defActions = actions; SSetCaptureState obj__i1(true, actions); SObjectConstruction obj__i(this)
  101. #define OBJECT_CONSTRUCTION_CUSTOM_CAPTURING(actions) SSetCaptureState obj__i1(true, actions); SObjectConstruction obj__i(this)
  102. #define OBJ_CONSTRUCTION_CAPTURING_ALL_NO_DISPOSE defActions = 255 - DISPOSE; SSetCaptureState obj__i1(true, 255 - DISPOSE); SObjectConstruction obj__i(this)