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