CGuiHandler.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  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. windowHandlerInstance->clear();
  111. adventureInt.reset();
  112. }
  113. ShortcutHandler & CGuiHandler::shortcuts()
  114. {
  115. assert(shortcutsHandlerInstance);
  116. return *shortcutsHandlerInstance;
  117. }
  118. FramerateManager & CGuiHandler::framerate()
  119. {
  120. assert(framerateManagerInstance);
  121. return *framerateManagerInstance;
  122. }
  123. bool CGuiHandler::isKeyboardCtrlDown() const
  124. {
  125. return inputHandlerInstance->isKeyboardCtrlDown();
  126. }
  127. bool CGuiHandler::isKeyboardCmdDown() const
  128. {
  129. return inputHandlerInstance->isKeyboardCmdDown();
  130. }
  131. bool CGuiHandler::isKeyboardAltDown() const
  132. {
  133. return inputHandlerInstance->isKeyboardAltDown();
  134. }
  135. bool CGuiHandler::isKeyboardShiftDown() const
  136. {
  137. return inputHandlerInstance->isKeyboardShiftDown();
  138. }
  139. const Point & CGuiHandler::getCursorPosition() const
  140. {
  141. return inputHandlerInstance->getCursorPosition();
  142. }
  143. Point CGuiHandler::screenDimensions() const
  144. {
  145. return screenHandlerInstance->getLogicalResolution();
  146. }
  147. void CGuiHandler::drawFPSCounter()
  148. {
  149. int scaling = screenHandlerInstance->getScalingFactor();
  150. int x = 7 * scaling;
  151. int y = screen->h-20 * scaling;
  152. int width3digitFPSIncludingPadding = 48 * scaling;
  153. int heightFPSTextIncludingPadding = 11 * scaling;
  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. const auto & font = GH.renderHandler().loadFont(FONT_SMALL);
  159. font->renderTextLeft(screen, fps, Colors::WHITE, Point(8 * scaling, screen->h-22 * scaling));
  160. }
  161. bool CGuiHandler::amIGuiThread()
  162. {
  163. return inGuiThread;
  164. }
  165. void CGuiHandler::dispatchMainThread(const std::function<void()> & functor)
  166. {
  167. inputHandlerInstance->dispatchMainThread(functor);
  168. }
  169. IScreenHandler & CGuiHandler::screenHandler()
  170. {
  171. return *screenHandlerInstance;
  172. }
  173. IRenderHandler & CGuiHandler::renderHandler()
  174. {
  175. return *renderHandlerInstance;
  176. }
  177. EventDispatcher & CGuiHandler::events()
  178. {
  179. return *eventDispatcherInstance;
  180. }
  181. InputHandler & CGuiHandler::input()
  182. {
  183. return *inputHandlerInstance;
  184. }
  185. WindowHandler & CGuiHandler::windows()
  186. {
  187. assert(windowHandlerInstance);
  188. return *windowHandlerInstance;
  189. }
  190. std::shared_ptr<IStatusBar> CGuiHandler::statusbar()
  191. {
  192. auto locked = currentStatusBar.lock();
  193. if (!locked)
  194. return fakeStatusBar;
  195. return locked;
  196. }
  197. void CGuiHandler::setStatusbar(std::shared_ptr<IStatusBar> newStatusBar)
  198. {
  199. currentStatusBar = newStatusBar;
  200. }
  201. void CGuiHandler::onScreenResize(bool resolutionChanged)
  202. {
  203. if(resolutionChanged)
  204. screenHandler().onScreenResize();
  205. windows().onScreenResize();
  206. CCS->curh->onScreenResize();
  207. }