CGuiHandler.h 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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. // TODO: event handling need refactoring
  28. enum class EUserEvent
  29. {
  30. /*CHANGE_SCREEN_RESOLUTION = 1,*/
  31. RETURN_TO_MAIN_MENU = 2,
  32. //STOP_CLIENT = 3,
  33. RESTART_GAME = 4,
  34. RETURN_TO_MENU_LOAD,
  35. FULLSCREEN_TOGGLED,
  36. CAMPAIGN_START_SCENARIO,
  37. FORCE_QUIT, //quit client without question
  38. };
  39. // Handles GUI logic and drawing
  40. class CGuiHandler
  41. {
  42. public:
  43. std::list<std::shared_ptr<IShowActivatable>> listInt; //list of interfaces - front=foreground; back = background (includes adventure map, window interfaces, all kind of active dialogs, and so on)
  44. std::shared_ptr<IStatusBar> statusbar;
  45. private:
  46. Point cursorPosition;
  47. uint32_t mouseButtonsMask;
  48. std::vector<std::shared_ptr<IShowActivatable>> disposed;
  49. std::unique_ptr<ShortcutHandler> shortcutsHandlerInstance;
  50. std::atomic<bool> continueEventHandling;
  51. using CIntObjectList = std::list<CIntObject *>;
  52. //active GUI elements (listening for events
  53. CIntObjectList lclickable;
  54. CIntObjectList rclickable;
  55. CIntObjectList mclickable;
  56. CIntObjectList hoverable;
  57. CIntObjectList keyinterested;
  58. CIntObjectList motioninterested;
  59. CIntObjectList timeinterested;
  60. CIntObjectList wheelInterested;
  61. CIntObjectList doubleClickInterested;
  62. CIntObjectList textInterested;
  63. std::unique_ptr<IScreenHandler> screenHandlerInstance;
  64. std::unique_ptr<FramerateManager> framerateManagerInstance;
  65. void handleMouseButtonClick(CIntObjectList & interestedObjs, MouseButton btn, bool isPressed);
  66. void processLists(const ui16 activityFlag, std::function<void (std::list<CIntObject*> *)> cb);
  67. void handleCurrentEvent(SDL_Event &current);
  68. void handleMouseMotion(const SDL_Event & current);
  69. void handleMoveInterested( const SDL_MouseMotionEvent & motion );
  70. void convertTouchToMouse(SDL_Event * current);
  71. void fakeMoveCursor(float dx, float dy);
  72. void fakeMouseButtonEventRelativeMode(bool down, bool right);
  73. public:
  74. void handleElementActivate(CIntObject * elem, ui16 activityFlag);
  75. void handleElementDeActivate(CIntObject * elem, ui16 activityFlag);
  76. public:
  77. //objs to blit
  78. std::vector<std::shared_ptr<IShowActivatable>> objsToBlit;
  79. /// returns current position of mouse cursor, relative to vcmi window
  80. const Point & getCursorPosition() const;
  81. ShortcutHandler & shortcutsHandler();
  82. FramerateManager & framerateManager();
  83. uint32_t getFrameDeltaMilliseconds() const;
  84. Point screenDimensions() const;
  85. /// returns true if at least one mouse button is pressed
  86. bool isMouseButtonPressed() const;
  87. /// returns true if specified mouse button is pressed
  88. bool isMouseButtonPressed(MouseButton button) const;
  89. /// returns true if chosen keyboard key is currently pressed down
  90. bool isKeyboardAltDown() const;
  91. bool isKeyboardCtrlDown() const;
  92. bool isKeyboardShiftDown() const;
  93. void startTextInput(const Rect & where);
  94. void stopTextInput();
  95. /// moves mouse pointer into specified position inside vcmi window
  96. void moveCursorToPosition(const Point & position);
  97. IScreenHandler & screenHandler();
  98. IUpdateable *curInt;
  99. Point lastClick;
  100. unsigned lastClickTime;
  101. bool multifinger;
  102. bool isPointerRelativeMode;
  103. float pointerSpeedMultiplier;
  104. ui8 defActionsDef; //default auto actions
  105. bool captureChildren; //all newly created objects will get their parents from stack and will be added to parents children list
  106. std::list<CIntObject *> createdObj; //stack of objs being created
  107. CGuiHandler();
  108. ~CGuiHandler();
  109. void init();
  110. void renderFrame();
  111. void totalRedraw(); //forces total redraw (using showAll), sets a flag, method gets called at the end of the rendering
  112. void simpleRedraw(); //update only top interface and draw background from buffer, sets a flag, method gets called at the end of the rendering
  113. /// called whenever user selects different resolution, requiring to center/resize all windows
  114. void onScreenResize();
  115. void pushInt(std::shared_ptr<IShowActivatable> newInt); //deactivate old top interface, activates this one and pushes to the top
  116. template <typename T, typename ... Args>
  117. void pushIntT(Args && ... args)
  118. {
  119. auto newInt = std::make_shared<T>(std::forward<Args>(args)...);
  120. pushInt(newInt);
  121. }
  122. void popInts(int howMany); //pops one or more interfaces - deactivates top, deletes and removes given number of interfaces, activates new front
  123. void popInt(std::shared_ptr<IShowActivatable> top); //removes given interface from the top and activates next
  124. std::shared_ptr<IShowActivatable> topInt(); //returns top interface
  125. void updateTime(); //handles timeInterested
  126. void handleEvents(); //takes events from queue and calls interested objects
  127. void fakeMouseMove();
  128. void breakEventHandling(); //current event won't be propagated anymore
  129. void drawFPSCounter(); // draws the FPS to the upper left corner of the screen
  130. static bool amIGuiThread();
  131. static void pushUserEvent(EUserEvent usercode);
  132. static void pushUserEvent(EUserEvent usercode, void * userdata);
  133. CondSh<bool> * terminate_cond; // confirm termination
  134. };
  135. extern CGuiHandler GH; //global gui handler
  136. struct SObjectConstruction
  137. {
  138. CIntObject *myObj;
  139. SObjectConstruction(CIntObject *obj);
  140. ~SObjectConstruction();
  141. };
  142. struct SSetCaptureState
  143. {
  144. bool previousCapture;
  145. ui8 prevActions;
  146. SSetCaptureState(bool allow, ui8 actions);
  147. ~SSetCaptureState();
  148. };
  149. #define OBJ_CONSTRUCTION SObjectConstruction obj__i(this)
  150. #define OBJ_CONSTRUCTION_TARGETED(obj) SObjectConstruction obj__i(obj)
  151. #define OBJECT_CONSTRUCTION_CAPTURING(actions) defActions = actions; SSetCaptureState obj__i1(true, actions); SObjectConstruction obj__i(this)
  152. #define OBJECT_CONSTRUCTION_CUSTOM_CAPTURING(actions) SSetCaptureState obj__i1(true, actions); SObjectConstruction obj__i(this)
  153. #define OBJ_CONSTRUCTION_CAPTURING_ALL_NO_DISPOSE defActions = 255 - DISPOSE; SSetCaptureState obj__i1(true, 255 - DISPOSE); SObjectConstruction obj__i(this)