CGuiHandler.h 4.9 KB

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