CGuiHandler.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  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 "../renderSDL/RenderHandler.h"
  27. #include "../CMT.h"
  28. #include "../CPlayerInterface.h"
  29. #include "../battle/BattleInterface.h"
  30. #include "../../lib/CThreadHelper.h"
  31. #include "../../lib/CConfigHandler.h"
  32. #include <SDL_render.h>
  33. CGuiHandler GH;
  34. static thread_local bool inGuiThread = false;
  35. SObjectConstruction::SObjectConstruction(CIntObject *obj)
  36. :myObj(obj)
  37. {
  38. GH.createdObj.push_front(obj);
  39. GH.captureChildren = true;
  40. }
  41. SObjectConstruction::~SObjectConstruction()
  42. {
  43. assert(GH.createdObj.size());
  44. assert(GH.createdObj.front() == myObj);
  45. GH.createdObj.pop_front();
  46. GH.captureChildren = GH.createdObj.size();
  47. }
  48. SSetCaptureState::SSetCaptureState(bool allow, ui8 actions)
  49. {
  50. previousCapture = GH.captureChildren;
  51. GH.captureChildren = false;
  52. prevActions = GH.defActionsDef;
  53. GH.defActionsDef = actions;
  54. }
  55. SSetCaptureState::~SSetCaptureState()
  56. {
  57. GH.captureChildren = previousCapture;
  58. GH.defActionsDef = prevActions;
  59. }
  60. void CGuiHandler::init()
  61. {
  62. inGuiThread = true;
  63. inputHandlerInstance = std::make_unique<InputHandler>();
  64. eventDispatcherInstance = std::make_unique<EventDispatcher>();
  65. windowHandlerInstance = std::make_unique<WindowHandler>();
  66. screenHandlerInstance = std::make_unique<ScreenHandler>();
  67. renderHandlerInstance = std::make_unique<RenderHandler>();
  68. shortcutsHandlerInstance = std::make_unique<ShortcutHandler>();
  69. framerateManagerInstance = std::make_unique<FramerateManager>(settings["video"]["targetfps"].Integer());
  70. }
  71. void CGuiHandler::handleEvents()
  72. {
  73. events().dispatchTimer(framerate().getElapsedMilliseconds());
  74. //player interface may want special event handling
  75. if(nullptr != LOCPLINT && LOCPLINT->capturedAllEvents())
  76. return;
  77. input().processEvents();
  78. }
  79. void CGuiHandler::fakeMouseMove()
  80. {
  81. dispatchMainThread([](){
  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. {
  96. boost::mutex::scoped_lock interfaceLock(GH.interfaceMutex);
  97. if(nullptr != curInt)
  98. curInt->update();
  99. if(settings["video"]["showfps"].Bool())
  100. drawFPSCounter();
  101. SDL_UpdateTexture(screenTexture, nullptr, screen->pixels, screen->pitch);
  102. SDL_RenderClear(mainRenderer);
  103. SDL_RenderCopy(mainRenderer, screenTexture, nullptr, nullptr);
  104. CCS->curh->render();
  105. windows().onFrameRendered();
  106. }
  107. SDL_RenderPresent(mainRenderer);
  108. framerate().framerateDelay(); // holds a constant FPS
  109. }
  110. CGuiHandler::CGuiHandler()
  111. : defActionsDef(0)
  112. , captureChildren(false)
  113. , curInt(nullptr)
  114. , fakeStatusBar(std::make_shared<EmptyStatusBar>())
  115. {
  116. }
  117. CGuiHandler::~CGuiHandler() = default;
  118. ShortcutHandler & CGuiHandler::shortcuts()
  119. {
  120. assert(shortcutsHandlerInstance);
  121. return *shortcutsHandlerInstance;
  122. }
  123. FramerateManager & CGuiHandler::framerate()
  124. {
  125. assert(framerateManagerInstance);
  126. return *framerateManagerInstance;
  127. }
  128. bool CGuiHandler::isKeyboardCtrlDown() const
  129. {
  130. return inputHandlerInstance->isKeyboardCtrlDown();
  131. }
  132. bool CGuiHandler::isKeyboardAltDown() const
  133. {
  134. return inputHandlerInstance->isKeyboardAltDown();
  135. }
  136. bool CGuiHandler::isKeyboardShiftDown() const
  137. {
  138. return inputHandlerInstance->isKeyboardShiftDown();
  139. }
  140. const Point & CGuiHandler::getCursorPosition() const
  141. {
  142. return inputHandlerInstance->getCursorPosition();
  143. }
  144. Point CGuiHandler::screenDimensions() const
  145. {
  146. return Point(screen->w, screen->h);
  147. }
  148. void CGuiHandler::drawFPSCounter()
  149. {
  150. static SDL_Rect overlay = { 0, 0, 24, 24};
  151. uint32_t black = SDL_MapRGB(screen->format, 10, 10, 10);
  152. SDL_FillRect(screen, &overlay, black);
  153. std::string fps = std::to_string(framerate().getFramerate());
  154. graphics->fonts[FONT_BIG]->renderTextLeft(screen, fps, Colors::YELLOW, Point(4, 2));
  155. }
  156. bool CGuiHandler::amIGuiThread()
  157. {
  158. return inGuiThread;
  159. }
  160. void CGuiHandler::dispatchMainThread(const std::function<void()> & functor)
  161. {
  162. inputHandlerInstance->dispatchMainThread(functor);
  163. }
  164. IScreenHandler & CGuiHandler::screenHandler()
  165. {
  166. return *screenHandlerInstance;
  167. }
  168. IRenderHandler & CGuiHandler::renderHandler()
  169. {
  170. return *renderHandlerInstance;
  171. }
  172. EventDispatcher & CGuiHandler::events()
  173. {
  174. return *eventDispatcherInstance;
  175. }
  176. InputHandler & CGuiHandler::input()
  177. {
  178. return *inputHandlerInstance;
  179. }
  180. WindowHandler & CGuiHandler::windows()
  181. {
  182. assert(windowHandlerInstance);
  183. return *windowHandlerInstance;
  184. }
  185. std::shared_ptr<IStatusBar> CGuiHandler::statusbar()
  186. {
  187. auto locked = currentStatusBar.lock();
  188. if (!locked)
  189. return fakeStatusBar;
  190. return locked;
  191. }
  192. void CGuiHandler::setStatusbar(std::shared_ptr<IStatusBar> newStatusBar)
  193. {
  194. currentStatusBar = newStatusBar;
  195. }
  196. void CGuiHandler::onScreenResize()
  197. {
  198. screenHandler().onScreenResize();
  199. windows().onScreenResize();
  200. }