CGuiHandler.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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. typedef std::list<CIntObject *> CIntObjectList;
  46. //active GUI elements (listening for events
  47. CIntObjectList lclickable,
  48. rclickable,
  49. mclickable,
  50. hoverable,
  51. keyinterested,
  52. motioninterested,
  53. timeinterested,
  54. wheelInterested,
  55. doubleClickInterested,
  56. textInterested;
  57. void handleMouseButtonClick(CIntObjectList & interestedObjs, EIntObjMouseBtnType btn, bool isPressed);
  58. void processLists(const ui16 activityFlag, std::function<void(std::list<CIntObject *> *)> cb);
  59. public:
  60. void handleElementActivate(CIntObject * elem, ui16 activityFlag);
  61. void handleElementDeActivate(CIntObject * elem, ui16 activityFlag);
  62. public:
  63. //objs to blit
  64. std::vector<IShowable *> objsToBlit;
  65. SDL_Event * current; //current event - can be set to nullptr to stop handling event
  66. IUpdateable * curInt;
  67. Point lastClick;
  68. unsigned lastClickTime;
  69. ui8 defActionsDef; //default auto actions
  70. bool captureChildren; //all newly created objects will get their parents from stack and will be added to parents children list
  71. std::list<CIntObject *> createdObj; //stack of objs being created
  72. CGuiHandler();
  73. ~CGuiHandler();
  74. void renderFrame();
  75. void totalRedraw(); //forces total redraw (using showAll), sets a flag, method gets called at the end of the rendering
  76. void simpleRedraw(); //update only top interface and draw background from buffer, sets a flag, method gets called at the end of the rendering
  77. void popInt(IShowActivatable * top); //removes given interface from the top and activates next
  78. void popIntTotally(IShowActivatable * top); //deactivates, deletes, removes given interface from the top and activates next
  79. void pushInt(IShowActivatable * newInt); //deactivate old top interface, activates this one and pushes to the top
  80. void popInts(int howMany); //pops one or more interfaces - deactivates top, deletes and removes given number of interfaces, activates new front
  81. IShowActivatable * topInt(); //returns top interface
  82. void updateTime(); //handles timeInterested
  83. void handleEvents(); //takes events from queue and calls interested objects
  84. void handleEvent(SDL_Event * sEvent);
  85. void handleMouseMotion(SDL_Event * sEvent);
  86. void handleMoveInterested(const SDL_MouseMotionEvent & motion);
  87. void fakeMouseMove();
  88. void breakEventHandling(); //current event won't be propagated anymore
  89. void drawFPSCounter(); // draws the FPS to the upper left corner of the screen
  90. static SDL_Keycode arrowToNum(SDL_Keycode key); //converts arrow key to according numpad key
  91. static SDL_Keycode numToDigit(SDL_Keycode key); //converts numpad digit key to normal digit key
  92. static bool isNumKey(SDL_Keycode key, bool number = true); //checks if key is on numpad (numbers - check only for numpad digits)
  93. static bool isArrowKey(SDL_Keycode key);
  94. static bool amIGuiThread();
  95. static void pushSDLEvent(int type, int usercode = 0);
  96. static CondSh<bool> terminate_cond; // confirm termination
  97. };
  98. extern CGuiHandler GH; //global gui handler
  99. template<typename T> void pushIntT()
  100. {
  101. GH.pushInt(new T());
  102. }
  103. struct SObjectConstruction
  104. {
  105. CIntObject * myObj;
  106. SObjectConstruction(CIntObject * obj);
  107. ~SObjectConstruction();
  108. };
  109. struct SSetCaptureState
  110. {
  111. bool previousCapture;
  112. ui8 prevActions;
  113. SSetCaptureState(bool allow, ui8 actions);
  114. ~SSetCaptureState();
  115. };
  116. #define OBJ_CONSTRUCTION SObjectConstruction obj__i(this)
  117. #define OBJ_CONSTRUCTION_CAPTURING_ALL defActions = 255; SSetCaptureState obj__i1(true, 255); SObjectConstruction obj__i(this)
  118. #define BLOCK_CAPTURING SSetCaptureState obj__i(false, 0)
  119. #define BLOCK_CAPTURING_DONT_TOUCH_REC_ACTIONS SSetCaptureState obj__i(false, GH.defActionsDef)