90 lines
3.0 KiB
C++
90 lines
3.0 KiB
C++
#pragma once
|
|
|
|
#include "XCUIBackend/ImGuiTransitionBackend.h"
|
|
#include "XCUIBackend/XCUIHostedPreviewPresenter.h"
|
|
|
|
#include <imgui.h>
|
|
|
|
#include <memory>
|
|
|
|
namespace XCEngine {
|
|
namespace Editor {
|
|
namespace XCUIBackend {
|
|
|
|
class IImGuiXCUIHostedPreviewTargetBinding {
|
|
public:
|
|
virtual ~IImGuiXCUIHostedPreviewTargetBinding() = default;
|
|
|
|
virtual ImDrawList* ResolveTargetDrawList(const XCUIHostedPreviewFrame& frame) const = 0;
|
|
};
|
|
|
|
class ImGuiCurrentWindowXCUIHostedPreviewTargetBinding final
|
|
: public IImGuiXCUIHostedPreviewTargetBinding {
|
|
public:
|
|
ImDrawList* ResolveTargetDrawList(const XCUIHostedPreviewFrame& frame) const override {
|
|
(void)frame;
|
|
return ImGui::GetWindowDrawList();
|
|
}
|
|
};
|
|
|
|
class ImGuiXCUIHostedPreviewPresenter final : public IXCUIHostedPreviewPresenter {
|
|
public:
|
|
explicit ImGuiXCUIHostedPreviewPresenter(
|
|
std::unique_ptr<IImGuiXCUIHostedPreviewTargetBinding> targetBinding = {})
|
|
: m_targetBinding(std::move(targetBinding)) {
|
|
if (m_targetBinding == nullptr) {
|
|
m_targetBinding = std::make_unique<ImGuiCurrentWindowXCUIHostedPreviewTargetBinding>();
|
|
}
|
|
}
|
|
|
|
bool Present(const XCUIHostedPreviewFrame& frame) override {
|
|
m_lastStats = {};
|
|
if (frame.drawData == nullptr) {
|
|
return false;
|
|
}
|
|
|
|
m_backend.BeginFrame();
|
|
m_backend.Submit(*frame.drawData);
|
|
m_lastStats.submittedDrawListCount = m_backend.GetPendingDrawListCount();
|
|
m_lastStats.submittedCommandCount = m_backend.GetPendingCommandCount();
|
|
ImDrawList* targetDrawList =
|
|
m_targetBinding != nullptr ? m_targetBinding->ResolveTargetDrawList(frame) : nullptr;
|
|
if (targetDrawList == nullptr) {
|
|
m_backend.BeginFrame();
|
|
return false;
|
|
}
|
|
|
|
m_lastStats.presented = m_backend.EndFrame(targetDrawList);
|
|
m_lastStats.flushedDrawListCount = m_backend.GetLastFlushedDrawListCount();
|
|
m_lastStats.flushedCommandCount = m_backend.GetLastFlushedCommandCount();
|
|
return m_lastStats.presented;
|
|
}
|
|
|
|
const XCUIHostedPreviewStats& GetLastStats() const override {
|
|
return m_lastStats;
|
|
}
|
|
|
|
private:
|
|
ImGuiTransitionBackend m_backend = {};
|
|
std::unique_ptr<IImGuiXCUIHostedPreviewTargetBinding> m_targetBinding = {};
|
|
XCUIHostedPreviewStats m_lastStats = {};
|
|
};
|
|
|
|
inline std::unique_ptr<IImGuiXCUIHostedPreviewTargetBinding>
|
|
CreateImGuiCurrentWindowXCUIHostedPreviewTargetBinding() {
|
|
return std::make_unique<ImGuiCurrentWindowXCUIHostedPreviewTargetBinding>();
|
|
}
|
|
|
|
inline std::unique_ptr<IXCUIHostedPreviewPresenter> CreateImGuiXCUIHostedPreviewPresenter(
|
|
std::unique_ptr<IImGuiXCUIHostedPreviewTargetBinding> targetBinding) {
|
|
return std::make_unique<ImGuiXCUIHostedPreviewPresenter>(std::move(targetBinding));
|
|
}
|
|
|
|
inline std::unique_ptr<IXCUIHostedPreviewPresenter> CreateImGuiXCUIHostedPreviewPresenter() {
|
|
return CreateImGuiXCUIHostedPreviewPresenter(CreateImGuiCurrentWindowXCUIHostedPreviewTargetBinding());
|
|
}
|
|
|
|
} // namespace XCUIBackend
|
|
} // namespace Editor
|
|
} // namespace XCEngine
|