CGuiHandler.h 6.1 KB

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