CGuiHandler.h 4.5 KB

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