CGuiHandler.h 4.9 KB

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