CGuiHandler.h 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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 CFramerateManager;
  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. // A fps manager which holds game updates at a constant rate
  40. class CFramerateManager
  41. {
  42. private:
  43. double rateticks;
  44. ui32 lastticks;
  45. ui32 timeElapsed;
  46. int rate;
  47. int fps; // the actual fps value
  48. ui32 accumulatedTime;
  49. ui32 accumulatedFrames;
  50. public:
  51. CFramerateManager(int newRate); // initializes the manager with a given fps rate
  52. void init(int newRate); // needs to be called directly before the main game loop to reset the internal timer
  53. void framerateDelay(); // needs to be called every game update cycle
  54. ui32 getElapsedMilliseconds() const {return this->timeElapsed;}
  55. ui32 getFrameNumber() const { return accumulatedFrames; }
  56. ui32 getFramerate() const { return fps; };
  57. };
  58. // Handles GUI logic and drawing
  59. class CGuiHandler
  60. {
  61. public:
  62. CFramerateManager * mainFPSmng; //to keep const framerate
  63. 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)
  64. std::shared_ptr<IStatusBar> statusbar;
  65. private:
  66. Point cursorPosition;
  67. uint32_t mouseButtonsMask;
  68. std::vector<std::shared_ptr<IShowActivatable>> disposed;
  69. std::unique_ptr<ShortcutHandler> shortcutsHandlerInstance;
  70. std::atomic<bool> continueEventHandling;
  71. using CIntObjectList = std::list<CIntObject *>;
  72. //active GUI elements (listening for events
  73. CIntObjectList lclickable;
  74. CIntObjectList rclickable;
  75. CIntObjectList mclickable;
  76. CIntObjectList hoverable;
  77. CIntObjectList keyinterested;
  78. CIntObjectList motioninterested;
  79. CIntObjectList timeinterested;
  80. CIntObjectList wheelInterested;
  81. CIntObjectList doubleClickInterested;
  82. CIntObjectList textInterested;
  83. std::unique_ptr<IScreenHandler> screenHandlerInstance;
  84. void handleMouseButtonClick(CIntObjectList & interestedObjs, MouseButton btn, bool isPressed);
  85. void processLists(const ui16 activityFlag, std::function<void (std::list<CIntObject*> *)> cb);
  86. void handleCurrentEvent(SDL_Event &current);
  87. void handleMouseMotion(const SDL_Event & current);
  88. void handleMoveInterested( const SDL_MouseMotionEvent & motion );
  89. void convertTouchToMouse(SDL_Event * current);
  90. void fakeMoveCursor(float dx, float dy);
  91. void fakeMouseButtonEventRelativeMode(bool down, bool right);
  92. public:
  93. void handleElementActivate(CIntObject * elem, ui16 activityFlag);
  94. void handleElementDeActivate(CIntObject * elem, ui16 activityFlag);
  95. public:
  96. //objs to blit
  97. std::vector<std::shared_ptr<IShowActivatable>> objsToBlit;
  98. /// returns current position of mouse cursor, relative to vcmi window
  99. const Point & getCursorPosition() const;
  100. ShortcutHandler & shortcutsHandler();
  101. Point screenDimensions() const;
  102. /// returns true if at least one mouse button is pressed
  103. bool isMouseButtonPressed() const;
  104. /// returns true if specified mouse button is pressed
  105. bool isMouseButtonPressed(MouseButton button) const;
  106. /// returns true if chosen keyboard key is currently pressed down
  107. bool isKeyboardAltDown() const;
  108. bool isKeyboardCtrlDown() const;
  109. bool isKeyboardShiftDown() const;
  110. void startTextInput(const Rect & where);
  111. void stopTextInput();
  112. /// moves mouse pointer into specified position inside vcmi window
  113. void moveCursorToPosition(const Point & position);
  114. IScreenHandler & screenHandler();
  115. IUpdateable *curInt;
  116. Point lastClick;
  117. unsigned lastClickTime;
  118. bool multifinger;
  119. bool isPointerRelativeMode;
  120. float pointerSpeedMultiplier;
  121. ui8 defActionsDef; //default auto actions
  122. bool captureChildren; //all newly created objects will get their parents from stack and will be added to parents children list
  123. std::list<CIntObject *> createdObj; //stack of objs being created
  124. CGuiHandler();
  125. ~CGuiHandler();
  126. void init();
  127. void renderFrame();
  128. void totalRedraw(); //forces total redraw (using showAll), sets a flag, method gets called at the end of the rendering
  129. void simpleRedraw(); //update only top interface and draw background from buffer, sets a flag, method gets called at the end of the rendering
  130. /// called whenever user selects different resolution, requiring to center/resize all windows
  131. void onScreenResize();
  132. void pushInt(std::shared_ptr<IShowActivatable> newInt); //deactivate old top interface, activates this one and pushes to the top
  133. template <typename T, typename ... Args>
  134. void pushIntT(Args && ... args)
  135. {
  136. auto newInt = std::make_shared<T>(std::forward<Args>(args)...);
  137. pushInt(newInt);
  138. }
  139. void popInts(int howMany); //pops one or more interfaces - deactivates top, deletes and removes given number of interfaces, activates new front
  140. void popInt(std::shared_ptr<IShowActivatable> top); //removes given interface from the top and activates next
  141. std::shared_ptr<IShowActivatable> topInt(); //returns top interface
  142. void updateTime(); //handles timeInterested
  143. void handleEvents(); //takes events from queue and calls interested objects
  144. void fakeMouseMove();
  145. void breakEventHandling(); //current event won't be propagated anymore
  146. void drawFPSCounter(); // draws the FPS to the upper left corner of the screen
  147. static bool amIGuiThread();
  148. static void pushUserEvent(EUserEvent usercode);
  149. static void pushUserEvent(EUserEvent usercode, void * userdata);
  150. CondSh<bool> * terminate_cond; // confirm termination
  151. };
  152. extern CGuiHandler GH; //global gui handler
  153. struct SObjectConstruction
  154. {
  155. CIntObject *myObj;
  156. SObjectConstruction(CIntObject *obj);
  157. ~SObjectConstruction();
  158. };
  159. struct SSetCaptureState
  160. {
  161. bool previousCapture;
  162. ui8 prevActions;
  163. SSetCaptureState(bool allow, ui8 actions);
  164. ~SSetCaptureState();
  165. };
  166. #define OBJ_CONSTRUCTION SObjectConstruction obj__i(this)
  167. #define OBJ_CONSTRUCTION_TARGETED(obj) SObjectConstruction obj__i(obj)
  168. #define OBJECT_CONSTRUCTION_CAPTURING(actions) defActions = actions; SSetCaptureState obj__i1(true, actions); SObjectConstruction obj__i(this)
  169. #define OBJECT_CONSTRUCTION_CUSTOM_CAPTURING(actions) SSetCaptureState obj__i1(true, actions); SObjectConstruction obj__i(this)
  170. #define OBJ_CONSTRUCTION_CAPTURING_ALL_NO_DISPOSE defActions = 255 - DISPOSE; SSetCaptureState obj__i1(true, 255 - DISPOSE); SObjectConstruction obj__i(this)