CGuiHandler.cpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  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 "../adventureMap/AdventureMapInterface.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. shortcutsHandlerInstance = std::make_unique<ShortcutHandler>();
  68. inputHandlerInstance = std::make_unique<InputHandler>(); // Must be after windowHandlerInstance and shortcutsHandlerInstance
  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. }
  103. SDL_RenderClear(mainRenderer);
  104. SDL_RenderCopy(mainRenderer, screenTexture, nullptr, nullptr);
  105. {
  106. boost::mutex::scoped_lock interfaceLock(GH.interfaceMutex);
  107. CCS->curh->render();
  108. windows().onFrameRendered();
  109. }
  110. SDL_RenderPresent(mainRenderer);
  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. {
  119. }
  120. CGuiHandler::~CGuiHandler()
  121. {
  122. // enforce deletion order on shutdown
  123. // all UI elements including adventure map must be destroyed before Gui Handler
  124. // proper solution would be removal of adventureInt global
  125. adventureInt.reset();
  126. }
  127. ShortcutHandler & CGuiHandler::shortcuts()
  128. {
  129. assert(shortcutsHandlerInstance);
  130. return *shortcutsHandlerInstance;
  131. }
  132. FramerateManager & CGuiHandler::framerate()
  133. {
  134. assert(framerateManagerInstance);
  135. return *framerateManagerInstance;
  136. }
  137. bool CGuiHandler::isKeyboardCtrlDown() const
  138. {
  139. return inputHandlerInstance->isKeyboardCtrlDown();
  140. }
  141. bool CGuiHandler::isKeyboardCmdDown() const
  142. {
  143. return inputHandlerInstance->isKeyboardCmdDown();
  144. }
  145. bool CGuiHandler::isKeyboardAltDown() const
  146. {
  147. return inputHandlerInstance->isKeyboardAltDown();
  148. }
  149. bool CGuiHandler::isKeyboardShiftDown() const
  150. {
  151. return inputHandlerInstance->isKeyboardShiftDown();
  152. }
  153. const Point & CGuiHandler::getCursorPosition() const
  154. {
  155. return inputHandlerInstance->getCursorPosition();
  156. }
  157. Point CGuiHandler::screenDimensions() const
  158. {
  159. return Point(screen->w, screen->h);
  160. }
  161. void CGuiHandler::drawFPSCounter()
  162. {
  163. int x = 7;
  164. int y = screen->h-20;
  165. int width3digitFPSIncludingPadding = 48;
  166. int heightFPSTextIncludingPadding = 11;
  167. SDL_Rect overlay = { x, y, width3digitFPSIncludingPadding, heightFPSTextIncludingPadding};
  168. uint32_t black = SDL_MapRGB(screen->format, 10, 10, 10);
  169. SDL_FillRect(screen, &overlay, black);
  170. std::string fps = std::to_string(framerate().getFramerate())+" FPS";
  171. graphics->fonts[FONT_SMALL]->renderTextLeft(screen, fps, Colors::WHITE, Point(8, screen->h-22));
  172. }
  173. bool CGuiHandler::amIGuiThread()
  174. {
  175. return inGuiThread;
  176. }
  177. void CGuiHandler::dispatchMainThread(const std::function<void()> & functor)
  178. {
  179. inputHandlerInstance->dispatchMainThread(functor);
  180. }
  181. IScreenHandler & CGuiHandler::screenHandler()
  182. {
  183. return *screenHandlerInstance;
  184. }
  185. IRenderHandler & CGuiHandler::renderHandler()
  186. {
  187. return *renderHandlerInstance;
  188. }
  189. EventDispatcher & CGuiHandler::events()
  190. {
  191. return *eventDispatcherInstance;
  192. }
  193. InputHandler & CGuiHandler::input()
  194. {
  195. return *inputHandlerInstance;
  196. }
  197. WindowHandler & CGuiHandler::windows()
  198. {
  199. assert(windowHandlerInstance);
  200. return *windowHandlerInstance;
  201. }
  202. std::shared_ptr<IStatusBar> CGuiHandler::statusbar()
  203. {
  204. auto locked = currentStatusBar.lock();
  205. if (!locked)
  206. return fakeStatusBar;
  207. return locked;
  208. }
  209. void CGuiHandler::setStatusbar(std::shared_ptr<IStatusBar> newStatusBar)
  210. {
  211. currentStatusBar = newStatusBar;
  212. }
  213. void CGuiHandler::onScreenResize(bool resolutionChanged)
  214. {
  215. if(resolutionChanged)
  216. {
  217. screenHandler().onScreenResize();
  218. }
  219. windows().onScreenResize();
  220. }