Files
XCEngine/engine/src/UI/Runtime/UIScreenPlayer.cpp

113 lines
3.1 KiB
C++

#include <XCEngine/UI/Runtime/UIScreenPlayer.h>
namespace XCEngine {
namespace UI {
namespace Runtime {
namespace {
UIScreenFrameResult MakeNotLoadedFrame(const UIScreenFrameInput& input) {
UIScreenFrameResult frame = {};
frame.errorMessage = "UIScreenPlayer has no loaded screen document.";
frame.stats.documentLoaded = false;
frame.stats.inputEventCount = input.events.size();
frame.stats.presentedFrameIndex = input.frameIndex;
return frame;
}
} // namespace
UIScreenPlayer::UIScreenPlayer(IUIScreenDocumentHost& documentHost)
: m_documentHost(&documentHost) {
}
bool UIScreenPlayer::Load(const UIScreenAsset& asset) {
if (m_documentHost == nullptr) {
m_lastError = "UIScreenPlayer has no document host.";
m_document = {};
m_asset = {};
m_lastFrame = {};
m_presentedFrameCount = 0;
return false;
}
const UIScreenLoadResult loadResult = m_documentHost->LoadScreen(asset);
if (!loadResult.succeeded || !loadResult.document.IsValid()) {
m_asset = {};
m_document = {};
m_lastFrame = {};
m_presentedFrameCount = 0;
m_lastError = loadResult.errorMessage.empty()
? "UIScreenPlayer failed to load screen document."
: loadResult.errorMessage;
return false;
}
m_asset = asset;
m_document = loadResult.document;
m_lastFrame = {};
m_lastError.clear();
m_presentedFrameCount = 0;
return true;
}
void UIScreenPlayer::Unload() {
m_asset = {};
m_document = {};
m_lastFrame = {};
m_lastError.clear();
m_presentedFrameCount = 0;
}
bool UIScreenPlayer::IsLoaded() const {
return m_document.IsValid();
}
const UIScreenAsset* UIScreenPlayer::GetAsset() const {
return m_asset.IsValid() ? &m_asset : nullptr;
}
const UIScreenDocument* UIScreenPlayer::GetDocument() const {
return m_document.IsValid() ? &m_document : nullptr;
}
const UIScreenFrameResult& UIScreenPlayer::GetLastFrame() const {
return m_lastFrame;
}
UIScreenFrameResult UIScreenPlayer::ConsumeLastFrame() {
UIScreenFrameResult frame = std::move(m_lastFrame);
m_lastFrame = {};
return frame;
}
const std::string& UIScreenPlayer::GetLastError() const {
return m_lastError;
}
std::uint64_t UIScreenPlayer::GetPresentedFrameCount() const {
return m_presentedFrameCount;
}
const UIScreenFrameResult& UIScreenPlayer::Update(const UIScreenFrameInput& input) {
if (!IsLoaded() || m_documentHost == nullptr) {
m_lastFrame = MakeNotLoadedFrame(input);
m_lastError = m_lastFrame.errorMessage;
return m_lastFrame;
}
m_lastFrame = m_documentHost->BuildFrame(m_document, input);
m_lastFrame.stats.documentLoaded = true;
m_lastFrame.stats.drawListCount = m_lastFrame.drawData.GetDrawListCount();
m_lastFrame.stats.commandCount = m_lastFrame.drawData.GetTotalCommandCount();
m_lastFrame.stats.inputEventCount = input.events.size();
m_lastFrame.stats.presentedFrameIndex = input.frameIndex;
m_lastError = m_lastFrame.errorMessage;
++m_presentedFrameCount;
return m_lastFrame;
}
} // namespace Runtime
} // namespace UI
} // namespace XCEngine