diff --git a/SHADE_Engine/src/Editor/EditorWindow/MenuBar/SHEditorMenuBar.h b/SHADE_Engine/src/Editor/EditorWindow/MenuBar/SHEditorMenuBar.h index 77ebcf55..e265b33e 100644 --- a/SHADE_Engine/src/Editor/EditorWindow/MenuBar/SHEditorMenuBar.h +++ b/SHADE_Engine/src/Editor/EditorWindow/MenuBar/SHEditorMenuBar.h @@ -4,6 +4,7 @@ //|| SHADE Includes || //#==============================================================# #include "Editor/EditorWindow/SHEditorWindow.h" +#include "Editor/SHEditor.h" namespace SHADE { diff --git a/SHADE_Engine/src/Scripting/SHScriptEngine.cpp b/SHADE_Engine/src/Scripting/SHScriptEngine.cpp index 96f1539d..435cb6fb 100644 --- a/SHADE_Engine/src/Scripting/SHScriptEngine.cpp +++ b/SHADE_Engine/src/Scripting/SHScriptEngine.cpp @@ -31,6 +31,9 @@ of DigiPen Institute of Technology is prohibited. #include "Assets/SHAssetMacros.h" #include "Tools/Utilities/SHExecUtilities.h" #include "SHVSUtilities.h" +#include "UI/Events/SHButtonClickEvent.h" +#include "UI/SHUIComponent.h" +#include "Editor/EditorWindow/MenuBar/SHEditorMenuBar.h" namespace SHADE { @@ -121,6 +124,11 @@ namespace SHADE csScriptsRemoveAllImmediately(entity, callOnDestroy); } + void SHScriptEngine::RemoveAllScriptsFromAllImmediately(bool callOnDestroy) + { + csScriptRemoveAllForAllNow(callOnDestroy); + } + /*---------------------------------------------------------------------------------*/ /* Script Serialisation Functions */ /*---------------------------------------------------------------------------------*/ @@ -346,6 +354,21 @@ namespace SHADE return eventData->handle; } + SHEventHandle SHScriptEngine::onUIElementRemoved(SHEventPtr eventPtr) + { + auto eventData = reinterpret_cast*>(eventPtr.get()); + if (eventData->data->removedComponentType == ComponentFamily::GetID()) + csUIElementOnRemoved(eventData->data->eid); + return eventData->handle; + } + + SHEventHandle SHScriptEngine::onUIElementClicked(SHEventPtr eventPtr) + { + auto eventData = reinterpret_cast*>(eventPtr.get()); + csUIElementOnClicked(eventData->data->EID); + return eventData->handle; + } + SHEventHandle SHScriptEngine::onSceneNodeChildrenAdded(SHEventPtr eventPtr) { auto eventData = reinterpret_cast*>(eventPtr.get()); @@ -360,6 +383,13 @@ namespace SHADE return eventData->handle; } + SHEventHandle SHScriptEngine::onSceneDestroyed(SHEventPtr eventPtr) + { + auto eventData = reinterpret_cast*>(eventPtr.get()); + csScriptRemoveAllForAllNow(true); + return eventData->handle; + } + /*-----------------------------------------------------------------------------------*/ /* Helper Functions */ /*-----------------------------------------------------------------------------------*/ @@ -460,6 +490,12 @@ namespace SHADE DEFAULT_CSHARP_NAMESPACE + ".ScriptStore", "RemoveAllScriptsImmediately" ); + csScriptRemoveAllForAllNow = dotNet.GetFunctionPtr + ( + DEFAULT_CSHARP_LIB_NAME, + DEFAULT_CSHARP_NAMESPACE + ".ScriptStore", + "RemoveAllScriptsFromAllImmediately" + ); csScriptsSerialiseYaml = dotNet.GetFunctionPtr ( DEFAULT_CSHARP_LIB_NAME, @@ -490,6 +526,18 @@ namespace SHADE DEFAULT_CSHARP_NAMESPACE + ".ChildListCache", "OnChildrenChanged" ); + csUIElementOnRemoved = dotNet.GetFunctionPtr + ( + DEFAULT_CSHARP_LIB_NAME, + DEFAULT_CSHARP_NAMESPACE + ".UIElement", + "OnComponentRemoved" + ); + csUIElementOnClicked = dotNet.GetFunctionPtr + ( + DEFAULT_CSHARP_LIB_NAME, + DEFAULT_CSHARP_NAMESPACE + ".UIElement", + "OnClicked" + ); csEditorRenderScripts = dotNet.GetFunctionPtr ( DEFAULT_CSHARP_LIB_NAME, @@ -520,6 +568,14 @@ namespace SHADE }; SHEventManager::SubscribeTo(SH_ENTITY_DESTROYED_EVENT, std::dynamic_pointer_cast(destroyedEventReceiver)); + /* Editor */ + // Register for editor state change event + std::shared_ptr> destroyedSceneEventReceiver + { + std::make_shared>(this, &SHScriptEngine::onSceneDestroyed) + }; + SHEventManager::SubscribeTo(SH_EDITOR_ON_STOP_EVENT, std::dynamic_pointer_cast(destroyedSceneEventReceiver)); + /* Colliders */ // Register for collider added event std::shared_ptr> addedColliderEventReceiver @@ -540,6 +596,18 @@ namespace SHADE }; SHEventManager::SubscribeTo(SH_COMPONENT_REMOVED_EVENT, std::dynamic_pointer_cast(removedColliderComponentEventReceiver)); + /* UI Element */ + std::shared_ptr> removedUIElementEventReceiver + { + std::make_shared>(this, &SHScriptEngine::onUIElementRemoved) + }; + SHEventManager::SubscribeTo(SH_COMPONENT_REMOVED_EVENT, std::dynamic_pointer_cast(removedUIElementEventReceiver)); + std::shared_ptr> clickedUIElementEventReceiver + { + std::make_shared>(this, &SHScriptEngine::onUIElementClicked) + }; + SHEventManager::SubscribeTo(SH_BUTTON_CLICK_EVENT, std::dynamic_pointer_cast(clickedUIElementEventReceiver)); + /* SceneGraph */ // Register for SceneNode child added event std::shared_ptr> addChildEventReceiver diff --git a/SHADE_Engine/src/Scripting/SHScriptEngine.h b/SHADE_Engine/src/Scripting/SHScriptEngine.h index 9710f5c5..fd88a283 100644 --- a/SHADE_Engine/src/Scripting/SHScriptEngine.h +++ b/SHADE_Engine/src/Scripting/SHScriptEngine.h @@ -148,6 +148,13 @@ namespace SHADE /// play mode. /// void RemoveAllScriptsImmediately(EntityID entity, bool callOnDestroy); + /// + /// Removes all Scripts attached to all entities immediately. The + /// + /// + /// Whether or not to call OnDestroy on the scripts. + /// + void RemoveAllScriptsFromAllImmediately(bool callOnDestroy); /*-----------------------------------------------------------------------------*/ /* Script Serialisation Functions */ @@ -235,6 +242,7 @@ namespace SHADE using CsScriptManipFuncPtr = bool(*)(EntityID, const char*); using CsScriptBasicFuncPtr = void(*)(EntityID); using CsScriptOptionalFuncPtr = void(*)(EntityID, bool); + using CsScriptBoolFuncPtr = void(*)(bool); using CsScriptSerialiseYamlFuncPtr = bool(*)(EntityID, void*); using CsScriptDeserialiseYamlFuncPtr = bool(*)(EntityID, const void*); using CsScriptEditorFuncPtr = void(*)(EntityID); @@ -271,12 +279,15 @@ namespace SHADE CsScriptManipFuncPtr csScriptsAdd = nullptr; CsScriptBasicFuncPtr csScriptsRemoveAll = nullptr; CsScriptOptionalFuncPtr csScriptsRemoveAllImmediately = nullptr; + CsScriptBoolFuncPtr csScriptRemoveAllForAllNow = nullptr; CsScriptSerialiseYamlFuncPtr csScriptsSerialiseYaml = nullptr; CsScriptDeserialiseYamlFuncPtr csScriptsDeserialiseYaml = nullptr; // - Events CsEventRelayFuncPtr csColliderOnListChanged = nullptr; CsEventRelayFuncPtr csColliderOnRemoved = nullptr; CsEventRelayFuncPtr csSceneNodeChildrenChanged = nullptr; + CsEventRelayFuncPtr csUIElementOnRemoved = nullptr; + CsEventRelayFuncPtr csUIElementOnClicked = nullptr; // - Editor CsScriptEditorFuncPtr csEditorRenderScripts = nullptr; CsFuncPtr csEditorUndo = nullptr; @@ -289,8 +300,11 @@ namespace SHADE SHEventHandle onColliderAdded(SHEventPtr eventPtr); SHEventHandle onColliderRemoved(SHEventPtr eventPtr); SHEventHandle onColliderComponentRemoved(SHEventPtr eventPtr); + SHEventHandle onUIElementRemoved(SHEventPtr eventPtr); + SHEventHandle onUIElementClicked(SHEventPtr eventPtr); SHEventHandle onSceneNodeChildrenAdded(SHEventPtr eventPtr); SHEventHandle onSceneNodeChildrenRemoved(SHEventPtr eventPtr); + SHEventHandle onSceneDestroyed(SHEventPtr eventPtr); /*-----------------------------------------------------------------------------*/ /* Helper Functions */ diff --git a/SHADE_Engine/src/Scripting/SHScriptEngineRoutines.cpp b/SHADE_Engine/src/Scripting/SHScriptEngineRoutines.cpp index 699776ca..9f5b2818 100644 --- a/SHADE_Engine/src/Scripting/SHScriptEngineRoutines.cpp +++ b/SHADE_Engine/src/Scripting/SHScriptEngineRoutines.cpp @@ -65,7 +65,7 @@ namespace SHADE /* System Routine Functions - FrameCleanUpRoutine */ /*-----------------------------------------------------------------------------------*/ SHScriptEngine::FrameCleanUpRoutine::FrameCleanUpRoutine() - : SHSystemRoutine("Script Engine Frame Clean Up", true) + : SHSystemRoutine("Script Engine Frame Clean Up", true) {} void SHScriptEngine::FrameCleanUpRoutine::Execute(double) noexcept { diff --git a/SHADE_Managed/src/Assets/NativeAsset.cxx b/SHADE_Managed/src/Assets/NativeAsset.cxx index 9480b02a..2e32c459 100644 --- a/SHADE_Managed/src/Assets/NativeAsset.cxx +++ b/SHADE_Managed/src/Assets/NativeAsset.cxx @@ -41,6 +41,8 @@ namespace SHADE /*---------------------------------------------------------------------------------*/ Asset::operator bool(Asset asset) { + static_assert(INVALID_ASSET_ID == 0, + "This must be 0 due to the way structs are default initialized to ensure Assets are invalid if default constructed."); return asset.NativeAssetID != INVALID_ASSET_ID; } } \ No newline at end of file diff --git a/SHADE_Managed/src/Assets/NativeAsset.hxx b/SHADE_Managed/src/Assets/NativeAsset.hxx index 40f7e628..2a5a2906 100644 --- a/SHADE_Managed/src/Assets/NativeAsset.hxx +++ b/SHADE_Managed/src/Assets/NativeAsset.hxx @@ -22,7 +22,8 @@ of DigiPen Institute of Technology is prohibited. namespace SHADE { /// - /// Struct that contains native asset information. + /// Struct that contains native asset information. Default constructed assets have + /// an internval value of 0 which is the invalid ID. /// public value struct Asset { diff --git a/SHADE_Managed/src/Components/Renderable.cxx b/SHADE_Managed/src/Components/Renderable.cxx index 819760e6..1aa83637 100644 --- a/SHADE_Managed/src/Components/Renderable.cxx +++ b/SHADE_Managed/src/Components/Renderable.cxx @@ -30,34 +30,36 @@ namespace SHADE /*---------------------------------------------------------------------------------*/ /* Properties */ /*---------------------------------------------------------------------------------*/ - SHADE::MeshAsset^ Renderable::Mesh::get() + MeshAsset Renderable::Mesh::get() { - return gcnew SHADE::MeshAsset(GetNativeComponent()->GetMesh()); + auto mesh = GetNativeComponent()->GetMesh(); + return mesh ? MeshAsset(mesh) : MeshAsset(); } - void Renderable::Mesh::set(SHADE::MeshAsset^ value) + void Renderable::Mesh::set(MeshAsset value) { - if (value == nullptr) + if (value) { GetNativeComponent()->SetMesh(Handle()); } else { - GetNativeComponent()->SetMesh(value->NativeObject); + GetNativeComponent()->SetMesh(value.NativeObject); } } - SHADE::Material^ Renderable::Material::get() + SHADE::Material Renderable::Material::get() { - return gcnew SHADE::Material(GetNativeComponent()->GetMaterial()); + auto mat = GetNativeComponent()->GetMaterial(); + return mat ? SHADE::Material(mat) : SHADE::Material(); } - void Renderable::Material::set(SHADE::Material^ value) + void Renderable::Material::set(SHADE::Material value) { - if (value == nullptr) + if (value) { GetNativeComponent()->SetMaterial(Handle()); } else { - GetNativeComponent()->SetMaterial(Handle(Convert::ToNative(value->NativeObjectHandle))); + GetNativeComponent()->SetMaterial(Handle(Convert::ToNative(value.NativeObjectHandle))); } } System::Byte Renderable::LightLayer::get() diff --git a/SHADE_Managed/src/Components/Renderable.hxx b/SHADE_Managed/src/Components/Renderable.hxx index d52a01e5..b0786035 100644 --- a/SHADE_Managed/src/Components/Renderable.hxx +++ b/SHADE_Managed/src/Components/Renderable.hxx @@ -49,18 +49,18 @@ namespace SHADE /// /// Mesh used to render this Renderable. /// - property SHADE::MeshAsset^ Mesh + property MeshAsset Mesh { - SHADE::MeshAsset^ get(); - void set(SHADE::MeshAsset^ value); + MeshAsset get(); + void set(MeshAsset value); } /// /// Material used to render this Renderable. /// - property SHADE::Material^ Material + property SHADE::Material Material { - SHADE::Material^ get(); - void set(SHADE::Material^ value); + SHADE::Material get(); + void set(SHADE::Material value); } /// /// Material used to render this Renderable. diff --git a/SHADE_Managed/src/Components/TextRenderable.cxx b/SHADE_Managed/src/Components/TextRenderable.cxx index 88e43823..3eb1f3b9 100644 --- a/SHADE_Managed/src/Components/TextRenderable.cxx +++ b/SHADE_Managed/src/Components/TextRenderable.cxx @@ -39,19 +39,20 @@ namespace SHADE { GetNativeComponent()->SetText(Convert::ToNative(value)); } - SHADE::FontAsset^ TextRenderable::Font::get() + FontAsset TextRenderable::Font::get() { - return gcnew SHADE::FontAsset(GetNativeComponent()->GetFont()); + auto font = GetNativeComponent()->GetFont(); + return font ? FontAsset(font) : FontAsset(); } - void TextRenderable::Font::set(SHADE::FontAsset^ value) + void TextRenderable::Font::set(FontAsset value) { - if (value == nullptr) + if (value) { GetNativeComponent()->SetFont(Handle()); } else { - GetNativeComponent()->SetFont(value->NativeObject); + GetNativeComponent()->SetFont(value.NativeObject); } } } diff --git a/SHADE_Managed/src/Components/TextRenderable.hxx b/SHADE_Managed/src/Components/TextRenderable.hxx index bcd99bcf..b17a919c 100644 --- a/SHADE_Managed/src/Components/TextRenderable.hxx +++ b/SHADE_Managed/src/Components/TextRenderable.hxx @@ -55,10 +55,10 @@ namespace SHADE /// /// Font to use to render using this TextRenderable. /// - property SHADE::FontAsset^ Font + property FontAsset Font { - SHADE::FontAsset^ get(); - void set(SHADE::FontAsset^ value); + FontAsset get(); + void set(FontAsset value); } }; } diff --git a/SHADE_Managed/src/Components/UIElement.cxx b/SHADE_Managed/src/Components/UIElement.cxx new file mode 100644 index 00000000..d76d6d42 --- /dev/null +++ b/SHADE_Managed/src/Components/UIElement.cxx @@ -0,0 +1,75 @@ +/************************************************************************************//*! +\file UIElement.cxx +\author Tng Kah Wei, kahwei.tng, 390009620 +\par email: kahwei.tng\@digipen.edu +\date Jan 30, 2023 +\brief Contains the definition of the functions of the managed UIElement class. + + Note: This file is written in C++17/CLI. + +Copyright (C) 2023 DigiPen Institute of Technology. +Reproduction or disclosure of this file or its contents without the prior written consent +of DigiPen Institute of Technology is prohibited. +*//*************************************************************************************/ +// Precompiled Headers +#include "SHpch.h" +// Primary Header +#include "UIElement.hxx" +#include "Assets/NativeAsset.hxx" +#include "Utility/Convert.hxx" +#include "Utility/Debug.hxx" + +namespace SHADE +{ + /*---------------------------------------------------------------------------------*/ + /* Constructors */ + /*---------------------------------------------------------------------------------*/ + UIElement::UIElement(Entity entity) + : Component(entity) + {} + + /*---------------------------------------------------------------------------------*/ + /* Properties */ + /*---------------------------------------------------------------------------------*/ + CallbackEvent^ UIElement::OnClick::get() + { + // Create map if it wasn't before + if (onClickEventMap == nullptr) + { + onClickEventMap = gcnew System::Collections::Generic::Dictionary(); + } + + // Create event if it wasn't before + if (!onClickEventMap->ContainsKey(owner.EntityId)) + { + onClickEventMap->Add(owner.EntityId, gcnew CallbackEvent()); + } + + // Return the event + return onClickEventMap[owner.EntityId]; + } + + /*---------------------------------------------------------------------------------*/ + /* Event Handling Functions */ + /*-----------------------------------------------------------------------------a----*/ + void UIElement::OnComponentRemoved(EntityID entity) + { + SAFE_NATIVE_CALL_BEGIN + // Remove the event if it contained an event + if (onClickEventMap != nullptr && onClickEventMap->ContainsKey(entity)) + { + onClickEventMap->Remove(entity); + } + SAFE_NATIVE_CALL_END("UIElement.OnComponentRemoved") + } + void UIElement::OnClicked(EntityID entity) + { + SAFE_NATIVE_CALL_BEGIN + // Remove the event if it contained an event + if (onClickEventMap != nullptr && onClickEventMap->ContainsKey(entity)) + { + onClickEventMap[entity]->Invoke(); + } + SAFE_NATIVE_CALL_END("UIElement.OnClicked") + } +} diff --git a/SHADE_Managed/src/Components/UIElement.hxx b/SHADE_Managed/src/Components/UIElement.hxx new file mode 100644 index 00000000..a969e4b5 --- /dev/null +++ b/SHADE_Managed/src/Components/UIElement.hxx @@ -0,0 +1,75 @@ +/************************************************************************************//*! +\file UIElement.hxx +\author Tng Kah Wei, kahwei.tng, 390009620 +\par email: kahwei.tng\@digipen.edu +\date Jan 30, 2023 +\brief Contains the definition of the managed UIElement class with the + declaration of functions for working with it. + + Note: This file is written in C++17/CLI. + +Copyright (C) 2023 DigiPen Institute of Technology. +Reproduction or disclosure of this file or its contents without the prior written consent +of DigiPen Institute of Technology is prohibited. +*//*************************************************************************************/ +#pragma once + +// Project Includes +#include "Components/Component.hxx" +#include "Math/Vector3.hxx" +#include "Math/Quaternion.hxx" +// External Dependencies +#include "UI/SHUIComponent.h" + +namespace SHADE +{ + /// + /// CLR version of the SHADE Engine's SHUIComponent. + /// + public ref class UIElement : public Component + { + internal: + /*-----------------------------------------------------------------------------*/ + /* Constructors */ + /*-----------------------------------------------------------------------------*/ + /// + /// Constructs a UIElement Component that represents a native SHUIComponent + /// tied to the specified Entity. + /// + /// Entity that this Component will be tied to. + UIElement(Entity entity); + + public: + /*-----------------------------------------------------------------------------*/ + /* Properties */ + /*-----------------------------------------------------------------------------*/ + /// + /// Event that is raised when this UIElement is clicked. + /// + property CallbackEvent^ OnClick + { + CallbackEvent^ get(); + } + + private: + /*-----------------------------------------------------------------------------*/ + /* Event Handling Functions */ + /*-----------------------------------------------------------------------------*/ + /// + /// To be called from native code when this component is removed. + /// + /// The entity which has it's component removed. + static void OnComponentRemoved(EntityID entity); + /// + /// To be called from native code when this component is clicked. + /// + /// The entity which was clicked. + static void OnClicked(EntityID entity); + + /*-----------------------------------------------------------------------------*/ + /* Static Data Members */ + /*-----------------------------------------------------------------------------*/ + static System::Collections::Generic::Dictionary^ onClickEventMap; + }; +} + diff --git a/SHADE_Managed/src/Engine/ECS.cxx b/SHADE_Managed/src/Engine/ECS.cxx index c388f0cd..ecd3784d 100644 --- a/SHADE_Managed/src/Engine/ECS.cxx +++ b/SHADE_Managed/src/Engine/ECS.cxx @@ -29,6 +29,7 @@ of DigiPen Institute of Technology is prohibited. #include "Tools/Logger/SHLog.h" #include "Graphics\MiddleEnd\Interface\SHRenderable.h" #include "Graphics\MiddleEnd\TextRendering\SHTextRenderableComponent.h" +#include "UI\SHUIComponent.h" // Project Headers #include "Utility/Convert.hxx" #include "Utility/Debug.hxx" @@ -40,6 +41,7 @@ of DigiPen Institute of Technology is prohibited. #include "Components/Light.hxx" #include "Components\Renderable.hxx" #include "Components\TextRenderable.hxx" +#include "Components\UIElement.hxx" namespace SHADE { @@ -324,6 +326,7 @@ namespace SHADE componentMap.Add(createComponentSet()); componentMap.Add(createComponentSet()); componentMap.Add(createComponentSet()); + componentMap.Add(createComponentSet()); } /*---------------------------------------------------------------------------------*/ diff --git a/SHADE_Managed/src/Engine/GameObject.cxx b/SHADE_Managed/src/Engine/GameObject.cxx index 0e03a643..a78576cb 100644 --- a/SHADE_Managed/src/Engine/GameObject.cxx +++ b/SHADE_Managed/src/Engine/GameObject.cxx @@ -259,6 +259,13 @@ namespace SHADE ScriptStore::RemoveScript(entity); } + void GameObject::RemoveScript(Script^ script) + { + if (!valid) + throw gcnew System::NullReferenceException(); + ScriptStore::RemoveScript(entity, script); + } + /*---------------------------------------------------------------------------------*/ /* Scene Graph Functions */ /*---------------------------------------------------------------------------------*/ diff --git a/SHADE_Managed/src/Engine/GameObject.hxx b/SHADE_Managed/src/Engine/GameObject.hxx index 1ebfc250..054075ac 100644 --- a/SHADE_Managed/src/Engine/GameObject.hxx +++ b/SHADE_Managed/src/Engine/GameObject.hxx @@ -259,6 +259,12 @@ namespace SHADE /// Type of Scripts to remove. generic where T : ref class, Script void RemoveScript(); + /// + /// Removes a specific script from this script's parent. + /// + /// The script to remove. + /// True if successfully removed. False otherwise. + void RemoveScript(Script^ script); /*-----------------------------------------------------------------------------*/ /* Scene Graph Functions */ diff --git a/SHADE_Managed/src/Scripts/Script.cxx b/SHADE_Managed/src/Scripts/Script.cxx index 294f4096..4a84565f 100644 --- a/SHADE_Managed/src/Scripts/Script.cxx +++ b/SHADE_Managed/src/Scripts/Script.cxx @@ -126,6 +126,11 @@ namespace SHADE ScriptStore::RemoveScript(owner.GetEntity()); } + void Script::RemoveScript(Script^ script) + { + ScriptStore::RemoveScript(owner.GetEntity(), script); + } + Script::operator bool(Script^ s) { return s != nullptr; diff --git a/SHADE_Managed/src/Scripts/Script.hxx b/SHADE_Managed/src/Scripts/Script.hxx index 8fc36544..c2906bd4 100644 --- a/SHADE_Managed/src/Scripts/Script.hxx +++ b/SHADE_Managed/src/Scripts/Script.hxx @@ -198,6 +198,12 @@ namespace SHADE /// generic where T : ref class, Script void RemoveScript(); + /// + /// Removes a specific script from this script's parent. + /// + /// The script to remove. + /// True if successfully removed. False otherwise. + void RemoveScript(Script^ script); /*-----------------------------------------------------------------------------*/ /* Operator Overloads */ diff --git a/SHADE_Managed/src/Scripts/ScriptStore.cxx b/SHADE_Managed/src/Scripts/ScriptStore.cxx index a5a0ebc7..c1747852 100644 --- a/SHADE_Managed/src/Scripts/ScriptStore.cxx +++ b/SHADE_Managed/src/Scripts/ScriptStore.cxx @@ -400,24 +400,24 @@ namespace SHADE return; // Clear all - System::Collections::Generic::List^ scriptList = scripts[entity]; - for each (Script ^ script in scriptList) + removeAllScriptsImmediately(entity, callOnDestroy && Application::IsPlaying || Application::IsPaused); + SAFE_NATIVE_CALL_END_N("SHADE_Managed.ScriptStore") + } + + void ScriptStore::RemoveAllScriptsFromAllImmediately(bool callOnDestroy) + { + SAFE_NATIVE_CALL_BEGIN + // Clear all + for each (System::Collections::Generic::KeyValuePair^ pair in scripts) { - // Call OnDestroy only if indicated and also if the game has run - if (callOnDestroy && Application::IsPlaying || Application::IsPaused) - { - script->OnDestroy(); - } - script->OnDetached(); - - // Remove scripts from awakening if they were not woken up to begin with - awakeList.Remove(script); - startList.Remove(script); + removeAllScriptsImmediately(pair->Key, callOnDestroy); } - scriptList->Clear(); + awakeList.Clear(); + startList.Clear(); + disposalQueue.Clear(); SAFE_NATIVE_CALL_END_N("SHADE_Managed.ScriptStore") } - + /*---------------------------------------------------------------------------------*/ /* Lifecycle Functions */ /*---------------------------------------------------------------------------------*/ @@ -798,6 +798,25 @@ namespace SHADE script->OnDetached(); } + void ScriptStore::removeAllScriptsImmediately(Entity entity, bool callOnDestroy) + { + System::Collections::Generic::List^ scriptList = scripts[entity]; + for each (Script ^ script in scriptList) + { + // Call OnDestroy only if indicated and also if the game has run + if (callOnDestroy) + { + script->OnDestroy(); + } + script->OnDetached(); + + // Remove scripts from awakening if they were not woken up to begin with + awakeList.Remove(script); + startList.Remove(script); + } + scriptList->Clear(); + } + namespace { /* Select Many */ diff --git a/SHADE_Managed/src/Scripts/ScriptStore.hxx b/SHADE_Managed/src/Scripts/ScriptStore.hxx index f31836d8..bac58a77 100644 --- a/SHADE_Managed/src/Scripts/ScriptStore.hxx +++ b/SHADE_Managed/src/Scripts/ScriptStore.hxx @@ -203,7 +203,7 @@ namespace SHADE generic where T : ref class, Script static void RemoveScript(Entity entity); /// - /// Removes a specific script from the + /// Removes a specific script from the specified entity. /// /// The entity to remove the script from. /// The script to remove. @@ -228,6 +228,13 @@ namespace SHADE /// play mode. /// static void RemoveAllScriptsImmediately(Entity entity, bool callOnDestroy); + /// + /// Removes all Scripts attached to all entities immediately. The + /// + /// + /// Whether or not to call OnDestroy on the scripts. + /// + static void RemoveAllScriptsFromAllImmediately(bool callOnDestroy); internal: /*-----------------------------------------------------------------------------*/ @@ -343,6 +350,7 @@ namespace SHADE /* Helper Functions */ /*-----------------------------------------------------------------------------*/ static void removeScript(Script^ script); + static void removeAllScriptsImmediately(Entity script, bool callOnDestroy); static void refreshScriptTypeList(); static void getGenericMethods(); static System::Type^ getScriptType(System::String^ scriptName);