CGuiHandler.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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/Canvas.h"
  22. #include "../render/Colors.h"
  23. #include "../render/Graphics.h"
  24. #include "../render/IFont.h"
  25. #include "../render/EFont.h"
  26. #include "../renderSDL/ScreenHandler.h"
  27. #include "../renderSDL/RenderHandler.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. ObjectConstruction::ObjectConstruction(CIntObject *obj)
  36. {
  37. GH.createdObj.push_front(obj);
  38. GH.captureChildren = true;
  39. }
  40. ObjectConstruction::~ObjectConstruction()
  41. {
  42. assert(!GH.createdObj.empty());
  43. GH.createdObj.pop_front();
  44. GH.captureChildren = !GH.createdObj.empty();
  45. }
  46. void CGuiHandler::init()
  47. {
  48. inGuiThread = true;
  49. eventDispatcherInstance = std::make_unique<EventDispatcher>();
  50. windowHandlerInstance = std::make_unique<WindowHandler>();
  51. screenHandlerInstance = std::make_unique<ScreenHandler>();
  52. renderHandlerInstance = std::make_unique<RenderHandler>();
  53. shortcutsHandlerInstance = std::make_unique<ShortcutHandler>();
  54. inputHandlerInstance = std::make_unique<InputHandler>(); // Must be after windowHandlerInstance and shortcutsHandlerInstance
  55. framerateManagerInstance = std::make_unique<FramerateManager>(settings["video"]["targetfps"].Integer());
  56. }
  57. void CGuiHandler::handleEvents()
  58. {
  59. events().dispatchTimer(framerate().getElapsedMilliseconds());
  60. //player interface may want special event handling
  61. if(nullptr != LOCPLINT && LOCPLINT->capturedAllEvents())
  62. return;
  63. input().processEvents();
  64. }
  65. void CGuiHandler::fakeMouseMove()
  66. {
  67. dispatchMainThread([](){
  68. GH.events().dispatchMouseMoved(Point(0, 0), GH.getCursorPosition());
  69. });
  70. }
  71. void CGuiHandler::startTextInput(const Rect & whereInput)
  72. {
  73. input().startTextInput(whereInput);
  74. }
  75. void CGuiHandler::stopTextInput()
  76. {
  77. input().stopTextInput();
  78. }
  79. void CGuiHandler::renderFrame()
  80. {
  81. {
  82. boost::mutex::scoped_lock interfaceLock(GH.interfaceMutex);
  83. if (nullptr != curInt)
  84. curInt->update();
  85. if (settings["video"]["showfps"].Bool())
  86. drawFPSCounter();
  87. screenHandlerInstance->updateScreenTexture();
  88. windows().onFrameRendered();
  89. CCS->curh->update();
  90. }
  91. screenHandlerInstance->presentScreenTexture();
  92. framerate().framerateDelay(); // holds a constant FPS
  93. }
  94. CGuiHandler::CGuiHandler()
  95. : captureChildren(false)
  96. , curInt(nullptr)
  97. , fakeStatusBar(std::make_shared<EmptyStatusBar>())
  98. {
  99. }
  100. CGuiHandler::~CGuiHandler()
  101. {
  102. // enforce deletion order on shutdown
  103. // all UI elements including adventure map must be destroyed before Gui Handler
  104. // proper solution would be removal of adventureInt global
  105. adventureInt.reset();
  106. }
  107. ShortcutHandler & CGuiHandler::shortcuts()
  108. {
  109. assert(shortcutsHandlerInstance);
  110. return *shortcutsHandlerInstance;
  111. }
  112. FramerateManager & CGuiHandler::framerate()
  113. {
  114. assert(framerateManagerInstance);
  115. return *framerateManagerInstance;
  116. }
  117. bool CGuiHandler::isKeyboardCtrlDown() const
  118. {
  119. return inputHandlerInstance->isKeyboardCtrlDown();
  120. }
  121. bool CGuiHandler::isKeyboardCmdDown() const
  122. {
  123. return inputHandlerInstance->isKeyboardCmdDown();
  124. }
  125. bool CGuiHandler::isKeyboardAltDown() const
  126. {
  127. return inputHandlerInstance->isKeyboardAltDown();
  128. }
  129. bool CGuiHandler::isKeyboardShiftDown() const
  130. {
  131. return inputHandlerInstance->isKeyboardShiftDown();
  132. }
  133. const Point & CGuiHandler::getCursorPosition() const
  134. {
  135. return inputHandlerInstance->getCursorPosition();
  136. }
  137. Point CGuiHandler::screenDimensions() const
  138. {
  139. return screenHandlerInstance->getLogicalResolution();
  140. }
  141. void CGuiHandler::drawFPSCounter()
  142. {
  143. Canvas target = GH.screenHandler().getScreenCanvas();
  144. Rect targetArea(0, screenDimensions().y - 20, 48, 11);
  145. std::string fps = std::to_string(framerate().getFramerate())+" FPS";
  146. target.drawColor(targetArea, ColorRGBA(10, 10, 10));
  147. target.drawText(targetArea.center(), EFonts::FONT_SMALL, Colors::WHITE, ETextAlignment::CENTER, fps);
  148. }
  149. bool CGuiHandler::amIGuiThread()
  150. {
  151. return inGuiThread;
  152. }
  153. void CGuiHandler::dispatchMainThread(const std::function<void()> & functor)
  154. {
  155. inputHandlerInstance->dispatchMainThread(functor);
  156. }
  157. IScreenHandler & CGuiHandler::screenHandler()
  158. {
  159. return *screenHandlerInstance;
  160. }
  161. IRenderHandler & CGuiHandler::renderHandler()
  162. {
  163. return *renderHandlerInstance;
  164. }
  165. EventDispatcher & CGuiHandler::events()
  166. {
  167. return *eventDispatcherInstance;
  168. }
  169. InputHandler & CGuiHandler::input()
  170. {
  171. return *inputHandlerInstance;
  172. }
  173. WindowHandler & CGuiHandler::windows()
  174. {
  175. assert(windowHandlerInstance);
  176. return *windowHandlerInstance;
  177. }
  178. std::shared_ptr<IStatusBar> CGuiHandler::statusbar()
  179. {
  180. auto locked = currentStatusBar.lock();
  181. if (!locked)
  182. return fakeStatusBar;
  183. return locked;
  184. }
  185. void CGuiHandler::setStatusbar(std::shared_ptr<IStatusBar> newStatusBar)
  186. {
  187. currentStatusBar = newStatusBar;
  188. }
  189. void CGuiHandler::onScreenResize(bool resolutionChanged)
  190. {
  191. if(resolutionChanged)
  192. screenHandler().onScreenResize();
  193. windows().onScreenResize();
  194. CCS->curh->onScreenResize();
  195. }