CGuiHandler.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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 IScreenHandler;
  24. class WindowHandler;
  25. class EventDispatcher;
  26. class InputHandler;
  27. // TODO: event handling need refactoring. Perhaps convert into delayed function call?
  28. enum class EUserEvent
  29. {
  30. RETURN_TO_MAIN_MENU,
  31. RESTART_GAME,
  32. RETURN_TO_MENU_LOAD,
  33. FULLSCREEN_TOGGLED,
  34. CAMPAIGN_START_SCENARIO,
  35. FORCE_QUIT,
  36. FAKE_MOUSE_MOVE,
  37. };
  38. // Handles GUI logic and drawing
  39. class CGuiHandler
  40. {
  41. private:
  42. /// Fake no-op version status bar, for use in windows that have no status bar
  43. std::shared_ptr<IStatusBar> fakeStatusBar;
  44. /// Status bar of current window, if any. Uses weak_ptr to allow potential hanging reference after owned window has been deleted
  45. std::weak_ptr<IStatusBar> currentStatusBar;
  46. std::unique_ptr<ShortcutHandler> shortcutsHandlerInstance;
  47. std::unique_ptr<WindowHandler> windowHandlerInstance;
  48. std::unique_ptr<IScreenHandler> screenHandlerInstance;
  49. std::unique_ptr<FramerateManager> framerateManagerInstance;
  50. std::unique_ptr<EventDispatcher> eventDispatcherInstance;
  51. std::unique_ptr<InputHandler> inputHandlerInstance;
  52. public:
  53. /// returns current position of mouse cursor, relative to vcmi window
  54. const Point & getCursorPosition() const;
  55. ShortcutHandler & shortcuts();
  56. FramerateManager & framerate();
  57. EventDispatcher & events();
  58. InputHandler & input();
  59. /// Returns current logical screen dimensions
  60. /// May not match size of window if user has UI scaling different from 100%
  61. Point screenDimensions() const;
  62. /// returns true if specified mouse button is pressed
  63. bool isMouseButtonPressed(MouseButton button) const;
  64. /// returns true if chosen keyboard key is currently pressed down
  65. bool isKeyboardAltDown() const;
  66. bool isKeyboardCtrlDown() const;
  67. bool isKeyboardShiftDown() const;
  68. void startTextInput(const Rect & where);
  69. void stopTextInput();
  70. IScreenHandler & screenHandler();
  71. WindowHandler & windows();
  72. /// Returns currently active status bar. Guaranteed to be non-null
  73. std::shared_ptr<IStatusBar> statusbar();
  74. /// Set currently active status bar
  75. void setStatusbar(std::shared_ptr<IStatusBar>);
  76. IUpdateable *curInt;
  77. ui8 defActionsDef; //default auto actions
  78. bool captureChildren; //all newly created objects will get their parents from stack and will be added to parents children list
  79. std::list<CIntObject *> createdObj; //stack of objs being created
  80. CGuiHandler();
  81. ~CGuiHandler();
  82. void init();
  83. void renderFrame();
  84. /// called whenever user selects different resolution, requiring to center/resize all windows
  85. void onScreenResize();
  86. void handleEvents(); //takes events from queue and calls interested objects
  87. void fakeMouseMove();
  88. void drawFPSCounter(); // draws the FPS to the upper left corner of the screen
  89. bool amIGuiThread();
  90. void pushUserEvent(EUserEvent usercode);
  91. void pushUserEvent(EUserEvent usercode, void * userdata);
  92. CondSh<bool> * terminate_cond; // confirm termination
  93. };
  94. extern CGuiHandler GH; //global gui handler
  95. struct SObjectConstruction
  96. {
  97. CIntObject *myObj;
  98. SObjectConstruction(CIntObject *obj);
  99. ~SObjectConstruction();
  100. };
  101. struct SSetCaptureState
  102. {
  103. bool previousCapture;
  104. ui8 prevActions;
  105. SSetCaptureState(bool allow, ui8 actions);
  106. ~SSetCaptureState();
  107. };
  108. #define OBJ_CONSTRUCTION SObjectConstruction obj__i(this)
  109. #define OBJ_CONSTRUCTION_TARGETED(obj) SObjectConstruction obj__i(obj)
  110. #define OBJECT_CONSTRUCTION_CAPTURING(actions) defActions = actions; SSetCaptureState obj__i1(true, actions); SObjectConstruction obj__i(this)
  111. #define OBJECT_CONSTRUCTION_CUSTOM_CAPTURING(actions) SSetCaptureState obj__i1(true, actions); SObjectConstruction obj__i(this)
  112. #define OBJ_CONSTRUCTION_CAPTURING_ALL_NO_DISPOSE defActions = 255 - DISPOSE; SSetCaptureState obj__i1(true, 255 - DISPOSE); SObjectConstruction obj__i(this)