Added OnDrawGizmos for debug draw for scripts

This commit is contained in:
Kah Wei 2022-11-08 20:49:07 +08:00
parent 715699b63b
commit e89f5b4b9e
8 changed files with 76 additions and 0 deletions

View File

@ -106,6 +106,7 @@ namespace Sandbox
SHSystemManager::RegisterRoutine<SHTransformSystem, SHTransformSystem::TransformPostPhysicsUpdate>(); SHSystemManager::RegisterRoutine<SHTransformSystem, SHTransformSystem::TransformPostPhysicsUpdate>();
SHSystemManager::RegisterRoutine<SHDebugDrawSystem, SHDebugDrawSystem::ProcessPointsRoutine>(); SHSystemManager::RegisterRoutine<SHDebugDrawSystem, SHDebugDrawSystem::ProcessPointsRoutine>();
SHSystemManager::RegisterRoutine<SHScriptEngine, SHScriptEngine::GizmosDrawRoutine>();
SHSystemManager::RegisterRoutine<SHGraphicsSystem, SHGraphicsSystem::BatcherDispatcherRoutine>(); SHSystemManager::RegisterRoutine<SHGraphicsSystem, SHGraphicsSystem::BatcherDispatcherRoutine>();
SHSystemManager::RegisterRoutine<SHGraphicsSystem, SHGraphicsSystem::BeginRoutine>(); SHSystemManager::RegisterRoutine<SHGraphicsSystem, SHGraphicsSystem::BeginRoutine>();

View File

@ -380,6 +380,12 @@ namespace SHADE
DEFAULT_CSHARP_NAMESPACE + ".ScriptStore", DEFAULT_CSHARP_NAMESPACE + ".ScriptStore",
"ExecuteLateUpdate" "ExecuteLateUpdate"
); );
csScriptsExecuteDrawGizmos = dotNet.GetFunctionPtr<CsFuncPtr>
(
DEFAULT_CSHARP_LIB_NAME,
DEFAULT_CSHARP_NAMESPACE + ".ScriptStore",
"ExecuteOnDrawGizmos"
);
csScriptsExecutePhysicsEvents = dotNet.GetFunctionPtr<CsFuncPtr> csScriptsExecutePhysicsEvents = dotNet.GetFunctionPtr<CsFuncPtr>
( (
DEFAULT_CSHARP_LIB_NAME, DEFAULT_CSHARP_LIB_NAME,

View File

@ -55,6 +55,12 @@ namespace SHADE
LateUpdateRoutine(); LateUpdateRoutine();
void Execute(double dt) noexcept override final; 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 class SH_API FrameCleanUpRoutine final : public SHSystemRoutine
{ {
public: public:
@ -250,6 +256,7 @@ namespace SHADE
CsFuncPtr csScriptsExecuteFixedUpdate = nullptr; CsFuncPtr csScriptsExecuteFixedUpdate = nullptr;
CsFuncPtr csScriptsExecuteUpdate = nullptr; CsFuncPtr csScriptsExecuteUpdate = nullptr;
CsFuncPtr csScriptsExecuteLateUpdate = nullptr; CsFuncPtr csScriptsExecuteLateUpdate = nullptr;
CsFuncPtr csScriptsExecuteDrawGizmos = nullptr;
CsFuncPtr csScriptsExecutePhysicsEvents = nullptr; CsFuncPtr csScriptsExecutePhysicsEvents = nullptr;
CsFuncPtr csScriptsFrameCleanUp = nullptr; CsFuncPtr csScriptsFrameCleanUp = nullptr;
CsScriptManipFuncPtr csScriptsAdd = nullptr; CsScriptManipFuncPtr csScriptsAdd = nullptr;

View File

@ -50,6 +50,17 @@ namespace SHADE
reinterpret_cast<SHScriptEngine*>(system)->csScriptsExecuteLateUpdate(); reinterpret_cast<SHScriptEngine*>(system)->csScriptsExecuteLateUpdate();
} }
/*-----------------------------------------------------------------------------------*/
/* System Routine Functions - GizmosDrawRoutine */
/*-----------------------------------------------------------------------------------*/
SHScriptEngine::GizmosDrawRoutine::GizmosDrawRoutine()
: SHSystemRoutine("Script Engine Gizmos Draw", true)
{}
void SHScriptEngine::GizmosDrawRoutine::Execute(double dt) noexcept
{
reinterpret_cast<SHScriptEngine*>(system)->csScriptsExecuteDrawGizmos();
}
/*-----------------------------------------------------------------------------------*/ /*-----------------------------------------------------------------------------------*/
/* System Routine Functions - FrameCleanUpRoutine */ /* System Routine Functions - FrameCleanUpRoutine */
/*-----------------------------------------------------------------------------------*/ /*-----------------------------------------------------------------------------------*/

View File

@ -140,6 +140,13 @@ namespace SHADE
lateUpdate(); lateUpdate();
SAFE_NATIVE_CALL_END(this) SAFE_NATIVE_CALL_END(this)
} }
void Script::OnDrawGizmos()
{
SAFE_NATIVE_CALL_BEGIN
OnGizmosDrawOverriden = true;
onDrawGizmos();
SAFE_NATIVE_CALL_END(this)
}
void Script::OnDestroy() void Script::OnDestroy()
{ {
SAFE_NATIVE_CALL_BEGIN SAFE_NATIVE_CALL_BEGIN
@ -194,6 +201,7 @@ namespace SHADE
/*---------------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------------*/
Script::Script(GameObject gameObj) Script::Script(GameObject gameObj)
: owner { gameObj } : owner { gameObj }
, OnGizmosDrawOverriden { false }
{} {}
/*---------------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------------*/
@ -210,6 +218,10 @@ namespace SHADE
void Script::fixedUpdate() {} void Script::fixedUpdate() {}
void Script::update() {} void Script::update() {}
void Script::lateUpdate() {} void Script::lateUpdate() {}
void Script::onDrawGizmos()
{
OnGizmosDrawOverriden = false;
}
void Script::onDestroy() {} void Script::onDestroy() {}
/*---------------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------------*/

View File

@ -164,6 +164,14 @@ namespace SHADE
static operator bool(Script^ s); static operator bool(Script^ s);
internal: internal:
/*-----------------------------------------------------------------------------*/
/* Properties */
/*-----------------------------------------------------------------------------*/
/// <summary>
/// If true, the OnGizmosDraw function was overridden.
/// </summary>
bool OnGizmosDrawOverriden;
/*-----------------------------------------------------------------------------*/ /*-----------------------------------------------------------------------------*/
/* "All-Time" Lifecycle Functions */ /* "All-Time" Lifecycle Functions */
/*-----------------------------------------------------------------------------*/ /*-----------------------------------------------------------------------------*/
@ -208,6 +216,11 @@ namespace SHADE
/// </summary> /// </summary>
void LateUpdate(); void LateUpdate();
/// <summary> /// <summary>
/// Used to call onDrawGizmos(). This should be called just before rendering
/// the scene. This will only be called when working in the editor.
/// </summary>
void OnDrawGizmos();
/// <summary>
/// Used to call onDestroy(). This should be called at the end of the frame /// 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 /// where the attached GameObject or this script is destroyed directly or
/// indirectly due to destruction of the owner. /// indirectly due to destruction of the owner.
@ -308,6 +321,10 @@ namespace SHADE
/// </summary> /// </summary>
virtual void lateUpdate(); virtual void lateUpdate();
/// <summary> /// <summary>
/// Called every frame just before rendering but only if working in the editor.
/// </summary>
virtual void onDrawGizmos();
/// <summary>
/// Called just before the end of the frame where the attached GameObject or /// 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 /// this script is destroyed directly or indirectly due to destruction of the
/// owner. /// owner.

View File

@ -478,6 +478,24 @@ namespace SHADE
SAFE_NATIVE_CALL_END_N("SHADE_Managed.ScriptStore") SAFE_NATIVE_CALL_END_N("SHADE_Managed.ScriptStore")
} }
void ScriptStore::ExecuteOnDrawGizmos()
{
SAFE_NATIVE_CALL_BEGIN
for each (System::Collections::Generic::KeyValuePair<Entity, ScriptList^> 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() void ScriptStore::ExecuteCollisionFunctions()
{ {
SAFE_NATIVE_CALL_BEGIN SAFE_NATIVE_CALL_BEGIN

View File

@ -234,6 +234,10 @@ namespace SHADE
/// </summary> /// </summary>
static void ExecuteLateUpdate(); static void ExecuteLateUpdate();
/// <summary> /// <summary>
/// Executes OnDrawGizmos() for all scripts.
/// </summary>
static void ExecuteOnDrawGizmos();
/// <summary>
/// Executes OnCollision*() and OnTrigger*() for all scripts. /// Executes OnCollision*() and OnTrigger*() for all scripts.
/// </summary> /// </summary>
static void ExecuteCollisionFunctions(); static void ExecuteCollisionFunctions();