123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257 |
- /*
- * CGuiHandler.cpp, part of VCMI engine
- *
- * Authors: listed in file AUTHORS in main folder
- *
- * License: GNU General Public License v2.0 or later
- * Full text of license available in license.txt file, in main folder
- *
- */
- #include "StdInc.h"
- #include "CGuiHandler.h"
- #include "CIntObject.h"
- #include "CursorHandler.h"
- #include "ShortcutHandler.h"
- #include "FramerateManager.h"
- #include "WindowHandler.h"
- #include "EventDispatcher.h"
- #include "../eventsSDL/InputHandler.h"
- #include "../CGameInfo.h"
- #include "../adventureMap/AdventureMapInterface.h"
- #include "../render/Colors.h"
- #include "../render/Graphics.h"
- #include "../render/IFont.h"
- #include "../render/EFont.h"
- #include "../renderSDL/ScreenHandler.h"
- #include "../renderSDL/RenderHandler.h"
- #include "../CMT.h"
- #include "../CPlayerInterface.h"
- #include "../battle/BattleInterface.h"
- #include "../../lib/CThreadHelper.h"
- #include "../../lib/CConfigHandler.h"
- #include <SDL_render.h>
- CGuiHandler GH;
- static thread_local bool inGuiThread = false;
- ObjectConstruction::ObjectConstruction(CIntObject *obj)
- :myObj(obj)
- {
- GH.createdObj.push_front(obj);
- GH.captureChildren = true;
- }
- ObjectConstruction::~ObjectConstruction()
- {
- assert(GH.createdObj.size());
- assert(GH.createdObj.front() == myObj);
- GH.createdObj.pop_front();
- GH.captureChildren = GH.createdObj.size();
- }
- void CGuiHandler::init()
- {
- inGuiThread = true;
- eventDispatcherInstance = std::make_unique<EventDispatcher>();
- windowHandlerInstance = std::make_unique<WindowHandler>();
- screenHandlerInstance = std::make_unique<ScreenHandler>();
- renderHandlerInstance = std::make_unique<RenderHandler>();
- shortcutsHandlerInstance = std::make_unique<ShortcutHandler>();
- inputHandlerInstance = std::make_unique<InputHandler>(); // Must be after windowHandlerInstance and shortcutsHandlerInstance
- framerateManagerInstance = std::make_unique<FramerateManager>(settings["video"]["targetfps"].Integer());
- }
- void CGuiHandler::handleEvents()
- {
- events().dispatchTimer(framerate().getElapsedMilliseconds());
- //player interface may want special event handling
- if(nullptr != LOCPLINT && LOCPLINT->capturedAllEvents())
- return;
- input().processEvents();
- }
- void CGuiHandler::fakeMouseMove()
- {
- dispatchMainThread([](){
- GH.events().dispatchMouseMoved(Point(0, 0), GH.getCursorPosition());
- });
- }
- void CGuiHandler::startTextInput(const Rect & whereInput)
- {
- input().startTextInput(whereInput);
- }
- void CGuiHandler::stopTextInput()
- {
- input().stopTextInput();
- }
- void CGuiHandler::renderFrame()
- {
- {
- boost::mutex::scoped_lock interfaceLock(GH.interfaceMutex);
- if (nullptr != curInt)
- curInt->update();
- if (settings["video"]["showfps"].Bool())
- drawFPSCounter();
- SDL_UpdateTexture(screenTexture, nullptr, screen->pixels, screen->pitch);
- }
- SDL_RenderClear(mainRenderer);
- SDL_RenderCopy(mainRenderer, screenTexture, nullptr, nullptr);
- {
- boost::mutex::scoped_lock interfaceLock(GH.interfaceMutex);
- CCS->curh->render();
- windows().onFrameRendered();
- }
- SDL_RenderPresent(mainRenderer);
- framerate().framerateDelay(); // holds a constant FPS
- }
- CGuiHandler::CGuiHandler()
- : captureChildren(false)
- , curInt(nullptr)
- , fakeStatusBar(std::make_shared<EmptyStatusBar>())
- {
- }
- CGuiHandler::~CGuiHandler()
- {
- // enforce deletion order on shutdown
- // all UI elements including adventure map must be destroyed before Gui Handler
- // proper solution would be removal of adventureInt global
- adventureInt.reset();
- }
- ShortcutHandler & CGuiHandler::shortcuts()
- {
- assert(shortcutsHandlerInstance);
- return *shortcutsHandlerInstance;
- }
- FramerateManager & CGuiHandler::framerate()
- {
- assert(framerateManagerInstance);
- return *framerateManagerInstance;
- }
- bool CGuiHandler::isKeyboardCtrlDown() const
- {
- return inputHandlerInstance->isKeyboardCtrlDown();
- }
- bool CGuiHandler::isKeyboardCmdDown() const
- {
- return inputHandlerInstance->isKeyboardCmdDown();
- }
- bool CGuiHandler::isKeyboardAltDown() const
- {
- return inputHandlerInstance->isKeyboardAltDown();
- }
- bool CGuiHandler::isKeyboardShiftDown() const
- {
- return inputHandlerInstance->isKeyboardShiftDown();
- }
- const Point & CGuiHandler::getCursorPosition() const
- {
- return inputHandlerInstance->getCursorPosition();
- }
- Point CGuiHandler::screenDimensions() const
- {
- return Point(screen->w, screen->h);
- }
- void CGuiHandler::drawFPSCounter()
- {
- int x = 7;
- int y = screen->h-20;
- int width3digitFPSIncludingPadding = 48;
- int heightFPSTextIncludingPadding = 11;
- SDL_Rect overlay = { x, y, width3digitFPSIncludingPadding, heightFPSTextIncludingPadding};
- uint32_t black = SDL_MapRGB(screen->format, 10, 10, 10);
- SDL_FillRect(screen, &overlay, black);
- std::string fps = std::to_string(framerate().getFramerate())+" FPS";
- graphics->fonts[FONT_SMALL]->renderTextLeft(screen, fps, Colors::WHITE, Point(8, screen->h-22));
- }
- bool CGuiHandler::amIGuiThread()
- {
- return inGuiThread;
- }
- void CGuiHandler::dispatchMainThread(const std::function<void()> & functor)
- {
- inputHandlerInstance->dispatchMainThread(functor);
- }
- IScreenHandler & CGuiHandler::screenHandler()
- {
- return *screenHandlerInstance;
- }
- IRenderHandler & CGuiHandler::renderHandler()
- {
- return *renderHandlerInstance;
- }
- EventDispatcher & CGuiHandler::events()
- {
- return *eventDispatcherInstance;
- }
- InputHandler & CGuiHandler::input()
- {
- return *inputHandlerInstance;
- }
- WindowHandler & CGuiHandler::windows()
- {
- assert(windowHandlerInstance);
- return *windowHandlerInstance;
- }
- std::shared_ptr<IStatusBar> CGuiHandler::statusbar()
- {
- auto locked = currentStatusBar.lock();
- if (!locked)
- return fakeStatusBar;
- return locked;
- }
- void CGuiHandler::setStatusbar(std::shared_ptr<IStatusBar> newStatusBar)
- {
- currentStatusBar = newStatusBar;
- }
- void CGuiHandler::onScreenResize(bool resolutionChanged)
- {
- if(resolutionChanged)
- {
- screenHandler().onScreenResize();
- }
- windows().onScreenResize();
- }
|