CGuiHandler.cpp 5.7 KB

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