CGuiHandler.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. /*
  2. * CGuiHandler.cpp, 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. #include "StdInc.h"
  11. #include "CGuiHandler.h"
  12. #include "CIntObject.h"
  13. #include "CursorHandler.h"
  14. #include "ShortcutHandler.h"
  15. #include "FramerateManager.h"
  16. #include "WindowHandler.h"
  17. #include "EventDispatcher.h"
  18. #include "../eventsSDL/InputHandler.h"
  19. #include "../CGameInfo.h"
  20. #include "../adventureMap/AdventureMapInterface.h"
  21. #include "../render/Colors.h"
  22. #include "../render/Graphics.h"
  23. #include "../render/IFont.h"
  24. #include "../render/EFont.h"
  25. #include "../renderSDL/ScreenHandler.h"
  26. #include "../renderSDL/RenderHandler.h"
  27. #include "../CMT.h"
  28. #include "../CPlayerInterface.h"
  29. #include "../battle/BattleInterface.h"
  30. #include "../../lib/CThreadHelper.h"
  31. #include "../../lib/CConfigHandler.h"
  32. #include <SDL_render.h>
  33. CGuiHandler GH;
  34. static thread_local bool inGuiThread = false;
  35. ObjectConstruction::ObjectConstruction(CIntObject *obj)
  36. :myObj(obj)
  37. {
  38. GH.createdObj.push_front(obj);
  39. GH.captureChildren = true;
  40. }
  41. ObjectConstruction::~ObjectConstruction()
  42. {
  43. assert(GH.createdObj.size());
  44. assert(GH.createdObj.front() == myObj);
  45. GH.createdObj.pop_front();
  46. GH.captureChildren = GH.createdObj.size();
  47. }
  48. void CGuiHandler::init()
  49. {
  50. inGuiThread = true;
  51. eventDispatcherInstance = std::make_unique<EventDispatcher>();
  52. windowHandlerInstance = std::make_unique<WindowHandler>();
  53. screenHandlerInstance = std::make_unique<ScreenHandler>();
  54. renderHandlerInstance = std::make_unique<RenderHandler>();
  55. shortcutsHandlerInstance = std::make_unique<ShortcutHandler>();
  56. inputHandlerInstance = std::make_unique<InputHandler>(); // Must be after windowHandlerInstance and shortcutsHandlerInstance
  57. framerateManagerInstance = std::make_unique<FramerateManager>(settings["video"]["targetfps"].Integer());
  58. }
  59. void CGuiHandler::handleEvents()
  60. {
  61. events().dispatchTimer(framerate().getElapsedMilliseconds());
  62. //player interface may want special event handling
  63. if(nullptr != LOCPLINT && LOCPLINT->capturedAllEvents())
  64. return;
  65. input().processEvents();
  66. }
  67. void CGuiHandler::fakeMouseMove()
  68. {
  69. dispatchMainThread([](){
  70. GH.events().dispatchMouseMoved(Point(0, 0), GH.getCursorPosition());
  71. });
  72. }
  73. void CGuiHandler::startTextInput(const Rect & whereInput)
  74. {
  75. input().startTextInput(whereInput);
  76. }
  77. void CGuiHandler::stopTextInput()
  78. {
  79. input().stopTextInput();
  80. }
  81. void CGuiHandler::renderFrame()
  82. {
  83. {
  84. boost::mutex::scoped_lock interfaceLock(GH.interfaceMutex);
  85. if (nullptr != curInt)
  86. curInt->update();
  87. if (settings["video"]["showfps"].Bool())
  88. drawFPSCounter();
  89. SDL_UpdateTexture(screenTexture, nullptr, screen->pixels, screen->pitch);
  90. }
  91. SDL_RenderClear(mainRenderer);
  92. SDL_RenderCopy(mainRenderer, screenTexture, nullptr, nullptr);
  93. {
  94. boost::mutex::scoped_lock interfaceLock(GH.interfaceMutex);
  95. CCS->curh->render();
  96. windows().onFrameRendered();
  97. }
  98. SDL_RenderPresent(mainRenderer);
  99. framerate().framerateDelay(); // holds a constant FPS
  100. }
  101. CGuiHandler::CGuiHandler()
  102. : captureChildren(false)
  103. , curInt(nullptr)
  104. , fakeStatusBar(std::make_shared<EmptyStatusBar>())
  105. {
  106. }
  107. CGuiHandler::~CGuiHandler()
  108. {
  109. // enforce deletion order on shutdown
  110. // all UI elements including adventure map must be destroyed before Gui Handler
  111. // proper solution would be removal of adventureInt global
  112. adventureInt.reset();
  113. }
  114. ShortcutHandler & CGuiHandler::shortcuts()
  115. {
  116. assert(shortcutsHandlerInstance);
  117. return *shortcutsHandlerInstance;
  118. }
  119. FramerateManager & CGuiHandler::framerate()
  120. {
  121. assert(framerateManagerInstance);
  122. return *framerateManagerInstance;
  123. }
  124. bool CGuiHandler::isKeyboardCtrlDown() const
  125. {
  126. return inputHandlerInstance->isKeyboardCtrlDown();
  127. }
  128. bool CGuiHandler::isKeyboardCmdDown() const
  129. {
  130. return inputHandlerInstance->isKeyboardCmdDown();
  131. }
  132. bool CGuiHandler::isKeyboardAltDown() const
  133. {
  134. return inputHandlerInstance->isKeyboardAltDown();
  135. }
  136. bool CGuiHandler::isKeyboardShiftDown() const
  137. {
  138. return inputHandlerInstance->isKeyboardShiftDown();
  139. }
  140. const Point & CGuiHandler::getCursorPosition() const
  141. {
  142. return inputHandlerInstance->getCursorPosition();
  143. }
  144. Point CGuiHandler::screenDimensions() const
  145. {
  146. return Point(screen->w, screen->h);
  147. }
  148. void CGuiHandler::drawFPSCounter()
  149. {
  150. int x = 7;
  151. int y = screen->h-20;
  152. int width3digitFPSIncludingPadding = 48;
  153. int heightFPSTextIncludingPadding = 11;
  154. SDL_Rect overlay = { x, y, width3digitFPSIncludingPadding, heightFPSTextIncludingPadding};
  155. uint32_t black = SDL_MapRGB(screen->format, 10, 10, 10);
  156. SDL_FillRect(screen, &overlay, black);
  157. std::string fps = std::to_string(framerate().getFramerate())+" FPS";
  158. graphics->fonts[FONT_SMALL]->renderTextLeft(screen, fps, Colors::WHITE, Point(8, screen->h-22));
  159. }
  160. bool CGuiHandler::amIGuiThread()
  161. {
  162. return inGuiThread;
  163. }
  164. void CGuiHandler::dispatchMainThread(const std::function<void()> & functor)
  165. {
  166. inputHandlerInstance->dispatchMainThread(functor);
  167. }
  168. IScreenHandler & CGuiHandler::screenHandler()
  169. {
  170. return *screenHandlerInstance;
  171. }
  172. IRenderHandler & CGuiHandler::renderHandler()
  173. {
  174. return *renderHandlerInstance;
  175. }
  176. EventDispatcher & CGuiHandler::events()
  177. {
  178. return *eventDispatcherInstance;
  179. }
  180. InputHandler & CGuiHandler::input()
  181. {
  182. return *inputHandlerInstance;
  183. }
  184. WindowHandler & CGuiHandler::windows()
  185. {
  186. assert(windowHandlerInstance);
  187. return *windowHandlerInstance;
  188. }
  189. std::shared_ptr<IStatusBar> CGuiHandler::statusbar()
  190. {
  191. auto locked = currentStatusBar.lock();
  192. if (!locked)
  193. return fakeStatusBar;
  194. return locked;
  195. }
  196. void CGuiHandler::setStatusbar(std::shared_ptr<IStatusBar> newStatusBar)
  197. {
  198. currentStatusBar = newStatusBar;
  199. }
  200. void CGuiHandler::onScreenResize(bool resolutionChanged)
  201. {
  202. if(resolutionChanged)
  203. {
  204. screenHandler().onScreenResize();
  205. }
  206. windows().onScreenResize();
  207. }