CGuiHandler.h 5.5 KB

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