CGuiHandler.h 5.1 KB

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