From 99803e739107f1d082580ecc032b241b9f0925aa Mon Sep 17 00:00:00 2001 From: ouczbs Date: Tue, 17 Dec 2024 20:57:52 +0800 Subject: [PATCH] editor window --- engine/3rdparty/xmake.lua | 3 +- .../editor/panel/asset_preview_panel.h | 8 ++++ engine/include/editor/panel/menu_bar_panel.h | 8 ++++ .../editor/window/editor_main_window.h | 9 +++++ engine/modules/engine/asset/xmake.lua | 3 +- .../engine/render/impl/renderapi_impl.inl | 6 +-- .../render/include/render/editor_system.h | 40 +++++++++++++++++++ .../render/include/render/imgui_system.h | 12 ------ .../{vulkan_imgui.h => vulkan_imgui_editor.h} | 8 +++- engine/modules/render/vulkan/src/module.cpp | 4 +- ...lkan_imgui.cpp => vulkan_imgui_editor.cpp} | 32 ++++++++------- engine/src/editor/editor.cpp | 4 +- .../src/editor/panel/asset_preview_panel.cpp | 9 +++++ engine/src/editor/panel/menu_bar_panel.cpp | 25 ++++++++++++ .../src/editor/window/editor_main_window.cpp | 19 +++++++++ engine/xmake.lua | 6 +-- game/zworld/imgui.ini | 6 +++ 17 files changed, 163 insertions(+), 39 deletions(-) create mode 100644 engine/include/editor/panel/asset_preview_panel.h create mode 100644 engine/include/editor/panel/menu_bar_panel.h create mode 100644 engine/include/editor/window/editor_main_window.h create mode 100644 engine/modules/engine/render/include/render/editor_system.h delete mode 100644 engine/modules/engine/render/include/render/imgui_system.h rename engine/modules/render/vulkan/include/vkn/{vulkan_imgui.h => vulkan_imgui_editor.h} (61%) rename engine/modules/render/vulkan/src/{vulkan_imgui.cpp => vulkan_imgui_editor.cpp} (89%) create mode 100644 engine/src/editor/panel/asset_preview_panel.cpp create mode 100644 engine/src/editor/panel/menu_bar_panel.cpp create mode 100644 engine/src/editor/window/editor_main_window.cpp diff --git a/engine/3rdparty/xmake.lua b/engine/3rdparty/xmake.lua index 5e19495..54e6c24 100644 --- a/engine/3rdparty/xmake.lua +++ b/engine/3rdparty/xmake.lua @@ -1,4 +1,5 @@ -add_requires("spdlog", "libsdl", "vulkansdk","shaderc","spirv","spirv-cross") +add_requires("spdlog", "vulkansdk","shaderc","spirv","spirv-cross") add_requires("mimalloc", {configs = {shared = true, debug = true, copy = true}}) add_requires("imgui",{configs = {shared = true, debug = true, copy = true}}) +add_requires("libsdl",{configs = {shared = true}}) includes("*/xmake.lua") \ No newline at end of file diff --git a/engine/include/editor/panel/asset_preview_panel.h b/engine/include/editor/panel/asset_preview_panel.h new file mode 100644 index 0000000..aeef233 --- /dev/null +++ b/engine/include/editor/panel/asset_preview_panel.h @@ -0,0 +1,8 @@ +#pragma once +#include "render/editor_system.h" +namespace api { + class AssetPreviewPanel : public EditorPanel{ + public: + void DrawPanel() override; + }; +} \ No newline at end of file diff --git a/engine/include/editor/panel/menu_bar_panel.h b/engine/include/editor/panel/menu_bar_panel.h new file mode 100644 index 0000000..379fa39 --- /dev/null +++ b/engine/include/editor/panel/menu_bar_panel.h @@ -0,0 +1,8 @@ +#pragma once +#include "render/editor_system.h" +namespace api { + class MenuBarPanel : public EditorPanel { + public: + void DrawPanel() override; + }; +} \ No newline at end of file diff --git a/engine/include/editor/window/editor_main_window.h b/engine/include/editor/window/editor_main_window.h new file mode 100644 index 0000000..48a38b2 --- /dev/null +++ b/engine/include/editor/window/editor_main_window.h @@ -0,0 +1,9 @@ +#pragma once +#include "render/editor_system.h" +namespace api { + class EditorMainWindow : public EditorWindow { + public: + EditorMainWindow(); + void Draw() override; + }; +} \ No newline at end of file diff --git a/engine/modules/engine/asset/xmake.lua b/engine/modules/engine/asset/xmake.lua index 666f83b..fe95adf 100644 --- a/engine/modules/engine/asset/xmake.lua +++ b/engine/modules/engine/asset/xmake.lua @@ -4,4 +4,5 @@ static_component("asset","engine") }) add_headerfiles("include/**.h","include/**.inl") add_files("src/**.cpp") - add_deps("core", "zlib") \ No newline at end of file + add_deps("core", "zlib") + add_syslinks("Ole32",{public = true}) \ No newline at end of file diff --git a/engine/modules/engine/render/impl/renderapi_impl.inl b/engine/modules/engine/render/impl/renderapi_impl.inl index 60dbb3c..2dc6531 100644 --- a/engine/modules/engine/render/impl/renderapi_impl.inl +++ b/engine/modules/engine/render/impl/renderapi_impl.inl @@ -1,10 +1,10 @@ #include "render/renderapi.h" #include "render/module.h" #ifdef WITH_EDITOR -#include "render/imgui_system.h" +#include "render/editor_system.h" namespace api { - SINGLETON_DEFINE(ImguiSystem) - ImguiSystem::ImguiSystem() + SINGLETON_DEFINE(EditorSystem) + EditorSystem::EditorSystem() { SINGLETON_PTR(); } diff --git a/engine/modules/engine/render/include/render/editor_system.h b/engine/modules/engine/render/include/render/editor_system.h new file mode 100644 index 0000000..4017849 --- /dev/null +++ b/engine/modules/engine/render/include/render/editor_system.h @@ -0,0 +1,40 @@ +#pragma once +#ifdef WITH_EDITOR +#include "module/module_manager.h" +namespace api { + class EditorPanel { + public: + EditorPanel() = default; + ~EditorPanel() = default; + virtual void DrawPanel() = 0; + }; + class EditorWindow { + protected: + std::vector mPanels; + public: + EditorWindow() = default; + ~EditorWindow() = default; + virtual void Draw() = 0; + template + T* AddPanel(Args&&... args) { + T* ptr = new (GlobalPool()) T(std::forward(args)...); + mPanels.push_back(ptr); + return ptr; + } + }; + class RENDER_API EditorSystem : public ISystem + { + SINGLETON_IMPL(EditorSystem) + protected: + std::vector mWindows; + public: + EditorSystem(); + template + T* AddWindow(Args&&... args) { + T* ptr = new (GlobalPool()) T(std::forward(args)...); + mWindows.push_back(ptr); + return ptr; + } + }; +} +#endif // WITH_EDITOR \ No newline at end of file diff --git a/engine/modules/engine/render/include/render/imgui_system.h b/engine/modules/engine/render/include/render/imgui_system.h deleted file mode 100644 index e4e89e0..0000000 --- a/engine/modules/engine/render/include/render/imgui_system.h +++ /dev/null @@ -1,12 +0,0 @@ -#pragma once -#ifdef WITH_EDITOR -#include "module/module_manager.h" -namespace api { - class RENDER_API ImguiSystem : public ISystem - { - SINGLETON_IMPL(ImguiSystem) - public: - ImguiSystem(); - }; -} -#endif // WITH_EDITOR \ No newline at end of file diff --git a/engine/modules/render/vulkan/include/vkn/vulkan_imgui.h b/engine/modules/render/vulkan/include/vkn/vulkan_imgui_editor.h similarity index 61% rename from engine/modules/render/vulkan/include/vkn/vulkan_imgui.h rename to engine/modules/render/vulkan/include/vkn/vulkan_imgui_editor.h index fbcc436..bddc231 100644 --- a/engine/modules/render/vulkan/include/vkn/vulkan_imgui.h +++ b/engine/modules/render/vulkan/include/vkn/vulkan_imgui_editor.h @@ -1,14 +1,18 @@ #pragma once -#include "render/imgui_system.h" +#include "render/editor_system.h" #include "render/graph/frame_graph.h" namespace vkn { using api::FrameGraph; using api::RenderPassContext; - class VulkanImguiSystem : public api::ImguiSystem { + class VulkanImguiEditor : public api::EditorSystem { public: void Initialize() override; void Finalize() override; void Render(); + void OnBeginRenderFrame(); + static VulkanImguiEditor* Ptr() { + return (VulkanImguiEditor*)api::EditorSystem::Ptr(); + }; static void Setup(FrameGraph& graph, FrameGraph::RenderPassBuilder& builder); static void Execute(FrameGraph&, RenderPassContext&); }; diff --git a/engine/modules/render/vulkan/src/module.cpp b/engine/modules/render/vulkan/src/module.cpp index 6dee0cc..a9d282f 100644 --- a/engine/modules/render/vulkan/src/module.cpp +++ b/engine/modules/render/vulkan/src/module.cpp @@ -1,6 +1,6 @@ #include "xmalloc_new_delete.h" #include "vkn/module.h" -#include "vkn/vulkan_imgui.h" +#include "vkn/vulkan_imgui_editor.h" #include "vkn/loader/vulkan_glsl_loader.h" #include "pmr/frame_allocator.h" using namespace vkn; @@ -8,7 +8,7 @@ void VulkanModule::OnLoad(int argc, char** argv) { VulkanGlslLoader::Init(); #ifdef WITH_EDITOR - AddSystem(); + AddSystem(); #endif // WITH_EDITOR } diff --git a/engine/modules/render/vulkan/src/vulkan_imgui.cpp b/engine/modules/render/vulkan/src/vulkan_imgui_editor.cpp similarity index 89% rename from engine/modules/render/vulkan/src/vulkan_imgui.cpp rename to engine/modules/render/vulkan/src/vulkan_imgui_editor.cpp index 842dfc7..432397a 100644 --- a/engine/modules/render/vulkan/src/vulkan_imgui.cpp +++ b/engine/modules/render/vulkan/src/vulkan_imgui_editor.cpp @@ -1,4 +1,4 @@ -#include "vkn/vulkan_imgui.h" +#include "vkn/vulkan_imgui_editor.h" #include "vkn/vulkan_window.h" #include "vkn/vulkan_api.h" #include "vkn/vulkan_api_help.h" @@ -14,6 +14,7 @@ namespace vkn { using namespace api; static Name ImguiPassName{"ImguiPass"}; static VkDescriptorPool g_DescriptorPool = VK_NULL_HANDLE; + // Vulkan的ImGui接入比较麻烦,参考教程: https://frguthmann.github.io/posts/vulkan_imgui/ VkDescriptorPool CreateDescriptorPool(VkDevice device) { VkDescriptorPoolSize pool_sizes[] = { @@ -80,7 +81,7 @@ namespace vkn { throw std::runtime_error("Could not create Dear ImGui's render pass"); return renderPass; } - void VulkanImguiSystem::Initialize() + void VulkanImguiEditor::Initialize() { VulkanAPI* API = VulkanAPI::Ptr(); VulkanWindow* window = VulkanWindow::Ptr(); @@ -105,18 +106,24 @@ namespace vkn { init_info.Allocator = VK_NULL_HANDLE; ImGui_ImplVulkan_Init(&init_info); API->SetRenderPassInfo(ImguiPassName, renderPass); - EventSystem::Ptr()->BeginRenderFrame.Subscribe(&VulkanImguiSystem::Render, this); - + EventSystem::Ptr()->BeginRenderFrame.Subscribe(&VulkanImguiEditor::OnBeginRenderFrame, this); } - void VulkanImguiSystem::Finalize() + void VulkanImguiEditor::Finalize() { } - void VulkanImguiSystem::Render() + void VulkanImguiEditor::Render() { - VulkanAPI::Ptr()->graph.AddRenderPass(); + for (auto win : mWindows) + { + win->Draw(); + } } - void VulkanImguiSystem::Setup(FrameGraph& graph, FrameGraph::RenderPassBuilder& builder) + void VulkanImguiEditor::OnBeginRenderFrame() + { + VulkanAPI::Ptr()->graph.AddRenderPass(); + } + void VulkanImguiEditor::Setup(FrameGraph& graph, FrameGraph::RenderPassBuilder& builder) { AttachmentDesc surface{}; surface.FromTexture(graph.mSurface); @@ -127,17 +134,14 @@ namespace vkn { .Type(RenderPassNodeType::Imgui, RenderPassNodeFlag::Output) .Write(edge, ResourceState::COLOR_ATTACHMENT); } - void VulkanImguiSystem::Execute(FrameGraph& graph, RenderPassContext& context) + void VulkanImguiEditor::Execute(FrameGraph& graph, RenderPassContext& context) { graph.mSurface.state = ResourceState::PRESENT; ImGui_ImplVulkan_NewFrame(); ImGui_ImplSDL2_NewFrame(); ImGui::NewFrame(); - static float my_float = 0.5f; - ImGui::Begin("Hello, ImGui!"); - ImGui::Text("This is some useful text."); - ImGui::SliderFloat("float", &my_float, 0.0f, 1.0f); - ImGui::End(); + + VulkanImguiEditor::Ptr()->Render(); ImGui::Render(); VulkanContext& ctx = *(VulkanContext*)context.parent; diff --git a/engine/src/editor/editor.cpp b/engine/src/editor/editor.cpp index c0c9ce2..f9c0c5f 100644 --- a/engine/src/editor/editor.cpp +++ b/engine/src/editor/editor.cpp @@ -2,6 +2,7 @@ #include "data/global.h" #include "render/window.h" #include "render/renderapi.h" +#include "editor/window/editor_main_window.h" #include "imgui.h" namespace api { void EditorModule::OnLoad(int argc, char** argv) @@ -20,6 +21,7 @@ namespace api { } void EditorModule::Initialize(void) { - + EditorSystem* Editor = EditorSystem::Ptr(); + Editor->AddWindow(); } } \ No newline at end of file diff --git a/engine/src/editor/panel/asset_preview_panel.cpp b/engine/src/editor/panel/asset_preview_panel.cpp new file mode 100644 index 0000000..0b9a1db --- /dev/null +++ b/engine/src/editor/panel/asset_preview_panel.cpp @@ -0,0 +1,9 @@ +#include "editor/panel/asset_preview_panel.h" +#include +namespace api { + void AssetPreviewPanel::DrawPanel() { + static float my_float = 0.5f; + ImGui::Text("This is some useful text."); + ImGui::SliderFloat("float", &my_float, 0.0f, 1.0f); + } +} \ No newline at end of file diff --git a/engine/src/editor/panel/menu_bar_panel.cpp b/engine/src/editor/panel/menu_bar_panel.cpp new file mode 100644 index 0000000..e8d7ed9 --- /dev/null +++ b/engine/src/editor/panel/menu_bar_panel.cpp @@ -0,0 +1,25 @@ +#include "editor/panel/menu_bar_panel.h" +#include + +namespace api { + void MenuBarPanel::DrawPanel() + { + // 面板大小和位置 + if (ImGui::BeginMenu("Tools")) { + if (ImGui::MenuItem("Performance Analyzer")) { + // 显示性能分析工具 + } + if (ImGui::MenuItem("Debug Console")) { + // 显示调试控制台 + } + ImGui::EndMenu(); + } + + if (ImGui::BeginMenu("Help")) { + if (ImGui::MenuItem("About")) { + // 显示关于信息 + } + ImGui::EndMenu(); + } + } +} \ No newline at end of file diff --git a/engine/src/editor/window/editor_main_window.cpp b/engine/src/editor/window/editor_main_window.cpp new file mode 100644 index 0000000..062849f --- /dev/null +++ b/engine/src/editor/window/editor_main_window.cpp @@ -0,0 +1,19 @@ +#include "editor/window/editor_main_window.h" +#include "editor/panel/asset_preview_panel.h" +#include "editor/panel/menu_bar_panel.h" +#include +namespace api { + EditorMainWindow::EditorMainWindow() + { + AddPanel(); + AddPanel(); + } + void EditorMainWindow::Draw() + { + ImGui::Begin("MainWindow"); + for (auto panel : mPanels) { + panel->DrawPanel(); + } + ImGui::End(); + } +} \ No newline at end of file diff --git a/engine/xmake.lua b/engine/xmake.lua index 9e83b4c..9ca8010 100644 --- a/engine/xmake.lua +++ b/engine/xmake.lua @@ -6,14 +6,14 @@ target("engine") set_kind("shared") set_group("Engine") add_rules("engine.api") - add_files("src/engine/*.cpp") + add_files("src/engine/**.cpp") target("editor") set_kind("shared") set_group("Engine") add_rules("engine.api") - add_headerfiles("include/editor/*.h") + add_headerfiles("include/editor/**.h") add_includedirs("include") - add_files("src/editor/*.cpp") + add_files("src/editor/**.cpp") add_deps("engine",{public = true}) includes("xmake/xmake.lua") includes("3rdparty/xmake.lua") diff --git a/game/zworld/imgui.ini b/game/zworld/imgui.ini index 979773d..b4ee362 100644 --- a/game/zworld/imgui.ini +++ b/game/zworld/imgui.ini @@ -10,5 +10,11 @@ ViewportId=0x41ACA57A Size=191,71 Collapsed=0 +[Window][MainWindow] +ViewportPos=480,240 +ViewportId=0x72FC8CA3 +Size=191,105 +Collapsed=0 + [Docking][Data]