CGuiHandler.h 5.0 KB

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