2023-10-19 16:19:09 +02:00
|
|
|
/*
|
|
|
|
* 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"
|
2024-08-02 17:02:37 +02:00
|
|
|
#include "../adventureMap/AdventureMapInterface.h"
|
2023-10-19 16:19:09 +02:00
|
|
|
#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;
|
|
|
|
|
2024-08-09 17:30:04 +02:00
|
|
|
ObjectConstruction::ObjectConstruction(CIntObject *obj)
|
2023-10-19 16:19:09 +02:00
|
|
|
{
|
|
|
|
GH.createdObj.push_front(obj);
|
|
|
|
GH.captureChildren = true;
|
|
|
|
}
|
|
|
|
|
2024-08-09 17:30:04 +02:00
|
|
|
ObjectConstruction::~ObjectConstruction()
|
2023-10-19 16:19:09 +02:00
|
|
|
{
|
2024-08-09 18:11:07 +02:00
|
|
|
assert(!GH.createdObj.empty());
|
2023-10-19 16:19:09 +02:00
|
|
|
GH.createdObj.pop_front();
|
2024-08-09 18:11:07 +02:00
|
|
|
GH.captureChildren = !GH.createdObj.empty();
|
2023-10-19 16:19:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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>();
|
2024-04-30 11:01:03 +02:00
|
|
|
inputHandlerInstance = std::make_unique<InputHandler>(); // Must be after windowHandlerInstance and shortcutsHandlerInstance
|
2023-10-19 16:19:09 +02:00
|
|
|
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);
|
|
|
|
|
2023-11-18 19:41:00 +02:00
|
|
|
if (nullptr != curInt)
|
2023-10-19 16:19:09 +02:00
|
|
|
curInt->update();
|
|
|
|
|
2023-11-18 19:41:00 +02:00
|
|
|
if (settings["video"]["showfps"].Bool())
|
2023-10-19 16:19:09 +02:00
|
|
|
drawFPSCounter();
|
2023-11-18 19:41:00 +02:00
|
|
|
|
2024-06-12 20:12:03 +02:00
|
|
|
SDL_UpdateTexture(screenTexture, nullptr, screen->pixels, screen->pitch);
|
|
|
|
}
|
2023-10-19 16:19:09 +02:00
|
|
|
|
2023-11-18 19:41:00 +02:00
|
|
|
SDL_RenderClear(mainRenderer);
|
|
|
|
SDL_RenderCopy(mainRenderer, screenTexture, nullptr, nullptr);
|
2023-10-19 16:19:09 +02:00
|
|
|
|
2023-11-18 19:41:00 +02:00
|
|
|
{
|
|
|
|
boost::mutex::scoped_lock interfaceLock(GH.interfaceMutex);
|
2023-10-19 16:19:09 +02:00
|
|
|
|
|
|
|
CCS->curh->render();
|
|
|
|
|
|
|
|
windows().onFrameRendered();
|
|
|
|
}
|
|
|
|
|
|
|
|
SDL_RenderPresent(mainRenderer);
|
|
|
|
framerate().framerateDelay(); // holds a constant FPS
|
|
|
|
}
|
|
|
|
|
|
|
|
CGuiHandler::CGuiHandler()
|
2024-08-09 16:26:53 +02:00
|
|
|
: captureChildren(false)
|
2023-10-19 16:19:09 +02:00
|
|
|
, curInt(nullptr)
|
|
|
|
, fakeStatusBar(std::make_shared<EmptyStatusBar>())
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2024-08-02 17:02:37 +02:00
|
|
|
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();
|
|
|
|
}
|
2023-10-19 16:19:09 +02:00
|
|
|
|
|
|
|
ShortcutHandler & CGuiHandler::shortcuts()
|
|
|
|
{
|
|
|
|
assert(shortcutsHandlerInstance);
|
|
|
|
return *shortcutsHandlerInstance;
|
|
|
|
}
|
|
|
|
|
|
|
|
FramerateManager & CGuiHandler::framerate()
|
|
|
|
{
|
|
|
|
assert(framerateManagerInstance);
|
|
|
|
return *framerateManagerInstance;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CGuiHandler::isKeyboardCtrlDown() const
|
|
|
|
{
|
|
|
|
return inputHandlerInstance->isKeyboardCtrlDown();
|
|
|
|
}
|
|
|
|
|
2024-05-12 19:09:31 +02:00
|
|
|
bool CGuiHandler::isKeyboardCmdDown() const
|
|
|
|
{
|
|
|
|
return inputHandlerInstance->isKeyboardCmdDown();
|
|
|
|
}
|
|
|
|
|
2023-10-19 16:19:09 +02:00
|
|
|
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
|
|
|
|
{
|
2024-07-21 23:11:02 +02:00
|
|
|
return screenHandlerInstance->getLogicalResolution();
|
2023-10-19 16:19:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void CGuiHandler::drawFPSCounter()
|
|
|
|
{
|
2024-09-12 16:37:24 +02:00
|
|
|
int scaling = screenHandlerInstance->getScalingFactor();
|
|
|
|
int x = 7 * scaling;
|
|
|
|
int y = screen->h-20 * scaling;
|
|
|
|
int width3digitFPSIncludingPadding = 48 * scaling;
|
|
|
|
int heightFPSTextIncludingPadding = 11 * scaling;
|
2024-02-12 13:22:54 +02:00
|
|
|
SDL_Rect overlay = { x, y, width3digitFPSIncludingPadding, heightFPSTextIncludingPadding};
|
2023-10-19 16:19:09 +02:00
|
|
|
uint32_t black = SDL_MapRGB(screen->format, 10, 10, 10);
|
|
|
|
SDL_FillRect(screen, &overlay, black);
|
2023-11-13 01:04:42 +02:00
|
|
|
|
|
|
|
std::string fps = std::to_string(framerate().getFramerate())+" FPS";
|
|
|
|
|
2024-09-23 17:43:11 +02:00
|
|
|
const auto & font = GH.renderHandler().loadFont(FONT_SMALL);
|
|
|
|
font->renderTextLeft(screen, fps, Colors::WHITE, Point(8 * scaling, screen->h-22 * scaling));
|
2023-10-19 16:19:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2024-02-03 16:14:59 +02:00
|
|
|
void CGuiHandler::onScreenResize(bool resolutionChanged)
|
2023-10-19 16:19:09 +02:00
|
|
|
{
|
2024-02-03 16:14:59 +02:00
|
|
|
if(resolutionChanged)
|
|
|
|
screenHandler().onScreenResize();
|
2024-10-01 21:40:12 +02:00
|
|
|
|
2023-10-19 16:19:09 +02:00
|
|
|
windows().onScreenResize();
|
2024-10-01 21:40:12 +02:00
|
|
|
CCS->curh->onScreenResize();
|
2023-10-19 16:19:09 +02:00
|
|
|
}
|