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