CGuiHandler.h 7.0 KB

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