From c86508ef5781a589fff07f24c9eb07eae0bff393 Mon Sep 17 00:00:00 2001 From: Wojtek Figat Date: Fri, 19 Feb 2021 11:01:21 +0100 Subject: [PATCH] Add support for updating particle effects in editor view when editing --- Source/Engine/Level/Level.cpp | 6 +-- Source/Engine/Level/Scene/SceneTicking.h | 46 ++++++++++++++------- Source/Engine/Particles/ParticleEffect.cpp | 14 +++++++ Source/Engine/Particles/ParticleEffect.h | 3 ++ Source/Engine/Particles/ParticleManager.cpp | 10 +++++ 5 files changed, 61 insertions(+), 18 deletions(-) diff --git a/Source/Engine/Level/Level.cpp b/Source/Engine/Level/Level.cpp index ba4ce4014..be07f56ec 100644 --- a/Source/Engine/Level/Level.cpp +++ b/Source/Engine/Level/Level.cpp @@ -194,7 +194,7 @@ void LevelService::Update() for (int32 i = 0; i < scenes.Count(); i++) { if (scenes[i]->GetIsActive()) - scenes[i]->Ticking.Update.TickEditorScripts(); + scenes[i]->Ticking.Update.TickExecuteInEditor(); } } #endif @@ -223,7 +223,7 @@ void LevelService::LateUpdate() for (int32 i = 0; i < scenes.Count(); i++) { if (scenes[i]->GetIsActive()) - scenes[i]->Ticking.LateUpdate.TickEditorScripts(); + scenes[i]->Ticking.LateUpdate.TickExecuteInEditor(); } } #endif @@ -255,7 +255,7 @@ void LevelService::FixedUpdate() for (int32 i = 0; i < scenes.Count(); i++) { if (scenes[i]->GetIsActive()) - scenes[i]->Ticking.FixedUpdate.TickEditorScripts(); + scenes[i]->Ticking.FixedUpdate.TickExecuteInEditor(); } } #endif diff --git a/Source/Engine/Level/Scene/SceneTicking.h b/Source/Engine/Level/Scene/SceneTicking.h index bb26e83c1..8962f1810 100644 --- a/Source/Engine/Level/Scene/SceneTicking.h +++ b/Source/Engine/Level/Scene/SceneTicking.h @@ -17,15 +17,9 @@ public: /// /// Tick function type. /// - class Tick + struct Tick { - public: - - /// - /// Signature of the function to call - /// typedef void (*Signature)(); - typedef void (*SignatureObj)(void*); template @@ -51,8 +45,7 @@ public: /// /// Calls the binded function. /// - /// Function result - void Call() const + FORCE_INLINE void Call() const { (*FunctionObj)(Callee); } @@ -63,10 +56,11 @@ public: public: Array Scripts; + Array Ticks; #if USE_EDITOR Array ScriptsExecuteInEditor; + Array TicksExecuteInEditor; #endif - Array Ticks; TickData(int32 capacity) : Scripts(capacity) @@ -81,18 +75,17 @@ public: TickScripts(Scripts); for (int32 i = 0; i < Ticks.Count(); i++) - { Ticks[i].Call(); - } } #if USE_EDITOR - - void TickEditorScripts() + void TickExecuteInEditor() { TickScripts(ScriptsExecuteInEditor); - } + for (int32 i = 0; i < TicksExecuteInEditor.Count(); i++) + TicksExecuteInEditor[i].Call(); + } #endif void AddScript(Script* script); @@ -118,12 +111,35 @@ public: } } +#if USE_EDITOR + template + void AddTickExecuteInEditor(T* callee) + { + SceneTicking::Tick tick; + tick.Bind(callee); + TicksExecuteInEditor.Add(tick); + } + + void RemoveTickExecuteInEditor(void* callee) + { + for (int32 i = 0; i < TicksExecuteInEditor.Count(); i++) + { + if (TicksExecuteInEditor[i].Callee == callee) + { + TicksExecuteInEditor.RemoveAt(i); + break; + } + } + } +#endif + void Clear() { Scripts.Clear(); Ticks.Clear(); #if USE_EDITOR ScriptsExecuteInEditor.Clear(); + TicksExecuteInEditor.Clear(); #endif } }; diff --git a/Source/Engine/Particles/ParticleEffect.cpp b/Source/Engine/Particles/ParticleEffect.cpp index b669eeabc..61f0ba8ae 100644 --- a/Source/Engine/Particles/ParticleEffect.cpp +++ b/Source/Engine/Particles/ParticleEffect.cpp @@ -373,6 +373,18 @@ void ParticleEffect::Update() UpdateSimulation(); } +#if USE_EDITOR + +#include "Editor/Editor.h" + +void ParticleEffect::UpdateExecuteInEditor() +{ + if (!Editor::IsPlayMode) + Update(); +} + +#endif + void ParticleEffect::CacheModifiedParameters() { if (_parameters.IsEmpty()) @@ -649,6 +661,7 @@ void ParticleEffect::OnEnable() GetSceneRendering()->AddGeometry(this); #if USE_EDITOR GetSceneRendering()->AddViewportIcon(this); + GetScene()->Ticking.Update.AddTickExecuteInEditor(this); #endif // Base @@ -658,6 +671,7 @@ void ParticleEffect::OnEnable() void ParticleEffect::OnDisable() { #if USE_EDITOR + GetScene()->Ticking.Update.RemoveTickExecuteInEditor(this); GetSceneRendering()->RemoveViewportIcon(this); #endif GetSceneRendering()->RemoveGeometry(this); diff --git a/Source/Engine/Particles/ParticleEffect.h b/Source/Engine/Particles/ParticleEffect.h index 67d7ebd19..65004278c 100644 --- a/Source/Engine/Particles/ParticleEffect.h +++ b/Source/Engine/Particles/ParticleEffect.h @@ -366,6 +366,9 @@ protected: private: void Update(); +#if USE_EDITOR + void UpdateExecuteInEditor(); +#endif void CacheModifiedParameters(); void ApplyModifiedParameters(); void OnParticleSystemModified(); diff --git a/Source/Engine/Particles/ParticleManager.cpp b/Source/Engine/Particles/ParticleManager.cpp index f2e70fee3..0ad96c74a 100644 --- a/Source/Engine/Particles/ParticleManager.cpp +++ b/Source/Engine/Particles/ParticleManager.cpp @@ -17,6 +17,9 @@ #include "Engine/Profiler/ProfilerGPU.h" #include "Engine/Renderer/Utils/BitonicSort.h" #endif +#if USE_EDITOR +#include "Editor/Editor.h" +#endif struct SpriteParticleVertex { @@ -1155,6 +1158,13 @@ void ParticleManagerService::Update() // Simulation delta time can be based on a time since last update or the current delta time float dt = effect->UseTimeScale ? deltaTime : deltaTimeUnscaled; float t = effect->UseTimeScale ? time : timeUnscaled; +#if USE_EDITOR + if (!Editor::IsPlayMode) + { + dt = deltaTimeUnscaled; + t = timeUnscaled; + } +#endif const float lastUpdateTime = instance.LastUpdateTime; if (lastUpdateTime > 0 && t > lastUpdateTime) {