CGuiHandler.cpp 6.4 KB

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