CGuiHandler.cpp 5.8 KB

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