CGuiHandler.cpp 5.4 KB

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