From 913eb0b685ae745745ac8eb4d6aaac62cd7829a1 Mon Sep 17 00:00:00 2001 From: Earth_Studio Date: Mon, 27 Apr 2026 14:41:32 +0800 Subject: [PATCH 01/34] OptiRender: fix optimization failed issue --- src/engine/framework/Framework.cpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/engine/framework/Framework.cpp b/src/engine/framework/Framework.cpp index 08c46f6..aa7243c 100644 --- a/src/engine/framework/Framework.cpp +++ b/src/engine/framework/Framework.cpp @@ -98,6 +98,8 @@ namespace cmaterial { windows.erase(name); delete win; } + + deadWindows.clear(); } return OK; From d1c7457ffe3b5599753f81cdb0bc8446f90f0843 Mon Sep 17 00:00:00 2001 From: Earth_Studio Date: Mon, 27 Apr 2026 18:21:03 +0800 Subject: [PATCH 02/34] Window: reabstract interface --- .../window/BasicWindow/BasicWindow.cpp | 40 +++++-------------- src/content/window/BasicWindow/BasicWindow.h | 1 + src/engine/window/IWindow.cpp | 37 ++++++++++++++++- src/engine/window/IWindow.h | 7 +++- 4 files changed, 52 insertions(+), 33 deletions(-) diff --git a/src/content/window/BasicWindow/BasicWindow.cpp b/src/content/window/BasicWindow/BasicWindow.cpp index 8f6e63c..c2e40e3 100644 --- a/src/content/window/BasicWindow/BasicWindow.cpp +++ b/src/content/window/BasicWindow/BasicWindow.cpp @@ -12,38 +12,16 @@ #include "engine/eventbus/EventBus.h" namespace cmaterial::window { - void BasicWindow::render(ImGuiIO *io) { - ImGuiWindowFlags flags = ImGuiWindowFlags_NoDecoration | ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoSavedSettings; - - ImGui::PushStyleVar(ImGuiStyleVar_FrameRounding, 10.0f); - ImGui::PushStyleVar(ImGuiStyleVar_WindowBorderSize, 0.0f); - ImGui::PushStyleVar(ImGuiStyleVar_FrameBorderSize, 1.0f); - if (ImGui::Begin(this->name.c_str(), nullptr, flags)) { - ImGui::Text("Hello, Engine!"); - ImGui::Text("Frame time: %.3f ms/frame (%.1f FPS)", 1000.0f / io->Framerate, io->Framerate); - - ImGui::Separator(); - - for (auto &pair: components) { - if (pair.second->getIsDead()) { - deadComponents.push_back(pair.first); - continue; - } - pair.second->render(io); - } - - for (const std::string &name: deadComponents) { - component::IComponent *component = components[name]; - components.erase(name); - delete component; - } + void BasicWindow::postInit(ImGuiIO *io) { + addStyle(ImGuiStyleVar_FrameRounding, 10.0f); + addStyle(ImGuiStyleVar_WindowBorderSize, 0.0f); + addStyle(ImGuiStyleVar_FrameBorderSize, 1.0f); + } - deadComponents.clear(); - } + void BasicWindow::render(ImGuiIO *io) { + ImGui::Text("Hello, Engine!"); + ImGui::Text("Frame time: %.3f ms/frame (%.1f FPS)", 1000.0f / io->Framerate, io->Framerate); - ImGui::End(); - ImGui::PopStyleVar(); - ImGui::PopStyleVar(); - ImGui::PopStyleVar(); + ImGui::Separator(); } } \ No newline at end of file diff --git a/src/content/window/BasicWindow/BasicWindow.h b/src/content/window/BasicWindow/BasicWindow.h index dda1d68..dd650fc 100644 --- a/src/content/window/BasicWindow/BasicWindow.h +++ b/src/content/window/BasicWindow/BasicWindow.h @@ -14,6 +14,7 @@ namespace cmaterial::window { using IWindow::IWindow; ~BasicWindow() = default; + void postInit(ImGuiIO *io) override; void render(ImGuiIO *io) override; }; } diff --git a/src/engine/window/IWindow.cpp b/src/engine/window/IWindow.cpp index cfd3793..212675c 100644 --- a/src/engine/window/IWindow.cpp +++ b/src/engine/window/IWindow.cpp @@ -81,6 +81,14 @@ namespace cmaterial::window { postUpdate(); } + void IWindow::addStyle(ImGuiStyleVar styleVar, float value) { + styles[styleVar] = value; + } + + void IWindow::removeStyle(ImGuiStyleVar styleVar) { + styles.erase(styleVar); + } + void IWindow::drawWindow(bool isVirtual) { if (!isVirtual) glfwMakeContextCurrent(glfwWindow); @@ -95,7 +103,34 @@ namespace cmaterial::window { ImGui::SetNextWindowPos(ImVec2(0, 0)); ImGui::SetNextWindowSize(io->DisplaySize); - this->render(io); + for (auto style : styles) { + ImGui::PushStyleVar(style.first, style.second); + } + + if (ImGui::Begin(this->name.c_str(), nullptr, windowFlags)) { + this->render(io); + + for (auto &pair: components) { + if (pair.second->getIsDead()) { + deadComponents.push_back(pair.first); + continue; + } + pair.second->drawComponent(io); + } + + for (const std::string &name: deadComponents) { + component::IComponent *component = components[name]; + components.erase(name); + delete component; + } + + deadComponents.clear(); + } + ImGui::End(); + + for (int i = 0; i < styles.size(); i++) { + ImGui::PopStyleVar(); + } ImGui::Render(); diff --git a/src/engine/window/IWindow.h b/src/engine/window/IWindow.h index 6e8a382..f20f007 100644 --- a/src/engine/window/IWindow.h +++ b/src/engine/window/IWindow.h @@ -28,14 +28,18 @@ namespace cmaterial::window { virtual void postInit(ImGuiIO *io) {}; virtual void postUpdate() {}; virtual void update(); - virtual void render(ImGuiIO* io) = 0; + virtual void render(ImGuiIO* io) {}; + void addComponent(component::IComponent *comp); + void addStyle(ImGuiStyleVar styleVar, float value); + void removeStyle(ImGuiStyleVar styleVar); std::string name; int width, height; bool isDead = false; bool isInitialized = false; + ImGuiWindowFlags windowFlags = ImGuiWindowFlags_NoDecoration | ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoSavedSettings; bool _isSizeChange = false; @@ -49,6 +53,7 @@ namespace cmaterial::window { std::unordered_map components; std::vector deadComponents; + std::unordered_map styles; }; } From 888f41765ea041e2937a5f003529534445dff5e9 Mon Sep 17 00:00:00 2001 From: Earth_Studio Date: Mon, 27 Apr 2026 18:51:53 +0800 Subject: [PATCH 03/34] add some docs --- src/main.cpp | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/main.cpp b/src/main.cpp index e5f2ff4..740a927 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -10,25 +10,32 @@ #include "content/window/BasicWindow/BasicWindow.h" int main() { + // Before you start to write application, you must initialize the Framework first: + cmaterial::Framework app; app.initialize(); + // Add your Listener: cmaterial::event::EventBus::addListener(new cmaterial::event::WindowLogic(&app)); + // Create your windows: cmaterial::window::BasicWindow* window1 = new cmaterial::window::BasicWindow("window1", 300, 100); cmaterial::window::BasicWindow* window2 = new cmaterial::window::BasicWindow("window2", 300, 100); + // Create your components: cmaterial::component::BasicButton* button1 = new cmaterial::component::BasicButton(); button1->name = "buttonInWindow1"; cmaterial::component::CreatorButton* button2 = new cmaterial::component::CreatorButton(); button2->name = "buttonInWindow2"; + // To inject components in an exist window: app.addWindow(window1); window1->addComponent(button1); app.addWindow(window2); window2->addComponent(button2); + // Finally, run your app by doing this: app.run(); return 0; } \ No newline at end of file From 7f62b2697ccb04c64c3bba5a6b5cfb7fdc5f4790 Mon Sep 17 00:00:00 2001 From: Earth_Studio Date: Mon, 27 Apr 2026 18:53:45 +0800 Subject: [PATCH 04/34] add some docs --- src/main.cpp | 1 - 1 file changed, 1 deletion(-) diff --git a/src/main.cpp b/src/main.cpp index 740a927..dafda6e 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -11,7 +11,6 @@ int main() { // Before you start to write application, you must initialize the Framework first: - cmaterial::Framework app; app.initialize(); From dfcd53c0639fc74e5566d9c2fd9818859cdac3bb Mon Sep 17 00:00:00 2001 From: Earth_Studio Date: Mon, 27 Apr 2026 19:52:42 +0800 Subject: [PATCH 05/34] OptiRender: force redraw when components/layers update --- src/content/logic/listener/WindowLogic.h | 2 +- .../{EmptyEvent.hpp => ForceRedrawEvent.hpp} | 4 +-- .../internal/listener/ForceRedrawListener.hpp | 35 +++++++++++++++++++ src/engine/framework/Framework.cpp | 10 ++++-- src/engine/framework/Framework.h | 1 + src/engine/window/IWindow.cpp | 2 +- 6 files changed, 47 insertions(+), 7 deletions(-) rename src/engine/eventbus/internal/event/{EmptyEvent.hpp => ForceRedrawEvent.hpp} (74%) create mode 100644 src/engine/eventbus/internal/listener/ForceRedrawListener.hpp diff --git a/src/content/logic/listener/WindowLogic.h b/src/content/logic/listener/WindowLogic.h index 5181bd2..7b604ef 100644 --- a/src/content/logic/listener/WindowLogic.h +++ b/src/content/logic/listener/WindowLogic.h @@ -18,7 +18,7 @@ namespace cmaterial::event { public: WindowLogic(Framework* a) : app(a) {} - EVENTHANDLER(cmaterial::event::SpawnWindowRequest, onSpawnRequest) + EVENTHANDLER(SpawnWindowRequest, onSpawnRequest) void onSpawnRequest(SpawnWindowRequest* e) { auto* newWin = new window::BasicWindow(e->title, 300, 100); app->addWindow(newWin); diff --git a/src/engine/eventbus/internal/event/EmptyEvent.hpp b/src/engine/eventbus/internal/event/ForceRedrawEvent.hpp similarity index 74% rename from src/engine/eventbus/internal/event/EmptyEvent.hpp rename to src/engine/eventbus/internal/event/ForceRedrawEvent.hpp index f97975b..bb2f64e 100644 --- a/src/engine/eventbus/internal/event/EmptyEvent.hpp +++ b/src/engine/eventbus/internal/event/ForceRedrawEvent.hpp @@ -8,8 +8,8 @@ namespace cmaterial::event::internal { - struct EmptyEvent : IEvent { - REGISTEREVENT(EmptyEvent); + struct ForceRedrawEvent : IEvent { + REGISTEREVENT(ForceRedrawEvent); }; } diff --git a/src/engine/eventbus/internal/listener/ForceRedrawListener.hpp b/src/engine/eventbus/internal/listener/ForceRedrawListener.hpp new file mode 100644 index 0000000..018b4a4 --- /dev/null +++ b/src/engine/eventbus/internal/listener/ForceRedrawListener.hpp @@ -0,0 +1,35 @@ +// +// Created by Earth_Studio on 2026/4/27. +// + +#ifndef CMATERIAL_FORCEREDRAWLISTENER_H +#define CMATERIAL_FORCEREDRAWLISTENER_H + + +#include "engine/eventbus/IListener.h" +#include "engine/eventbus/internal/event/ForceRedrawEvent.hpp" + +#include "glad/gl.h" +#include "glfw/glfw3.h" + +namespace cmaterial::event::internal { + class ForceRedrawListener : public IListener { + public: + ForceRedrawListener(bool* isForceRedraw) { + this->isForceRedraw = isForceRedraw; + } + + EVENTHANDLER(ForceRedrawEvent, onForceRedraw); + void onForceRedraw(ForceRedrawEvent* e) { + *isForceRedraw = true; + glfwPostEmptyEvent(); // Just in case + }; + + private: + bool* isForceRedraw; + }; +} + + + +#endif //CMATERIAL_FORCEREDRAWLISTENER_H diff --git a/src/engine/framework/Framework.cpp b/src/engine/framework/Framework.cpp index aa7243c..c1d33bf 100644 --- a/src/engine/framework/Framework.cpp +++ b/src/engine/framework/Framework.cpp @@ -14,12 +14,13 @@ #endif #include "engine/eventbus/EventBus.h" -#include "engine/eventbus/internal/event/EmptyEvent.hpp" +#include "engine/eventbus/internal/event/ForceRedrawEvent.hpp" #include #include #include "engine/animation/Player.h" +#include "engine/eventbus/internal/listener/ForceRedrawListener.hpp" using EventBus = cmaterial::event::EventBus; @@ -51,6 +52,8 @@ namespace cmaterial { hiddenImgui = ImGui::CreateContext(fontAtlas); ImGui::SetCurrentContext(hiddenImgui); + EventBus::addListener(new event::internal::ForceRedrawListener(&isForceRedraw)); + isInitialized = true; return OK; @@ -60,7 +63,7 @@ namespace cmaterial { if (!isInitialized) return NOT_INIT; - EventBus::postEvent(new event::internal::EmptyEvent); + EventBus::postEvent(new event::internal::ForceRedrawEvent); while (!glfwWindowShouldClose(hiddenWindow) && !windows.empty()) { bool isEventBusy = EventBus::dispatch(); bool isAnimationBusy = animation::Player::update(); @@ -85,7 +88,7 @@ namespace cmaterial { else continue; } - pair.second->drawWindow(!isAnimationBusy); + pair.second->drawWindow(!(isForceRedraw || isAnimationBusy)); } if (!deadWindows.empty()) { @@ -100,6 +103,7 @@ namespace cmaterial { } deadWindows.clear(); + isForceRedraw = false; } return OK; diff --git a/src/engine/framework/Framework.h b/src/engine/framework/Framework.h index 88adc63..8c6c708 100644 --- a/src/engine/framework/Framework.h +++ b/src/engine/framework/Framework.h @@ -33,6 +33,7 @@ namespace cmaterial { private: bool isInitialized = false; int globalFrameCount = 0; + bool isForceRedraw = false; GLFWwindow* hiddenWindow = nullptr; ImGuiContext* hiddenImgui = nullptr; diff --git a/src/engine/window/IWindow.cpp b/src/engine/window/IWindow.cpp index 212675c..0a1879e 100644 --- a/src/engine/window/IWindow.cpp +++ b/src/engine/window/IWindow.cpp @@ -11,7 +11,7 @@ #include "GLFW/glfw3.h" #include "engine/eventbus/EventBus.h" -#include "engine/eventbus/internal/event/EmptyEvent.hpp" +#include "engine/eventbus/internal/event/ForceRedrawEvent.hpp" #include "imgui_internal.h" namespace cmaterial::window { From e65f9cf727307ec6bec7cb00a9cacb8c7cdb9c31 Mon Sep 17 00:00:00 2001 From: Earth_Studio Date: Mon, 27 Apr 2026 20:25:35 +0800 Subject: [PATCH 06/34] add some docs --- src/engine/window/IWindow.cpp | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/engine/window/IWindow.cpp b/src/engine/window/IWindow.cpp index 0a1879e..40c4f5c 100644 --- a/src/engine/window/IWindow.cpp +++ b/src/engine/window/IWindow.cpp @@ -143,6 +143,16 @@ namespace cmaterial::window { ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData()); glfwSwapBuffers(glfwWindow); } + // This is the most genius code I've ever wrote!!! + // We all know ImGui is Immediately Mode, so its logic and render is force bind, you cannot trigger logic or render independent. + // HOWEVER, to be portable across different platforms, ImGui split itself into two parts: the core **library** and the **backend**. + // In other words, the ImGui core is responsible for calculating vertex data, which it then submits to the backend for rendering. + // This is where the magic happens: although ImGui's rendering and logic are forcibly bound, rendering MUST actually go through the backend submission. + // The REAL rendering is ultimately done by the **backend**, and the vertex computation overhead is actually tiny compared to full rendering. + // So, by calling ImGui::Render() to let the ImGui core execute the complete rendering pipeline — but WITHOUT submitting to the backend for + // actual rendering — we can achieve a complete DECOUPLING of logic and rendering! + // Although the CPU will inevitably still calculate the vertices, we have successfully completely decoupled rendering from logic, + // and the **possibilities** from here are INFINITE! } void IWindow::addComponent(component::IComponent* comp) { From e1d8e8748e3209bb31791d6af37512d4105b9006 Mon Sep 17 00:00:00 2001 From: Earth_Studio Date: Mon, 27 Apr 2026 21:34:44 +0800 Subject: [PATCH 07/34] Component: Layer render test --- .../AnimationButton/AnimationButton.cpp | 16 ++++ .../AnimationButton/AnimationButton.h | 31 ++++++ .../layers/BackgroundLayer.cpp | 16 ++++ .../AnimationButton/layers/BackgroundLayer.h | 22 +++++ .../AnimationButton/layers/OverlayLayer.cpp | 12 +++ .../AnimationButton/layers/OverlayLayer.h | 22 +++++ src/engine/component/IComponent.cpp | 96 ++++++++++++++++++- src/engine/component/IComponent.h | 17 ++-- src/engine/component/ILayer.h | 2 +- src/main.cpp | 5 + 10 files changed, 227 insertions(+), 12 deletions(-) create mode 100644 src/content/component/AnimationButton/AnimationButton.cpp create mode 100644 src/content/component/AnimationButton/AnimationButton.h create mode 100644 src/content/component/AnimationButton/layers/BackgroundLayer.cpp create mode 100644 src/content/component/AnimationButton/layers/BackgroundLayer.h create mode 100644 src/content/component/AnimationButton/layers/OverlayLayer.cpp create mode 100644 src/content/component/AnimationButton/layers/OverlayLayer.h diff --git a/src/content/component/AnimationButton/AnimationButton.cpp b/src/content/component/AnimationButton/AnimationButton.cpp new file mode 100644 index 0000000..30d47c0 --- /dev/null +++ b/src/content/component/AnimationButton/AnimationButton.cpp @@ -0,0 +1,16 @@ +// +// Created by Earth_Studio on 2026/4/25. +// + +#include "AnimationButton.h" + +namespace cmaterial::component { + AnimationButton::AnimationButton() { + addLayer(&backgroundLayer); + addLayer(&overlayLayer); + } + + void AnimationButton::render(ImGuiIO *io) { + ImGui::Button(name.c_str()); + } +} // namespace cmaterial::component diff --git a/src/content/component/AnimationButton/AnimationButton.h b/src/content/component/AnimationButton/AnimationButton.h new file mode 100644 index 0000000..9f970b0 --- /dev/null +++ b/src/content/component/AnimationButton/AnimationButton.h @@ -0,0 +1,31 @@ +// +// Created by Earth_Studio on 2026/4/25. +// + +#ifndef CMATERIAL_ANIMATIONBUTTON_H +#define CMATERIAL_ANIMATIONBUTTON_H + + +#include "engine/component/IComponent.h" + +#include "layers/BackgroundLayer.h" +#include "layers/OverlayLayer.h" + + +namespace cmaterial::component { + class AnimationButton : public IComponent { + public: + AnimationButton(); + ~AnimationButton() = default; + + void render(ImGuiIO *io) override; + + private: + layer::animationbutton::BackgroundLayer backgroundLayer; + layer::animationbutton::OverlayLayer overlayLayer; + }; +} + + + +#endif //CMATERIAL_ANIMATIONBUTTON_H diff --git a/src/content/component/AnimationButton/layers/BackgroundLayer.cpp b/src/content/component/AnimationButton/layers/BackgroundLayer.cpp new file mode 100644 index 0000000..131daad --- /dev/null +++ b/src/content/component/AnimationButton/layers/BackgroundLayer.cpp @@ -0,0 +1,16 @@ +// +// Created by Earth_Studio on 2026/4/27. +// + +#include "BackgroundLayer.h" + +namespace cmaterial::component::layer::animationbutton { + BackgroundLayer::BackgroundLayer() { + priority = -1; + } + + void BackgroundLayer::render(ImDrawList *drawList, ImVec2 startPos, ImVec2 size) { + ImGui::SetCursorPos(startPos); + ImGui::Button("BackGround", ImVec2(size.x + 5, size.y + 5)); + } +} diff --git a/src/content/component/AnimationButton/layers/BackgroundLayer.h b/src/content/component/AnimationButton/layers/BackgroundLayer.h new file mode 100644 index 0000000..6ac3887 --- /dev/null +++ b/src/content/component/AnimationButton/layers/BackgroundLayer.h @@ -0,0 +1,22 @@ +// +// Created by Earth_Studio on 2026/4/27. +// + +#ifndef CMATERIAL_BACKGROUNDLAYER_H +#define CMATERIAL_BACKGROUNDLAYER_H +#include "engine/component/ILayer.h" + + +namespace cmaterial::component::layer::animationbutton { + class BackgroundLayer : public ILayer{ + public: + BackgroundLayer(); + ~BackgroundLayer() = default; + + void render(ImDrawList *drawList, ImVec2 startPos, ImVec2 size) override; + }; +} + + + +#endif //CMATERIAL_BACKGROUNDLAYER_H diff --git a/src/content/component/AnimationButton/layers/OverlayLayer.cpp b/src/content/component/AnimationButton/layers/OverlayLayer.cpp new file mode 100644 index 0000000..424773a --- /dev/null +++ b/src/content/component/AnimationButton/layers/OverlayLayer.cpp @@ -0,0 +1,12 @@ +// +// Created by Earth_Studio on 2026/4/27. +// + +#include "OverlayLayer.h" + +namespace cmaterial::component::layer::animationbutton { + void OverlayLayer::render(ImDrawList *drawList, ImVec2 startPos, ImVec2 size) { + ImGui::SetCursorPos(startPos); + ImGui::Button("OverLay", ImVec2(size.x - 5, size.y - 5)); + } +} // namespace cmaterial::component::layer::animationbutton diff --git a/src/content/component/AnimationButton/layers/OverlayLayer.h b/src/content/component/AnimationButton/layers/OverlayLayer.h new file mode 100644 index 0000000..cc1fb01 --- /dev/null +++ b/src/content/component/AnimationButton/layers/OverlayLayer.h @@ -0,0 +1,22 @@ +// +// Created by Earth_Studio on 2026/4/27. +// + +#ifndef CMATERIAL_OVERLAYLAYER_H +#define CMATERIAL_OVERLAYLAYER_H +#include "engine/component/ILayer.h" + + +namespace cmaterial::component::layer::animationbutton { + class OverlayLayer : public ILayer { + public: + OverlayLayer() = default; + ~OverlayLayer() = default; + + void render(ImDrawList *drawList, ImVec2 startPos, ImVec2 size) override; + }; +} + + + +#endif //CMATERIAL_OVERLAYLAYER_H diff --git a/src/engine/component/IComponent.cpp b/src/engine/component/IComponent.cpp index 05daba7..5306724 100644 --- a/src/engine/component/IComponent.cpp +++ b/src/engine/component/IComponent.cpp @@ -6,7 +6,12 @@ #include "GLFW/glfw3.h" #include "engine/eventbus/EventBus.h" -#include "engine/eventbus/internal/event/EmptyEvent.hpp" +#include "engine/eventbus/internal/event/ForceRedrawEvent.hpp" + +#include "ILayer.h" + +#include +#include namespace cmaterial::component { void IComponent::addComponent(IComponent *component) { @@ -16,11 +21,94 @@ namespace cmaterial::component { if (component->name.empty()) return; - components.insert({component->name, component}); - event::EventBus::postEvent(new event::internal::EmptyEvent); + components.push_back(component); + event::EventBus::postEvent(new event::internal::ForceRedrawEvent); + } + + void IComponent::removeComponent(IComponent *component) { + if (component == nullptr) + return; + + std::erase(components, component); + } + + void IComponent::addLayer(ILayer *layer) { + if (layer == nullptr) + return; + + std::vector& layers = (layer->priority < 0) ? layersBefore : layersAfter; + + auto it = std::lower_bound(layers.begin(), layers.end(), layer, + [](ILayer* a, ILayer* b) { + return a->priority < b->priority; + }); + + layers.insert(it, layer); + event::EventBus::postEvent(new event::internal::ForceRedrawEvent); + } + + void IComponent::removeLayer(ILayer *layer) { + if (layer == nullptr) + return; + + std::erase(layersBefore, layer); + std::erase(layersAfter, layer); } - bool IComponent::getIsDead() { + void IComponent::drawComponent(ImGuiIO *io) { + if (!isActive) return; + + ImDrawList* drawList = ImGui::GetWindowDrawList(); + + // 🔮 1. 开启“次元拆分”:我们准备 3 个通道 + // 通道 0: 背景与阴影 (Before) + // 通道 1: 组件本体内容 (render) + // 通道 2: 特效与覆盖层 (After) + ImDrawListSplitter splitter; + splitter.Split(drawList, 3); + + // 记录起始位置 + ImVec2 startPos = ImGui::GetCursorScreenPos(); + + // 🔮 2. 切换到通道 1:先跑组件逻辑和本体绘制 + // 为什么要先画通道 1?因为我们要靠本体绘制来撑开布局,确定组件的最终 Size + splitter.SetCurrentChannel(drawList, 1); + + // 开启一个组,确保内部组件被视为一个整体 + ImGui::BeginGroup(); + this->render(io); + ImGui::EndGroup(); + + // 🔮 3. 现在我们拿到了组件真实的物理尺寸 + ImVec2 minPos = ImGui::GetItemRectMin(); + ImVec2 maxPos = ImGui::GetItemRectMax(); + ImVec2 size = ImVec2(maxPos.x - minPos.x, maxPos.y - minPos.y); + + // 🔮 4. 切换到通道 0:补画背景和阴影 + // 虽然我们在代码里后写,但在 GPU 渲染时,它会排在最前面 + splitter.SetCurrentChannel(drawList, 0); + for (auto* layer : layersBefore) { + layer->render(drawList, minPos, size); + } + + // 🔮 5. 切换到通道 2:补画特效 + splitter.SetCurrentChannel(drawList, 2); + for (auto* layer : layersAfter) { + layer->render(drawList, minPos, size); + } + + // 🔮 6. 合并通道:物理级合批就在这一步发生 + splitter.Merge(drawList); + // TODO: child component render support, do this after Layout System complete. + } + + bool IComponent::getIsDead() const { return isDead; } + + IComponent::~IComponent() { + components.clear(); + layersBefore.clear(); + layersAfter.clear(); + } } diff --git a/src/engine/component/IComponent.h b/src/engine/component/IComponent.h index 6a461d8..f5c5766 100644 --- a/src/engine/component/IComponent.h +++ b/src/engine/component/IComponent.h @@ -9,7 +9,6 @@ #include "imgui.h" -#include #include #include @@ -19,13 +18,16 @@ namespace cmaterial::component { class IComponent { public: - virtual ~IComponent() = default; + virtual ~IComponent(); virtual void render(ImGuiIO *io) = 0; - virtual void addComponent(IComponent *component); + virtual void drawComponent(ImGuiIO *io); - void addLayer(); + void addComponent(IComponent *component); + void removeComponent(IComponent *component); + void addLayer(ILayer *layer); + void removeLayer(ILayer *layer); - bool getIsDead(); + bool getIsDead() const; std::string name; int xPercent = 0; @@ -34,8 +36,9 @@ namespace cmaterial::component { protected: bool isDead = false; - std::unordered_map components; - std::vector layers; + std::vector components; + std::vector layersBefore; + std::vector layersAfter; }; } diff --git a/src/engine/component/ILayer.h b/src/engine/component/ILayer.h index bb27561..79a85fa 100644 --- a/src/engine/component/ILayer.h +++ b/src/engine/component/ILayer.h @@ -14,7 +14,7 @@ namespace cmaterial::component { public: virtual ~ILayer() = default; - virtual ImDrawData render() = 0; + virtual void render(ImDrawList* drawList, ImVec2 startPos, ImVec2 size) = 0; int priority = 0; }; diff --git a/src/main.cpp b/src/main.cpp index dafda6e..8bfe69f 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -2,6 +2,7 @@ // Created by Earth_Studio on 2026/4/12. // +#include "content/component/AnimationButton/AnimationButton.h" #include "engine/framework/Framework.h" #include "content/component/BasicButton/BasicButton.h" @@ -27,9 +28,13 @@ int main() { cmaterial::component::CreatorButton* button2 = new cmaterial::component::CreatorButton(); button2->name = "buttonInWindow2"; + cmaterial::component::AnimationButton* md3Button = new cmaterial::component::AnimationButton(); + md3Button->name = "MD3 Button"; + // To inject components in an exist window: app.addWindow(window1); window1->addComponent(button1); + window1->addComponent(md3Button); app.addWindow(window2); window2->addComponent(button2); From 8ca5d05e65ef493098b666a92dd5f33cf729182b Mon Sep 17 00:00:00 2001 From: Earth_Studio Date: Mon, 27 Apr 2026 21:48:51 +0800 Subject: [PATCH 08/34] Window: fix PopStyle logic --- src/engine/window/IWindow.cpp | 7 ++++--- src/engine/window/IWindow.h | 1 + 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/src/engine/window/IWindow.cpp b/src/engine/window/IWindow.cpp index 40c4f5c..fd98c9a 100644 --- a/src/engine/window/IWindow.cpp +++ b/src/engine/window/IWindow.cpp @@ -107,6 +107,8 @@ namespace cmaterial::window { ImGui::PushStyleVar(style.first, style.second); } + ImGui::PushStyleColor(ImGuiCol_WindowBg, backgroundColor); + if (ImGui::Begin(this->name.c_str(), nullptr, windowFlags)) { this->render(io); @@ -128,9 +130,8 @@ namespace cmaterial::window { } ImGui::End(); - for (int i = 0; i < styles.size(); i++) { - ImGui::PopStyleVar(); - } + ImGui::PopStyleColor(); + ImGui::PopStyleVar(styles.size()); ImGui::Render(); diff --git a/src/engine/window/IWindow.h b/src/engine/window/IWindow.h index f20f007..0f28a25 100644 --- a/src/engine/window/IWindow.h +++ b/src/engine/window/IWindow.h @@ -40,6 +40,7 @@ namespace cmaterial::window { bool isDead = false; bool isInitialized = false; ImGuiWindowFlags windowFlags = ImGuiWindowFlags_NoDecoration | ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoSavedSettings; + ImVec4 backgroundColor = ImVec4(0.0f, 0.0f, 0.0f, 1.0f); bool _isSizeChange = false; From b0ca0fb6b127ecfc3b1fc5ef7493f8cd6798a578 Mon Sep 17 00:00:00 2001 From: Earth_Studio Date: Mon, 27 Apr 2026 22:02:47 +0800 Subject: [PATCH 09/34] Component: Layer test --- src/content/component/AnimationButton/AnimationButton.cpp | 2 ++ .../component/AnimationButton/layers/BackgroundLayer.cpp | 2 ++ src/content/component/AnimationButton/layers/OverlayLayer.cpp | 2 ++ 3 files changed, 6 insertions(+) diff --git a/src/content/component/AnimationButton/AnimationButton.cpp b/src/content/component/AnimationButton/AnimationButton.cpp index 30d47c0..01fd349 100644 --- a/src/content/component/AnimationButton/AnimationButton.cpp +++ b/src/content/component/AnimationButton/AnimationButton.cpp @@ -11,6 +11,8 @@ namespace cmaterial::component { } void AnimationButton::render(ImGuiIO *io) { + ImGui::PushStyleColor(ImGuiCol_Button, ImVec4(0.0f, 1.0f, 0.0f, 1.0f)); ImGui::Button(name.c_str()); + ImGui::PopStyleColor(); } } // namespace cmaterial::component diff --git a/src/content/component/AnimationButton/layers/BackgroundLayer.cpp b/src/content/component/AnimationButton/layers/BackgroundLayer.cpp index 131daad..4011daf 100644 --- a/src/content/component/AnimationButton/layers/BackgroundLayer.cpp +++ b/src/content/component/AnimationButton/layers/BackgroundLayer.cpp @@ -11,6 +11,8 @@ namespace cmaterial::component::layer::animationbutton { void BackgroundLayer::render(ImDrawList *drawList, ImVec2 startPos, ImVec2 size) { ImGui::SetCursorPos(startPos); + ImGui::PushStyleColor(ImGuiCol_Button, ImVec4(0.0f, 0.0f, 1.0f, 1.0f)); ImGui::Button("BackGround", ImVec2(size.x + 5, size.y + 5)); + ImGui::PopStyleColor(); } } diff --git a/src/content/component/AnimationButton/layers/OverlayLayer.cpp b/src/content/component/AnimationButton/layers/OverlayLayer.cpp index 424773a..b6484a3 100644 --- a/src/content/component/AnimationButton/layers/OverlayLayer.cpp +++ b/src/content/component/AnimationButton/layers/OverlayLayer.cpp @@ -7,6 +7,8 @@ namespace cmaterial::component::layer::animationbutton { void OverlayLayer::render(ImDrawList *drawList, ImVec2 startPos, ImVec2 size) { ImGui::SetCursorPos(startPos); + ImGui::PushStyleColor(ImGuiCol_Button, ImVec4(1.0f, 0.0f, 0.0f, 1.0f)); ImGui::Button("OverLay", ImVec2(size.x - 5, size.y - 5)); + ImGui::PopStyleColor(); } } // namespace cmaterial::component::layer::animationbutton From aa40831e392eb14fc22834624faefe7a9d5f5bda Mon Sep 17 00:00:00 2001 From: Earth_Studio Date: Mon, 27 Apr 2026 22:04:51 +0800 Subject: [PATCH 10/34] Component: Layer test --- src/engine/component/IComponent.cpp | 13 ------------- 1 file changed, 13 deletions(-) diff --git a/src/engine/component/IComponent.cpp b/src/engine/component/IComponent.cpp index 5306724..cb29a4d 100644 --- a/src/engine/component/IComponent.cpp +++ b/src/engine/component/IComponent.cpp @@ -60,44 +60,31 @@ namespace cmaterial::component { ImDrawList* drawList = ImGui::GetWindowDrawList(); - // 🔮 1. 开启“次元拆分”:我们准备 3 个通道 - // 通道 0: 背景与阴影 (Before) - // 通道 1: 组件本体内容 (render) - // 通道 2: 特效与覆盖层 (After) ImDrawListSplitter splitter; splitter.Split(drawList, 3); - // 记录起始位置 ImVec2 startPos = ImGui::GetCursorScreenPos(); - // 🔮 2. 切换到通道 1:先跑组件逻辑和本体绘制 - // 为什么要先画通道 1?因为我们要靠本体绘制来撑开布局,确定组件的最终 Size splitter.SetCurrentChannel(drawList, 1); - // 开启一个组,确保内部组件被视为一个整体 ImGui::BeginGroup(); this->render(io); ImGui::EndGroup(); - // 🔮 3. 现在我们拿到了组件真实的物理尺寸 ImVec2 minPos = ImGui::GetItemRectMin(); ImVec2 maxPos = ImGui::GetItemRectMax(); ImVec2 size = ImVec2(maxPos.x - minPos.x, maxPos.y - minPos.y); - // 🔮 4. 切换到通道 0:补画背景和阴影 - // 虽然我们在代码里后写,但在 GPU 渲染时,它会排在最前面 splitter.SetCurrentChannel(drawList, 0); for (auto* layer : layersBefore) { layer->render(drawList, minPos, size); } - // 🔮 5. 切换到通道 2:补画特效 splitter.SetCurrentChannel(drawList, 2); for (auto* layer : layersAfter) { layer->render(drawList, minPos, size); } - // 🔮 6. 合并通道:物理级合批就在这一步发生 splitter.Merge(drawList); // TODO: child component render support, do this after Layout System complete. } From 31c891b53cee21398854b6073c803ebdd5467c04 Mon Sep 17 00:00:00 2001 From: Earth_Studio Date: Tue, 28 Apr 2026 12:55:32 +0800 Subject: [PATCH 11/34] MD3: first buggy implement test --- ...imationButton.cpp => MultiLayerButton.cpp} | 6 +- .../{AnimationButton.h => MultiLayerButton.h} | 10 +- .../{layers => layer}/BackgroundLayer.cpp | 0 .../{layers => layer}/BackgroundLayer.h | 0 .../{layers => layer}/OverlayLayer.cpp | 0 .../{layers => layer}/OverlayLayer.h | 0 .../component/material/Button/Button.cpp | 92 +++++++++++++++++++ .../component/material/Button/Button.h | 86 +++++++++++++++++ .../window/BasicWindow/BasicWindow.cpp | 2 - src/engine/animation/IAnimation.h | 2 +- src/engine/animation/Player.cpp | 15 ++- src/engine/component/IComponent.cpp | 2 - src/engine/component/ILayer.cpp | 8 +- src/engine/component/ILayer.h | 7 ++ src/main.cpp | 14 ++- 15 files changed, 222 insertions(+), 22 deletions(-) rename src/content/component/AnimationButton/{AnimationButton.cpp => MultiLayerButton.cpp} (74%) rename src/content/component/AnimationButton/{AnimationButton.h => MultiLayerButton.h} (70%) rename src/content/component/AnimationButton/{layers => layer}/BackgroundLayer.cpp (100%) rename src/content/component/AnimationButton/{layers => layer}/BackgroundLayer.h (100%) rename src/content/component/AnimationButton/{layers => layer}/OverlayLayer.cpp (100%) rename src/content/component/AnimationButton/{layers => layer}/OverlayLayer.h (100%) create mode 100644 src/content/component/material/Button/Button.cpp create mode 100644 src/content/component/material/Button/Button.h diff --git a/src/content/component/AnimationButton/AnimationButton.cpp b/src/content/component/AnimationButton/MultiLayerButton.cpp similarity index 74% rename from src/content/component/AnimationButton/AnimationButton.cpp rename to src/content/component/AnimationButton/MultiLayerButton.cpp index 01fd349..e627ba1 100644 --- a/src/content/component/AnimationButton/AnimationButton.cpp +++ b/src/content/component/AnimationButton/MultiLayerButton.cpp @@ -2,15 +2,15 @@ // Created by Earth_Studio on 2026/4/25. // -#include "AnimationButton.h" +#include "MultiLayerButton.h" namespace cmaterial::component { - AnimationButton::AnimationButton() { + MultiLayerButton::MultiLayerButton() { addLayer(&backgroundLayer); addLayer(&overlayLayer); } - void AnimationButton::render(ImGuiIO *io) { + void MultiLayerButton::render(ImGuiIO *io) { ImGui::PushStyleColor(ImGuiCol_Button, ImVec4(0.0f, 1.0f, 0.0f, 1.0f)); ImGui::Button(name.c_str()); ImGui::PopStyleColor(); diff --git a/src/content/component/AnimationButton/AnimationButton.h b/src/content/component/AnimationButton/MultiLayerButton.h similarity index 70% rename from src/content/component/AnimationButton/AnimationButton.h rename to src/content/component/AnimationButton/MultiLayerButton.h index 9f970b0..52c735e 100644 --- a/src/content/component/AnimationButton/AnimationButton.h +++ b/src/content/component/AnimationButton/MultiLayerButton.h @@ -8,15 +8,15 @@ #include "engine/component/IComponent.h" -#include "layers/BackgroundLayer.h" -#include "layers/OverlayLayer.h" +#include "layer/BackgroundLayer.h" +#include "layer/OverlayLayer.h" namespace cmaterial::component { - class AnimationButton : public IComponent { + class MultiLayerButton : public IComponent { public: - AnimationButton(); - ~AnimationButton() = default; + MultiLayerButton(); + ~MultiLayerButton() = default; void render(ImGuiIO *io) override; diff --git a/src/content/component/AnimationButton/layers/BackgroundLayer.cpp b/src/content/component/AnimationButton/layer/BackgroundLayer.cpp similarity index 100% rename from src/content/component/AnimationButton/layers/BackgroundLayer.cpp rename to src/content/component/AnimationButton/layer/BackgroundLayer.cpp diff --git a/src/content/component/AnimationButton/layers/BackgroundLayer.h b/src/content/component/AnimationButton/layer/BackgroundLayer.h similarity index 100% rename from src/content/component/AnimationButton/layers/BackgroundLayer.h rename to src/content/component/AnimationButton/layer/BackgroundLayer.h diff --git a/src/content/component/AnimationButton/layers/OverlayLayer.cpp b/src/content/component/AnimationButton/layer/OverlayLayer.cpp similarity index 100% rename from src/content/component/AnimationButton/layers/OverlayLayer.cpp rename to src/content/component/AnimationButton/layer/OverlayLayer.cpp diff --git a/src/content/component/AnimationButton/layers/OverlayLayer.h b/src/content/component/AnimationButton/layer/OverlayLayer.h similarity index 100% rename from src/content/component/AnimationButton/layers/OverlayLayer.h rename to src/content/component/AnimationButton/layer/OverlayLayer.h diff --git a/src/content/component/material/Button/Button.cpp b/src/content/component/material/Button/Button.cpp new file mode 100644 index 0000000..3fdf4c0 --- /dev/null +++ b/src/content/component/material/Button/Button.cpp @@ -0,0 +1,92 @@ +// +// Created by Earth_Studio on 2026/4/27. +// + +#include "Button.h" + +#include "engine/animation/Player.h" +#include "engine/eventbus/EventBus.h" +#include "engine/eventbus/internal/event/ForceRedrawEvent.hpp" + +namespace cmaterial::component::material { + Button::Button() + : hoverAnim(&stateAlpha), rippleAnim(&rippleRadius, &rippleAlpha), bgLayer(this), fgLayer(this) + { + + // 把图层挂载进组件的流水线 + addLayer(&bgLayer); + addLayer(&fgLayer); + animation::Player::reverse(&hoverAnim); + } + + // 🔮 1. 逻辑与交互中枢 + void Button::render(ImGuiIO *io) { + // 放置隐形按钮,夺取 120x40 的交互判定权 + ImGui::InvisibleButton(name.c_str(), buttonSize); + + bool isHovered = ImGui::IsItemHovered(); + bool isClicked = ImGui::IsItemClicked(); + + // 💡 状态机:处理 Hover 动画的平滑正反转 + if (isHovered != lastHoverState) { + lastHoverState = isHovered; + if (isHovered) { + animation::Player::reverse(&hoverAnim); + animation::Player::play(&hoverAnim); + } else { + animation::Player::reverse(&hoverAnim); + animation::Player::play(&hoverAnim); + } + } + + // 💡 状态机:处理 Ripple 动画的爆炸 + if (isClicked) { + rippleCenter = io->MousePos; + // 计算对角线作为最大半径,保证波纹填满按钮 + double maxR = std::sqrt(buttonSize.x * buttonSize.x + buttonSize.y * buttonSize.y); + rippleAnim.setMaxRadius(maxR); + rippleAnim.reset(); // 回归 0 + animation::Player::play(&rippleAnim); + event::EventBus::postEvent(new event::internal::ForceRedrawEvent); + } + } + + // 🔮 2. 底层排面:背景 + void Button::BgLayer::render(ImDrawList* drawList, ImVec2 startPos, ImVec2 size) { + // M3 标准 Primary 紫色:#6750A4 + ImU32 primaryColor = IM_COL32(103, 80, 164, 255); + // 高度一半即为胶囊圆角 + float rounding = size.y * 0.5f; + + drawList->AddRectFilled(startPos, ImVec2(startPos.x + size.x, startPos.y + size.y), primaryColor, rounding); + } + + // 🔮 3. 顶层排面:状态覆盖、波纹与文字 + void Button::FgLayer::render(ImDrawList* drawList, ImVec2 startPos, ImVec2 size) { + float rounding = size.y * 0.5f; + ImVec2 endPos = ImVec2(startPos.x + size.x, startPos.y + size.y); + + // A. 绘制 State Overlay (Hover 状态的白光叠加) + if (btn->stateAlpha > 0.001) { + ImU32 overlayColor = IM_COL32(255, 255, 255, (int)(btn->stateAlpha * 255)); + drawList->AddRectFilled(startPos, endPos, overlayColor, rounding); + } + + // B. 绘制 Ripple 波纹 + if (btn->rippleAlpha > 0.001) { + // 物理裁剪:波纹绝对不会溢出胶囊圆角按钮! + drawList->PushClipRect(startPos, endPos, true); + ImU32 rippleColor = IM_COL32(255, 255, 255, (int)(btn->rippleAlpha * 255)); + drawList->AddCircleFilled(btn->rippleCenter, (float)btn->rippleRadius, rippleColor); + drawList->PopClipRect(); + } + + // C. 绘制居中文字 + ImVec2 textSize = ImGui::CalcTextSize(btn->name.c_str()); + ImVec2 textPos = ImVec2( + startPos.x + (size.x - textSize.x) * 0.5f, + startPos.y + (size.y - textSize.y) * 0.5f + ); + drawList->AddText(textPos, IM_COL32(255, 255, 255, 255), btn->name.c_str()); + } +} // namespace cmaterial::component::material diff --git a/src/content/component/material/Button/Button.h b/src/content/component/material/Button/Button.h new file mode 100644 index 0000000..e4ca4b4 --- /dev/null +++ b/src/content/component/material/Button/Button.h @@ -0,0 +1,86 @@ +// +// Created by Earth_Studio on 2026/4/27. +// + +#ifndef CMATERIAL_BUTTON_H +#define CMATERIAL_BUTTON_H + + +#include "engine/component/IComponent.h" + +#include + +#include "engine/animation/IAnimation.h" + + +namespace cmaterial::component::material { + class Button : public IComponent { + public: + Button(); + ~Button() = default; + + void render(ImGuiIO *io) override; + + // 🔮 物理状态:暴露给 Layer 和 Animation + double stateAlpha = 0.0; // Hover 的 8% 叠加透明度 + double rippleRadius = 0.0; // 涟漪半径 + double rippleAlpha = 0.0; // 涟漪透明度 + ImVec2 rippleCenter = {0, 0}; + ImVec2 buttonSize = {120, 40}; // MD3 标准高度 40dp + + private: + bool lastHoverState = false; + + // ---------------------------------------------------- + // 🔮 动画定义区:利用你写的 100000.0f 高精度时间轴 + // ---------------------------------------------------- + class HoverAnimation : public animation::IAnimation { + public: + tweeny::tween alphaTween; + HoverAnimation(double* targetAlpha) { + // 200ms (20000步),透明度从 0 变到 0.08 (MD3 Hover 规范) + alphaTween = tweeny::from(0.0).to(0.08).during(20000).via(tweeny::easing::cubicOut); + bind(targetAlpha, &alphaTween); + } + } hoverAnim; + + class RippleAnimation : public animation::IAnimation { + public: + tweeny::tween radiusTween; + tweeny::tween alphaTween; + RippleAnimation(double* r, double* a) { + // 300ms (30000步),半径变大,透明度先深后浅 + radiusTween = tweeny::from(0.0).to(150.0).during(30000).via(tweeny::easing::quadraticOut); + alphaTween = tweeny::from(0.12).to(0.0).during(30000).via(tweeny::easing::linear); + bind(r, &radiusTween); + bind(a, &alphaTween); + } + // 每次点击时动态更新最大半径 + void setMaxRadius(double maxR) { + radiusTween.seek(0); // 回归起点 + radiusTween = tweeny::from(0.0).to(maxR).during(30000).via(tweeny::easing::quadraticOut); + } + } rippleAnim; + + // ---------------------------------------------------- + // 🔮 图层定义区:严守分段渲染契约 + // ---------------------------------------------------- + class BgLayer : public ILayer { + Button* btn; + public: + BgLayer(Button* b) : btn(b) { priority = -10; } // 垫底 + void render(ImDrawList* drawList, ImVec2 startPos, ImVec2 size) override; + } bgLayer; + + class FgLayer : public ILayer { + Button* btn; + public: + FgLayer(Button* b) : btn(b) { priority = 10; } // 盖在上面 + void render(ImDrawList* drawList, ImVec2 startPos, ImVec2 size) override; + } fgLayer; + }; +} + + + +#endif //CMATERIAL_BUTTON_H diff --git a/src/content/window/BasicWindow/BasicWindow.cpp b/src/content/window/BasicWindow/BasicWindow.cpp index c2e40e3..4c9236f 100644 --- a/src/content/window/BasicWindow/BasicWindow.cpp +++ b/src/content/window/BasicWindow/BasicWindow.cpp @@ -13,9 +13,7 @@ namespace cmaterial::window { void BasicWindow::postInit(ImGuiIO *io) { - addStyle(ImGuiStyleVar_FrameRounding, 10.0f); addStyle(ImGuiStyleVar_WindowBorderSize, 0.0f); - addStyle(ImGuiStyleVar_FrameBorderSize, 1.0f); } void BasicWindow::render(ImGuiIO *io) { diff --git a/src/engine/animation/IAnimation.h b/src/engine/animation/IAnimation.h index c29a336..41f3efb 100644 --- a/src/engine/animation/IAnimation.h +++ b/src/engine/animation/IAnimation.h @@ -22,7 +22,7 @@ namespace cmaterial::animation { bool isLoop = false; bool _isReverse = false; - private: + protected: void bind(double* property, tweeny::tween* tween); std::unordered_map *> animationBinders; diff --git a/src/engine/animation/Player.cpp b/src/engine/animation/Player.cpp index 8efa118..16becaa 100644 --- a/src/engine/animation/Player.cpp +++ b/src/engine/animation/Player.cpp @@ -14,6 +14,10 @@ namespace cmaterial::animation { void Player::play(IAnimation *animation) { if (!animation) return; + if (std::find(playingAnimations.begin(), playingAnimations.end(), animation) != playingAnimations.end()) { + return; + } + playingAnimations.push_back(animation); } @@ -49,9 +53,16 @@ namespace cmaterial::animation { for (auto binder : *animation->getAnimationBinders()) { *binder.first = binder.second->step(dtTime); - if (!binder.second->isFinished()) { - allTweensFinished = false; + if (!animation->_isReverse) { + if (!binder.second->isFinished()) { + allTweensFinished = false; + } + } else { + if (binder.second->progress() != 0) { + allTweensFinished = false; + } } + } if (allTweensFinished) { diff --git a/src/engine/component/IComponent.cpp b/src/engine/component/IComponent.cpp index cb29a4d..8d44995 100644 --- a/src/engine/component/IComponent.cpp +++ b/src/engine/component/IComponent.cpp @@ -63,8 +63,6 @@ namespace cmaterial::component { ImDrawListSplitter splitter; splitter.Split(drawList, 3); - ImVec2 startPos = ImGui::GetCursorScreenPos(); - splitter.SetCurrentChannel(drawList, 1); ImGui::BeginGroup(); diff --git a/src/engine/component/ILayer.cpp b/src/engine/component/ILayer.cpp index be70242..f5733d5 100644 --- a/src/engine/component/ILayer.cpp +++ b/src/engine/component/ILayer.cpp @@ -4,6 +4,10 @@ #include "ILayer.h" -namespace cmaterial::component { +#include "IComponent.h" -} \ No newline at end of file +namespace cmaterial::component { + void ILayer::setParent(IComponent *parent) { + this->parent = parent; + } +} diff --git a/src/engine/component/ILayer.h b/src/engine/component/ILayer.h index 79a85fa..8eb0387 100644 --- a/src/engine/component/ILayer.h +++ b/src/engine/component/ILayer.h @@ -10,13 +10,20 @@ namespace cmaterial::component { + class IComponent; + class ILayer { public: + ILayer() = default; virtual ~ILayer() = default; + virtual void setParent(IComponent *parent); virtual void render(ImDrawList* drawList, ImVec2 startPos, ImVec2 size) = 0; int priority = 0; + + protected: + IComponent* parent = nullptr; }; } diff --git a/src/main.cpp b/src/main.cpp index 8bfe69f..d5e8ba8 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -2,11 +2,12 @@ // Created by Earth_Studio on 2026/4/12. // -#include "content/component/AnimationButton/AnimationButton.h" +#include "content/component/AnimationButton/MultiLayerButton.h" #include "engine/framework/Framework.h" #include "content/component/BasicButton/BasicButton.h" #include "content/component/CreatorButton/CreatorButton.h" +#include "content/component/material/Button/Button.h" #include "content/logic/listener/WindowLogic.h" #include "content/window/BasicWindow/BasicWindow.h" @@ -19,8 +20,8 @@ int main() { cmaterial::event::EventBus::addListener(new cmaterial::event::WindowLogic(&app)); // Create your windows: - cmaterial::window::BasicWindow* window1 = new cmaterial::window::BasicWindow("window1", 300, 100); - cmaterial::window::BasicWindow* window2 = new cmaterial::window::BasicWindow("window2", 300, 100); + cmaterial::window::BasicWindow* window1 = new cmaterial::window::BasicWindow("Milestone Single Test", 300, 100); + cmaterial::window::BasicWindow* window2 = new cmaterial::window::BasicWindow("Hybrid Final Test", 300, 100); // Create your components: cmaterial::component::BasicButton* button1 = new cmaterial::component::BasicButton(); @@ -28,16 +29,19 @@ int main() { cmaterial::component::CreatorButton* button2 = new cmaterial::component::CreatorButton(); button2->name = "buttonInWindow2"; - cmaterial::component::AnimationButton* md3Button = new cmaterial::component::AnimationButton(); + cmaterial::component::MultiLayerButton* multiLayerButton = new cmaterial::component::MultiLayerButton(); + multiLayerButton->name = "MultiLayer"; + cmaterial::component::material::Button* md3Button = new cmaterial::component::material::Button(); md3Button->name = "MD3 Button"; // To inject components in an exist window: app.addWindow(window1); window1->addComponent(button1); - window1->addComponent(md3Button); + window1->addComponent(multiLayerButton); app.addWindow(window2); window2->addComponent(button2); + window2->addComponent(md3Button); // Finally, run your app by doing this: app.run(); From e312acde83a2e6de42005bee75265bde1b98a67e Mon Sep 17 00:00:00 2001 From: Earth_Studio Date: Fri, 1 May 2026 21:14:43 +0800 Subject: [PATCH 12/34] fix build issue on linux/macos --- src/engine/eventbus/internal/listener/ForceRedrawListener.hpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/engine/eventbus/internal/listener/ForceRedrawListener.hpp b/src/engine/eventbus/internal/listener/ForceRedrawListener.hpp index 018b4a4..67f23ee 100644 --- a/src/engine/eventbus/internal/listener/ForceRedrawListener.hpp +++ b/src/engine/eventbus/internal/listener/ForceRedrawListener.hpp @@ -10,7 +10,7 @@ #include "engine/eventbus/internal/event/ForceRedrawEvent.hpp" #include "glad/gl.h" -#include "glfw/glfw3.h" +#include "GLFW/glfw3.h" namespace cmaterial::event::internal { class ForceRedrawListener : public IListener { From 4e20248de534b7bdfa3f005f71df38ada045f77a Mon Sep 17 00:00:00 2001 From: GADDQ Date: Fri, 1 May 2026 23:31:54 +0800 Subject: [PATCH 13/34] fix render issue on linux/macos --- src/engine/component/IComponent.cpp | 2 + src/engine/framework/Framework.cpp | 2 +- src/engine/framework/Framework.h | 3 +- src/engine/utils/ordered_map.hpp | 105 ++++++++++++++++++++++++++++ src/engine/window/IWindow.h | 3 +- 5 files changed, 112 insertions(+), 3 deletions(-) create mode 100644 src/engine/utils/ordered_map.hpp diff --git a/src/engine/component/IComponent.cpp b/src/engine/component/IComponent.cpp index 8d44995..8ed6ae8 100644 --- a/src/engine/component/IComponent.cpp +++ b/src/engine/component/IComponent.cpp @@ -12,6 +12,8 @@ #include #include +#include +#include "engine/utils/ordered_map.hpp" namespace cmaterial::component { void IComponent::addComponent(IComponent *component) { diff --git a/src/engine/framework/Framework.cpp b/src/engine/framework/Framework.cpp index c1d33bf..8ac0038 100644 --- a/src/engine/framework/Framework.cpp +++ b/src/engine/framework/Framework.cpp @@ -16,7 +16,7 @@ #include "engine/eventbus/EventBus.h" #include "engine/eventbus/internal/event/ForceRedrawEvent.hpp" -#include +#include "engine/utils/ordered_map.hpp" #include #include "engine/animation/Player.h" diff --git a/src/engine/framework/Framework.h b/src/engine/framework/Framework.h index 8c6c708..fe63b0a 100644 --- a/src/engine/framework/Framework.h +++ b/src/engine/framework/Framework.h @@ -8,6 +8,7 @@ #include #include "GLFW/glfw3.h" +#include "engine/utils/ordered_map.hpp" #include "engine/window/IWindow.h" @@ -38,7 +39,7 @@ namespace cmaterial { GLFWwindow* hiddenWindow = nullptr; ImGuiContext* hiddenImgui = nullptr; ImFontAtlas* fontAtlas = nullptr; - std::unordered_map windows; + utils::ordered_map windows; std::vector deadWindows; }; } diff --git a/src/engine/utils/ordered_map.hpp b/src/engine/utils/ordered_map.hpp new file mode 100644 index 0000000..b0b1725 --- /dev/null +++ b/src/engine/utils/ordered_map.hpp @@ -0,0 +1,105 @@ +// +// Created by Earth_Studio on 2026/5/1. +// + +#ifndef CMATERIAL_ORDERED_MAP_H +#define CMATERIAL_ORDERED_MAP_H + + + +#include +#include +#include +#include + +namespace cmaterial::utils { + template + class ordered_map { + public: + // 为了支持 for(auto& [k, v] : map),必须定义这个内部类型 + using ValueType = std::pair; + using ListIter = typename std::list::iterator; + + // 1. 插入:保持顺序,$O(1)$ + std::pair insert(const K& key, V value) { + auto it = lookup.find(key); + if (it != lookup.end()) { + return {it->second, false}; // 已存在,不覆盖 + } + + // 插入链表末尾,保证遍历顺序 + data.emplace_back(key, value); + ListIter lastIt = std::prev(data.end()); + + // 存入哈希表实现 O(1) 索引 + lookup[key] = lastIt; + return {lastIt, true}; + } + + // 🔮 新增:接受 std::pair 的重载 + std::pair insert(const ValueType& value) { + return insert(value.first, value.second); + } + + // 🔮 进阶:接受右值引用的重载(支持 move,更体面) + std::pair insert(ValueType&& value) { + auto it = lookup.find(value.first); + if (it != lookup.end()) return {it->second, false}; + + data.emplace_back(std::move(value)); + ListIter lastIt = std::prev(data.end()); + lookup[lastIt->first] = lastIt; + return {lastIt, true}; + } + + // 2. 下标操作:最顺手的语法 + V& operator[](const K& key) { + auto it = lookup.find(key); + if (it == lookup.end()) { + return insert(key, V{}).first->second; + } + return it->second->second; + } + + // 3. 删除:真正的 $O(1)$ 且维持链表完整性 + size_t erase(const K& key) { + auto it = lookup.find(key); + if (it == lookup.end()) return 0; + + data.erase(it->second); // $O(1)$ 从链表移除 + lookup.erase(it); // $O(1)$ 从哈希表移除 + return 1; + } + + // 4. 迭代器:让 for(auto& [k, v] : map) 绝对有序 + auto begin() noexcept { return data.begin(); } + auto end() noexcept { return data.end(); } + auto begin() const noexcept { return data.begin(); } + auto end() const noexcept { return data.end(); } + + // 5. C++20 风格接口 + bool contains(const K& key) const { return lookup.contains(key); } + bool empty() const noexcept { return lookup.empty(); } + size_t size() const noexcept { return lookup.size(); } + + void clear() noexcept { + lookup.clear(); + data.clear(); + } + + auto find(const K& key) { + auto it = lookup.find(key); + return (it == lookup.end()) ? data.end() : it->second; + } + + private: + // 物理存储:链表负责“序” + std::list data; + // 逻辑索引:哈希表负责“速” + std::unordered_map lookup; + }; +} + + + +#endif //CMATERIAL_ORDERED_MAP_H diff --git a/src/engine/window/IWindow.h b/src/engine/window/IWindow.h index 0f28a25..f8f5b46 100644 --- a/src/engine/window/IWindow.h +++ b/src/engine/window/IWindow.h @@ -13,6 +13,7 @@ #include "GLFW/glfw3.h" #include "engine/component/IComponent.h" +#include "engine/utils/ordered_map.hpp" #include "imgui.h" @@ -52,7 +53,7 @@ namespace cmaterial::window { ImGuiIO* io = nullptr; ImFontAtlas* fontAtlas = nullptr; - std::unordered_map components; + utils::ordered_map components; std::vector deadComponents; std::unordered_map styles; }; From 8c0dd4aad378a9d0784f2c07e944c20cfd37e4b5 Mon Sep 17 00:00:00 2001 From: Earth_Studio Date: Wed, 6 May 2026 20:42:26 +0800 Subject: [PATCH 14/34] fix animation stop playing when mouse didn't hover the window --- .../AnimationButton/layer/BackgroundLayer.h | 2 +- .../AnimationButton/layer/OverlayLayer.h | 2 +- .../component/material/Button/Button.cpp | 4 +++ src/engine/animation/IAnimation.h | 3 +- src/engine/animation/Player.cpp | 7 ++--- src/engine/animation/Player.h | 2 +- src/engine/component/IComponent.cpp | 13 ++++++-- src/engine/component/IComponent.h | 6 ++-- src/engine/component/ILayer.cpp | 13 -------- src/engine/component/{ILayer.h => ILayer.hpp} | 8 ++--- src/engine/framework/Framework.cpp | 31 ++++++++++++++----- src/engine/utils/INode.h | 19 ++++++++++++ src/engine/window/IWindow.cpp | 9 ++++-- src/engine/window/IWindow.h | 3 +- 14 files changed, 80 insertions(+), 42 deletions(-) delete mode 100644 src/engine/component/ILayer.cpp rename src/engine/component/{ILayer.h => ILayer.hpp} (76%) create mode 100644 src/engine/utils/INode.h diff --git a/src/content/component/AnimationButton/layer/BackgroundLayer.h b/src/content/component/AnimationButton/layer/BackgroundLayer.h index 6ac3887..4ae09c7 100644 --- a/src/content/component/AnimationButton/layer/BackgroundLayer.h +++ b/src/content/component/AnimationButton/layer/BackgroundLayer.h @@ -4,7 +4,7 @@ #ifndef CMATERIAL_BACKGROUNDLAYER_H #define CMATERIAL_BACKGROUNDLAYER_H -#include "engine/component/ILayer.h" +#include "engine/component/ILayer.hpp" namespace cmaterial::component::layer::animationbutton { diff --git a/src/content/component/AnimationButton/layer/OverlayLayer.h b/src/content/component/AnimationButton/layer/OverlayLayer.h index cc1fb01..4c325df 100644 --- a/src/content/component/AnimationButton/layer/OverlayLayer.h +++ b/src/content/component/AnimationButton/layer/OverlayLayer.h @@ -4,7 +4,7 @@ #ifndef CMATERIAL_OVERLAYLAYER_H #define CMATERIAL_OVERLAYLAYER_H -#include "engine/component/ILayer.h" +#include "engine/component/ILayer.hpp" namespace cmaterial::component::layer::animationbutton { diff --git a/src/content/component/material/Button/Button.cpp b/src/content/component/material/Button/Button.cpp index 3fdf4c0..110e25e 100644 --- a/src/content/component/material/Button/Button.cpp +++ b/src/content/component/material/Button/Button.cpp @@ -14,6 +14,10 @@ namespace cmaterial::component::material { { // 把图层挂载进组件的流水线 + bgLayer.parent = this; + fgLayer.parent = this; + hoverAnim.parent = this; + rippleAnim.parent = this; addLayer(&bgLayer); addLayer(&fgLayer); animation::Player::reverse(&hoverAnim); diff --git a/src/engine/animation/IAnimation.h b/src/engine/animation/IAnimation.h index 41f3efb..a160627 100644 --- a/src/engine/animation/IAnimation.h +++ b/src/engine/animation/IAnimation.h @@ -8,11 +8,12 @@ #include +#include "engine/utils/INode.h" #include "tween.h" namespace cmaterial::animation { - class IAnimation { + class IAnimation : public camterial::utils::INode { public: virtual ~IAnimation(); diff --git a/src/engine/animation/Player.cpp b/src/engine/animation/Player.cpp index 16becaa..cfa49a8 100644 --- a/src/engine/animation/Player.cpp +++ b/src/engine/animation/Player.cpp @@ -41,10 +41,7 @@ namespace cmaterial::animation { std::erase(playingAnimations, animation); } - bool Player::update() { - if (playingAnimations.empty()) - return false; - + std::vector* Player::update() { int dtTime = static_cast(ImGui::GetIO().DeltaTime * 100000.0f); if (dtTime <= 0) dtTime = 1; // 1 step = 0.01 ms @@ -79,7 +76,7 @@ namespace cmaterial::animation { finishedAnimations.clear(); - return true; + return &playingAnimations; } void Player::shutdown() { diff --git a/src/engine/animation/Player.h b/src/engine/animation/Player.h index 74ce66f..79b51fe 100644 --- a/src/engine/animation/Player.h +++ b/src/engine/animation/Player.h @@ -21,7 +21,7 @@ namespace cmaterial::animation { static void reverse(IAnimation* animation); static void stop(IAnimation* animation); - static bool update(); + static std::vector* update(); static void shutdown(); diff --git a/src/engine/component/IComponent.cpp b/src/engine/component/IComponent.cpp index 8ed6ae8..426a06e 100644 --- a/src/engine/component/IComponent.cpp +++ b/src/engine/component/IComponent.cpp @@ -8,11 +8,11 @@ #include "engine/eventbus/EventBus.h" #include "engine/eventbus/internal/event/ForceRedrawEvent.hpp" -#include "ILayer.h" +#include "ILayer.hpp" -#include #include #include +#include #include "engine/utils/ordered_map.hpp" namespace cmaterial::component { @@ -23,6 +23,7 @@ namespace cmaterial::component { if (component->name.empty()) return; + component->parent = this; components.push_back(component); event::EventBus::postEvent(new event::internal::ForceRedrawEvent); } @@ -95,7 +96,15 @@ namespace cmaterial::component { IComponent::~IComponent() { components.clear(); + + for (auto layerB : layersBefore) { + delete layerB; + } layersBefore.clear(); + + for (auto layerA : layersAfter) { + delete layerA; + } layersAfter.clear(); } } diff --git a/src/engine/component/IComponent.h b/src/engine/component/IComponent.h index f5c5766..54994eb 100644 --- a/src/engine/component/IComponent.h +++ b/src/engine/component/IComponent.h @@ -12,13 +12,15 @@ #include #include -#include "ILayer.h" +#include "ILayer.hpp" +#include "engine/utils/INode.h" namespace cmaterial::component { - class IComponent { + class IComponent : public camterial::utils::INode { public: virtual ~IComponent(); + virtual void render(ImGuiIO *io) = 0; virtual void drawComponent(ImGuiIO *io); diff --git a/src/engine/component/ILayer.cpp b/src/engine/component/ILayer.cpp deleted file mode 100644 index f5733d5..0000000 --- a/src/engine/component/ILayer.cpp +++ /dev/null @@ -1,13 +0,0 @@ -// -// Created by Earth_Studio on 2026/4/26. -// - -#include "ILayer.h" - -#include "IComponent.h" - -namespace cmaterial::component { - void ILayer::setParent(IComponent *parent) { - this->parent = parent; - } -} diff --git a/src/engine/component/ILayer.h b/src/engine/component/ILayer.hpp similarity index 76% rename from src/engine/component/ILayer.h rename to src/engine/component/ILayer.hpp index 8eb0387..3c26613 100644 --- a/src/engine/component/ILayer.h +++ b/src/engine/component/ILayer.hpp @@ -6,24 +6,20 @@ #define CMATERIAL_ILAYER_H +#include "engine/utils/INode.h" #include "imgui.h" namespace cmaterial::component { class IComponent; - class ILayer { + class ILayer : public camterial::utils::INode { public: ILayer() = default; virtual ~ILayer() = default; - virtual void setParent(IComponent *parent); - virtual void render(ImDrawList* drawList, ImVec2 startPos, ImVec2 size) = 0; int priority = 0; - - protected: - IComponent* parent = nullptr; }; } diff --git a/src/engine/framework/Framework.cpp b/src/engine/framework/Framework.cpp index 8ac0038..7c25c6d 100644 --- a/src/engine/framework/Framework.cpp +++ b/src/engine/framework/Framework.cpp @@ -66,7 +66,9 @@ namespace cmaterial { EventBus::postEvent(new event::internal::ForceRedrawEvent); while (!glfwWindowShouldClose(hiddenWindow) && !windows.empty()) { bool isEventBusy = EventBus::dispatch(); - bool isAnimationBusy = animation::Player::update(); + + auto playingAnimations = animation::Player::update(); + bool isAnimationBusy = !playingAnimations->empty(); if (!(isEventBusy || isAnimationBusy)) glfwWaitEvents(); @@ -75,6 +77,7 @@ namespace cmaterial { for (std::pair &pair : windows) { bool isSizeChange = pair.second->_isSizeChange; + bool isWindowBusy = false; pair.second->update(); if (pair.second->isDead) { @@ -85,15 +88,24 @@ namespace cmaterial { if (!isSizeChange && !pair.second->isHovered()) { if (!pair.second->isInitialized) pair.second->isInitialized = true; - else continue; } - pair.second->drawWindow(!(isForceRedraw || isAnimationBusy)); - } + if (isAnimationBusy) { + for (auto animation : *playingAnimations) { + auto node = animation->parent; + while (node->parent != nullptr) { + node = node->parent; + } + if (node == pair.second) { + isWindowBusy = true; + goto render; + } + } + continue; + } - if (!deadWindows.empty()) { - glfwMakeContextCurrent(hiddenWindow); - ImGui::SetCurrentContext(hiddenImgui); + render: + pair.second->drawWindow(!(isForceRedraw || isWindowBusy)); } for (std::string &name : deadWindows) { @@ -102,6 +114,11 @@ namespace cmaterial { delete win; } + if (!deadWindows.empty()) { + glfwMakeContextCurrent(hiddenWindow); + ImGui::SetCurrentContext(hiddenImgui); + } + deadWindows.clear(); isForceRedraw = false; } diff --git a/src/engine/utils/INode.h b/src/engine/utils/INode.h new file mode 100644 index 0000000..831ef48 --- /dev/null +++ b/src/engine/utils/INode.h @@ -0,0 +1,19 @@ +// +// Created by Earth_Studio on 2026/5/6. +// + +#ifndef CMATERIAL_INODE_H +#define CMATERIAL_INODE_H + + +namespace camterial::utils { + class INode{ + public: + virtual ~INode() = default; + INode * parent = nullptr; + }; +} + + + +#endif //CMATERIAL_INODE_H diff --git a/src/engine/window/IWindow.cpp b/src/engine/window/IWindow.cpp index fd98c9a..7439d34 100644 --- a/src/engine/window/IWindow.cpp +++ b/src/engine/window/IWindow.cpp @@ -144,7 +144,6 @@ namespace cmaterial::window { ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData()); glfwSwapBuffers(glfwWindow); } - // This is the most genius code I've ever wrote!!! // We all know ImGui is Immediately Mode, so its logic and render is force bind, you cannot trigger logic or render independent. // HOWEVER, to be portable across different platforms, ImGui split itself into two parts: the core **library** and the **backend**. // In other words, the ImGui core is responsible for calculating vertex data, which it then submits to the backend for rendering. @@ -157,8 +156,14 @@ namespace cmaterial::window { } void IWindow::addComponent(component::IComponent* comp) { - if (comp && !comp->name.empty()) + if (comp && !comp->name.empty()) { components[comp->name] = comp; + comp->parent = this; + } + } + + void IWindow::removeComponent(component::IComponent *comp) { + components.erase(comp->name); } bool IWindow::isHovered() const { diff --git a/src/engine/window/IWindow.h b/src/engine/window/IWindow.h index f8f5b46..33b8d86 100644 --- a/src/engine/window/IWindow.h +++ b/src/engine/window/IWindow.h @@ -18,7 +18,7 @@ namespace cmaterial::window { - class IWindow { + class IWindow : public camterial::utils::INode { public: IWindow(const std::string &name, int width, int height); virtual ~IWindow(); @@ -34,6 +34,7 @@ namespace cmaterial::window { void addComponent(component::IComponent *comp); void addStyle(ImGuiStyleVar styleVar, float value); void removeStyle(ImGuiStyleVar styleVar); + void removeComponent(component::IComponent *comp); std::string name; int width, height; From 527dd1b8fe40b741029faa75fb25abbbf558f5d0 Mon Sep 17 00:00:00 2001 From: Earth_Studio Date: Wed, 6 May 2026 21:59:26 +0800 Subject: [PATCH 15/34] fix namespace issue --- src/content/component/material/Button/Button.cpp | 7 ++++--- src/engine/component/IComponent.h | 2 +- src/engine/component/ILayer.hpp | 2 +- src/engine/utils/INode.h | 2 +- src/engine/window/IWindow.cpp | 2 -- src/engine/window/IWindow.h | 2 +- 6 files changed, 8 insertions(+), 9 deletions(-) diff --git a/src/content/component/material/Button/Button.cpp b/src/content/component/material/Button/Button.cpp index 110e25e..fa0e664 100644 --- a/src/content/component/material/Button/Button.cpp +++ b/src/content/component/material/Button/Button.cpp @@ -4,6 +4,8 @@ #include "Button.h" +#include + #include "engine/animation/Player.h" #include "engine/eventbus/EventBus.h" #include "engine/eventbus/internal/event/ForceRedrawEvent.hpp" @@ -29,7 +31,6 @@ namespace cmaterial::component::material { ImGui::InvisibleButton(name.c_str(), buttonSize); bool isHovered = ImGui::IsItemHovered(); - bool isClicked = ImGui::IsItemClicked(); // 💡 状态机:处理 Hover 动画的平滑正反转 if (isHovered != lastHoverState) { @@ -44,14 +45,14 @@ namespace cmaterial::component::material { } // 💡 状态机:处理 Ripple 动画的爆炸 - if (isClicked) { + if (ImGui::IsItemClicked()) { + std::cout << "clicked" << std::endl; rippleCenter = io->MousePos; // 计算对角线作为最大半径,保证波纹填满按钮 double maxR = std::sqrt(buttonSize.x * buttonSize.x + buttonSize.y * buttonSize.y); rippleAnim.setMaxRadius(maxR); rippleAnim.reset(); // 回归 0 animation::Player::play(&rippleAnim); - event::EventBus::postEvent(new event::internal::ForceRedrawEvent); } } diff --git a/src/engine/component/IComponent.h b/src/engine/component/IComponent.h index 54994eb..cc17615 100644 --- a/src/engine/component/IComponent.h +++ b/src/engine/component/IComponent.h @@ -17,7 +17,7 @@ namespace cmaterial::component { - class IComponent : public camterial::utils::INode { + class IComponent : public cmaterial::utils::INode { public: virtual ~IComponent(); diff --git a/src/engine/component/ILayer.hpp b/src/engine/component/ILayer.hpp index 3c26613..1d99d09 100644 --- a/src/engine/component/ILayer.hpp +++ b/src/engine/component/ILayer.hpp @@ -13,7 +13,7 @@ namespace cmaterial::component { class IComponent; - class ILayer : public camterial::utils::INode { + class ILayer : public cmaterial::utils::INode { public: ILayer() = default; virtual ~ILayer() = default; diff --git a/src/engine/utils/INode.h b/src/engine/utils/INode.h index 831ef48..09e0904 100644 --- a/src/engine/utils/INode.h +++ b/src/engine/utils/INode.h @@ -6,7 +6,7 @@ #define CMATERIAL_INODE_H -namespace camterial::utils { +namespace cmaterial::utils { class INode{ public: virtual ~INode() = default; diff --git a/src/engine/window/IWindow.cpp b/src/engine/window/IWindow.cpp index 7439d34..0afba6a 100644 --- a/src/engine/window/IWindow.cpp +++ b/src/engine/window/IWindow.cpp @@ -11,8 +11,6 @@ #include "GLFW/glfw3.h" #include "engine/eventbus/EventBus.h" -#include "engine/eventbus/internal/event/ForceRedrawEvent.hpp" -#include "imgui_internal.h" namespace cmaterial::window { void window_size_callback(GLFWwindow* window, int width, int height) { diff --git a/src/engine/window/IWindow.h b/src/engine/window/IWindow.h index 33b8d86..2b5fc3b 100644 --- a/src/engine/window/IWindow.h +++ b/src/engine/window/IWindow.h @@ -18,7 +18,7 @@ namespace cmaterial::window { - class IWindow : public camterial::utils::INode { + class IWindow : public cmaterial::utils::INode { public: IWindow(const std::string &name, int width, int height); virtual ~IWindow(); From 30d76635b5d636a27816f151e29af750c38cf8fd Mon Sep 17 00:00:00 2001 From: Earth_Studio Date: Wed, 6 May 2026 22:00:35 +0800 Subject: [PATCH 16/34] fix a possible segmentation fault --- src/engine/animation/IAnimation.cpp | 2 ++ src/engine/animation/IAnimation.h | 2 +- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/src/engine/animation/IAnimation.cpp b/src/engine/animation/IAnimation.cpp index 4c26009..d25112a 100644 --- a/src/engine/animation/IAnimation.cpp +++ b/src/engine/animation/IAnimation.cpp @@ -6,6 +6,7 @@ #include +#include "Player.h" #include "tween.h" namespace cmaterial::animation { @@ -28,5 +29,6 @@ namespace cmaterial::animation { IAnimation::~IAnimation() { animationBinders.clear(); + Player::stop(this); } } \ No newline at end of file diff --git a/src/engine/animation/IAnimation.h b/src/engine/animation/IAnimation.h index a160627..5c51a72 100644 --- a/src/engine/animation/IAnimation.h +++ b/src/engine/animation/IAnimation.h @@ -13,7 +13,7 @@ namespace cmaterial::animation { - class IAnimation : public camterial::utils::INode { + class IAnimation : public cmaterial::utils::INode { public: virtual ~IAnimation(); From aaa6c8cd145eae16955d8571f108d711e5563dd6 Mon Sep 17 00:00:00 2001 From: Earth_Studio Date: Wed, 6 May 2026 22:04:36 +0800 Subject: [PATCH 17/34] fix incorrect parent relationship when removeComponent --- src/engine/component/IComponent.cpp | 1 + src/engine/window/IWindow.cpp | 3 ++- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/src/engine/component/IComponent.cpp b/src/engine/component/IComponent.cpp index 426a06e..79e87fd 100644 --- a/src/engine/component/IComponent.cpp +++ b/src/engine/component/IComponent.cpp @@ -32,6 +32,7 @@ namespace cmaterial::component { if (component == nullptr) return; + component->parent = nullptr; std::erase(components, component); } diff --git a/src/engine/window/IWindow.cpp b/src/engine/window/IWindow.cpp index 0afba6a..8f84924 100644 --- a/src/engine/window/IWindow.cpp +++ b/src/engine/window/IWindow.cpp @@ -155,12 +155,13 @@ namespace cmaterial::window { void IWindow::addComponent(component::IComponent* comp) { if (comp && !comp->name.empty()) { - components[comp->name] = comp; comp->parent = this; + components[comp->name] = comp; } } void IWindow::removeComponent(component::IComponent *comp) { + comp->parent = nullptr; components.erase(comp->name); } From e06332ce0cdaed7e3325a687114f48fa3594fede Mon Sep 17 00:00:00 2001 From: Earth_Studio Date: Wed, 6 May 2026 22:20:14 +0800 Subject: [PATCH 18/34] add const keyword --- src/engine/eventbus/IEvent.cpp | 2 +- src/engine/eventbus/IEvent.h | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/engine/eventbus/IEvent.cpp b/src/engine/eventbus/IEvent.cpp index 965d172..a561904 100644 --- a/src/engine/eventbus/IEvent.cpp +++ b/src/engine/eventbus/IEvent.cpp @@ -5,7 +5,7 @@ #include "IEvent.h" namespace cmaterial::event { - bool IEvent::getIsCancelled() { + bool IEvent::getIsCancelled() const { return isCancelled; } } diff --git a/src/engine/eventbus/IEvent.h b/src/engine/eventbus/IEvent.h index 31088f6..0fafc5d 100644 --- a/src/engine/eventbus/IEvent.h +++ b/src/engine/eventbus/IEvent.h @@ -12,7 +12,7 @@ namespace cmaterial::event { virtual ~IEvent() = default; virtual const void* getEventType() const = 0; - bool getIsCancelled(); + bool getIsCancelled() const; protected: bool isCancelled = false; From da25c2fc8a025b566a23128479a310c74cf56c51 Mon Sep 17 00:00:00 2001 From: Earth_Studio Date: Thu, 7 May 2026 00:00:00 +0800 Subject: [PATCH 19/34] why cout will cause issue? --- src/content/component/material/Button/Button.cpp | 4 ---- 1 file changed, 4 deletions(-) diff --git a/src/content/component/material/Button/Button.cpp b/src/content/component/material/Button/Button.cpp index fa0e664..8031b38 100644 --- a/src/content/component/material/Button/Button.cpp +++ b/src/content/component/material/Button/Button.cpp @@ -4,11 +4,8 @@ #include "Button.h" -#include - #include "engine/animation/Player.h" #include "engine/eventbus/EventBus.h" -#include "engine/eventbus/internal/event/ForceRedrawEvent.hpp" namespace cmaterial::component::material { Button::Button() @@ -46,7 +43,6 @@ namespace cmaterial::component::material { // 💡 状态机:处理 Ripple 动画的爆炸 if (ImGui::IsItemClicked()) { - std::cout << "clicked" << std::endl; rippleCenter = io->MousePos; // 计算对角线作为最大半径,保证波纹填满按钮 double maxR = std::sqrt(buttonSize.x * buttonSize.x + buttonSize.y * buttonSize.y); From 9c558c9f4b4e993e2a43616a640aefd8b00f9b48 Mon Sep 17 00:00:00 2001 From: Earth_Studio Date: Thu, 7 May 2026 10:27:06 +0800 Subject: [PATCH 20/34] remove unused headers --- src/content/window/BasicWindow/BasicWindow.cpp | 5 ----- src/engine/component/IComponent.cpp | 2 -- 2 files changed, 7 deletions(-) diff --git a/src/content/window/BasicWindow/BasicWindow.cpp b/src/content/window/BasicWindow/BasicWindow.cpp index 4c9236f..1468f7f 100644 --- a/src/content/window/BasicWindow/BasicWindow.cpp +++ b/src/content/window/BasicWindow/BasicWindow.cpp @@ -6,11 +6,6 @@ #include "imgui.h" -#include -#include - -#include "engine/eventbus/EventBus.h" - namespace cmaterial::window { void BasicWindow::postInit(ImGuiIO *io) { addStyle(ImGuiStyleVar_WindowBorderSize, 0.0f); diff --git a/src/engine/component/IComponent.cpp b/src/engine/component/IComponent.cpp index 79e87fd..05a3c1e 100644 --- a/src/engine/component/IComponent.cpp +++ b/src/engine/component/IComponent.cpp @@ -11,9 +11,7 @@ #include "ILayer.hpp" #include -#include #include -#include "engine/utils/ordered_map.hpp" namespace cmaterial::component { void IComponent::addComponent(IComponent *component) { From 81b9381f182f389b25625027cb9d01c16bb0eac0 Mon Sep 17 00:00:00 2001 From: Earth_Studio Date: Thu, 7 May 2026 10:27:51 +0800 Subject: [PATCH 21/34] replace imgui timer to glfw timer --- src/engine/animation/Player.cpp | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/src/engine/animation/Player.cpp b/src/engine/animation/Player.cpp index cfa49a8..7eaa3cd 100644 --- a/src/engine/animation/Player.cpp +++ b/src/engine/animation/Player.cpp @@ -7,6 +7,9 @@ #include #include +#include "glad/gl.h" +#include "GLFW/glfw3.h" + namespace cmaterial::animation { std::vector Player::playingAnimations; std::vector Player::finishedAnimations; @@ -42,8 +45,12 @@ namespace cmaterial::animation { } std::vector* Player::update() { - int dtTime = static_cast(ImGui::GetIO().DeltaTime * 100000.0f); - if (dtTime <= 0) dtTime = 1; // 1 step = 0.01 ms + static double lastTime = glfwGetTime(); + double currentTime = glfwGetTime(); + double dt = currentTime - lastTime; + lastTime = currentTime; + if (dt <= 0.0) dt = 0.00001; + int dtTime = static_cast(dt * 100000.0); // 1 step = 0.01 ms for (auto animation : playingAnimations) { bool allTweensFinished = true; From 58d4d479c148c469b703ba908490dfec5343e3a7 Mon Sep 17 00:00:00 2001 From: Earth_Studio Date: Thu, 7 May 2026 10:50:52 +0800 Subject: [PATCH 22/34] fix high cpu/gpu usage --- src/engine/framework/Framework.cpp | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/src/engine/framework/Framework.cpp b/src/engine/framework/Framework.cpp index 7c25c6d..a509c10 100644 --- a/src/engine/framework/Framework.cpp +++ b/src/engine/framework/Framework.cpp @@ -18,6 +18,8 @@ #include "engine/utils/ordered_map.hpp" #include +#include +#include #include "engine/animation/Player.h" #include "engine/eventbus/internal/listener/ForceRedrawListener.hpp" @@ -63,6 +65,13 @@ namespace cmaterial { if (!isInitialized) return NOT_INIT; + GLFWmonitor* primary = glfwGetPrimaryMonitor(); + const GLFWvidmode* mode = glfwGetVideoMode(primary); + double targetHz = (mode && mode->refreshRate > 0) ? mode->refreshRate : 60.0; + double frameDuration = 1.0 / targetHz; + + double nextFrameTime = glfwGetTime(); + EventBus::postEvent(new event::internal::ForceRedrawEvent); while (!glfwWindowShouldClose(hiddenWindow) && !windows.empty()) { bool isEventBusy = EventBus::dispatch(); @@ -70,8 +79,21 @@ namespace cmaterial { auto playingAnimations = animation::Player::update(); bool isAnimationBusy = !playingAnimations->empty(); - if (!(isEventBusy || isAnimationBusy)) + if (!(isEventBusy || isAnimationBusy)) { + double now = glfwGetTime(); + + if (now < nextFrameTime) { + std::this_thread::sleep_for(std::chrono::microseconds(100)); + continue; + } + + nextFrameTime += frameDuration; + + if (nextFrameTime < now) + nextFrameTime = now + frameDuration; + glfwWaitEvents(); + } ImFontAtlasUpdateNewFrame(fontAtlas, ++globalFrameCount, true); From 9bd92cc848c6e70593d678b5dbde034ebe781081 Mon Sep 17 00:00:00 2001 From: Earth_Studio Date: Thu, 7 May 2026 13:15:00 +0800 Subject: [PATCH 23/34] change name --- .../{AnimationButton => MultiLayerButton}/MultiLayerButton.cpp | 2 +- .../{AnimationButton => MultiLayerButton}/MultiLayerButton.h | 2 +- .../layer/OverlayLayer.cpp | 0 .../{AnimationButton => MultiLayerButton}/layer/OverlayLayer.h | 0 src/main.cpp | 2 +- 5 files changed, 3 insertions(+), 3 deletions(-) rename src/content/component/{AnimationButton => MultiLayerButton}/MultiLayerButton.cpp (89%) rename src/content/component/{AnimationButton => MultiLayerButton}/MultiLayerButton.h (93%) rename src/content/component/{AnimationButton => MultiLayerButton}/layer/OverlayLayer.cpp (100%) rename src/content/component/{AnimationButton => MultiLayerButton}/layer/OverlayLayer.h (100%) diff --git a/src/content/component/AnimationButton/MultiLayerButton.cpp b/src/content/component/MultiLayerButton/MultiLayerButton.cpp similarity index 89% rename from src/content/component/AnimationButton/MultiLayerButton.cpp rename to src/content/component/MultiLayerButton/MultiLayerButton.cpp index e627ba1..d707e0a 100644 --- a/src/content/component/AnimationButton/MultiLayerButton.cpp +++ b/src/content/component/MultiLayerButton/MultiLayerButton.cpp @@ -10,7 +10,7 @@ namespace cmaterial::component { addLayer(&overlayLayer); } - void MultiLayerButton::render(ImGuiIO *io) { + void MultiLayerButton::update(ImGuiIO *io) { ImGui::PushStyleColor(ImGuiCol_Button, ImVec4(0.0f, 1.0f, 0.0f, 1.0f)); ImGui::Button(name.c_str()); ImGui::PopStyleColor(); diff --git a/src/content/component/AnimationButton/MultiLayerButton.h b/src/content/component/MultiLayerButton/MultiLayerButton.h similarity index 93% rename from src/content/component/AnimationButton/MultiLayerButton.h rename to src/content/component/MultiLayerButton/MultiLayerButton.h index 52c735e..32f3ec9 100644 --- a/src/content/component/AnimationButton/MultiLayerButton.h +++ b/src/content/component/MultiLayerButton/MultiLayerButton.h @@ -18,7 +18,7 @@ namespace cmaterial::component { MultiLayerButton(); ~MultiLayerButton() = default; - void render(ImGuiIO *io) override; + void update(ImGuiIO *io) override; private: layer::animationbutton::BackgroundLayer backgroundLayer; diff --git a/src/content/component/AnimationButton/layer/OverlayLayer.cpp b/src/content/component/MultiLayerButton/layer/OverlayLayer.cpp similarity index 100% rename from src/content/component/AnimationButton/layer/OverlayLayer.cpp rename to src/content/component/MultiLayerButton/layer/OverlayLayer.cpp diff --git a/src/content/component/AnimationButton/layer/OverlayLayer.h b/src/content/component/MultiLayerButton/layer/OverlayLayer.h similarity index 100% rename from src/content/component/AnimationButton/layer/OverlayLayer.h rename to src/content/component/MultiLayerButton/layer/OverlayLayer.h diff --git a/src/main.cpp b/src/main.cpp index d5e8ba8..f1fa051 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -2,7 +2,7 @@ // Created by Earth_Studio on 2026/4/12. // -#include "content/component/AnimationButton/MultiLayerButton.h" +#include "content/component/MultiLayerButton/MultiLayerButton.h" #include "engine/framework/Framework.h" #include "content/component/BasicButton/BasicButton.h" From 0c9b1b9b3663bf8764361a85be6a74c005f478f8 Mon Sep 17 00:00:00 2001 From: Earth_Studio Date: Thu, 7 May 2026 13:15:00 +0800 Subject: [PATCH 24/34] change name --- .../{AnimationButton => MultiLayerButton}/MultiLayerButton.cpp | 2 +- .../{AnimationButton => MultiLayerButton}/MultiLayerButton.h | 2 +- .../layer/BackgroundLayer.cpp | 0 .../layer/BackgroundLayer.h | 0 .../layer/OverlayLayer.cpp | 0 .../{AnimationButton => MultiLayerButton}/layer/OverlayLayer.h | 0 src/main.cpp | 2 +- 7 files changed, 3 insertions(+), 3 deletions(-) rename src/content/component/{AnimationButton => MultiLayerButton}/MultiLayerButton.cpp (89%) rename src/content/component/{AnimationButton => MultiLayerButton}/MultiLayerButton.h (93%) rename src/content/component/{AnimationButton => MultiLayerButton}/layer/BackgroundLayer.cpp (100%) rename src/content/component/{AnimationButton => MultiLayerButton}/layer/BackgroundLayer.h (100%) rename src/content/component/{AnimationButton => MultiLayerButton}/layer/OverlayLayer.cpp (100%) rename src/content/component/{AnimationButton => MultiLayerButton}/layer/OverlayLayer.h (100%) diff --git a/src/content/component/AnimationButton/MultiLayerButton.cpp b/src/content/component/MultiLayerButton/MultiLayerButton.cpp similarity index 89% rename from src/content/component/AnimationButton/MultiLayerButton.cpp rename to src/content/component/MultiLayerButton/MultiLayerButton.cpp index e627ba1..d707e0a 100644 --- a/src/content/component/AnimationButton/MultiLayerButton.cpp +++ b/src/content/component/MultiLayerButton/MultiLayerButton.cpp @@ -10,7 +10,7 @@ namespace cmaterial::component { addLayer(&overlayLayer); } - void MultiLayerButton::render(ImGuiIO *io) { + void MultiLayerButton::update(ImGuiIO *io) { ImGui::PushStyleColor(ImGuiCol_Button, ImVec4(0.0f, 1.0f, 0.0f, 1.0f)); ImGui::Button(name.c_str()); ImGui::PopStyleColor(); diff --git a/src/content/component/AnimationButton/MultiLayerButton.h b/src/content/component/MultiLayerButton/MultiLayerButton.h similarity index 93% rename from src/content/component/AnimationButton/MultiLayerButton.h rename to src/content/component/MultiLayerButton/MultiLayerButton.h index 52c735e..32f3ec9 100644 --- a/src/content/component/AnimationButton/MultiLayerButton.h +++ b/src/content/component/MultiLayerButton/MultiLayerButton.h @@ -18,7 +18,7 @@ namespace cmaterial::component { MultiLayerButton(); ~MultiLayerButton() = default; - void render(ImGuiIO *io) override; + void update(ImGuiIO *io) override; private: layer::animationbutton::BackgroundLayer backgroundLayer; diff --git a/src/content/component/AnimationButton/layer/BackgroundLayer.cpp b/src/content/component/MultiLayerButton/layer/BackgroundLayer.cpp similarity index 100% rename from src/content/component/AnimationButton/layer/BackgroundLayer.cpp rename to src/content/component/MultiLayerButton/layer/BackgroundLayer.cpp diff --git a/src/content/component/AnimationButton/layer/BackgroundLayer.h b/src/content/component/MultiLayerButton/layer/BackgroundLayer.h similarity index 100% rename from src/content/component/AnimationButton/layer/BackgroundLayer.h rename to src/content/component/MultiLayerButton/layer/BackgroundLayer.h diff --git a/src/content/component/AnimationButton/layer/OverlayLayer.cpp b/src/content/component/MultiLayerButton/layer/OverlayLayer.cpp similarity index 100% rename from src/content/component/AnimationButton/layer/OverlayLayer.cpp rename to src/content/component/MultiLayerButton/layer/OverlayLayer.cpp diff --git a/src/content/component/AnimationButton/layer/OverlayLayer.h b/src/content/component/MultiLayerButton/layer/OverlayLayer.h similarity index 100% rename from src/content/component/AnimationButton/layer/OverlayLayer.h rename to src/content/component/MultiLayerButton/layer/OverlayLayer.h diff --git a/src/main.cpp b/src/main.cpp index d5e8ba8..f1fa051 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -2,7 +2,7 @@ // Created by Earth_Studio on 2026/4/12. // -#include "content/component/AnimationButton/MultiLayerButton.h" +#include "content/component/MultiLayerButton/MultiLayerButton.h" #include "engine/framework/Framework.h" #include "content/component/BasicButton/BasicButton.h" From 65b3e395f95a14c11d7f58af03ec6fa8ebb30f19 Mon Sep 17 00:00:00 2001 From: Earth_Studio Date: Thu, 7 May 2026 18:17:05 +0800 Subject: [PATCH 25/34] add some docs --- .../component/BasicButton/BasicButton.cpp | 2 +- .../component/BasicButton/BasicButton.h | 2 +- .../component/CreatorButton/CreatorButton.h | 2 +- .../component/material/Button/Button.cpp | 2 +- .../component/material/Button/Button.h | 2 +- src/engine/animation/IAnimation.h | 16 +++++++- src/engine/component/IComponent.cpp | 6 ++- src/engine/component/IComponent.h | 23 ++++++++++- src/engine/component/ILayer.hpp | 17 ++++++-- src/engine/framework/Framework.cpp | 16 +++++++- src/engine/window/IWindow.cpp | 32 ++++++++++----- src/engine/window/IWindow.h | 40 +++++++++++++++++-- 12 files changed, 134 insertions(+), 26 deletions(-) diff --git a/src/content/component/BasicButton/BasicButton.cpp b/src/content/component/BasicButton/BasicButton.cpp index 9926587..481f00b 100644 --- a/src/content/component/BasicButton/BasicButton.cpp +++ b/src/content/component/BasicButton/BasicButton.cpp @@ -4,7 +4,7 @@ #include "BasicButton.h" -void cmaterial::component::BasicButton::render(ImGuiIO *io) { +void cmaterial::component::BasicButton::update(ImGuiIO *io) { if (ImGui::Button(name.c_str())) onActive(); } \ No newline at end of file diff --git a/src/content/component/BasicButton/BasicButton.h b/src/content/component/BasicButton/BasicButton.h index 841e8bb..7d74d56 100644 --- a/src/content/component/BasicButton/BasicButton.h +++ b/src/content/component/BasicButton/BasicButton.h @@ -15,7 +15,7 @@ namespace cmaterial::component { BasicButton() = default; ~BasicButton() = default; - void render(ImGuiIO *io) override; + void update(ImGuiIO *io) override; void (*onActive)() =[]() {}; }; } diff --git a/src/content/component/CreatorButton/CreatorButton.h b/src/content/component/CreatorButton/CreatorButton.h index 402643f..3afab6e 100644 --- a/src/content/component/CreatorButton/CreatorButton.h +++ b/src/content/component/CreatorButton/CreatorButton.h @@ -16,7 +16,7 @@ namespace cmaterial::component { public: using IComponent::IComponent; - void render(ImGuiIO *io) override { + void update(ImGuiIO *io) override { if (ImGui::Button("SpawnWindow")) { static int count = 0; auto* req = new event::SpawnWindowRequest(); diff --git a/src/content/component/material/Button/Button.cpp b/src/content/component/material/Button/Button.cpp index 8031b38..87cf6f8 100644 --- a/src/content/component/material/Button/Button.cpp +++ b/src/content/component/material/Button/Button.cpp @@ -23,7 +23,7 @@ namespace cmaterial::component::material { } // 🔮 1. 逻辑与交互中枢 - void Button::render(ImGuiIO *io) { + void Button::update(ImGuiIO *io) { // 放置隐形按钮,夺取 120x40 的交互判定权 ImGui::InvisibleButton(name.c_str(), buttonSize); diff --git a/src/content/component/material/Button/Button.h b/src/content/component/material/Button/Button.h index e4ca4b4..a1aa76f 100644 --- a/src/content/component/material/Button/Button.h +++ b/src/content/component/material/Button/Button.h @@ -19,7 +19,7 @@ namespace cmaterial::component::material { Button(); ~Button() = default; - void render(ImGuiIO *io) override; + void update(ImGuiIO *io) override; // 🔮 物理状态:暴露给 Layer 和 Animation double stateAlpha = 0.0; // Hover 的 8% 叠加透明度 diff --git a/src/engine/animation/IAnimation.h b/src/engine/animation/IAnimation.h index 5c51a72..4136ae5 100644 --- a/src/engine/animation/IAnimation.h +++ b/src/engine/animation/IAnimation.h @@ -13,7 +13,21 @@ namespace cmaterial::animation { - class IAnimation : public cmaterial::utils::INode { + /** + * @brief The base of the Animation. + * @details Basically, it's just a place to bind property and tweeny object. + * + * @details --- Extend Hook --- + * @details @code Constructor()@endcode : You should bind the properties and tweens in your own constructor. + * + * @details --- Property --- + * @details @code bool isLoop@endcode : Is this animation loop? + * @details @code double ...@endcode : Any property you want to apply animation. + * + * @details --- Helper --- + * @details @code void bind(double*, tween*)@endcode : Bind property and tween. + */ + class IAnimation : public utils::INode { public: virtual ~IAnimation(); diff --git a/src/engine/component/IComponent.cpp b/src/engine/component/IComponent.cpp index 05a3c1e..d0b8d6a 100644 --- a/src/engine/component/IComponent.cpp +++ b/src/engine/component/IComponent.cpp @@ -57,6 +57,10 @@ namespace cmaterial::component { std::erase(layersAfter, layer); } + /** + * @brief The core of the update + render. Only render component when current frame is not virtual. + * @see IWindow + */ void IComponent::drawComponent(ImGuiIO *io) { if (!isActive) return; @@ -68,7 +72,7 @@ namespace cmaterial::component { splitter.SetCurrentChannel(drawList, 1); ImGui::BeginGroup(); - this->render(io); + this->update(io); ImGui::EndGroup(); ImVec2 minPos = ImGui::GetItemRectMin(); diff --git a/src/engine/component/IComponent.h b/src/engine/component/IComponent.h index cc17615..979679c 100644 --- a/src/engine/component/IComponent.h +++ b/src/engine/component/IComponent.h @@ -17,11 +17,30 @@ namespace cmaterial::component { - class IComponent : public cmaterial::utils::INode { + /** + * @brief The base of Component. + * @details Basically, it's a state machine. + * @details You need to figure out how to distinguish between different states, when to post an Event, and how to + * draw your component every frame. + * @details It's probably the most complex part in CMaterial, but once you understand, it's very easy to create any + * component for yourself. + * + * @details --- Extend Hook --- + * @details @code virtual update() = 0@endcode : The core update logic of the component. + * + * @details --- Property --- + * @details @code std::string name@endcode : The name of the Component. + * @details @code bool isActive@endcode : Is Component should update + render? + * + * @details --- DANGER ZONE --- + * @details @code virtual drawComponent()@endcode : The core of the update + render. Only render component when current frame is not virtual. + * If you TRULY have a compelling reason that you must use it, you can override it completely. But for the most case, you SHOULDN'T touch it. + */ + class IComponent : public utils::INode { public: virtual ~IComponent(); - virtual void render(ImGuiIO *io) = 0; + virtual void update(ImGuiIO *io) = 0; virtual void drawComponent(ImGuiIO *io); void addComponent(IComponent *component); diff --git a/src/engine/component/ILayer.hpp b/src/engine/component/ILayer.hpp index 1d99d09..6233ae8 100644 --- a/src/engine/component/ILayer.hpp +++ b/src/engine/component/ILayer.hpp @@ -11,9 +11,20 @@ namespace cmaterial::component { - class IComponent; - - class ILayer : public cmaterial::utils::INode { + /** + * @brief The base of the Layer, the extra render part of the component. + * @details You may want render some special effect on your component (e.g. shadow, ripple). Here + * is the perfect place for you to draw them. + * + * @details --- Extend Hook --- + * @details @code virtual render() = 0@endcode : The core render logic for Layer. + * + * @details --- Property --- + * @details @code int priority@endcode : The render priority, just like Z-axis. If priority < 0, Layer will draw + * under the Component. If priority >= 0 , Layer will draw above the Component. The order of the render is based on + * the number, smaller to bigger. + */ + class ILayer : public utils::INode { public: ILayer() = default; virtual ~ILayer() = default; diff --git a/src/engine/framework/Framework.cpp b/src/engine/framework/Framework.cpp index a509c10..a42dadc 100644 --- a/src/engine/framework/Framework.cpp +++ b/src/engine/framework/Framework.cpp @@ -27,6 +27,12 @@ using EventBus = cmaterial::event::EventBus; namespace cmaterial { + /** + * @brief + * @details Initialize CMaterial Engine. + * @details This should be called at the beginning. + * @return Statement for initialize + */ Framework::error Framework::initialize() { if (!glfwInit()) return GLFW_INIT_FAILED; @@ -61,6 +67,10 @@ namespace cmaterial { return OK; } + /** + * @brief Start CMaterial engine. + * @return Statement for result + */ Framework::error Framework::run() { if (!isInitialized) return NOT_INIT; @@ -83,7 +93,7 @@ namespace cmaterial { double now = glfwGetTime(); if (now < nextFrameTime) { - std::this_thread::sleep_for(std::chrono::microseconds(100)); + std::this_thread::sleep_for(std::chrono::milliseconds(1)); // who will have 1000+ Hz monitor? 1 ms should be fine in all the time continue; } @@ -148,6 +158,10 @@ namespace cmaterial { return OK; } + /** + * @brief Add a new window to the CMaterial engine. + * @param window Pointer of the @code IWindow@endcode + */ void Framework::addWindow(window::IWindow *window) { window->initialize(hiddenWindow, fontAtlas); windows.insert({window->name, window}); diff --git a/src/engine/window/IWindow.cpp b/src/engine/window/IWindow.cpp index 8f84924..36f0295 100644 --- a/src/engine/window/IWindow.cpp +++ b/src/engine/window/IWindow.cpp @@ -63,6 +63,12 @@ namespace cmaterial::window { postInit(io); } + /** + * @brief + * @details The core update logic of the window. + * @details If you TRULY have a compelling reason that you must use it, you can override it completely. + * @details But for the most case, you should override @code postUpdate()@endcode instead of this. + */ void IWindow::update() { if (glfwWindowShouldClose(glfwWindow)) isDead = true; @@ -87,6 +93,23 @@ namespace cmaterial::window { styles.erase(styleVar); } + /** + * @brief Draw Window. + * + * @details + * We all know ImGui is Immediately Mode, so its logic and render is force bind, you cannot trigger logic or render + * independent. HOWEVER, to be portable across different platforms, ImGui split itself into two parts: the core + * **library** and the **backend**. In other words, the ImGui core is responsible for calculating vertex data, which + * it then submits to the backend for rendering. This is where the magic happens: although ImGui's rendering and + * logic are forcibly bound, rendering MUST actually go through the backend submission. The REAL rendering is + * ultimately done by the **backend**, and the vertex computation overhead is actually tiny compared to full + * rendering. So, by calling ImGui::Render() to let the ImGui core execute the complete rendering pipeline — but + * WITHOUT submitting to the backend for actual rendering — we can achieve a complete DECOUPLING of logic and + * rendering! Although the CPU will inevitably still calculate the vertices, we have successfully completely + * decoupled rendering from logic, and the **possibilities** from here are INFINITE! + * + * @param isVirtual If true, CMaterial will not actually render anything, just update internal ImGui logic. + */ void IWindow::drawWindow(bool isVirtual) { if (!isVirtual) glfwMakeContextCurrent(glfwWindow); @@ -142,15 +165,6 @@ namespace cmaterial::window { ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData()); glfwSwapBuffers(glfwWindow); } - // We all know ImGui is Immediately Mode, so its logic and render is force bind, you cannot trigger logic or render independent. - // HOWEVER, to be portable across different platforms, ImGui split itself into two parts: the core **library** and the **backend**. - // In other words, the ImGui core is responsible for calculating vertex data, which it then submits to the backend for rendering. - // This is where the magic happens: although ImGui's rendering and logic are forcibly bound, rendering MUST actually go through the backend submission. - // The REAL rendering is ultimately done by the **backend**, and the vertex computation overhead is actually tiny compared to full rendering. - // So, by calling ImGui::Render() to let the ImGui core execute the complete rendering pipeline — but WITHOUT submitting to the backend for - // actual rendering — we can achieve a complete DECOUPLING of logic and rendering! - // Although the CPU will inevitably still calculate the vertices, we have successfully completely decoupled rendering from logic, - // and the **possibilities** from here are INFINITE! } void IWindow::addComponent(component::IComponent* comp) { diff --git a/src/engine/window/IWindow.h b/src/engine/window/IWindow.h index 2b5fc3b..110e4b9 100644 --- a/src/engine/window/IWindow.h +++ b/src/engine/window/IWindow.h @@ -18,17 +18,49 @@ namespace cmaterial::window { - class IWindow : public cmaterial::utils::INode { + /** + * @brief The base of the window. + * + * @details --- Extend Hook --- + * + * @details @code virtual postInit()@endcode : The custom logic of the initialize window + * @details @code virtual postUpdate()@endcode : The custom logic of the update window. + * @details @code virtual render()@endcode : The content of the window, Design your window in here. + * + * @details --- Property --- + * @details @code std::string name@endcode : The name of the window. + * @details @code int width, height@endcode : The width and height of the window. + * @details @code ImGuiWindowFlags windowFlags@endcode : The flag of the ImGuiWindow. If you don't know what it is, DON'T touch it. + * @details @code ImVec4 backgroundColor@endcode : The background color of the window. Use ImVec4(r, g, b, alpha) to change it. + * + * @details --- DANGER ZONE --- + * + * @details @code virtual update()@endcode : The core update logic of the window. + * If you TRULY have a compelling reason that you must use it, you can override it completely. + * But for the most case, you should override @code postUpdate()@endcode instead of this. + * + * @warning You should extend it to make custom window. NEVER USE THE INTERFACE DIRECTLY! + */ + class IWindow : public utils::INode { public: IWindow(const std::string &name, int width, int height); virtual ~IWindow(); void initialize(GLFWwindow* window, ImFontAtlas* fontAtlas); - void drawWindow(bool isVirtual); // isVirtual: Is only force ImGui update private states? + void drawWindow(bool isVirtual); + /** + * The custom logic of the initialize window + */ virtual void postInit(ImGuiIO *io) {}; + /** + * The custom logic of the update window. + */ virtual void postUpdate() {}; virtual void update(); + /** + * The content of the window, Design your window in here. + */ virtual void render(ImGuiIO* io) {}; void addComponent(component::IComponent *comp); @@ -38,11 +70,11 @@ namespace cmaterial::window { std::string name; int width, height; + ImGuiWindowFlags windowFlags = ImGuiWindowFlags_NoDecoration | ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoSavedSettings; + ImVec4 backgroundColor = ImVec4(0.0f, 0.0f, 0.0f, 1.0f); bool isDead = false; bool isInitialized = false; - ImGuiWindowFlags windowFlags = ImGuiWindowFlags_NoDecoration | ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoSavedSettings; - ImVec4 backgroundColor = ImVec4(0.0f, 0.0f, 0.0f, 1.0f); bool _isSizeChange = false; From 851c28d6867cbdc37f4cc09cb9836c149069bc24 Mon Sep 17 00:00:00 2001 From: Earth_Studio Date: Thu, 7 May 2026 21:17:40 +0800 Subject: [PATCH 26/34] update docs --- README.md | 3 +++ README_CN.md | 3 +++ src/engine/animation/IAnimation.h | 10 ++++++---- src/engine/component/IComponent.cpp | 2 +- src/engine/component/IComponent.h | 10 ++++++---- src/engine/component/ILayer.hpp | 12 +++++++----- src/engine/window/IWindow.cpp | 8 ++++---- src/engine/window/IWindow.h | 18 +++++++++--------- 8 files changed, 39 insertions(+), 27 deletions(-) diff --git a/README.md b/README.md index 4d75ee6..d51083a 100644 --- a/README.md +++ b/README.md @@ -33,6 +33,9 @@ Design your own components however you wish using OpenGL 3.3 Core and ImGui APIs ## Hybrid Mode Neither Immediate Mode nor Retained Mode—but ***both***. CMaterial leverages Retained Mode at the **UI Layer** for the structural integrity of a mature framework, while empowering the **Component Layer** with Immediate Mode for unrestricted control over how every pixel is rendered. +## Pure Upper-Layer Construction +All of CMaterial's functionality is implemented entirely at the upper layer, **without modifying a single line of code from dependent libraries**. In most cases, you can update the dependent libraries on your own, enjoying all upstream updates **without spending time re-porting the framework**. + --- ## Libraries diff --git a/README_CN.md b/README_CN.md index 5e0739f..30d8f64 100644 --- a/README_CN.md +++ b/README_CN.md @@ -33,6 +33,9 @@ ## 混合模式 不是即时模式,也不是保留模式——因为它***全都是***。CMaterial 允许你在 **UI 层** 使用保留模式,就像任何一个成熟的 GUI 框架那样,而同时在 **组件层** 使用即时模式,尽情的定义组件如何进行绘制。 +## 纯上层构建 +CMaterial 的所有功能实现全部处于上层,**没有更改任何一行来自依赖库的代码**。多数情况下,你可以自行更新依赖库,享受来自上游的全部更新,而**无需花费时间重新移植框架**。 + --- ## 依赖库 diff --git a/src/engine/animation/IAnimation.h b/src/engine/animation/IAnimation.h index 4136ae5..907ebfc 100644 --- a/src/engine/animation/IAnimation.h +++ b/src/engine/animation/IAnimation.h @@ -15,17 +15,19 @@ namespace cmaterial::animation { /** * @brief The base of the Animation. - * @details Basically, it's just a place to bind property and tweeny object. + * @details Basically, it's just a place to bind a property and a tweeny object. * * @details --- Extend Hook --- * @details @code Constructor()@endcode : You should bind the properties and tweens in your own constructor. * * @details --- Property --- - * @details @code bool isLoop@endcode : Is this animation loop? - * @details @code double ...@endcode : Any property you want to apply animation. + * @details @code bool isLoop@endcode : Is this animation looping? + * @details @code double ...@endcode : Any property you want to apply animation to, you need to manually manage them. * * @details --- Helper --- - * @details @code void bind(double*, tween*)@endcode : Bind property and tween. + * @details @code void bind(double*, tween*)@endcode : Bind a property and a tween. + * + * @warning You should extend it to make a custom Animation. NEVER USE THE INTERFACE DIRECTLY! */ class IAnimation : public utils::INode { public: diff --git a/src/engine/component/IComponent.cpp b/src/engine/component/IComponent.cpp index d0b8d6a..5a3aa47 100644 --- a/src/engine/component/IComponent.cpp +++ b/src/engine/component/IComponent.cpp @@ -58,7 +58,7 @@ namespace cmaterial::component { } /** - * @brief The core of the update + render. Only render component when current frame is not virtual. + * @brief The core of the update + render. Only render the component when the current frame is not virtual. * @see IWindow */ void IComponent::drawComponent(ImGuiIO *io) { diff --git a/src/engine/component/IComponent.h b/src/engine/component/IComponent.h index 979679c..8afe0d9 100644 --- a/src/engine/component/IComponent.h +++ b/src/engine/component/IComponent.h @@ -22,7 +22,7 @@ namespace cmaterial::component { * @details Basically, it's a state machine. * @details You need to figure out how to distinguish between different states, when to post an Event, and how to * draw your component every frame. - * @details It's probably the most complex part in CMaterial, but once you understand, it's very easy to create any + * @details It's probably the most complex part in CMaterial, but once you understand it, it's very easy to create any * component for yourself. * * @details --- Extend Hook --- @@ -30,11 +30,13 @@ namespace cmaterial::component { * * @details --- Property --- * @details @code std::string name@endcode : The name of the Component. - * @details @code bool isActive@endcode : Is Component should update + render? + * @details @code bool isActive@endcode : Should the Component update + render? * * @details --- DANGER ZONE --- - * @details @code virtual drawComponent()@endcode : The core of the update + render. Only render component when current frame is not virtual. - * If you TRULY have a compelling reason that you must use it, you can override it completely. But for the most case, you SHOULDN'T touch it. + * @details @code virtual drawComponent()@endcode : The core of the update + render. Only render the component when the current frame is not virtual. + * If you TRULY have a compelling reason that you must use it, you can override it completely. But in most cases, you SHOULDN'T touch it. + * + * @warning You should extend it to make a custom Component. NEVER USE THE INTERFACE DIRECTLY! */ class IComponent : public utils::INode { public: diff --git a/src/engine/component/ILayer.hpp b/src/engine/component/ILayer.hpp index 6233ae8..f4ff243 100644 --- a/src/engine/component/ILayer.hpp +++ b/src/engine/component/ILayer.hpp @@ -13,16 +13,18 @@ namespace cmaterial::component { /** * @brief The base of the Layer, the extra render part of the component. - * @details You may want render some special effect on your component (e.g. shadow, ripple). Here + * @details You may want to render some special effect on your component (e.g. shadow, ripple). Here * is the perfect place for you to draw them. * * @details --- Extend Hook --- - * @details @code virtual render() = 0@endcode : The core render logic for Layer. + * @details @code virtual render() = 0@endcode : The core render logic for the Layer. * * @details --- Property --- - * @details @code int priority@endcode : The render priority, just like Z-axis. If priority < 0, Layer will draw - * under the Component. If priority >= 0 , Layer will draw above the Component. The order of the render is based on - * the number, smaller to bigger. + * @details @code int priority@endcode : The render priority, just like the Z-axis. If priority < 0, the Layer will draw + * under the Component. If priority >= 0, the Layer will draw above the Component. The render order is based on + * the number, from smaller to bigger. + * + * @warning You should extend it to make a custom Layer. NEVER USE THE INTERFACE DIRECTLY! */ class ILayer : public utils::INode { public: diff --git a/src/engine/window/IWindow.cpp b/src/engine/window/IWindow.cpp index 36f0295..53ab086 100644 --- a/src/engine/window/IWindow.cpp +++ b/src/engine/window/IWindow.cpp @@ -67,7 +67,7 @@ namespace cmaterial::window { * @brief * @details The core update logic of the window. * @details If you TRULY have a compelling reason that you must use it, you can override it completely. - * @details But for the most case, you should override @code postUpdate()@endcode instead of this. + * @details But in most cases, you should override @code postUpdate()@endcode instead of this. */ void IWindow::update() { if (glfwWindowShouldClose(glfwWindow)) @@ -97,15 +97,15 @@ namespace cmaterial::window { * @brief Draw Window. * * @details - * We all know ImGui is Immediately Mode, so its logic and render is force bind, you cannot trigger logic or render - * independent. HOWEVER, to be portable across different platforms, ImGui split itself into two parts: the core + * We all know ImGui is Immediate Mode, so its logic and render are forcibly bound; you cannot trigger logic or render + * independently. HOWEVER, to be portable across different platforms, ImGui splits itself into two parts: the core * **library** and the **backend**. In other words, the ImGui core is responsible for calculating vertex data, which * it then submits to the backend for rendering. This is where the magic happens: although ImGui's rendering and * logic are forcibly bound, rendering MUST actually go through the backend submission. The REAL rendering is * ultimately done by the **backend**, and the vertex computation overhead is actually tiny compared to full * rendering. So, by calling ImGui::Render() to let the ImGui core execute the complete rendering pipeline — but * WITHOUT submitting to the backend for actual rendering — we can achieve a complete DECOUPLING of logic and - * rendering! Although the CPU will inevitably still calculate the vertices, we have successfully completely + * rendering! Although the CPU will inevitably still calculate the vertices, we have successfully and completely * decoupled rendering from logic, and the **possibilities** from here are INFINITE! * * @param isVirtual If true, CMaterial will not actually render anything, just update internal ImGui logic. diff --git a/src/engine/window/IWindow.h b/src/engine/window/IWindow.h index 110e4b9..b4207f3 100644 --- a/src/engine/window/IWindow.h +++ b/src/engine/window/IWindow.h @@ -23,23 +23,23 @@ namespace cmaterial::window { * * @details --- Extend Hook --- * - * @details @code virtual postInit()@endcode : The custom logic of the initialize window - * @details @code virtual postUpdate()@endcode : The custom logic of the update window. - * @details @code virtual render()@endcode : The content of the window, Design your window in here. + * @details @code virtual postInit()@endcode : The custom logic of the window initialization. + * @details @code virtual postUpdate()@endcode : The custom logic of the window update. + * @details @code virtual render()@endcode : The content of the window. Design your window here. * * @details --- Property --- * @details @code std::string name@endcode : The name of the window. * @details @code int width, height@endcode : The width and height of the window. - * @details @code ImGuiWindowFlags windowFlags@endcode : The flag of the ImGuiWindow. If you don't know what it is, DON'T touch it. + * @details @code ImGuiWindowFlags windowFlags@endcode : The flags of the ImGuiWindow. If you don't know what they are, DON'T touch them. * @details @code ImVec4 backgroundColor@endcode : The background color of the window. Use ImVec4(r, g, b, alpha) to change it. * * @details --- DANGER ZONE --- * * @details @code virtual update()@endcode : The core update logic of the window. * If you TRULY have a compelling reason that you must use it, you can override it completely. - * But for the most case, you should override @code postUpdate()@endcode instead of this. + * But in most cases, you should override @code postUpdate()@endcode instead of this. * - * @warning You should extend it to make custom window. NEVER USE THE INTERFACE DIRECTLY! + * @warning You should extend it to make a custom Window. NEVER USE THE INTERFACE DIRECTLY! */ class IWindow : public utils::INode { public: @@ -50,16 +50,16 @@ namespace cmaterial::window { void drawWindow(bool isVirtual); /** - * The custom logic of the initialize window + * The custom logic of the window initialization. */ virtual void postInit(ImGuiIO *io) {}; /** - * The custom logic of the update window. + * The custom logic of the window update. */ virtual void postUpdate() {}; virtual void update(); /** - * The content of the window, Design your window in here. + * The custom logic of the window initialization. */ virtual void render(ImGuiIO* io) {}; From cf8bc3a649f9a00e6ac20f3096f30c8a3e5d88ee Mon Sep 17 00:00:00 2001 From: Earth_Studio Date: Fri, 8 May 2026 15:54:49 +0800 Subject: [PATCH 27/34] update docs --- src/engine/animation/Player.h | 9 ++++++++- src/main.cpp | 18 ++++++++++-------- 2 files changed, 18 insertions(+), 9 deletions(-) diff --git a/src/engine/animation/Player.h b/src/engine/animation/Player.h index 79b51fe..15b8a6b 100644 --- a/src/engine/animation/Player.h +++ b/src/engine/animation/Player.h @@ -12,7 +12,14 @@ namespace cmaterial::animation { - class Player{ + /** + * @brief The Animation player. + * @details You will play all the Animation in here. + * + * @warning No extra description because this is not finished. + * @todo Rebuild with consumer mode. + */ + class Player{ // TODO: Rebuild public: Player() = default; ~Player(); diff --git a/src/main.cpp b/src/main.cpp index f1fa051..d800637 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -11,27 +11,29 @@ #include "content/logic/listener/WindowLogic.h" #include "content/window/BasicWindow/BasicWindow.h" +using namespace cmaterial; + int main() { // Before you start to write application, you must initialize the Framework first: - cmaterial::Framework app; + Framework app; app.initialize(); // Add your Listener: - cmaterial::event::EventBus::addListener(new cmaterial::event::WindowLogic(&app)); + event::EventBus::addListener(new event::WindowLogic(&app)); // Create your windows: - cmaterial::window::BasicWindow* window1 = new cmaterial::window::BasicWindow("Milestone Single Test", 300, 100); - cmaterial::window::BasicWindow* window2 = new cmaterial::window::BasicWindow("Hybrid Final Test", 300, 100); + window::BasicWindow* window1 = new window::BasicWindow("Milestone Single Test", 300, 100); + window::BasicWindow* window2 = new window::BasicWindow("Hybrid Final Test", 300, 100); // Create your components: - cmaterial::component::BasicButton* button1 = new cmaterial::component::BasicButton(); + component::BasicButton* button1 = new component::BasicButton(); button1->name = "buttonInWindow1"; - cmaterial::component::CreatorButton* button2 = new cmaterial::component::CreatorButton(); + component::CreatorButton* button2 = new component::CreatorButton(); button2->name = "buttonInWindow2"; - cmaterial::component::MultiLayerButton* multiLayerButton = new cmaterial::component::MultiLayerButton(); + component::MultiLayerButton* multiLayerButton = new component::MultiLayerButton(); multiLayerButton->name = "MultiLayer"; - cmaterial::component::material::Button* md3Button = new cmaterial::component::material::Button(); + component::material::Button* md3Button = new component::material::Button(); md3Button->name = "MD3 Button"; // To inject components in an exist window: From 518978cbebf8bfd2f7a553ae48abbe7801ba1d41 Mon Sep 17 00:00:00 2001 From: Earth_Studio Date: Sat, 9 May 2026 16:28:48 +0800 Subject: [PATCH 28/34] update docs, rebuild IEvent --- README.md | 4 +- README_CN.md | 4 +- .../logic/event/SpawnWindowRequest.hpp | 2 +- src/engine/animation/IAnimation.h | 3 +- src/engine/animation/Player.h | 2 +- src/engine/component/IComponent.h | 10 ++-- src/engine/component/ILayer.hpp | 4 +- src/engine/eventbus/EventBus.cpp | 13 ++++- src/engine/eventbus/EventBus.h | 7 +++ src/engine/eventbus/IEvent.cpp | 11 ---- src/engine/eventbus/IEvent.h | 33 ----------- src/engine/eventbus/IEvent.hpp | 56 +++++++++++++++++++ src/engine/eventbus/IListener.h | 28 ++++++++++ .../internal/event/ForceRedrawEvent.hpp | 2 +- src/engine/window/IWindow.h | 6 +- 15 files changed, 123 insertions(+), 62 deletions(-) delete mode 100644 src/engine/eventbus/IEvent.cpp delete mode 100644 src/engine/eventbus/IEvent.h create mode 100644 src/engine/eventbus/IEvent.hpp diff --git a/README.md b/README.md index d51083a..20fe7d8 100644 --- a/README.md +++ b/README.md @@ -39,9 +39,9 @@ All of CMaterial's functionality is implemented entirely at the upper layer, **w --- ## Libraries -- **[GLFW](https://github.com/glfw/glfw)** : A lightweight, cross-platform API for creating windows, contexts, handling input and events. -- **[GLAD](https://github.com/dav1dde/glad)** : A multi-Language Vulkan/GL/GLES/EGL/GLX/WGL loader-generator based on the official specifications. - **[ImGui](https://github.com/ocornut/imgui)** : A bloat-free Graphical User interface for C++ with minimal dependencies. +- **[GLAD](https://github.com/dav1dde/glad)** : A multi-Language Vulkan/GL/GLES/EGL/GLX/WGL loader-generator based on the official specifications. +- **[GLFW](https://github.com/glfw/glfw)** : A lightweight, cross-platform API for creating windows, contexts, handling input and events. - **[tweeny](https://github.com/mobius3/tweeny)** : An inbetweening library designed for the creation of complex animations and beautiful interactive software. - **[stb_image](https://github.com/nothings/stb)** : A highly renowned single-file public domain library for loading images in C/C++. - **[mimalloc](https://github.com/microsoft/mimalloc)** : A compact general purpose allocator with excellent performance. diff --git a/README_CN.md b/README_CN.md index 30d8f64..34ae9bc 100644 --- a/README_CN.md +++ b/README_CN.md @@ -39,9 +39,9 @@ CMaterial 的所有功能实现全部处于上层,**没有更改任何一行 --- ## 依赖库 -- **[GLFW](https://github.com/glfw/glfw)** : 一个轻量级、跨平台的 API,用于创建窗口、上下文、读取输入及处理事件。 -- **[GLAD](https://github.com/dav1dde/glad)** : 基于官方规范的多语言 Vulkan/GL/GLES/EGL/GLX/WGL 加载器生成工具。 - **[ImGui](https://github.com/ocornut/imgui)** : 一个依赖极简、无冗余的 C++ 图形用户界面。 +- **[GLAD](https://github.com/dav1dde/glad)** : 基于官方规范的多语言 Vulkan/GL/GLES/EGL/GLX/WGL 加载器生成工具。 +- **[GLFW](https://github.com/glfw/glfw)** : 一个轻量级、跨平台的 API,用于创建窗口、上下文、读取输入及处理事件。 - **[tweeny](https://github.com/mobius3/tweeny)** : 一个专为游戏和其他精美交互式软件打造的复杂补间动画库。 - **[stb_image](https://github.com/nothings/stb)** : C/C++ 领域极具盛名的单头文件公共域图像加载库。 - **[mimalloc](https://github.com/microsoft/mimalloc)** : 一款性能卓越、体积精简的通用内存分配器。 diff --git a/src/content/logic/event/SpawnWindowRequest.hpp b/src/content/logic/event/SpawnWindowRequest.hpp index 2620b48..89cd5bb 100644 --- a/src/content/logic/event/SpawnWindowRequest.hpp +++ b/src/content/logic/event/SpawnWindowRequest.hpp @@ -6,7 +6,7 @@ #define CMATERIAL_TESTEVENT_H -#include "engine/eventbus/IEvent.h" +#include "engine/eventbus/IEvent.hpp" #include diff --git a/src/engine/animation/IAnimation.h b/src/engine/animation/IAnimation.h index 907ebfc..d2bd632 100644 --- a/src/engine/animation/IAnimation.h +++ b/src/engine/animation/IAnimation.h @@ -22,7 +22,8 @@ namespace cmaterial::animation { * * @details --- Property --- * @details @code bool isLoop@endcode : Is this animation looping? - * @details @code double ...@endcode : Any property you want to apply animation to, you need to manually manage them. + * @details @code double ...@endcode : Any property you want to apply animation to, you need to manually manage + * them. * * @details --- Helper --- * @details @code void bind(double*, tween*)@endcode : Bind a property and a tween. diff --git a/src/engine/animation/Player.h b/src/engine/animation/Player.h index 15b8a6b..71e2f2d 100644 --- a/src/engine/animation/Player.h +++ b/src/engine/animation/Player.h @@ -19,7 +19,7 @@ namespace cmaterial::animation { * @warning No extra description because this is not finished. * @todo Rebuild with consumer mode. */ - class Player{ // TODO: Rebuild + class Player { public: Player() = default; ~Player(); diff --git a/src/engine/component/IComponent.h b/src/engine/component/IComponent.h index 8afe0d9..4ba467d 100644 --- a/src/engine/component/IComponent.h +++ b/src/engine/component/IComponent.h @@ -22,8 +22,8 @@ namespace cmaterial::component { * @details Basically, it's a state machine. * @details You need to figure out how to distinguish between different states, when to post an Event, and how to * draw your component every frame. - * @details It's probably the most complex part in CMaterial, but once you understand it, it's very easy to create any - * component for yourself. + * @details It's probably the most complex part in CMaterial, but once you understand it, it's very easy to create + * any component for yourself. * * @details --- Extend Hook --- * @details @code virtual update() = 0@endcode : The core update logic of the component. @@ -33,8 +33,10 @@ namespace cmaterial::component { * @details @code bool isActive@endcode : Should the Component update + render? * * @details --- DANGER ZONE --- - * @details @code virtual drawComponent()@endcode : The core of the update + render. Only render the component when the current frame is not virtual. - * If you TRULY have a compelling reason that you must use it, you can override it completely. But in most cases, you SHOULDN'T touch it. + * @details @code virtual drawComponent()@endcode : The core of the update + render. Only render the component when + * the current frame is not virtual. + * @details If you TRULY have a compelling reason that you must use it, you can override it completely. But in most + * cases, you SHOULDN'T touch it. * * @warning You should extend it to make a custom Component. NEVER USE THE INTERFACE DIRECTLY! */ diff --git a/src/engine/component/ILayer.hpp b/src/engine/component/ILayer.hpp index f4ff243..96905e4 100644 --- a/src/engine/component/ILayer.hpp +++ b/src/engine/component/ILayer.hpp @@ -20,8 +20,8 @@ namespace cmaterial::component { * @details @code virtual render() = 0@endcode : The core render logic for the Layer. * * @details --- Property --- - * @details @code int priority@endcode : The render priority, just like the Z-axis. If priority < 0, the Layer will draw - * under the Component. If priority >= 0, the Layer will draw above the Component. The render order is based on + * @details @code int priority@endcode : The render priority, just like the Z-axis. If priority < 0, the Layer will + * draw under the Component. If priority >= 0, the Layer will draw above the Component. The render order is based on * the number, from smaller to bigger. * * @warning You should extend it to make a custom Layer. NEVER USE THE INTERFACE DIRECTLY! diff --git a/src/engine/eventbus/EventBus.cpp b/src/engine/eventbus/EventBus.cpp index 992427a..5b827fd 100644 --- a/src/engine/eventbus/EventBus.cpp +++ b/src/engine/eventbus/EventBus.cpp @@ -5,7 +5,7 @@ #include "EventBus.h" #include #include "GLFW/glfw3.h" -#include "IEvent.h" +#include "IEvent.hpp" #include "IListener.h" #include @@ -20,6 +20,9 @@ namespace cmaterial::event { std::mutex EventBus::queueMutex; std::mutex EventBus::registerMutex; + /** + * @brief Let a Listener start to work. + */ void EventBus::addListener(IListener* listener) { if (!listener) return; @@ -37,6 +40,9 @@ namespace cmaterial::event { } } + /** + * Post an Event to the Event System, Listener will receive it later. + */ void EventBus::postEvent(IEvent* event) { if (!event) return; @@ -65,7 +71,7 @@ namespace cmaterial::event { } for (auto* handler : handlersToCall) { - if (event->getIsCancelled()) break; + if (event->isCancelled) break; handler->execute(event); } @@ -75,6 +81,9 @@ namespace cmaterial::event { return true; } + /** + * @brief Let a Listener stop working. + */ void EventBus::removeListener(IListener* listener) { if (!listener) return; diff --git a/src/engine/eventbus/EventBus.h b/src/engine/eventbus/EventBus.h index 213fe56..2810458 100644 --- a/src/engine/eventbus/EventBus.h +++ b/src/engine/eventbus/EventBus.h @@ -18,6 +18,13 @@ namespace cmaterial::event { class IHandler; class IEvent; + /** + * @brief The core of the Event System. + * + * @details --- DANGER ZONE --- + * @details @code bool dispatch()@endcode : This is used by the framework itself to update. DO NOT USE IT! + * @details @code void shutdown()@endcode : This is used by the framework itself to clean. DO NOT USE IT! + */ class EventBus { public: ~EventBus(); diff --git a/src/engine/eventbus/IEvent.cpp b/src/engine/eventbus/IEvent.cpp deleted file mode 100644 index a561904..0000000 --- a/src/engine/eventbus/IEvent.cpp +++ /dev/null @@ -1,11 +0,0 @@ -// -// Created by Earth_Studio on 2026/4/17. -// - -#include "IEvent.h" - -namespace cmaterial::event { - bool IEvent::getIsCancelled() const { - return isCancelled; - } -} diff --git a/src/engine/eventbus/IEvent.h b/src/engine/eventbus/IEvent.h deleted file mode 100644 index 0fafc5d..0000000 --- a/src/engine/eventbus/IEvent.h +++ /dev/null @@ -1,33 +0,0 @@ -// -// Created by Earth_Studio on 2026/4/16. -// - -#ifndef CMATERIAL_IEVENT_H -#define CMATERIAL_IEVENT_H - - -namespace cmaterial::event { - class IEvent { - public: - virtual ~IEvent() = default; - virtual const void* getEventType() const = 0; - - bool getIsCancelled() const; - - protected: - bool isCancelled = false; - -#define REGISTEREVENT(T) \ - static const void *getStaticType() { \ - static int dummy; \ - return &dummy; \ - } \ - const void *getEventType() const override { \ - return getStaticType(); \ - } - }; -} - - - -#endif //CMATERIAL_IEVENT_H diff --git a/src/engine/eventbus/IEvent.hpp b/src/engine/eventbus/IEvent.hpp new file mode 100644 index 0000000..8c092b4 --- /dev/null +++ b/src/engine/eventbus/IEvent.hpp @@ -0,0 +1,56 @@ +// +// Created by Earth_Studio on 2026/4/16. +// + +#ifndef CMATERIAL_IEVENT_H +#define CMATERIAL_IEVENT_H + + +namespace cmaterial::event { + /** + * @brief The base of the Event. + * @details You want to tell others something has happened, and this is the tool to make a message about + * "something". + * + * @details --- Require Macro --- + * @details @code REGISTEREVENT(EventClass)@endcode : You must write it in the first line of the class. + * + * @details --- Property --- + * @details @code any ...@endcode : These are properties you want to include in the message. You can add them by + * yourself. + * + * @details --- Advanced --- + * @details You can use @code struct@endcode to make an Event, because most of the time you may just want to send a + * message and give away some properties but don't want to give functions in the Event. + * + * @details --- DANGER ZONE --- + * @details @code bool isCancelled@endcode : Does the Event need to be cancelled? You shouldn't modify this because + * this should be modified by the Listener. + * + * @warning You should extend it to make a custom Event. NEVER USE THE INTERFACE DIRECTLY! + */ + class IEvent { + public: + virtual ~IEvent() = default; + virtual const void* getEventType() const = 0; + + /** + * @brief Does the Event need to be cancelled? + * @details In most cases, you won't need to change this. + */ + bool isCancelled = false; + +#define REGISTEREVENT(T) \ + static const void *getStaticType() { \ + static int dummy; \ + return &dummy; \ + } \ + const void *getEventType() const override { \ + return getStaticType(); \ + } + }; +} + + + +#endif //CMATERIAL_IEVENT_H diff --git a/src/engine/eventbus/IListener.h b/src/engine/eventbus/IListener.h index 6d9d76a..a7e7be2 100644 --- a/src/engine/eventbus/IListener.h +++ b/src/engine/eventbus/IListener.h @@ -37,6 +37,34 @@ namespace cmaterial::event { Order getOrder() const override { return order; } }; + /** + * @brief The base of the Listener. + * @details You want to do something after it happens, and this is the tool to make it. + * + * @details --- Require Macro --- + * @details @code EVENTHANDLER(EventClass, FunctionName)@endcode : You must write it before the function you want to + * make a Listener. + * @details @code EVENTHANDLER(EventClass, FunctionName, Order)@endcode : Same as the last one, but you can use the + * Order property to control the Listeners' execution order. @code HIGH@endcode means execute as early as possible, + * @code LOW@endcode means the opposite, @code DEFAULT@endcode is completely the same as the last macro. + * + * @details --- Tips --- + * @details Some Events will include some useful properties to use, you can get them like this: + * @details @code event->property@endcode + * @details You could implement several functions and mark them with different @code EVENTHANDLER@endcode to listen + * to different events in a single Listener. + * @details (e.g. @code onButtonClicked()@endcode + @code onButtonHover()@endcode -> @code ButtonListener@endcode ) + * + * @details --- Advanced --- + * @details You could use @code event->isCancelled = true@endcode to cancel an event. This will make lower-order + * Listeners stop doing their work. + * @details BUT, if two Listeners have the same Order, the first loaded Listener will cancel the second one. + * @details In most cases, you won't need this, because for GUI development, cancellation will usually not happen. + * @details UNLESS you use my EventBus to connect a custom backend (e.g. Network). But don't get me wrong — this is + * a usage I fully endorse, and it's even expected. + * + * @warning You should extend it to make a custom Listener. NEVER USE THE INTERFACE DIRECTLY! + */ class IListener { public: using Order = IHandler::Order; diff --git a/src/engine/eventbus/internal/event/ForceRedrawEvent.hpp b/src/engine/eventbus/internal/event/ForceRedrawEvent.hpp index bb2f64e..e07493e 100644 --- a/src/engine/eventbus/internal/event/ForceRedrawEvent.hpp +++ b/src/engine/eventbus/internal/event/ForceRedrawEvent.hpp @@ -4,7 +4,7 @@ #ifndef CMATERIAL_EMPTYEVENT_H #define CMATERIAL_EMPTYEVENT_H -#include "engine/eventbus/IEvent.h" +#include "engine/eventbus/IEvent.hpp" namespace cmaterial::event::internal { diff --git a/src/engine/window/IWindow.h b/src/engine/window/IWindow.h index b4207f3..5716149 100644 --- a/src/engine/window/IWindow.h +++ b/src/engine/window/IWindow.h @@ -30,8 +30,10 @@ namespace cmaterial::window { * @details --- Property --- * @details @code std::string name@endcode : The name of the window. * @details @code int width, height@endcode : The width and height of the window. - * @details @code ImGuiWindowFlags windowFlags@endcode : The flags of the ImGuiWindow. If you don't know what they are, DON'T touch them. - * @details @code ImVec4 backgroundColor@endcode : The background color of the window. Use ImVec4(r, g, b, alpha) to change it. + * @details @code ImGuiWindowFlags windowFlags@endcode : The flags of the ImGuiWindow. If you don't know what they + * are, DON'T touch them. + * @details @code ImVec4 backgroundColor@endcode : The background color of the window. Use + * @code ImVec4(r, g, b, alpha)@endcode to change it. * * @details --- DANGER ZONE --- * From cb40d4ec32274d63b5bee5d3900641ffa9d6dfae Mon Sep 17 00:00:00 2001 From: Earth_Studio Date: Sun, 10 May 2026 04:01:27 +0800 Subject: [PATCH 29/34] update docs --- src/engine/utils/ordered_map.hpp | 19 +++---------------- 1 file changed, 3 insertions(+), 16 deletions(-) diff --git a/src/engine/utils/ordered_map.hpp b/src/engine/utils/ordered_map.hpp index b0b1725..b7de810 100644 --- a/src/engine/utils/ordered_map.hpp +++ b/src/engine/utils/ordered_map.hpp @@ -9,39 +9,32 @@ #include #include -#include #include namespace cmaterial::utils { template class ordered_map { public: - // 为了支持 for(auto& [k, v] : map),必须定义这个内部类型 using ValueType = std::pair; using ListIter = typename std::list::iterator; - // 1. 插入:保持顺序,$O(1)$ std::pair insert(const K& key, V value) { auto it = lookup.find(key); if (it != lookup.end()) { - return {it->second, false}; // 已存在,不覆盖 + return {it->second, false}; } - // 插入链表末尾,保证遍历顺序 data.emplace_back(key, value); ListIter lastIt = std::prev(data.end()); - // 存入哈希表实现 O(1) 索引 lookup[key] = lastIt; return {lastIt, true}; } - // 🔮 新增:接受 std::pair 的重载 std::pair insert(const ValueType& value) { return insert(value.first, value.second); } - // 🔮 进阶:接受右值引用的重载(支持 move,更体面) std::pair insert(ValueType&& value) { auto it = lookup.find(value.first); if (it != lookup.end()) return {it->second, false}; @@ -52,7 +45,6 @@ namespace cmaterial::utils { return {lastIt, true}; } - // 2. 下标操作:最顺手的语法 V& operator[](const K& key) { auto it = lookup.find(key); if (it == lookup.end()) { @@ -61,23 +53,20 @@ namespace cmaterial::utils { return it->second->second; } - // 3. 删除:真正的 $O(1)$ 且维持链表完整性 size_t erase(const K& key) { auto it = lookup.find(key); if (it == lookup.end()) return 0; - data.erase(it->second); // $O(1)$ 从链表移除 - lookup.erase(it); // $O(1)$ 从哈希表移除 + data.erase(it->second); + lookup.erase(it); return 1; } - // 4. 迭代器:让 for(auto& [k, v] : map) 绝对有序 auto begin() noexcept { return data.begin(); } auto end() noexcept { return data.end(); } auto begin() const noexcept { return data.begin(); } auto end() const noexcept { return data.end(); } - // 5. C++20 风格接口 bool contains(const K& key) const { return lookup.contains(key); } bool empty() const noexcept { return lookup.empty(); } size_t size() const noexcept { return lookup.size(); } @@ -93,9 +82,7 @@ namespace cmaterial::utils { } private: - // 物理存储:链表负责“序” std::list data; - // 逻辑索引:哈希表负责“速” std::unordered_map lookup; }; } From b8bbd4e5436e33f59d4218a78b09f166c70a262c Mon Sep 17 00:00:00 2001 From: Earth_Studio Date: Fri, 15 May 2026 00:33:21 +0800 Subject: [PATCH 30/34] update docs, expand Animation System, fix serious lag issue --- .../animation/material/HoverAnimation.h | 26 ++++ .../animation/material/RippleAnimation.h | 32 +++++ .../component/CreatorButton/CreatorButton.h | 3 + .../MultiLayerButton/layer/BackgroundLayer.h | 4 +- .../component/material/Button/Button.cpp | 121 ++++++++---------- .../component/material/Button/Button.h | 74 ++--------- .../component/material/Button/layer/BGLayer.h | 35 +++++ .../material/Button/layer/RippleLayer.cpp | 38 ++++++ .../material/Button/layer/RippleLayer.h | 34 +++++ src/engine/animation/IAnimation.cpp | 10 +- src/engine/animation/IAnimation.h | 10 +- src/engine/animation/Player.cpp | 29 ++++- src/engine/animation/Player.h | 6 +- src/engine/component/IComponent.cpp | 6 +- src/engine/framework/Framework.cpp | 9 ++ version.rc | 2 +- 16 files changed, 292 insertions(+), 147 deletions(-) create mode 100644 src/content/animation/material/HoverAnimation.h create mode 100644 src/content/animation/material/RippleAnimation.h create mode 100644 src/content/component/material/Button/layer/BGLayer.h create mode 100644 src/content/component/material/Button/layer/RippleLayer.cpp create mode 100644 src/content/component/material/Button/layer/RippleLayer.h diff --git a/src/content/animation/material/HoverAnimation.h b/src/content/animation/material/HoverAnimation.h new file mode 100644 index 0000000..d4add74 --- /dev/null +++ b/src/content/animation/material/HoverAnimation.h @@ -0,0 +1,26 @@ +// +// Created by Earth_Studio on 2026/5/13. +// + +#ifndef CMATERIAL_HOVERANIMATION_H +#define CMATERIAL_HOVERANIMATION_H +#include +#include "tweeny.h" + +namespace cmaterial::component::material::animation { + class HoverAnimation : public cmaterial::animation::IAnimation { + public: + HoverAnimation(double* alpha, double target, INode* parent) : IAnimation(parent) { + // 150ms = 15000 steps + bind(alpha, new tweeny::tween(tweeny::from(*alpha).to(target).during(15000))); + } + ~HoverAnimation() override { + for (auto& pair : animationBinders) delete pair.second; + animationBinders.clear(); + } + }; +} + + + +#endif //CMATERIAL_HOVERANIMATION_H diff --git a/src/content/animation/material/RippleAnimation.h b/src/content/animation/material/RippleAnimation.h new file mode 100644 index 0000000..4926bea --- /dev/null +++ b/src/content/animation/material/RippleAnimation.h @@ -0,0 +1,32 @@ +// +// Created by Earth_Studio on 2026/5/13. +// + +#ifndef CMATERIAL_RIPPLEANIMATION_H +#define CMATERIAL_RIPPLEANIMATION_H +#include "engine/animation/IAnimation.h" +#include "tweeny.h" + + +namespace cmaterial::component::material::animation { + class RippleAnimation : public cmaterial::animation::IAnimation { + public: + RippleAnimation(double* progress, double* alpha, INode* node) : IAnimation(node) { + progressTween = tweeny::from(0.0).to(1.0).during(40000).via(tweeny::easing::quadraticOut); + alphaTween = tweeny::from(0.12).to(0.0).during(40000).via(tweeny::easing::quadraticOut); + + bind(progress, &progressTween); + bind(alpha, &alphaTween); + } + ~RippleAnimation() override { + animationBinders.clear(); + } + + private: + tweeny::tween progressTween; + tweeny::tween alphaTween; + }; +} + + +#endif //CMATERIAL_RIPPLEANIMATION_H diff --git a/src/content/component/CreatorButton/CreatorButton.h b/src/content/component/CreatorButton/CreatorButton.h index 3afab6e..089e562 100644 --- a/src/content/component/CreatorButton/CreatorButton.h +++ b/src/content/component/CreatorButton/CreatorButton.h @@ -6,6 +6,8 @@ #define CMATERIAL_CREATORBUTTON_H +#include + #include "content/logic/event/SpawnWindowRequest.hpp" #include "engine/component/IComponent.h" #include "engine/eventbus/EventBus.h" @@ -18,6 +20,7 @@ namespace cmaterial::component { void update(ImGuiIO *io) override { if (ImGui::Button("SpawnWindow")) { + std::cerr << "SpawnWindow" << std::endl; static int count = 0; auto* req = new event::SpawnWindowRequest(); req->title = "Dynamic Window " + std::to_string(++count); diff --git a/src/content/component/MultiLayerButton/layer/BackgroundLayer.h b/src/content/component/MultiLayerButton/layer/BackgroundLayer.h index 4ae09c7..11ee84c 100644 --- a/src/content/component/MultiLayerButton/layer/BackgroundLayer.h +++ b/src/content/component/MultiLayerButton/layer/BackgroundLayer.h @@ -2,8 +2,8 @@ // Created by Earth_Studio on 2026/4/27. // -#ifndef CMATERIAL_BACKGROUNDLAYER_H -#define CMATERIAL_BACKGROUNDLAYER_H +#ifndef CMATERIAL_MULTILAYERBUTTON_BACKGROUNDLAYER_H +#define CMATERIAL_MULTILAYERBUTTON_BACKGROUNDLAYER_H #include "engine/component/ILayer.hpp" diff --git a/src/content/component/material/Button/Button.cpp b/src/content/component/material/Button/Button.cpp index 87cf6f8..e19a1e7 100644 --- a/src/content/component/material/Button/Button.cpp +++ b/src/content/component/material/Button/Button.cpp @@ -1,5 +1,5 @@ // -// Created by Earth_Studio on 2026/4/27. +// Created by Earth_Studio on 2026/5/12. // #include "Button.h" @@ -7,87 +7,72 @@ #include "engine/animation/Player.h" #include "engine/eventbus/EventBus.h" +#include "content/component/material/Button/layer/RippleLayer.h" + namespace cmaterial::component::material { - Button::Button() - : hoverAnim(&stateAlpha), rippleAnim(&rippleRadius, &rippleAlpha), bgLayer(this), fgLayer(this) - { + Button::Button() { + m_bgLayer = new layer::BGLayer(); + addLayer(m_bgLayer); + } - // 把图层挂载进组件的流水线 - bgLayer.parent = this; - fgLayer.parent = this; - hoverAnim.parent = this; - rippleAnim.parent = this; - addLayer(&bgLayer); - addLayer(&fgLayer); - animation::Player::reverse(&hoverAnim); + Button::~Button() { + if (m_hoverAnim) { + cmaterial::animation::Player::stop(m_hoverAnim); + delete m_hoverAnim; + } } - // 🔮 1. 逻辑与交互中枢 void Button::update(ImGuiIO *io) { - // 放置隐形按钮,夺取 120x40 的交互判定权 - ImGui::InvisibleButton(name.c_str(), buttonSize); + m_bgLayer->primaryColor = this->primaryColor; + m_bgLayer->onPrimaryColor = this->onPrimaryColor; - bool isHovered = ImGui::IsItemHovered(); + ImVec2 size(ImGui::CalcTextSize(name.c_str()).x + 48.0f, 40.0f); + ImVec2 p_min = ImGui::GetCursorScreenPos(); + ImGui::InvisibleButton(name.c_str(), size); - // 💡 状态机:处理 Hover 动画的平滑正反转 - if (isHovered != lastHoverState) { - lastHoverState = isHovered; - if (isHovered) { - animation::Player::reverse(&hoverAnim); - animation::Player::play(&hoverAnim); - } else { - animation::Player::reverse(&hoverAnim); - animation::Player::play(&hoverAnim); - } - } + bool hovered = ImGui::IsItemHovered(); + bool active = ImGui::IsItemActive(); + bool clicked = ImGui::IsItemClicked(); - // 💡 状态机:处理 Ripple 动画的爆炸 - if (ImGui::IsItemClicked()) { - rippleCenter = io->MousePos; - // 计算对角线作为最大半径,保证波纹填满按钮 - double maxR = std::sqrt(buttonSize.x * buttonSize.x + buttonSize.y * buttonSize.y); - rippleAnim.setMaxRadius(maxR); - rippleAnim.reset(); // 回归 0 - animation::Player::play(&rippleAnim); + if (hovered && !m_isHovering) { + m_isHovering = true; + if (m_hoverAnim) { cmaterial::animation::Player::stop(m_hoverAnim); delete m_hoverAnim; } + m_hoverAnim = new animation::HoverAnimation(&m_bgLayer->hoverAlpha, 1.0, this); + m_hoverAnim->parent = this; + cmaterial::animation::Player::play(m_hoverAnim); + } else if (!hovered && m_isHovering) { + m_isHovering = false; + if (m_hoverAnim) { cmaterial::animation::Player::stop(m_hoverAnim); delete m_hoverAnim; } + m_hoverAnim = new animation::HoverAnimation(&m_bgLayer->hoverAlpha, 0.0, this); + m_hoverAnim->parent = this; + cmaterial::animation::Player::play(m_hoverAnim); } - } - // 🔮 2. 底层排面:背景 - void Button::BgLayer::render(ImDrawList* drawList, ImVec2 startPos, ImVec2 size) { - // M3 标准 Primary 紫色:#6750A4 - ImU32 primaryColor = IM_COL32(103, 80, 164, 255); - // 高度一半即为胶囊圆角 - float rounding = size.y * 0.5f; - - drawList->AddRectFilled(startPos, ImVec2(startPos.x + size.x, startPos.y + size.y), primaryColor, rounding); - } - - // 🔮 3. 顶层排面:状态覆盖、波纹与文字 - void Button::FgLayer::render(ImDrawList* drawList, ImVec2 startPos, ImVec2 size) { - float rounding = size.y * 0.5f; - ImVec2 endPos = ImVec2(startPos.x + size.x, startPos.y + size.y); + if (clicked) { + addLayer(new layer::RippleLayer(io->MousePos, this->onPrimaryColor)); + } - // A. 绘制 State Overlay (Hover 状态的白光叠加) - if (btn->stateAlpha > 0.001) { - ImU32 overlayColor = IM_COL32(255, 255, 255, (int)(btn->stateAlpha * 255)); - drawList->AddRectFilled(startPos, endPos, overlayColor, rounding); + for (int i = (int)layersBefore.size() - 1; i >= 0; --i) { + auto* r = dynamic_cast(layersBefore[i]); + if (r && r->isAnimationFinished()) { + removeLayer(r); + } } - // B. 绘制 Ripple 波纹 - if (btn->rippleAlpha > 0.001) { - // 物理裁剪:波纹绝对不会溢出胶囊圆角按钮! - drawList->PushClipRect(startPos, endPos, true); - ImU32 rippleColor = IM_COL32(255, 255, 255, (int)(btn->rippleAlpha * 255)); - drawList->AddCircleFilled(btn->rippleCenter, (float)btn->rippleRadius, rippleColor); - drawList->PopClipRect(); + if (active && !m_isPressed) m_isPressed = true; + else if (!active && m_isPressed) { + m_isPressed = false; + if (hovered) { + // auto* e = new event::ButtonClickedEvent(); + // e->buttonName = this->name; + // e->buttonInstance = this; + // event::EventBus::postEvent(e); + } } - // C. 绘制居中文字 - ImVec2 textSize = ImGui::CalcTextSize(btn->name.c_str()); - ImVec2 textPos = ImVec2( - startPos.x + (size.x - textSize.x) * 0.5f, - startPos.y + (size.y - textSize.y) * 0.5f + ImGui::GetWindowDrawList()->AddText( + ImVec2(p_min.x + 24.0f, p_min.y + (40.0f - ImGui::CalcTextSize(name.c_str()).y) / 2.0f), + ImGui::GetColorU32(onPrimaryColor), name.c_str() ); - drawList->AddText(textPos, IM_COL32(255, 255, 255, 255), btn->name.c_str()); } -} // namespace cmaterial::component::material +} \ No newline at end of file diff --git a/src/content/component/material/Button/Button.h b/src/content/component/material/Button/Button.h index a1aa76f..c6e7e22 100644 --- a/src/content/component/material/Button/Button.h +++ b/src/content/component/material/Button/Button.h @@ -1,83 +1,29 @@ // -// Created by Earth_Studio on 2026/4/27. +// Created by Earth_Studio on 2026/5/12. // #ifndef CMATERIAL_BUTTON_H #define CMATERIAL_BUTTON_H - - +#include "content/animation/material/HoverAnimation.h" #include "engine/component/IComponent.h" - -#include - -#include "engine/animation/IAnimation.h" - +#include "layer/BGLayer.h" namespace cmaterial::component::material { class Button : public IComponent { public: Button(); - ~Button() = default; - + ~Button() override; void update(ImGuiIO *io) override; - // 🔮 物理状态:暴露给 Layer 和 Animation - double stateAlpha = 0.0; // Hover 的 8% 叠加透明度 - double rippleRadius = 0.0; // 涟漪半径 - double rippleAlpha = 0.0; // 涟漪透明度 - ImVec2 rippleCenter = {0, 0}; - ImVec2 buttonSize = {120, 40}; // MD3 标准高度 40dp + ImVec4 primaryColor = ImVec4(0.404f, 0.314f, 0.643f, 1.0f); + ImVec4 onPrimaryColor = ImVec4(1.0f, 1.0f, 1.0f, 1.0f); private: - bool lastHoverState = false; - - // ---------------------------------------------------- - // 🔮 动画定义区:利用你写的 100000.0f 高精度时间轴 - // ---------------------------------------------------- - class HoverAnimation : public animation::IAnimation { - public: - tweeny::tween alphaTween; - HoverAnimation(double* targetAlpha) { - // 200ms (20000步),透明度从 0 变到 0.08 (MD3 Hover 规范) - alphaTween = tweeny::from(0.0).to(0.08).during(20000).via(tweeny::easing::cubicOut); - bind(targetAlpha, &alphaTween); - } - } hoverAnim; - - class RippleAnimation : public animation::IAnimation { - public: - tweeny::tween radiusTween; - tweeny::tween alphaTween; - RippleAnimation(double* r, double* a) { - // 300ms (30000步),半径变大,透明度先深后浅 - radiusTween = tweeny::from(0.0).to(150.0).during(30000).via(tweeny::easing::quadraticOut); - alphaTween = tweeny::from(0.12).to(0.0).during(30000).via(tweeny::easing::linear); - bind(r, &radiusTween); - bind(a, &alphaTween); - } - // 每次点击时动态更新最大半径 - void setMaxRadius(double maxR) { - radiusTween.seek(0); // 回归起点 - radiusTween = tweeny::from(0.0).to(maxR).during(30000).via(tweeny::easing::quadraticOut); - } - } rippleAnim; - - // ---------------------------------------------------- - // 🔮 图层定义区:严守分段渲染契约 - // ---------------------------------------------------- - class BgLayer : public ILayer { - Button* btn; - public: - BgLayer(Button* b) : btn(b) { priority = -10; } // 垫底 - void render(ImDrawList* drawList, ImVec2 startPos, ImVec2 size) override; - } bgLayer; + bool m_isHovering = false; + bool m_isPressed = false; - class FgLayer : public ILayer { - Button* btn; - public: - FgLayer(Button* b) : btn(b) { priority = 10; } // 盖在上面 - void render(ImDrawList* drawList, ImVec2 startPos, ImVec2 size) override; - } fgLayer; + layer::BGLayer * m_bgLayer = nullptr; + animation::HoverAnimation* m_hoverAnim = nullptr; }; } diff --git a/src/content/component/material/Button/layer/BGLayer.h b/src/content/component/material/Button/layer/BGLayer.h new file mode 100644 index 0000000..901514a --- /dev/null +++ b/src/content/component/material/Button/layer/BGLayer.h @@ -0,0 +1,35 @@ +// +// Created by Earth_Studio on 2026/5/13. +// + +#ifndef CMATERIAL_BACKGROUNDLAYER_H +#define CMATERIAL_BACKGROUNDLAYER_H +#include "engine/component/ILayer.hpp" +#include "imgui.h" + + +namespace cmaterial::component::material::layer { + class BGLayer : public ILayer { + public: + BGLayer() { priority = -2; } + void render(ImDrawList* drawList, ImVec2 startPos, ImVec2 size) override { + float mix = static_cast(hoverAlpha) * 0.08f; + ImVec4 finalColor = ImVec4( + primaryColor.x * (1.0f - mix) + onPrimaryColor.x * mix, + primaryColor.y * (1.0f - mix) + onPrimaryColor.y * mix, + primaryColor.z * (1.0f - mix) + onPrimaryColor.z * mix, + primaryColor.w + ); + drawList->AddRectFilled(startPos, ImVec2(startPos.x + size.x, startPos.y + size.y), + ImGui::GetColorU32(finalColor), size.y / 2.0f); + } + + double hoverAlpha = 0.0; + ImVec4 primaryColor = ImVec4(0.404f, 0.314f, 0.643f, 1.0f); + ImVec4 onPrimaryColor = ImVec4(1.0f, 1.0f, 1.0f, 1.0f); + }; +} + + + +#endif //CMATERIAL_BACKGROUNDLAYER_H diff --git a/src/content/component/material/Button/layer/RippleLayer.cpp b/src/content/component/material/Button/layer/RippleLayer.cpp new file mode 100644 index 0000000..bc9c977 --- /dev/null +++ b/src/content/component/material/Button/layer/RippleLayer.cpp @@ -0,0 +1,38 @@ +// +// Created by Earth_Studio on 2026/5/13. +// + +#include "RippleLayer.h" + +#include "cmath" +#include "imgui.h" + +#include "engine/animation/Player.h" + +namespace cmaterial::component::material::layer { + RippleLayer::RippleLayer(ImVec2 pos, ImVec4 col) : m_clickPos(pos), m_color(col), m_anim(&m_progress, &m_alpha, this) { + priority = -1; + + cmaterial::animation::Player::play(&m_anim); + } + + RippleLayer::~RippleLayer() { + cmaterial::animation::Player::stop(&m_anim); + } + + void RippleLayer::render(ImDrawList* drawList, ImVec2 startPos, ImVec2 size) { + if (m_alpha <= 0.0) return; + + ImVec2 maxPos(startPos.x + size.x, startPos.y + size.y); + drawList->PushClipRect(startPos, maxPos, true); + + float dx = std::max(m_clickPos.x - startPos.x, maxPos.x - m_clickPos.x); + float dy = std::max(m_clickPos.y - startPos.y, maxPos.y - m_clickPos.y); + float maxR = std::sqrt(dx * dx + dy * dy); + + ImU32 c = ImGui::GetColorU32(ImVec4(m_color.x, m_color.y, m_color.z, static_cast(m_alpha))); + drawList->AddCircleFilled(m_clickPos, maxR * static_cast(m_progress), c, 32); + + drawList->PopClipRect(); + } +} \ No newline at end of file diff --git a/src/content/component/material/Button/layer/RippleLayer.h b/src/content/component/material/Button/layer/RippleLayer.h new file mode 100644 index 0000000..f85e385 --- /dev/null +++ b/src/content/component/material/Button/layer/RippleLayer.h @@ -0,0 +1,34 @@ +// +// Created by Earth_Studio on 2026/5/13. +// + +#ifndef CMATERIAL_RIPPLELAYER_H +#define CMATERIAL_RIPPLELAYER_H +#include "engine/component/ILayer.hpp" +#include "imgui.h" + +#include "content/animation/material/RippleAnimation.h" + +namespace cmaterial::component::material::layer { + class RippleLayer : public ILayer { + public: + RippleLayer(ImVec2 pos, ImVec4 col); + ~RippleLayer() override; + + void render(ImDrawList* drawList, ImVec2 startPos, ImVec2 size) override; + + bool isAnimationFinished() const { return m_anim._isFinished; } + + private: + double m_progress = 0.0; + double m_alpha = 0.0; + ImVec2 m_clickPos; + ImVec4 m_color; + + animation::RippleAnimation m_anim; + }; +} + + + +#endif //CMATERIAL_RIPPLELAYER_H diff --git a/src/engine/animation/IAnimation.cpp b/src/engine/animation/IAnimation.cpp index d25112a..434d0c8 100644 --- a/src/engine/animation/IAnimation.cpp +++ b/src/engine/animation/IAnimation.cpp @@ -10,6 +10,10 @@ #include "tween.h" namespace cmaterial::animation { + IAnimation::IAnimation(INode* parent) { + this->parent = parent; + } + void IAnimation::reset() { for (auto binder : animationBinders) { if (_isReverse) @@ -19,16 +23,16 @@ namespace cmaterial::animation { } } - std::unordered_map *>* IAnimation::getAnimationBinders() { + std::vector *>>* IAnimation::getAnimationBinders() { return &animationBinders; } void IAnimation::bind(double* property, tweeny::tween* tween) { - animationBinders[property] = tween; + animationBinders.push_back({property, tween}); } IAnimation::~IAnimation() { - animationBinders.clear(); Player::stop(this); + animationBinders.clear(); } } \ No newline at end of file diff --git a/src/engine/animation/IAnimation.h b/src/engine/animation/IAnimation.h index d2bd632..fb1001d 100644 --- a/src/engine/animation/IAnimation.h +++ b/src/engine/animation/IAnimation.h @@ -28,22 +28,28 @@ namespace cmaterial::animation { * @details --- Helper --- * @details @code void bind(double*, tween*)@endcode : Bind a property and a tween. * + * @details --- DANGER ZONE --- + * @details @code bool _isFinished@endcode : Is the animation finished? You should read it ONLY! + * @details @code bool _isReverse@endcode : Is the animation reversed? You should read it ONLY! + * * @warning You should extend it to make a custom Animation. NEVER USE THE INTERFACE DIRECTLY! */ class IAnimation : public utils::INode { public: + IAnimation(INode* parent); virtual ~IAnimation(); void reset(); - std::unordered_map *>* getAnimationBinders(); + std::vector *>>* getAnimationBinders(); bool isLoop = false; + bool _isFinished = false; bool _isReverse = false; protected: void bind(double* property, tweeny::tween* tween); - std::unordered_map *> animationBinders; + std::vector *>> animationBinders; }; } diff --git a/src/engine/animation/Player.cpp b/src/engine/animation/Player.cpp index 7eaa3cd..b789e14 100644 --- a/src/engine/animation/Player.cpp +++ b/src/engine/animation/Player.cpp @@ -5,7 +5,6 @@ #include "Player.h" #include -#include #include "glad/gl.h" #include "GLFW/glfw3.h" @@ -15,15 +14,33 @@ namespace cmaterial::animation { std::vector Player::finishedAnimations; void Player::play(IAnimation *animation) { - if (!animation) return; + if (animation == nullptr) return; - if (std::find(playingAnimations.begin(), playingAnimations.end(), animation) != playingAnimations.end()) { + if (std::ranges::find(playingAnimations, animation) != playingAnimations.end()) { return; } playingAnimations.push_back(animation); } + void Player::forward(IAnimation *animation) { + if (!animation) return; + + for (auto binder : *animation->getAnimationBinders()) { + binder.second->forward(); + } + animation->_isReverse = false; + } + + void Player::backward(IAnimation *animation) { + if (!animation) return; + + for (auto binder : *animation->getAnimationBinders()) { + binder.second->backward(); + } + animation->_isReverse = true; + } + void Player::reverse(IAnimation *animation) { if (!animation) return; @@ -49,7 +66,10 @@ namespace cmaterial::animation { double currentTime = glfwGetTime(); double dt = currentTime - lastTime; lastTime = currentTime; - if (dt <= 0.0) dt = 0.00001; + + if (dt > 0.1 || dt <= 0.0) + dt = 0.00001; + int dtTime = static_cast(dt * 100000.0); // 1 step = 0.01 ms for (auto animation : playingAnimations) { @@ -79,6 +99,7 @@ namespace cmaterial::animation { for (auto animation : finishedAnimations) { std::erase(playingAnimations, animation); + animation->_isFinished = true; } finishedAnimations.clear(); diff --git a/src/engine/animation/Player.h b/src/engine/animation/Player.h index 71e2f2d..a8af19c 100644 --- a/src/engine/animation/Player.h +++ b/src/engine/animation/Player.h @@ -16,8 +16,8 @@ namespace cmaterial::animation { * @brief The Animation player. * @details You will play all the Animation in here. * - * @warning No extra description because this is not finished. - * @todo Rebuild with consumer mode. + * @details --- Danger Zone --- + * @details @code update()@endcode : This is used by the framework itself to update. DO NOT USE IT! */ class Player { public: @@ -25,6 +25,8 @@ namespace cmaterial::animation { ~Player(); static void play(IAnimation* animation); + static void forward(IAnimation* animation); + static void backward(IAnimation* animation); static void reverse(IAnimation* animation); static void stop(IAnimation* animation); diff --git a/src/engine/component/IComponent.cpp b/src/engine/component/IComponent.cpp index 5a3aa47..59d2172 100644 --- a/src/engine/component/IComponent.cpp +++ b/src/engine/component/IComponent.cpp @@ -38,9 +38,11 @@ namespace cmaterial::component { if (layer == nullptr) return; + layer->parent = this; + std::vector& layers = (layer->priority < 0) ? layersBefore : layersAfter; - auto it = std::lower_bound(layers.begin(), layers.end(), layer, + auto it = std::ranges::lower_bound(layers, layer, [](ILayer* a, ILayer* b) { return a->priority < b->priority; }); @@ -55,6 +57,8 @@ namespace cmaterial::component { std::erase(layersBefore, layer); std::erase(layersAfter, layer); + + delete layer; } /** diff --git a/src/engine/framework/Framework.cpp b/src/engine/framework/Framework.cpp index a42dadc..fa1b964 100644 --- a/src/engine/framework/Framework.cpp +++ b/src/engine/framework/Framework.cpp @@ -21,6 +21,8 @@ #include #include +#include "backends/imgui_impl_glfw.h" +#include "backends/imgui_impl_opengl3.h" #include "engine/animation/Player.h" #include "engine/eventbus/internal/listener/ForceRedrawListener.hpp" @@ -82,6 +84,8 @@ namespace cmaterial { double nextFrameTime = glfwGetTime(); + bool isNotFirstRender = false; + EventBus::postEvent(new event::internal::ForceRedrawEvent); while (!glfwWindowShouldClose(hiddenWindow) && !windows.empty()) { bool isEventBusy = EventBus::dispatch(); @@ -103,6 +107,11 @@ namespace cmaterial { nextFrameTime = now + frameDuration; glfwWaitEvents(); + } else { + if (!isNotFirstRender) + isNotFirstRender = true; + else + glfwPollEvents(); } ImFontAtlasUpdateNewFrame(fontAtlas, ++globalFrameCount, true); diff --git a/version.rc b/version.rc index 0085c10..40d7387 100644 --- a/version.rc +++ b/version.rc @@ -37,6 +37,6 @@ BEGIN END BLOCK "VarFileInfo" BEGIN - VALUE "Translation", 0x0409, 0x04E4 // 英语美国 , 多字节->Unicode + VALUE "Translation", 0x0409, 0x04E4 END END \ No newline at end of file From 94874576657642d33c2ff98c822422c9a68147dc Mon Sep 17 00:00:00 2001 From: Earth_Studio Date: Fri, 15 May 2026 00:33:21 +0800 Subject: [PATCH 31/34] update docs, expand Animation System, fix serious lag issue --- .../animation/material/HoverAnimation.h | 26 ++++ .../animation/material/RippleAnimation.h | 32 +++++ .../component/CreatorButton/CreatorButton.h | 3 + .../MultiLayerButton/layer/BackgroundLayer.h | 4 +- .../component/material/Button/Button.cpp | 123 ++++++++---------- .../component/material/Button/Button.h | 74 ++--------- .../component/material/Button/layer/BGLayer.h | 35 +++++ .../material/Button/layer/RippleLayer.cpp | 44 +++++++ .../material/Button/layer/RippleLayer.h | 34 +++++ src/engine/animation/IAnimation.cpp | 10 +- src/engine/animation/IAnimation.h | 10 +- src/engine/animation/Player.cpp | 29 ++++- src/engine/animation/Player.h | 6 +- src/engine/component/IComponent.cpp | 6 +- src/engine/framework/Framework.cpp | 9 ++ version.rc | 2 +- 16 files changed, 299 insertions(+), 148 deletions(-) create mode 100644 src/content/animation/material/HoverAnimation.h create mode 100644 src/content/animation/material/RippleAnimation.h create mode 100644 src/content/component/material/Button/layer/BGLayer.h create mode 100644 src/content/component/material/Button/layer/RippleLayer.cpp create mode 100644 src/content/component/material/Button/layer/RippleLayer.h diff --git a/src/content/animation/material/HoverAnimation.h b/src/content/animation/material/HoverAnimation.h new file mode 100644 index 0000000..d4add74 --- /dev/null +++ b/src/content/animation/material/HoverAnimation.h @@ -0,0 +1,26 @@ +// +// Created by Earth_Studio on 2026/5/13. +// + +#ifndef CMATERIAL_HOVERANIMATION_H +#define CMATERIAL_HOVERANIMATION_H +#include +#include "tweeny.h" + +namespace cmaterial::component::material::animation { + class HoverAnimation : public cmaterial::animation::IAnimation { + public: + HoverAnimation(double* alpha, double target, INode* parent) : IAnimation(parent) { + // 150ms = 15000 steps + bind(alpha, new tweeny::tween(tweeny::from(*alpha).to(target).during(15000))); + } + ~HoverAnimation() override { + for (auto& pair : animationBinders) delete pair.second; + animationBinders.clear(); + } + }; +} + + + +#endif //CMATERIAL_HOVERANIMATION_H diff --git a/src/content/animation/material/RippleAnimation.h b/src/content/animation/material/RippleAnimation.h new file mode 100644 index 0000000..4926bea --- /dev/null +++ b/src/content/animation/material/RippleAnimation.h @@ -0,0 +1,32 @@ +// +// Created by Earth_Studio on 2026/5/13. +// + +#ifndef CMATERIAL_RIPPLEANIMATION_H +#define CMATERIAL_RIPPLEANIMATION_H +#include "engine/animation/IAnimation.h" +#include "tweeny.h" + + +namespace cmaterial::component::material::animation { + class RippleAnimation : public cmaterial::animation::IAnimation { + public: + RippleAnimation(double* progress, double* alpha, INode* node) : IAnimation(node) { + progressTween = tweeny::from(0.0).to(1.0).during(40000).via(tweeny::easing::quadraticOut); + alphaTween = tweeny::from(0.12).to(0.0).during(40000).via(tweeny::easing::quadraticOut); + + bind(progress, &progressTween); + bind(alpha, &alphaTween); + } + ~RippleAnimation() override { + animationBinders.clear(); + } + + private: + tweeny::tween progressTween; + tweeny::tween alphaTween; + }; +} + + +#endif //CMATERIAL_RIPPLEANIMATION_H diff --git a/src/content/component/CreatorButton/CreatorButton.h b/src/content/component/CreatorButton/CreatorButton.h index 3afab6e..089e562 100644 --- a/src/content/component/CreatorButton/CreatorButton.h +++ b/src/content/component/CreatorButton/CreatorButton.h @@ -6,6 +6,8 @@ #define CMATERIAL_CREATORBUTTON_H +#include + #include "content/logic/event/SpawnWindowRequest.hpp" #include "engine/component/IComponent.h" #include "engine/eventbus/EventBus.h" @@ -18,6 +20,7 @@ namespace cmaterial::component { void update(ImGuiIO *io) override { if (ImGui::Button("SpawnWindow")) { + std::cerr << "SpawnWindow" << std::endl; static int count = 0; auto* req = new event::SpawnWindowRequest(); req->title = "Dynamic Window " + std::to_string(++count); diff --git a/src/content/component/MultiLayerButton/layer/BackgroundLayer.h b/src/content/component/MultiLayerButton/layer/BackgroundLayer.h index 4ae09c7..11ee84c 100644 --- a/src/content/component/MultiLayerButton/layer/BackgroundLayer.h +++ b/src/content/component/MultiLayerButton/layer/BackgroundLayer.h @@ -2,8 +2,8 @@ // Created by Earth_Studio on 2026/4/27. // -#ifndef CMATERIAL_BACKGROUNDLAYER_H -#define CMATERIAL_BACKGROUNDLAYER_H +#ifndef CMATERIAL_MULTILAYERBUTTON_BACKGROUNDLAYER_H +#define CMATERIAL_MULTILAYERBUTTON_BACKGROUNDLAYER_H #include "engine/component/ILayer.hpp" diff --git a/src/content/component/material/Button/Button.cpp b/src/content/component/material/Button/Button.cpp index 87cf6f8..6e19ca5 100644 --- a/src/content/component/material/Button/Button.cpp +++ b/src/content/component/material/Button/Button.cpp @@ -1,5 +1,5 @@ // -// Created by Earth_Studio on 2026/4/27. +// Created by Earth_Studio on 2026/5/12. // #include "Button.h" @@ -7,87 +7,72 @@ #include "engine/animation/Player.h" #include "engine/eventbus/EventBus.h" +#include "content/component/material/Button/layer/RippleLayer.h" + namespace cmaterial::component::material { - Button::Button() - : hoverAnim(&stateAlpha), rippleAnim(&rippleRadius, &rippleAlpha), bgLayer(this), fgLayer(this) - { + Button::Button() { + m_bgLayer = new layer::BGLayer(); + addLayer(m_bgLayer); + } - // 把图层挂载进组件的流水线 - bgLayer.parent = this; - fgLayer.parent = this; - hoverAnim.parent = this; - rippleAnim.parent = this; - addLayer(&bgLayer); - addLayer(&fgLayer); - animation::Player::reverse(&hoverAnim); + Button::~Button() { + if (m_hoverAnim) { + cmaterial::animation::Player::stop(m_hoverAnim); + delete m_hoverAnim; + } } - // 🔮 1. 逻辑与交互中枢 void Button::update(ImGuiIO *io) { - // 放置隐形按钮,夺取 120x40 的交互判定权 - ImGui::InvisibleButton(name.c_str(), buttonSize); + m_bgLayer->primaryColor = this->primaryColor; + m_bgLayer->onPrimaryColor = this->onPrimaryColor; - bool isHovered = ImGui::IsItemHovered(); + ImVec2 size(ImGui::CalcTextSize(name.c_str()).x + 48.0f, 40.0f); + ImVec2 p_min = ImGui::GetCursorScreenPos(); + ImGui::InvisibleButton(name.c_str(), size); - // 💡 状态机:处理 Hover 动画的平滑正反转 - if (isHovered != lastHoverState) { - lastHoverState = isHovered; - if (isHovered) { - animation::Player::reverse(&hoverAnim); - animation::Player::play(&hoverAnim); - } else { - animation::Player::reverse(&hoverAnim); - animation::Player::play(&hoverAnim); - } - } + bool hovered = ImGui::IsItemHovered(); + bool active = ImGui::IsItemActive(); + bool clicked = ImGui::IsItemClicked(); - // 💡 状态机:处理 Ripple 动画的爆炸 - if (ImGui::IsItemClicked()) { - rippleCenter = io->MousePos; - // 计算对角线作为最大半径,保证波纹填满按钮 - double maxR = std::sqrt(buttonSize.x * buttonSize.x + buttonSize.y * buttonSize.y); - rippleAnim.setMaxRadius(maxR); - rippleAnim.reset(); // 回归 0 - animation::Player::play(&rippleAnim); + if (hovered && !m_isHovering) { + m_isHovering = true; + if (m_hoverAnim) { cmaterial::animation::Player::stop(m_hoverAnim); delete m_hoverAnim; } + m_hoverAnim = new animation::HoverAnimation(&m_bgLayer->hoverAlpha, 1.0, this); + m_hoverAnim->parent = this; + cmaterial::animation::Player::play(m_hoverAnim); + } else if (!hovered && m_isHovering) { + m_isHovering = false; + if (m_hoverAnim) { cmaterial::animation::Player::stop(m_hoverAnim); delete m_hoverAnim; } + m_hoverAnim = new animation::HoverAnimation(&m_bgLayer->hoverAlpha, 0.0, this); + m_hoverAnim->parent = this; + cmaterial::animation::Player::play(m_hoverAnim); } - } - // 🔮 2. 底层排面:背景 - void Button::BgLayer::render(ImDrawList* drawList, ImVec2 startPos, ImVec2 size) { - // M3 标准 Primary 紫色:#6750A4 - ImU32 primaryColor = IM_COL32(103, 80, 164, 255); - // 高度一半即为胶囊圆角 - float rounding = size.y * 0.5f; - - drawList->AddRectFilled(startPos, ImVec2(startPos.x + size.x, startPos.y + size.y), primaryColor, rounding); - } - - // 🔮 3. 顶层排面:状态覆盖、波纹与文字 - void Button::FgLayer::render(ImDrawList* drawList, ImVec2 startPos, ImVec2 size) { - float rounding = size.y * 0.5f; - ImVec2 endPos = ImVec2(startPos.x + size.x, startPos.y + size.y); - - // A. 绘制 State Overlay (Hover 状态的白光叠加) - if (btn->stateAlpha > 0.001) { - ImU32 overlayColor = IM_COL32(255, 255, 255, (int)(btn->stateAlpha * 255)); - drawList->AddRectFilled(startPos, endPos, overlayColor, rounding); + if (clicked) { + addLayer(new layer::RippleLayer(io->MousePos, this->onPrimaryColor)); } - // B. 绘制 Ripple 波纹 - if (btn->rippleAlpha > 0.001) { - // 物理裁剪:波纹绝对不会溢出胶囊圆角按钮! - drawList->PushClipRect(startPos, endPos, true); - ImU32 rippleColor = IM_COL32(255, 255, 255, (int)(btn->rippleAlpha * 255)); - drawList->AddCircleFilled(btn->rippleCenter, (float)btn->rippleRadius, rippleColor); - drawList->PopClipRect(); + // for (int i = layersBefore.size() - 1; i >= 0; --i) { + // auto r = (layer::RippleLayer*) layersBefore[i]; + // if (r && r->isAnimationFinished()) { + // removeLayer(r); + // } + // } + + if (active && !m_isPressed) m_isPressed = true; + else if (!active && m_isPressed) { + m_isPressed = false; + if (hovered) { + // auto* e = new event::ButtonClickedEvent(); + // e->buttonName = this->name; + // e->buttonInstance = this; + // event::EventBus::postEvent(e); + } } - // C. 绘制居中文字 - ImVec2 textSize = ImGui::CalcTextSize(btn->name.c_str()); - ImVec2 textPos = ImVec2( - startPos.x + (size.x - textSize.x) * 0.5f, - startPos.y + (size.y - textSize.y) * 0.5f + ImGui::GetWindowDrawList()->AddText( + ImVec2(p_min.x + 24.0f, p_min.y + (40.0f - ImGui::CalcTextSize(name.c_str()).y) / 2.0f), + ImGui::GetColorU32(onPrimaryColor), name.c_str() ); - drawList->AddText(textPos, IM_COL32(255, 255, 255, 255), btn->name.c_str()); } -} // namespace cmaterial::component::material +} \ No newline at end of file diff --git a/src/content/component/material/Button/Button.h b/src/content/component/material/Button/Button.h index a1aa76f..c6e7e22 100644 --- a/src/content/component/material/Button/Button.h +++ b/src/content/component/material/Button/Button.h @@ -1,83 +1,29 @@ // -// Created by Earth_Studio on 2026/4/27. +// Created by Earth_Studio on 2026/5/12. // #ifndef CMATERIAL_BUTTON_H #define CMATERIAL_BUTTON_H - - +#include "content/animation/material/HoverAnimation.h" #include "engine/component/IComponent.h" - -#include - -#include "engine/animation/IAnimation.h" - +#include "layer/BGLayer.h" namespace cmaterial::component::material { class Button : public IComponent { public: Button(); - ~Button() = default; - + ~Button() override; void update(ImGuiIO *io) override; - // 🔮 物理状态:暴露给 Layer 和 Animation - double stateAlpha = 0.0; // Hover 的 8% 叠加透明度 - double rippleRadius = 0.0; // 涟漪半径 - double rippleAlpha = 0.0; // 涟漪透明度 - ImVec2 rippleCenter = {0, 0}; - ImVec2 buttonSize = {120, 40}; // MD3 标准高度 40dp + ImVec4 primaryColor = ImVec4(0.404f, 0.314f, 0.643f, 1.0f); + ImVec4 onPrimaryColor = ImVec4(1.0f, 1.0f, 1.0f, 1.0f); private: - bool lastHoverState = false; - - // ---------------------------------------------------- - // 🔮 动画定义区:利用你写的 100000.0f 高精度时间轴 - // ---------------------------------------------------- - class HoverAnimation : public animation::IAnimation { - public: - tweeny::tween alphaTween; - HoverAnimation(double* targetAlpha) { - // 200ms (20000步),透明度从 0 变到 0.08 (MD3 Hover 规范) - alphaTween = tweeny::from(0.0).to(0.08).during(20000).via(tweeny::easing::cubicOut); - bind(targetAlpha, &alphaTween); - } - } hoverAnim; - - class RippleAnimation : public animation::IAnimation { - public: - tweeny::tween radiusTween; - tweeny::tween alphaTween; - RippleAnimation(double* r, double* a) { - // 300ms (30000步),半径变大,透明度先深后浅 - radiusTween = tweeny::from(0.0).to(150.0).during(30000).via(tweeny::easing::quadraticOut); - alphaTween = tweeny::from(0.12).to(0.0).during(30000).via(tweeny::easing::linear); - bind(r, &radiusTween); - bind(a, &alphaTween); - } - // 每次点击时动态更新最大半径 - void setMaxRadius(double maxR) { - radiusTween.seek(0); // 回归起点 - radiusTween = tweeny::from(0.0).to(maxR).during(30000).via(tweeny::easing::quadraticOut); - } - } rippleAnim; - - // ---------------------------------------------------- - // 🔮 图层定义区:严守分段渲染契约 - // ---------------------------------------------------- - class BgLayer : public ILayer { - Button* btn; - public: - BgLayer(Button* b) : btn(b) { priority = -10; } // 垫底 - void render(ImDrawList* drawList, ImVec2 startPos, ImVec2 size) override; - } bgLayer; + bool m_isHovering = false; + bool m_isPressed = false; - class FgLayer : public ILayer { - Button* btn; - public: - FgLayer(Button* b) : btn(b) { priority = 10; } // 盖在上面 - void render(ImDrawList* drawList, ImVec2 startPos, ImVec2 size) override; - } fgLayer; + layer::BGLayer * m_bgLayer = nullptr; + animation::HoverAnimation* m_hoverAnim = nullptr; }; } diff --git a/src/content/component/material/Button/layer/BGLayer.h b/src/content/component/material/Button/layer/BGLayer.h new file mode 100644 index 0000000..901514a --- /dev/null +++ b/src/content/component/material/Button/layer/BGLayer.h @@ -0,0 +1,35 @@ +// +// Created by Earth_Studio on 2026/5/13. +// + +#ifndef CMATERIAL_BACKGROUNDLAYER_H +#define CMATERIAL_BACKGROUNDLAYER_H +#include "engine/component/ILayer.hpp" +#include "imgui.h" + + +namespace cmaterial::component::material::layer { + class BGLayer : public ILayer { + public: + BGLayer() { priority = -2; } + void render(ImDrawList* drawList, ImVec2 startPos, ImVec2 size) override { + float mix = static_cast(hoverAlpha) * 0.08f; + ImVec4 finalColor = ImVec4( + primaryColor.x * (1.0f - mix) + onPrimaryColor.x * mix, + primaryColor.y * (1.0f - mix) + onPrimaryColor.y * mix, + primaryColor.z * (1.0f - mix) + onPrimaryColor.z * mix, + primaryColor.w + ); + drawList->AddRectFilled(startPos, ImVec2(startPos.x + size.x, startPos.y + size.y), + ImGui::GetColorU32(finalColor), size.y / 2.0f); + } + + double hoverAlpha = 0.0; + ImVec4 primaryColor = ImVec4(0.404f, 0.314f, 0.643f, 1.0f); + ImVec4 onPrimaryColor = ImVec4(1.0f, 1.0f, 1.0f, 1.0f); + }; +} + + + +#endif //CMATERIAL_BACKGROUNDLAYER_H diff --git a/src/content/component/material/Button/layer/RippleLayer.cpp b/src/content/component/material/Button/layer/RippleLayer.cpp new file mode 100644 index 0000000..bbb1ef4 --- /dev/null +++ b/src/content/component/material/Button/layer/RippleLayer.cpp @@ -0,0 +1,44 @@ +// +// Created by Earth_Studio on 2026/5/13. +// + +#include "RippleLayer.h" + +#include "cmath" +#include "imgui.h" + +#include "engine/animation/Player.h" +#include "engine/component/IComponent.h" + +namespace cmaterial::component::material::layer { + RippleLayer::RippleLayer(ImVec2 pos, ImVec4 col) : m_clickPos(pos), m_color(col), m_anim(&m_progress, &m_alpha, this) { + priority = -1; + + cmaterial::animation::Player::play(&m_anim); + } + + RippleLayer::~RippleLayer() { + cmaterial::animation::Player::stop(&m_anim); + } + + void RippleLayer::render(ImDrawList* drawList, ImVec2 startPos, ImVec2 size) { + if (m_anim._isFinished) { + ((IComponent*) parent)->removeLayer(this); + return; + } + + if (m_alpha <= 0.0) return; + + ImVec2 maxPos(startPos.x + size.x, startPos.y + size.y); + drawList->PushClipRect(startPos, maxPos, true); + + float dx = std::max(m_clickPos.x - startPos.x, maxPos.x - m_clickPos.x); + float dy = std::max(m_clickPos.y - startPos.y, maxPos.y - m_clickPos.y); + float maxR = std::sqrt(dx * dx + dy * dy); + + ImU32 c = ImGui::GetColorU32(ImVec4(m_color.x, m_color.y, m_color.z, static_cast(m_alpha))); + drawList->AddCircleFilled(m_clickPos, maxR * static_cast(m_progress), c, 32); + + drawList->PopClipRect(); + } +} \ No newline at end of file diff --git a/src/content/component/material/Button/layer/RippleLayer.h b/src/content/component/material/Button/layer/RippleLayer.h new file mode 100644 index 0000000..f85e385 --- /dev/null +++ b/src/content/component/material/Button/layer/RippleLayer.h @@ -0,0 +1,34 @@ +// +// Created by Earth_Studio on 2026/5/13. +// + +#ifndef CMATERIAL_RIPPLELAYER_H +#define CMATERIAL_RIPPLELAYER_H +#include "engine/component/ILayer.hpp" +#include "imgui.h" + +#include "content/animation/material/RippleAnimation.h" + +namespace cmaterial::component::material::layer { + class RippleLayer : public ILayer { + public: + RippleLayer(ImVec2 pos, ImVec4 col); + ~RippleLayer() override; + + void render(ImDrawList* drawList, ImVec2 startPos, ImVec2 size) override; + + bool isAnimationFinished() const { return m_anim._isFinished; } + + private: + double m_progress = 0.0; + double m_alpha = 0.0; + ImVec2 m_clickPos; + ImVec4 m_color; + + animation::RippleAnimation m_anim; + }; +} + + + +#endif //CMATERIAL_RIPPLELAYER_H diff --git a/src/engine/animation/IAnimation.cpp b/src/engine/animation/IAnimation.cpp index d25112a..434d0c8 100644 --- a/src/engine/animation/IAnimation.cpp +++ b/src/engine/animation/IAnimation.cpp @@ -10,6 +10,10 @@ #include "tween.h" namespace cmaterial::animation { + IAnimation::IAnimation(INode* parent) { + this->parent = parent; + } + void IAnimation::reset() { for (auto binder : animationBinders) { if (_isReverse) @@ -19,16 +23,16 @@ namespace cmaterial::animation { } } - std::unordered_map *>* IAnimation::getAnimationBinders() { + std::vector *>>* IAnimation::getAnimationBinders() { return &animationBinders; } void IAnimation::bind(double* property, tweeny::tween* tween) { - animationBinders[property] = tween; + animationBinders.push_back({property, tween}); } IAnimation::~IAnimation() { - animationBinders.clear(); Player::stop(this); + animationBinders.clear(); } } \ No newline at end of file diff --git a/src/engine/animation/IAnimation.h b/src/engine/animation/IAnimation.h index d2bd632..fb1001d 100644 --- a/src/engine/animation/IAnimation.h +++ b/src/engine/animation/IAnimation.h @@ -28,22 +28,28 @@ namespace cmaterial::animation { * @details --- Helper --- * @details @code void bind(double*, tween*)@endcode : Bind a property and a tween. * + * @details --- DANGER ZONE --- + * @details @code bool _isFinished@endcode : Is the animation finished? You should read it ONLY! + * @details @code bool _isReverse@endcode : Is the animation reversed? You should read it ONLY! + * * @warning You should extend it to make a custom Animation. NEVER USE THE INTERFACE DIRECTLY! */ class IAnimation : public utils::INode { public: + IAnimation(INode* parent); virtual ~IAnimation(); void reset(); - std::unordered_map *>* getAnimationBinders(); + std::vector *>>* getAnimationBinders(); bool isLoop = false; + bool _isFinished = false; bool _isReverse = false; protected: void bind(double* property, tweeny::tween* tween); - std::unordered_map *> animationBinders; + std::vector *>> animationBinders; }; } diff --git a/src/engine/animation/Player.cpp b/src/engine/animation/Player.cpp index 7eaa3cd..b789e14 100644 --- a/src/engine/animation/Player.cpp +++ b/src/engine/animation/Player.cpp @@ -5,7 +5,6 @@ #include "Player.h" #include -#include #include "glad/gl.h" #include "GLFW/glfw3.h" @@ -15,15 +14,33 @@ namespace cmaterial::animation { std::vector Player::finishedAnimations; void Player::play(IAnimation *animation) { - if (!animation) return; + if (animation == nullptr) return; - if (std::find(playingAnimations.begin(), playingAnimations.end(), animation) != playingAnimations.end()) { + if (std::ranges::find(playingAnimations, animation) != playingAnimations.end()) { return; } playingAnimations.push_back(animation); } + void Player::forward(IAnimation *animation) { + if (!animation) return; + + for (auto binder : *animation->getAnimationBinders()) { + binder.second->forward(); + } + animation->_isReverse = false; + } + + void Player::backward(IAnimation *animation) { + if (!animation) return; + + for (auto binder : *animation->getAnimationBinders()) { + binder.second->backward(); + } + animation->_isReverse = true; + } + void Player::reverse(IAnimation *animation) { if (!animation) return; @@ -49,7 +66,10 @@ namespace cmaterial::animation { double currentTime = glfwGetTime(); double dt = currentTime - lastTime; lastTime = currentTime; - if (dt <= 0.0) dt = 0.00001; + + if (dt > 0.1 || dt <= 0.0) + dt = 0.00001; + int dtTime = static_cast(dt * 100000.0); // 1 step = 0.01 ms for (auto animation : playingAnimations) { @@ -79,6 +99,7 @@ namespace cmaterial::animation { for (auto animation : finishedAnimations) { std::erase(playingAnimations, animation); + animation->_isFinished = true; } finishedAnimations.clear(); diff --git a/src/engine/animation/Player.h b/src/engine/animation/Player.h index 71e2f2d..a8af19c 100644 --- a/src/engine/animation/Player.h +++ b/src/engine/animation/Player.h @@ -16,8 +16,8 @@ namespace cmaterial::animation { * @brief The Animation player. * @details You will play all the Animation in here. * - * @warning No extra description because this is not finished. - * @todo Rebuild with consumer mode. + * @details --- Danger Zone --- + * @details @code update()@endcode : This is used by the framework itself to update. DO NOT USE IT! */ class Player { public: @@ -25,6 +25,8 @@ namespace cmaterial::animation { ~Player(); static void play(IAnimation* animation); + static void forward(IAnimation* animation); + static void backward(IAnimation* animation); static void reverse(IAnimation* animation); static void stop(IAnimation* animation); diff --git a/src/engine/component/IComponent.cpp b/src/engine/component/IComponent.cpp index 5a3aa47..59d2172 100644 --- a/src/engine/component/IComponent.cpp +++ b/src/engine/component/IComponent.cpp @@ -38,9 +38,11 @@ namespace cmaterial::component { if (layer == nullptr) return; + layer->parent = this; + std::vector& layers = (layer->priority < 0) ? layersBefore : layersAfter; - auto it = std::lower_bound(layers.begin(), layers.end(), layer, + auto it = std::ranges::lower_bound(layers, layer, [](ILayer* a, ILayer* b) { return a->priority < b->priority; }); @@ -55,6 +57,8 @@ namespace cmaterial::component { std::erase(layersBefore, layer); std::erase(layersAfter, layer); + + delete layer; } /** diff --git a/src/engine/framework/Framework.cpp b/src/engine/framework/Framework.cpp index a42dadc..fa1b964 100644 --- a/src/engine/framework/Framework.cpp +++ b/src/engine/framework/Framework.cpp @@ -21,6 +21,8 @@ #include #include +#include "backends/imgui_impl_glfw.h" +#include "backends/imgui_impl_opengl3.h" #include "engine/animation/Player.h" #include "engine/eventbus/internal/listener/ForceRedrawListener.hpp" @@ -82,6 +84,8 @@ namespace cmaterial { double nextFrameTime = glfwGetTime(); + bool isNotFirstRender = false; + EventBus::postEvent(new event::internal::ForceRedrawEvent); while (!glfwWindowShouldClose(hiddenWindow) && !windows.empty()) { bool isEventBusy = EventBus::dispatch(); @@ -103,6 +107,11 @@ namespace cmaterial { nextFrameTime = now + frameDuration; glfwWaitEvents(); + } else { + if (!isNotFirstRender) + isNotFirstRender = true; + else + glfwPollEvents(); } ImFontAtlasUpdateNewFrame(fontAtlas, ++globalFrameCount, true); diff --git a/version.rc b/version.rc index 0085c10..40d7387 100644 --- a/version.rc +++ b/version.rc @@ -37,6 +37,6 @@ BEGIN END BLOCK "VarFileInfo" BEGIN - VALUE "Translation", 0x0409, 0x04E4 // 英语美国 , 多字节->Unicode + VALUE "Translation", 0x0409, 0x04E4 END END \ No newline at end of file From d076355204aeb5cf36ecd9377757d82edf371368 Mon Sep 17 00:00:00 2001 From: Earth_Studio Date: Fri, 15 May 2026 01:38:40 +0800 Subject: [PATCH 32/34] update docs, expand Animation System, fix serious lag issue --- src/content/component/CreatorButton/CreatorButton.h | 3 --- 1 file changed, 3 deletions(-) diff --git a/src/content/component/CreatorButton/CreatorButton.h b/src/content/component/CreatorButton/CreatorButton.h index 089e562..3afab6e 100644 --- a/src/content/component/CreatorButton/CreatorButton.h +++ b/src/content/component/CreatorButton/CreatorButton.h @@ -6,8 +6,6 @@ #define CMATERIAL_CREATORBUTTON_H -#include - #include "content/logic/event/SpawnWindowRequest.hpp" #include "engine/component/IComponent.h" #include "engine/eventbus/EventBus.h" @@ -20,7 +18,6 @@ namespace cmaterial::component { void update(ImGuiIO *io) override { if (ImGui::Button("SpawnWindow")) { - std::cerr << "SpawnWindow" << std::endl; static int count = 0; auto* req = new event::SpawnWindowRequest(); req->title = "Dynamic Window " + std::to_string(++count); From 38c80c8e1017e31c94bcb07c71701622527a94cb Mon Sep 17 00:00:00 2001 From: Earth_Studio Date: Fri, 15 May 2026 01:43:07 +0800 Subject: [PATCH 33/34] update docs, expand Animation System, fix serious lag issue --- src/content/component/CreatorButton/CreatorButton.h | 3 --- 1 file changed, 3 deletions(-) diff --git a/src/content/component/CreatorButton/CreatorButton.h b/src/content/component/CreatorButton/CreatorButton.h index 089e562..3afab6e 100644 --- a/src/content/component/CreatorButton/CreatorButton.h +++ b/src/content/component/CreatorButton/CreatorButton.h @@ -6,8 +6,6 @@ #define CMATERIAL_CREATORBUTTON_H -#include - #include "content/logic/event/SpawnWindowRequest.hpp" #include "engine/component/IComponent.h" #include "engine/eventbus/EventBus.h" @@ -20,7 +18,6 @@ namespace cmaterial::component { void update(ImGuiIO *io) override { if (ImGui::Button("SpawnWindow")) { - std::cerr << "SpawnWindow" << std::endl; static int count = 0; auto* req = new event::SpawnWindowRequest(); req->title = "Dynamic Window " + std::to_string(++count); From 05c5ed1768dd7597b97c52979bfdb6b065158073 Mon Sep 17 00:00:00 2001 From: Earth_Studio Date: Fri, 15 May 2026 01:44:20 +0800 Subject: [PATCH 34/34] update docs, expand Animation System, fix serious lag issue --- src/content/component/material/Button/Button.cpp | 7 ------- 1 file changed, 7 deletions(-) diff --git a/src/content/component/material/Button/Button.cpp b/src/content/component/material/Button/Button.cpp index 6e19ca5..455b888 100644 --- a/src/content/component/material/Button/Button.cpp +++ b/src/content/component/material/Button/Button.cpp @@ -52,13 +52,6 @@ namespace cmaterial::component::material { addLayer(new layer::RippleLayer(io->MousePos, this->onPrimaryColor)); } - // for (int i = layersBefore.size() - 1; i >= 0; --i) { - // auto r = (layer::RippleLayer*) layersBefore[i]; - // if (r && r->isAnimationFinished()) { - // removeLayer(r); - // } - // } - if (active && !m_isPressed) m_isPressed = true; else if (!active && m_isPressed) { m_isPressed = false;