CGuiHandler.h 5.7 KB

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