CGuiHandler.cpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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 "../lib/CondSh.h"
  13. #include "CIntObject.h"
  14. #include "CursorHandler.h"
  15. #include "ShortcutHandler.h"
  16. #include "FramerateManager.h"
  17. #include "WindowHandler.h"
  18. #include "EventDispatcher.h"
  19. #include "../eventsSDL/InputHandler.h"
  20. #include "../CGameInfo.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 "../CMT.h"
  27. #include "../CPlayerInterface.h"
  28. #include "../battle/BattleInterface.h"
  29. #include "../../lib/CThreadHelper.h"
  30. #include "../../lib/CConfigHandler.h"
  31. #include <SDL_render.h>
  32. CGuiHandler GH;
  33. static thread_local bool inGuiThread = false;
  34. SObjectConstruction::SObjectConstruction(CIntObject *obj)
  35. :myObj(obj)
  36. {
  37. GH.createdObj.push_front(obj);
  38. GH.captureChildren = true;
  39. }
  40. SObjectConstruction::~SObjectConstruction()
  41. {
  42. assert(GH.createdObj.size());
  43. assert(GH.createdObj.front() == myObj);
  44. GH.createdObj.pop_front();
  45. GH.captureChildren = GH.createdObj.size();
  46. }
  47. SSetCaptureState::SSetCaptureState(bool allow, ui8 actions)
  48. {
  49. previousCapture = GH.captureChildren;
  50. GH.captureChildren = false;
  51. prevActions = GH.defActionsDef;
  52. GH.defActionsDef = actions;
  53. }
  54. SSetCaptureState::~SSetCaptureState()
  55. {
  56. GH.captureChildren = previousCapture;
  57. GH.defActionsDef = prevActions;
  58. }
  59. void CGuiHandler::init()
  60. {
  61. inGuiThread = true;
  62. inputHandlerInstance = std::make_unique<InputHandler>();
  63. eventDispatcherInstance = std::make_unique<EventDispatcher>();
  64. windowHandlerInstance = std::make_unique<WindowHandler>();
  65. screenHandlerInstance = std::make_unique<ScreenHandler>();
  66. shortcutsHandlerInstance = std::make_unique<ShortcutHandler>();
  67. framerateManagerInstance = std::make_unique<FramerateManager>(settings["video"]["targetfps"].Integer());
  68. }
  69. void CGuiHandler::handleEvents()
  70. {
  71. events().dispatchTimer(framerate().getElapsedMilliseconds());
  72. //player interface may want special event handling
  73. if(nullptr != LOCPLINT && LOCPLINT->capturedAllEvents())
  74. return;
  75. input().processEvents();
  76. }
  77. void CGuiHandler::fakeMouseMove()
  78. {
  79. dispatchMainThread([](){
  80. assert(CPlayerInterface::pim);
  81. boost::unique_lock lock(*CPlayerInterface::pim);
  82. GH.events().dispatchMouseMoved(Point(0, 0), GH.getCursorPosition());
  83. });
  84. }
  85. void CGuiHandler::startTextInput(const Rect & whereInput)
  86. {
  87. input().startTextInput(whereInput);
  88. }
  89. void CGuiHandler::stopTextInput()
  90. {
  91. input().stopTextInput();
  92. }
  93. void CGuiHandler::renderFrame()
  94. {
  95. // Updating GUI requires locking pim mutex (that protects screen and GUI state).
  96. // During game:
  97. // When ending the game, the pim mutex might be hold by other thread,
  98. // that will notify us about the ending game by setting terminate_cond flag.
  99. //in PreGame terminate_cond stay false
  100. bool acquiredTheLockOnPim = false; //for tracking whether pim mutex locking succeeded
  101. while(!terminate_cond->get() && !(acquiredTheLockOnPim = CPlayerInterface::pim->try_lock())) //try acquiring long until it succeeds or we are told to terminate
  102. boost::this_thread::sleep_for(boost::chrono::milliseconds(1));
  103. if(acquiredTheLockOnPim)
  104. {
  105. // If we are here, pim mutex has been successfully locked - let's store it in a safe RAII lock.
  106. boost::unique_lock<boost::recursive_mutex> un(*CPlayerInterface::pim, boost::adopt_lock);
  107. if(nullptr != curInt)
  108. curInt->update();
  109. if(settings["video"]["showfps"].Bool())
  110. drawFPSCounter();
  111. SDL_UpdateTexture(screenTexture, nullptr, screen->pixels, screen->pitch);
  112. SDL_RenderClear(mainRenderer);
  113. SDL_RenderCopy(mainRenderer, screenTexture, nullptr, nullptr);
  114. CCS->curh->render();
  115. SDL_RenderPresent(mainRenderer);
  116. windows().onFrameRendered();
  117. }
  118. framerate().framerateDelay(); // holds a constant FPS
  119. }
  120. CGuiHandler::CGuiHandler()
  121. : defActionsDef(0)
  122. , captureChildren(false)
  123. , curInt(nullptr)
  124. , fakeStatusBar(std::make_shared<EmptyStatusBar>())
  125. , terminate_cond (new CondSh<bool>(false))
  126. {
  127. }
  128. CGuiHandler::~CGuiHandler()
  129. {
  130. delete terminate_cond;
  131. }
  132. ShortcutHandler & CGuiHandler::shortcuts()
  133. {
  134. assert(shortcutsHandlerInstance);
  135. return *shortcutsHandlerInstance;
  136. }
  137. FramerateManager & CGuiHandler::framerate()
  138. {
  139. assert(framerateManagerInstance);
  140. return *framerateManagerInstance;
  141. }
  142. bool CGuiHandler::isKeyboardCtrlDown() const
  143. {
  144. return inputHandlerInstance->isKeyboardCtrlDown();
  145. }
  146. bool CGuiHandler::isKeyboardAltDown() const
  147. {
  148. return inputHandlerInstance->isKeyboardAltDown();
  149. }
  150. bool CGuiHandler::isKeyboardShiftDown() const
  151. {
  152. return inputHandlerInstance->isKeyboardShiftDown();
  153. }
  154. const Point & CGuiHandler::getCursorPosition() const
  155. {
  156. return inputHandlerInstance->getCursorPosition();
  157. }
  158. Point CGuiHandler::screenDimensions() const
  159. {
  160. return Point(screen->w, screen->h);
  161. }
  162. void CGuiHandler::drawFPSCounter()
  163. {
  164. static SDL_Rect overlay = { 0, 0, 64, 32};
  165. uint32_t black = SDL_MapRGB(screen->format, 10, 10, 10);
  166. SDL_FillRect(screen, &overlay, black);
  167. std::string fps = std::to_string(framerate().getFramerate());
  168. graphics->fonts[FONT_BIG]->renderTextLeft(screen, fps, Colors::YELLOW, Point(10, 10));
  169. }
  170. bool CGuiHandler::amIGuiThread()
  171. {
  172. return inGuiThread;
  173. }
  174. void CGuiHandler::dispatchMainThread(const std::function<void()> & functor)
  175. {
  176. inputHandlerInstance->dispatchMainThread(functor);
  177. }
  178. IScreenHandler & CGuiHandler::screenHandler()
  179. {
  180. return *screenHandlerInstance;
  181. }
  182. EventDispatcher & CGuiHandler::events()
  183. {
  184. return *eventDispatcherInstance;
  185. }
  186. InputHandler & CGuiHandler::input()
  187. {
  188. return *inputHandlerInstance;
  189. }
  190. WindowHandler & CGuiHandler::windows()
  191. {
  192. assert(windowHandlerInstance);
  193. return *windowHandlerInstance;
  194. }
  195. std::shared_ptr<IStatusBar> CGuiHandler::statusbar()
  196. {
  197. auto locked = currentStatusBar.lock();
  198. if (!locked)
  199. return fakeStatusBar;
  200. return locked;
  201. }
  202. void CGuiHandler::setStatusbar(std::shared_ptr<IStatusBar> newStatusBar)
  203. {
  204. currentStatusBar = newStatusBar;
  205. }
  206. void CGuiHandler::onScreenResize()
  207. {
  208. screenHandler().onScreenResize();
  209. windows().onScreenResize();
  210. }