From e89f5b4b9e0b640b93abe1456cdfd9b24491a777 Mon Sep 17 00:00:00 2001 From: Kah Wei Date: Tue, 8 Nov 2022 20:49:07 +0800 Subject: [PATCH] Added OnDrawGizmos for debug draw for scripts --- .../src/Application/SBApplication.cpp | 1 + SHADE_Engine/src/Scripting/SHScriptEngine.cpp | 6 ++++++ SHADE_Engine/src/Scripting/SHScriptEngine.h | 7 +++++++ .../src/Scripting/SHScriptEngineRoutines.cpp | 11 +++++++++++ SHADE_Managed/src/Scripts/Script.cxx | 12 ++++++++++++ SHADE_Managed/src/Scripts/Script.hxx | 17 +++++++++++++++++ SHADE_Managed/src/Scripts/ScriptStore.cxx | 18 ++++++++++++++++++ SHADE_Managed/src/Scripts/ScriptStore.hxx | 4 ++++ 8 files changed, 76 insertions(+) diff --git a/SHADE_Application/src/Application/SBApplication.cpp b/SHADE_Application/src/Application/SBApplication.cpp index f4102067..6b67dbce 100644 --- a/SHADE_Application/src/Application/SBApplication.cpp +++ b/SHADE_Application/src/Application/SBApplication.cpp @@ -106,6 +106,7 @@ namespace Sandbox SHSystemManager::RegisterRoutine(); SHSystemManager::RegisterRoutine(); + SHSystemManager::RegisterRoutine(); SHSystemManager::RegisterRoutine(); SHSystemManager::RegisterRoutine(); diff --git a/SHADE_Engine/src/Scripting/SHScriptEngine.cpp b/SHADE_Engine/src/Scripting/SHScriptEngine.cpp index 4a73342e..827f45a2 100644 --- a/SHADE_Engine/src/Scripting/SHScriptEngine.cpp +++ b/SHADE_Engine/src/Scripting/SHScriptEngine.cpp @@ -379,6 +379,12 @@ namespace SHADE DEFAULT_CSHARP_LIB_NAME, DEFAULT_CSHARP_NAMESPACE + ".ScriptStore", "ExecuteLateUpdate" + ); + csScriptsExecuteDrawGizmos = dotNet.GetFunctionPtr + ( + DEFAULT_CSHARP_LIB_NAME, + DEFAULT_CSHARP_NAMESPACE + ".ScriptStore", + "ExecuteOnDrawGizmos" ); csScriptsExecutePhysicsEvents = dotNet.GetFunctionPtr ( diff --git a/SHADE_Engine/src/Scripting/SHScriptEngine.h b/SHADE_Engine/src/Scripting/SHScriptEngine.h index 9ddd617a..7d83606e 100644 --- a/SHADE_Engine/src/Scripting/SHScriptEngine.h +++ b/SHADE_Engine/src/Scripting/SHScriptEngine.h @@ -55,6 +55,12 @@ namespace SHADE LateUpdateRoutine(); void Execute(double dt) noexcept override final; }; + class SH_API GizmosDrawRoutine final : public SHSystemRoutine + { + public: + GizmosDrawRoutine(); + void Execute(double dt) noexcept override final; + }; class SH_API FrameCleanUpRoutine final : public SHSystemRoutine { public: @@ -250,6 +256,7 @@ namespace SHADE CsFuncPtr csScriptsExecuteFixedUpdate = nullptr; CsFuncPtr csScriptsExecuteUpdate = nullptr; CsFuncPtr csScriptsExecuteLateUpdate = nullptr; + CsFuncPtr csScriptsExecuteDrawGizmos = nullptr; CsFuncPtr csScriptsExecutePhysicsEvents = nullptr; CsFuncPtr csScriptsFrameCleanUp = nullptr; CsScriptManipFuncPtr csScriptsAdd = nullptr; diff --git a/SHADE_Engine/src/Scripting/SHScriptEngineRoutines.cpp b/SHADE_Engine/src/Scripting/SHScriptEngineRoutines.cpp index a2981c06..699776ca 100644 --- a/SHADE_Engine/src/Scripting/SHScriptEngineRoutines.cpp +++ b/SHADE_Engine/src/Scripting/SHScriptEngineRoutines.cpp @@ -50,6 +50,17 @@ namespace SHADE reinterpret_cast(system)->csScriptsExecuteLateUpdate(); } + /*-----------------------------------------------------------------------------------*/ + /* System Routine Functions - GizmosDrawRoutine */ + /*-----------------------------------------------------------------------------------*/ + SHScriptEngine::GizmosDrawRoutine::GizmosDrawRoutine() + : SHSystemRoutine("Script Engine Gizmos Draw", true) + {} + void SHScriptEngine::GizmosDrawRoutine::Execute(double dt) noexcept + { + reinterpret_cast(system)->csScriptsExecuteDrawGizmos(); + } + /*-----------------------------------------------------------------------------------*/ /* System Routine Functions - FrameCleanUpRoutine */ /*-----------------------------------------------------------------------------------*/ diff --git a/SHADE_Managed/src/Scripts/Script.cxx b/SHADE_Managed/src/Scripts/Script.cxx index e476d69d..bd6d7bef 100644 --- a/SHADE_Managed/src/Scripts/Script.cxx +++ b/SHADE_Managed/src/Scripts/Script.cxx @@ -140,6 +140,13 @@ namespace SHADE lateUpdate(); SAFE_NATIVE_CALL_END(this) } + void Script::OnDrawGizmos() + { + SAFE_NATIVE_CALL_BEGIN + OnGizmosDrawOverriden = true; + onDrawGizmos(); + SAFE_NATIVE_CALL_END(this) + } void Script::OnDestroy() { SAFE_NATIVE_CALL_BEGIN @@ -194,6 +201,7 @@ namespace SHADE /*---------------------------------------------------------------------------------*/ Script::Script(GameObject gameObj) : owner { gameObj } + , OnGizmosDrawOverriden { false } {} /*---------------------------------------------------------------------------------*/ @@ -210,6 +218,10 @@ namespace SHADE void Script::fixedUpdate() {} void Script::update() {} void Script::lateUpdate() {} + void Script::onDrawGizmos() + { + OnGizmosDrawOverriden = false; + } void Script::onDestroy() {} /*---------------------------------------------------------------------------------*/ diff --git a/SHADE_Managed/src/Scripts/Script.hxx b/SHADE_Managed/src/Scripts/Script.hxx index afeaa8a0..bbe36784 100644 --- a/SHADE_Managed/src/Scripts/Script.hxx +++ b/SHADE_Managed/src/Scripts/Script.hxx @@ -164,6 +164,14 @@ namespace SHADE static operator bool(Script^ s); internal: + /*-----------------------------------------------------------------------------*/ + /* Properties */ + /*-----------------------------------------------------------------------------*/ + /// + /// If true, the OnGizmosDraw function was overridden. + /// + bool OnGizmosDrawOverriden; + /*-----------------------------------------------------------------------------*/ /* "All-Time" Lifecycle Functions */ /*-----------------------------------------------------------------------------*/ @@ -208,6 +216,11 @@ namespace SHADE /// void LateUpdate(); /// + /// Used to call onDrawGizmos(). This should be called just before rendering + /// the scene. This will only be called when working in the editor. + /// + void OnDrawGizmos(); + /// /// Used to call onDestroy(). This should be called at the end of the frame /// where the attached GameObject or this script is destroyed directly or /// indirectly due to destruction of the owner. @@ -308,6 +321,10 @@ namespace SHADE /// virtual void lateUpdate(); /// + /// Called every frame just before rendering but only if working in the editor. + /// + virtual void onDrawGizmos(); + /// /// Called just before the end of the frame where the attached GameObject or /// this script is destroyed directly or indirectly due to destruction of the /// owner. diff --git a/SHADE_Managed/src/Scripts/ScriptStore.cxx b/SHADE_Managed/src/Scripts/ScriptStore.cxx index 407d0fa8..a90b4f12 100644 --- a/SHADE_Managed/src/Scripts/ScriptStore.cxx +++ b/SHADE_Managed/src/Scripts/ScriptStore.cxx @@ -478,6 +478,24 @@ namespace SHADE SAFE_NATIVE_CALL_END_N("SHADE_Managed.ScriptStore") } + void ScriptStore::ExecuteOnDrawGizmos() + { + SAFE_NATIVE_CALL_BEGIN + for each (System::Collections::Generic::KeyValuePair entity in scripts) + { + // Check active state + if (!isEntityActive(entity.Key)) + continue; + + // Update each script + for each (Script^ script in entity.Value) + { + script->OnDrawGizmos(); + } + } + SAFE_NATIVE_CALL_END_N("SHADE_Managed.ScriptStore") + } + void ScriptStore::ExecuteCollisionFunctions() { SAFE_NATIVE_CALL_BEGIN diff --git a/SHADE_Managed/src/Scripts/ScriptStore.hxx b/SHADE_Managed/src/Scripts/ScriptStore.hxx index a4c6e824..c151392f 100644 --- a/SHADE_Managed/src/Scripts/ScriptStore.hxx +++ b/SHADE_Managed/src/Scripts/ScriptStore.hxx @@ -234,6 +234,10 @@ namespace SHADE /// static void ExecuteLateUpdate(); /// + /// Executes OnDrawGizmos() for all scripts. + /// + static void ExecuteOnDrawGizmos(); + /// /// Executes OnCollision*() and OnTrigger*() for all scripts. /// static void ExecuteCollisionFunctions();