CGuiHandler.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. #pragma once
  2. #include "../../lib/CStopWatch.h"
  3. #include "Geometries.h"
  4. class CFramerateManager;
  5. class IStatusBar;
  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. double 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. IStatusBar * statusbar;
  40. //active GUI elements (listening for events
  41. std::list<CIntObject*> lclickable;
  42. std::list<CIntObject*> rclickable;
  43. std::list<CIntObject*> hoverable;
  44. std::list<CIntObject*> keyinterested;
  45. std::list<CIntObject*> motioninterested;
  46. std::list<CIntObject*> timeinterested;
  47. std::list<CIntObject*> wheelInterested;
  48. std::list<CIntObject*> doubleClickInterested;
  49. //objs to blit
  50. std::vector<IShowable*> objsToBlit;
  51. SDL_Event * current; //current event - can be set to NULL to stop handling event
  52. IUpdateable *curInt;
  53. Point lastClick;
  54. unsigned lastClickTime;
  55. bool terminate;
  56. CGuiHandler();
  57. ~CGuiHandler();
  58. void run(); // holds the main loop for the whole program after initialization and manages the update/rendering system
  59. void totalRedraw(); //forces total redraw (using showAll), sets a flag, method gets called at the end of the rendering
  60. void simpleRedraw(); //update only top interface and draw background from buffer, sets a flag, method gets called at the end of the rendering
  61. void popInt(IShowActivatable *top); //removes given interface from the top and activates next
  62. void popIntTotally(IShowActivatable *top); //deactivates, deletes, removes given interface from the top and activates next
  63. void pushInt(IShowActivatable *newInt); //deactivate old top interface, activates this one and pushes to the top
  64. void popInts(int howMany); //pops one or more interfaces - deactivates top, deletes and removes given number of interfaces, activates new front
  65. IShowActivatable *topInt(); //returns top interface
  66. void updateTime(); //handles timeInterested
  67. void handleEvents(); //takes events from queue and calls interested objects
  68. void handleEvent(SDL_Event *sEvent);
  69. void handleMouseMotion(SDL_Event *sEvent);
  70. void handleMoveInterested( const SDL_MouseMotionEvent & motion );
  71. void fakeMouseMove();
  72. void breakEventHandling(); //current event won't be propagated anymore
  73. void drawFPSCounter(); // draws the FPS to the upper left corner of the screen
  74. ui8 defActionsDef; //default auto actions
  75. ui8 captureChildren; //all newly created objects will get their parents from stack and will be added to parents children list
  76. std::list<CIntObject *> createdObj; //stack of objs being created
  77. static SDLKey arrowToNum(SDLKey key); //converts arrow key to according numpad key
  78. static SDLKey numToDigit(SDLKey key);//converts numpad digit key to normal digit key
  79. static bool isNumKey(SDLKey key, bool number = true); //checks if key is on numpad (numbers - check only for numpad digits)
  80. static bool isArrowKey(SDLKey key);
  81. static bool amIGuiThread();
  82. static void pushSDLEvent(int type, int usercode = 0);
  83. };
  84. extern CGuiHandler GH; //global gui handler
  85. template <typename T> void pushIntT()
  86. {
  87. GH.pushInt(new T());
  88. }
  89. struct SObjectConstruction
  90. {
  91. CIntObject *myObj;
  92. SObjectConstruction(CIntObject *obj);
  93. ~SObjectConstruction();
  94. };
  95. struct SSetCaptureState
  96. {
  97. bool previousCapture;
  98. ui8 prevActions;
  99. SSetCaptureState(bool allow, ui8 actions);
  100. ~SSetCaptureState();
  101. };
  102. namespace Colors
  103. {
  104. }
  105. #define OBJ_CONSTRUCTION SObjectConstruction obj__i(this)
  106. #define OBJ_CONSTRUCTION_CAPTURING_ALL defActions = 255; SSetCaptureState obj__i1(true, 255); SObjectConstruction obj__i(this)
  107. #define BLOCK_CAPTURING SSetCaptureState obj__i(false, 0)
  108. #define BLOCK_CAPTURING_DONT_TOUCH_REC_ACTIONS SSetCaptureState obj__i(false, GH.defActionsDef)