Finished up MVP for scene callbacks

This commit is contained in:
WiggleWizard
2024-09-19 18:50:52 +01:00
parent d0c5ef1a1d
commit edce52bb17
8 changed files with 63 additions and 12 deletions

View File

@@ -259,12 +259,23 @@ namespace Nuake
return std::reinterpret_pointer_cast<EngineSubsystemScriptable>(subsystems[subsystemId]);
}
void Engine::OnWindowSetScene(Ref<Scene> scene)
void Engine::OnWindowSetScene(Ref<Scene> oldScene, Ref<Scene> newScene)
{
if (scene != nullptr)
// Inform the subsystems that we are going to destroy/swap out the old scene
for (auto subsystem : subsystems)
{
scene->OnPreInitialize().AddStatic(&Engine::OnScenePreInitialize, scene);
scene->OnPostInitialize().AddStatic(&Engine::OnScenePostInitialize, scene);
if (subsystem == nullptr)
continue;
subsystem->OnScenePreDestroy(oldScene);
}
// Hook into when the internal pieces of the scene are just about to be ready and when the scene is finally
// ready to present (ie, all initialized and loaded).
if (newScene != nullptr)
{
newScene->OnPreInitialize().AddStatic(&Engine::OnScenePreInitialize, newScene);
newScene->OnPostInitialize().AddStatic(&Engine::OnScenePostInitialize, newScene);
}
}

View File

@@ -58,7 +58,7 @@ namespace Nuake
static Ref<EngineSubsystemScriptable> GetScriptedSubsystem(const int subsystemId);
protected:
static void OnWindowSetScene(Ref<Scene> scene);
static void OnWindowSetScene(Ref<Scene> oldScene, Ref<Scene> newScene);
static void InitializeCoreSubsystems();
static void OnScriptingEngineGameAssemblyLoaded();

View File

@@ -21,6 +21,7 @@ namespace Nuake
virtual void OnScenePreInitialize(Ref<Scene> scene) {}
virtual void OnScenePostInitialize(Ref<Scene> scene) {}
virtual void OnScenePreDestroy(Ref<Scene> scene) {}
private:
bool canEverTick = false;

View File

@@ -1,5 +1,7 @@
#include "EngineSubsystemScriptable.h"
#include "Coral/Type.hpp"
namespace Nuake
{
@@ -32,6 +34,10 @@ void EngineSubsystemScriptable::Tick(float deltaTime)
void EngineSubsystemScriptable::OnScenePreInitialize(Ref<Scene> scene)
{
if (!cSharpObjectInstance.IsValid())
return;
cSharpObjectInstance.InvokeMethod("InternalOnScenePreInitialize");
}
void EngineSubsystemScriptable::OnScenePostInitialize(Ref<Scene> scene)
@@ -39,9 +45,19 @@ void EngineSubsystemScriptable::OnScenePostInitialize(Ref<Scene> scene)
if (!cSharpObjectInstance.IsValid())
return;
cSharpObjectInstance.InvokeMethod("OnScenePostInitialize", scene);
cSharpObjectInstance.InvokeMethod("InternalOnSceneReady");
}
void EngineSubsystemScriptable::OnScenePreDestroy(Ref<Scene> scene)
{
if (!cSharpObjectInstance.IsValid())
return;
if (cSharpObjectInstance.GetType().GetTypeId() == -1)
return;
cSharpObjectInstance.InvokeMethod("InternalOnScenePreDestroy");
}
}

View File

@@ -21,6 +21,7 @@ namespace Nuake
virtual void OnScenePreInitialize(Ref<Scene> scene) override;
virtual void OnScenePostInitialize(Ref<Scene> scene) override;
virtual void OnScenePreDestroy(Ref<Scene> scene) override;
private:
Coral::ManagedObject cSharpObjectInstance;

View File

@@ -305,9 +305,9 @@ Ref<Scene> Window::GetScene()
bool Window::SetScene(Ref<Scene> newScene)
{
windowSetSceneDelegate.Broadcast(scene, newScene);
scene = newScene;
windowSetSceneDelegate.Broadcast(newScene);
return true;
}

View File

@@ -14,7 +14,7 @@ namespace Nuake
class Scene;
class FrameBuffer;
DECLARE_MULTICAST_DELEGATE(OnWindowSetSceneDelegate, Ref<Scene>)
DECLARE_MULTICAST_DELEGATE(OnWindowSetSceneDelegate, Ref<Scene>, Ref<Scene>)
class Window
{
@@ -66,6 +66,8 @@ namespace Nuake
void SetOnWindowClosedCallback(std::function<void(Window& window)> callback);
void SetOnDragNDropCallback(std::function<void(Window&, const std::vector<std::string>& paths)> callback);
// Delegate is broadcasted BEFORE the actual internal scene has been reassigned, this is to keep
// the potential old scene relevant before its ultimate destruction.
OnWindowSetSceneDelegate& OnWindowSetScene() { return windowSetSceneDelegate; }
private:

View File

@@ -20,9 +20,29 @@
}
public virtual void Initialize() {}
public virtual void OnScenePreInit(Scene scene) {}
public virtual void OnScenePostInit(Scene scene) {}
public virtual void OnSceneUnloaded(Scene scene) {}
public virtual void OnScenePreInitialize(Scene scene) {}
public virtual void OnSceneReady(Scene scene) {}
public virtual void OnScenePreDestroy(Scene scene) {}
public virtual void OnTick(float deltaTime) {}
// Since the engine doesn't have the concept of scene instances, we just pass
// a new `Scene` here since all functions in Scene are statics. This is largely
// to keep the API a little more stable going forward.
private void InternalOnScenePreInitialize()
{
OnScenePreInitialize(new Scene());
}
private void InternalOnSceneReady()
{
OnSceneReady(new Scene());
}
private void InternalOnScenePreDestroy()
{
OnScenePreDestroy(new Scene());
}
}
}