CGuiHandler.h 6.6 KB

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