2026-04-05 06:15:24 +08:00
|
|
|
#include "XCUIBackend/IEditorHostCompositor.h"
|
|
|
|
|
|
|
|
|
|
#include "UI/ImGuiBackendBridge.h"
|
|
|
|
|
|
|
|
|
|
#include <imgui.h>
|
|
|
|
|
|
|
|
|
|
namespace XCEngine {
|
|
|
|
|
namespace Editor {
|
|
|
|
|
namespace XCUIBackend {
|
|
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
|
|
class ImGuiHostCompositor final : public IEditorHostCompositor {
|
|
|
|
|
public:
|
|
|
|
|
bool Initialize(
|
|
|
|
|
HWND hwnd,
|
|
|
|
|
::XCEngine::Editor::Platform::D3D12WindowRenderer& windowRenderer,
|
|
|
|
|
const ConfigureFontsCallback& configureFonts) override {
|
|
|
|
|
IMGUI_CHECKVERSION();
|
|
|
|
|
ImGui::CreateContext();
|
|
|
|
|
|
|
|
|
|
ImGuiIO& io = ImGui::GetIO();
|
|
|
|
|
io.ConfigFlags |= ImGuiConfigFlags_DockingEnable;
|
|
|
|
|
io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard;
|
|
|
|
|
|
|
|
|
|
if (configureFonts) {
|
|
|
|
|
configureFonts();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ImGui::StyleColorsDark();
|
|
|
|
|
m_backend.Initialize(
|
|
|
|
|
hwnd,
|
|
|
|
|
windowRenderer.GetDevice(),
|
|
|
|
|
windowRenderer.GetCommandQueue(),
|
|
|
|
|
windowRenderer.GetSrvHeap(),
|
|
|
|
|
windowRenderer.GetSrvDescriptorSize(),
|
|
|
|
|
windowRenderer.GetSrvDescriptorCount());
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Shutdown() override {
|
|
|
|
|
m_backend.Shutdown();
|
|
|
|
|
ImGui::DestroyContext();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool HandleWindowMessage(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) override {
|
|
|
|
|
return ::XCEngine::Editor::UI::ImGuiBackendBridge::HandleWindowMessage(
|
|
|
|
|
hwnd,
|
|
|
|
|
message,
|
|
|
|
|
wParam,
|
|
|
|
|
lParam);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void BeginFrame() override {
|
|
|
|
|
m_backend.BeginFrame();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void EndFrameAndPresent(
|
|
|
|
|
::XCEngine::Editor::Platform::D3D12WindowRenderer& windowRenderer,
|
|
|
|
|
const float clearColor[4],
|
|
|
|
|
const RenderCallback& beforeUiRender,
|
|
|
|
|
const RenderCallback& afterUiRender) override {
|
|
|
|
|
ImGui::Render();
|
|
|
|
|
windowRenderer.Render(m_backend, clearColor, beforeUiRender, afterUiRender);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool CreateTextureDescriptor(
|
|
|
|
|
::XCEngine::RHI::RHIDevice* device,
|
|
|
|
|
::XCEngine::RHI::RHITexture* texture,
|
2026-04-05 06:43:51 +08:00
|
|
|
UITextureRegistration& outRegistration) override {
|
|
|
|
|
ImTextureID textureId = {};
|
|
|
|
|
if (!m_backend.CreateTextureDescriptor(
|
2026-04-05 06:15:24 +08:00
|
|
|
device,
|
|
|
|
|
texture,
|
2026-04-05 06:43:51 +08:00
|
|
|
&outRegistration.cpuHandle,
|
|
|
|
|
&outRegistration.gpuHandle,
|
|
|
|
|
&textureId)) {
|
|
|
|
|
outRegistration = {};
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
outRegistration.texture.nativeHandle = (std::uintptr_t)textureId;
|
|
|
|
|
outRegistration.texture.width = texture != nullptr ? texture->GetWidth() : 0u;
|
|
|
|
|
outRegistration.texture.height = texture != nullptr ? texture->GetHeight() : 0u;
|
|
|
|
|
outRegistration.texture.kind = ::XCEngine::UI::UITextureHandleKind::ImGuiDescriptor;
|
|
|
|
|
return outRegistration.IsValid();
|
2026-04-05 06:15:24 +08:00
|
|
|
}
|
|
|
|
|
|
2026-04-05 06:43:51 +08:00
|
|
|
void FreeTextureDescriptor(const UITextureRegistration& registration) override {
|
|
|
|
|
m_backend.FreeTextureDescriptor(registration.cpuHandle, registration.gpuHandle);
|
2026-04-05 06:15:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
::XCEngine::Editor::UI::ImGuiBackendBridge m_backend;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
|
|
std::unique_ptr<IEditorHostCompositor> CreateImGuiHostCompositor() {
|
|
|
|
|
return std::make_unique<ImGuiHostCompositor>();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} // namespace XCUIBackend
|
|
|
|
|
} // namespace Editor
|
|
|
|
|
} // namespace XCEngine
|