CGuiHandler.h 7.0 KB

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