CGuiHandler.cpp 6.2 KB

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