CGuiHandler.cpp 6.1 KB

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