CGuiHandler.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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 IShowable;
  26. class IScreenHandler;
  27. class WindowHandler;
  28. class InterfaceEventDispatcher;
  29. // TODO: event handling need refactoring
  30. enum class EUserEvent
  31. {
  32. /*CHANGE_SCREEN_RESOLUTION = 1,*/
  33. RETURN_TO_MAIN_MENU = 2,
  34. //STOP_CLIENT = 3,
  35. RESTART_GAME = 4,
  36. RETURN_TO_MENU_LOAD,
  37. FULLSCREEN_TOGGLED,
  38. CAMPAIGN_START_SCENARIO,
  39. FORCE_QUIT, //quit client without question
  40. };
  41. // Handles GUI logic and drawing
  42. class CGuiHandler
  43. {
  44. friend class InterfaceEventDispatcher;
  45. private:
  46. /// Fake no-op version status bar, for use in windows that have no status bar
  47. std::shared_ptr<IStatusBar> fakeStatusBar;
  48. /// Status bar of current window, if any. Uses weak_ptr to allow potential hanging reference after owned window has been deleted
  49. std::weak_ptr<IStatusBar> currentStatusBar;
  50. Point cursorPosition;
  51. uint32_t mouseButtonsMask;
  52. std::unique_ptr<ShortcutHandler> shortcutsHandlerInstance;
  53. std::unique_ptr<WindowHandler> windowHandlerInstance;
  54. std::atomic<bool> continueEventHandling;
  55. std::unique_ptr<IScreenHandler> screenHandlerInstance;
  56. std::unique_ptr<FramerateManager> framerateManagerInstance;
  57. std::unique_ptr<InterfaceEventDispatcher> eventDispatcherInstance;
  58. void handleCurrentEvent(SDL_Event &current);
  59. void convertTouchToMouse(SDL_Event * current);
  60. void fakeMoveCursor(float dx, float dy);
  61. void fakeMouseButtonEventRelativeMode(bool down, bool right);
  62. void handleEventKeyDown(SDL_Event & current);
  63. void handleEventKeyUp(SDL_Event & current);
  64. void handleEventMouseMotion(SDL_Event & current);
  65. void handleEventMouseButtonDown(SDL_Event & current);
  66. void handleEventMouseWheel(SDL_Event & current);
  67. void handleEventTextInput(SDL_Event & current);
  68. void handleEventTextEditing(SDL_Event & current);
  69. void handleEventMouseButtonUp(SDL_Event & current);
  70. void handleEventFingerMotion(SDL_Event & current);
  71. void handleEventFingerDown(SDL_Event & current);
  72. void handleEventFingerUp(SDL_Event & current);
  73. public:
  74. /// returns current position of mouse cursor, relative to vcmi window
  75. const Point & getCursorPosition() const;
  76. ShortcutHandler & shortcutsHandler();
  77. FramerateManager & framerateManager();
  78. InterfaceEventDispatcher & eventDispatcher();
  79. /// Returns current logical screen dimensions
  80. /// May not match size of window if user has UI scaling different from 100%
  81. Point screenDimensions() const;
  82. /// returns true if at least one mouse button is pressed
  83. bool isMouseButtonPressed() const;
  84. /// returns true if specified mouse button is pressed
  85. bool isMouseButtonPressed(MouseButton button) const;
  86. /// returns true if chosen keyboard key is currently pressed down
  87. bool isKeyboardAltDown() const;
  88. bool isKeyboardCtrlDown() const;
  89. bool isKeyboardShiftDown() const;
  90. void startTextInput(const Rect & where);
  91. void stopTextInput();
  92. /// moves mouse pointer into specified position inside vcmi window
  93. void moveCursorToPosition(const Point & position);
  94. IScreenHandler & screenHandler();
  95. WindowHandler & windows();
  96. /// Returns currently active status bar. Guaranteed to be non-null
  97. std::shared_ptr<IStatusBar> statusbar();
  98. /// Set currently active status bar
  99. void setStatusbar(std::shared_ptr<IStatusBar>);
  100. IUpdateable *curInt;
  101. Point lastClick;
  102. unsigned lastClickTime;
  103. bool multifinger;
  104. bool isPointerRelativeMode;
  105. float pointerSpeedMultiplier;
  106. ui8 defActionsDef; //default auto actions
  107. bool captureChildren; //all newly created objects will get their parents from stack and will be added to parents children list
  108. std::list<CIntObject *> createdObj; //stack of objs being created
  109. CGuiHandler();
  110. ~CGuiHandler();
  111. void init();
  112. void renderFrame();
  113. /// called whenever user selects different resolution, requiring to center/resize all windows
  114. void onScreenResize();
  115. void updateTime();
  116. void handleEvents(); //takes events from queue and calls interested objects
  117. void fakeMouseMove();
  118. void breakEventHandling(); //current event won't be propagated anymore
  119. void drawFPSCounter(); // draws the FPS to the upper left corner of the screen
  120. static bool amIGuiThread();
  121. static void pushUserEvent(EUserEvent usercode);
  122. static void pushUserEvent(EUserEvent usercode, void * userdata);
  123. CondSh<bool> * terminate_cond; // confirm termination
  124. };
  125. extern CGuiHandler GH; //global gui handler
  126. struct SObjectConstruction
  127. {
  128. CIntObject *myObj;
  129. SObjectConstruction(CIntObject *obj);
  130. ~SObjectConstruction();
  131. };
  132. struct SSetCaptureState
  133. {
  134. bool previousCapture;
  135. ui8 prevActions;
  136. SSetCaptureState(bool allow, ui8 actions);
  137. ~SSetCaptureState();
  138. };
  139. #define OBJ_CONSTRUCTION SObjectConstruction obj__i(this)
  140. #define OBJ_CONSTRUCTION_TARGETED(obj) SObjectConstruction obj__i(obj)
  141. #define OBJECT_CONSTRUCTION_CAPTURING(actions) defActions = actions; SSetCaptureState obj__i1(true, actions); SObjectConstruction obj__i(this)
  142. #define OBJECT_CONSTRUCTION_CUSTOM_CAPTURING(actions) SSetCaptureState obj__i1(true, actions); SObjectConstruction obj__i(this)
  143. #define OBJ_CONSTRUCTION_CAPTURING_ALL_NO_DISPOSE defActions = 255 - DISPOSE; SSetCaptureState obj__i1(true, 255 - DISPOSE); SObjectConstruction obj__i(this)