CGuiHandler.cpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  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 "CIntObject.h"
  13. #include "CursorHandler.h"
  14. #include "ShortcutHandler.h"
  15. #include "FramerateManager.h"
  16. #include "WindowHandler.h"
  17. #include "EventDispatcher.h"
  18. #include "../eventsSDL/InputHandler.h"
  19. #include "../CGameInfo.h"
  20. #include "../render/Colors.h"
  21. #include "../render/Graphics.h"
  22. #include "../render/IFont.h"
  23. #include "../render/EFont.h"
  24. #include "../renderSDL/ScreenHandler.h"
  25. #include "../renderSDL/RenderHandler.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. static thread_local bool inGuiThread = false;
  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. inGuiThread = true;
  62. eventDispatcherInstance = std::make_unique<EventDispatcher>();
  63. windowHandlerInstance = std::make_unique<WindowHandler>();
  64. screenHandlerInstance = std::make_unique<ScreenHandler>();
  65. renderHandlerInstance = std::make_unique<RenderHandler>();
  66. shortcutsHandlerInstance = std::make_unique<ShortcutHandler>();
  67. inputHandlerInstance = std::make_unique<InputHandler>(); // Must be after windowHandlerInstance and shortcutsHandlerInstance
  68. framerateManagerInstance = std::make_unique<FramerateManager>(settings["video"]["targetfps"].Integer());
  69. }
  70. void CGuiHandler::handleEvents()
  71. {
  72. events().dispatchTimer(framerate().getElapsedMilliseconds());
  73. //player interface may want special event handling
  74. if(nullptr != LOCPLINT && LOCPLINT->capturedAllEvents())
  75. return;
  76. input().processEvents();
  77. }
  78. void CGuiHandler::fakeMouseMove()
  79. {
  80. dispatchMainThread([](){
  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. {
  95. boost::mutex::scoped_lock interfaceLock(GH.interfaceMutex);
  96. if (nullptr != curInt)
  97. curInt->update();
  98. if (settings["video"]["showfps"].Bool())
  99. drawFPSCounter();
  100. }
  101. SDL_UpdateTexture(screenTexture, nullptr, screen->pixels, screen->pitch);
  102. SDL_RenderClear(mainRenderer);
  103. SDL_RenderCopy(mainRenderer, screenTexture, nullptr, nullptr);
  104. {
  105. boost::mutex::scoped_lock interfaceLock(GH.interfaceMutex);
  106. CCS->curh->render();
  107. windows().onFrameRendered();
  108. }
  109. SDL_RenderPresent(mainRenderer);
  110. framerate().framerateDelay(); // holds a constant FPS
  111. }
  112. CGuiHandler::CGuiHandler()
  113. : defActionsDef(0)
  114. , captureChildren(false)
  115. , curInt(nullptr)
  116. , fakeStatusBar(std::make_shared<EmptyStatusBar>())
  117. {
  118. }
  119. CGuiHandler::~CGuiHandler() = default;
  120. ShortcutHandler & CGuiHandler::shortcuts()
  121. {
  122. assert(shortcutsHandlerInstance);
  123. return *shortcutsHandlerInstance;
  124. }
  125. FramerateManager & CGuiHandler::framerate()
  126. {
  127. assert(framerateManagerInstance);
  128. return *framerateManagerInstance;
  129. }
  130. bool CGuiHandler::isKeyboardCtrlDown() const
  131. {
  132. return inputHandlerInstance->isKeyboardCtrlDown();
  133. }
  134. bool CGuiHandler::isKeyboardCmdDown() const
  135. {
  136. return inputHandlerInstance->isKeyboardCmdDown();
  137. }
  138. bool CGuiHandler::isKeyboardAltDown() const
  139. {
  140. return inputHandlerInstance->isKeyboardAltDown();
  141. }
  142. bool CGuiHandler::isKeyboardShiftDown() const
  143. {
  144. return inputHandlerInstance->isKeyboardShiftDown();
  145. }
  146. const Point & CGuiHandler::getCursorPosition() const
  147. {
  148. return inputHandlerInstance->getCursorPosition();
  149. }
  150. Point CGuiHandler::screenDimensions() const
  151. {
  152. return Point(screen->w, screen->h);
  153. }
  154. void CGuiHandler::drawFPSCounter()
  155. {
  156. int x = 7;
  157. int y = screen->h-20;
  158. int width3digitFPSIncludingPadding = 48;
  159. int heightFPSTextIncludingPadding = 11;
  160. SDL_Rect overlay = { x, y, width3digitFPSIncludingPadding, heightFPSTextIncludingPadding};
  161. uint32_t black = SDL_MapRGB(screen->format, 10, 10, 10);
  162. SDL_FillRect(screen, &overlay, black);
  163. std::string fps = std::to_string(framerate().getFramerate())+" FPS";
  164. graphics->fonts[FONT_SMALL]->renderTextLeft(screen, fps, Colors::WHITE, Point(8, screen->h-22));
  165. }
  166. bool CGuiHandler::amIGuiThread()
  167. {
  168. return inGuiThread;
  169. }
  170. void CGuiHandler::dispatchMainThread(const std::function<void()> & functor)
  171. {
  172. inputHandlerInstance->dispatchMainThread(functor);
  173. }
  174. IScreenHandler & CGuiHandler::screenHandler()
  175. {
  176. return *screenHandlerInstance;
  177. }
  178. IRenderHandler & CGuiHandler::renderHandler()
  179. {
  180. return *renderHandlerInstance;
  181. }
  182. EventDispatcher & CGuiHandler::events()
  183. {
  184. return *eventDispatcherInstance;
  185. }
  186. InputHandler & CGuiHandler::input()
  187. {
  188. return *inputHandlerInstance;
  189. }
  190. WindowHandler & CGuiHandler::windows()
  191. {
  192. assert(windowHandlerInstance);
  193. return *windowHandlerInstance;
  194. }
  195. std::shared_ptr<IStatusBar> CGuiHandler::statusbar()
  196. {
  197. auto locked = currentStatusBar.lock();
  198. if (!locked)
  199. return fakeStatusBar;
  200. return locked;
  201. }
  202. void CGuiHandler::setStatusbar(std::shared_ptr<IStatusBar> newStatusBar)
  203. {
  204. currentStatusBar = newStatusBar;
  205. }
  206. void CGuiHandler::onScreenResize(bool resolutionChanged)
  207. {
  208. if(resolutionChanged)
  209. {
  210. screenHandler().onScreenResize();
  211. }
  212. windows().onScreenResize();
  213. }