From 9be58e7e5d961df6c90f60fbc20a4f8ea38a927d Mon Sep 17 00:00:00 2001 From: Kah Wei Date: Tue, 8 Nov 2022 18:32:13 +0800 Subject: [PATCH 1/7] Added SceneGraph interaction functions in GameObject --- SHADE_Managed/src/Engine/ChildListCache.cxx | 89 ++++++++++++++ SHADE_Managed/src/Engine/ChildListCache.hxx | 80 +++++++++++++ SHADE_Managed/src/Engine/EngineInterface.hxx | 2 +- SHADE_Managed/src/Engine/GameObject.cxx | 120 +++++++++++++++++-- SHADE_Managed/src/Engine/GameObject.hxx | 102 ++++++++++++++-- 5 files changed, 372 insertions(+), 21 deletions(-) create mode 100644 SHADE_Managed/src/Engine/ChildListCache.cxx create mode 100644 SHADE_Managed/src/Engine/ChildListCache.hxx diff --git a/SHADE_Managed/src/Engine/ChildListCache.cxx b/SHADE_Managed/src/Engine/ChildListCache.cxx new file mode 100644 index 00000000..b183646f --- /dev/null +++ b/SHADE_Managed/src/Engine/ChildListCache.cxx @@ -0,0 +1,89 @@ +/************************************************************************************//*! +\file ChildListCache.cxx +\author Tng Kah Wei, kahwei.tng, 390009620 +\par email: kahwei.tng\@digipen.edu +\date Nov 11, 2022 +\brief Contains the definition of the functions for the ChildListCache managed + class. + + Note: This file is written in C++17/CLI. + +Copyright (C) 2022 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 "ChildListCache.hxx" +// External Dependencies +#include "Scene/SHSceneManager.h" +// Project Headers +#include "Utility/Debug.hxx" + +namespace SHADE +{ + /*---------------------------------------------------------------------------------*/ + /* Static Usage Functions */ + /*---------------------------------------------------------------------------------*/ + ChildListCache::ChildEnumerable^ ChildListCache::GetChildList(Entity entity) + { + // Ignore if invalid + if (entity == MAX_EID) + return nullptr; + + // Check if in cache + if (cachedLists->ContainsKey(entity)) + return cachedLists[entity]; + + // Grab the native child list + auto node = GameObject(entity).GetSceneNode(); + if (!node || node->GetChildren().empty()) + return nullptr; + + // Otherwise + // - Create the list + ChildList^ list = gcnew ChildList(); + updateChildList(list, node); + // - Cache it + cachedLists[entity] = list; + + return list; + } + + void ChildListCache::UpdateChildList(Entity entity) + { + // Ignore if invalid + if (entity == MAX_EID) + return; + + // Check if in cache + if (!cachedLists->ContainsKey(entity)) + return; + + // Update + updateChildList(cachedLists[entity], GameObject(entity).GetSceneNode()); + } + + /*---------------------------------------------------------------------------------*/ + /* Event Handling Functions */ + /*---------------------------------------------------------------------------------*/ + void ChildListCache::OnChildrenChanged(EntityID entity) + { + SAFE_NATIVE_CALL_BEGIN + UpdateChildList(entity); + SAFE_NATIVE_CALL_END_N("SHADE_Managed.ChildListCache") + } + + /*---------------------------------------------------------------------------------*/ + /* Helper Functions */ + /*---------------------------------------------------------------------------------*/ + void ChildListCache::updateChildList(ChildList^ list, const SHSceneNode* sceneNode) + { + list->Clear(); + for (auto node : sceneNode->GetChildren()) + { + list->Add(GameObject(node->GetEntityID())); + } + } +} diff --git a/SHADE_Managed/src/Engine/ChildListCache.hxx b/SHADE_Managed/src/Engine/ChildListCache.hxx new file mode 100644 index 00000000..1a2637d3 --- /dev/null +++ b/SHADE_Managed/src/Engine/ChildListCache.hxx @@ -0,0 +1,80 @@ +/************************************************************************************//*! +\file ChildListCache.hxx +\author Tng Kah Wei, kahwei.tng, 390009620 +\par email: kahwei.tng\@digipen.edu +\date Nov 11, 2022 +\brief Contains the definition of the ChildListCache managed class. + + Note: This file is written in C++17/CLI. + +Copyright (C) 2022 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 "GameObject.hxx" + +namespace SHADE { } + +namespace SHADE +{ + /*---------------------------------------------------------------------------------*/ + /* Forward Declarations */ + /*---------------------------------------------------------------------------------*/ + class SHSceneNode; + + /*---------------------------------------------------------------------------------*/ + /* Type Definitions */ + /*---------------------------------------------------------------------------------*/ + /// + /// Static class that caches all the lists of children for GameObjects. + /// + private ref class ChildListCache abstract sealed + { + public: + /*-----------------------------------------------------------------------------*/ + /* Type Definitions */ + /*-----------------------------------------------------------------------------*/ + using ChildList = System::Collections::Generic::List; + using ChildEnumerable = System::Collections::Generic::IEnumerable; + using ListMap = System::Collections::Generic::Dictionary; + + internal: + /*-----------------------------------------------------------------------------*/ + /* Static Usage Functions */ + /*-----------------------------------------------------------------------------*/ + /// + /// Retrieves the children list for the specified Entity. + /// + /// + /// Enumerable read only list of an Entity's children. Null if entity is invalid + /// or there are no children. + /// + static ChildEnumerable^ GetChildList(Entity entity); + /// + /// Updates the children list for the specified Entity if it exists. + /// + static void UpdateChildList(Entity entity); + + /*-----------------------------------------------------------------------------*/ + /* Event Handling Functions */ + /*-----------------------------------------------------------------------------*/ + /// + /// To be + /// + static void OnChildrenChanged(EntityID entity); + + private: + /*-----------------------------------------------------------------------------*/ + /* Static Data Members */ + /*-----------------------------------------------------------------------------*/ + static ListMap^ cachedLists = gcnew ListMap(); + + /*-----------------------------------------------------------------------------*/ + /* Helper Functions */ + /*-----------------------------------------------------------------------------*/ + static void updateChildList(ChildList^ list, const SHSceneNode* sceneNode); + }; +} \ No newline at end of file diff --git a/SHADE_Managed/src/Engine/EngineInterface.hxx b/SHADE_Managed/src/Engine/EngineInterface.hxx index 4fd8f7b3..37ded4eb 100644 --- a/SHADE_Managed/src/Engine/EngineInterface.hxx +++ b/SHADE_Managed/src/Engine/EngineInterface.hxx @@ -20,7 +20,7 @@ namespace SHADE { /// /// Static class that contains the functions for interfacing with the core - /// PlushieEngine written in C++ for managing the lifecycle of managed code. + /// SHADE Engine written in C++ for managing the lifecycle of managed code. /// private ref class EngineInterface abstract sealed { diff --git a/SHADE_Managed/src/Engine/GameObject.cxx b/SHADE_Managed/src/Engine/GameObject.cxx index 9f15c6c9..ece163f9 100644 --- a/SHADE_Managed/src/Engine/GameObject.cxx +++ b/SHADE_Managed/src/Engine/GameObject.cxx @@ -23,6 +23,7 @@ of DigiPen Institute of Technology is prohibited. #include "Utility/Convert.hxx" #include "Scripts/ScriptStore.hxx" #include "Utility/Debug.hxx" +#include "ChildListCache.hxx" namespace SHADE { @@ -87,30 +88,43 @@ namespace SHADE throw gcnew System::NullReferenceException(); return entity; } - GameObject^ GameObject::Parent::get() + GameObject GameObject::Parent::get() { if (!valid) throw gcnew System::NullReferenceException(); + const auto& SCENE_GRAPH = SHSceneManager::GetCurrentSceneGraph(); const auto* ROOT = SCENE_GRAPH.GetRoot(); - const auto* NODE = SCENE_GRAPH.GetNode(entity); if (NODE == nullptr) throw gcnew System::InvalidOperationException("Unable to retrieve SceneGraphNode for Entity " + entity.ToString()); const auto* PARENT = NODE->GetParent(); - return PARENT != ROOT ? gcnew GameObject(PARENT->GetEntityID()) : nullptr; + return PARENT != ROOT ? GameObject(PARENT->GetEntityID()) : GameObject(); } - void GameObject::Parent::set(GameObject^ newParent) + void GameObject::Parent::set(GameObject newParent) { if (!valid) throw gcnew System::NullReferenceException(); const auto& SCENE_GRAPH = SHSceneManager::GetCurrentSceneGraph(); - if (newParent == nullptr) - SCENE_GRAPH.SetParent(entity, nullptr); + if (newParent) + SCENE_GRAPH.SetParent(entity, newParent.EntityId); else - SCENE_GRAPH.SetParent(entity, newParent->EntityId); + SCENE_GRAPH.SetParent(entity, nullptr); + } + int GameObject::ChildCount::get() + { + if (!valid) + throw gcnew System::NullReferenceException(); + + const auto& SCENE_GRAPH = SHSceneManager::GetCurrentSceneGraph(); + const auto* ROOT = SCENE_GRAPH.GetRoot(); + const auto* NODE = SCENE_GRAPH.GetNode(entity); + if (NODE == nullptr) + throw gcnew System::InvalidOperationException("Unable to retrieve SceneGraphNode for Entity " + entity.ToString()); + + return static_cast(NODE->GetChildren().size()); } /*---------------------------------------------------------------------------------*/ @@ -215,6 +229,88 @@ namespace SHADE ScriptStore::RemoveScript(entity); } + /*---------------------------------------------------------------------------------*/ + /* Scene Graph Functions */ + /*---------------------------------------------------------------------------------*/ + void GameObject::DetachChildren() + { + // Validity Checks + if (!valid) + throw gcnew System::NullReferenceException(); + auto node = GetSceneNode(); + if (!node) + throw gcnew System::NullReferenceException(); + + // Unparent all children to the root + for (auto child : node->GetChildren()) + { + child->SetParent(nullptr); + ChildListCache::UpdateChildList(child->GetEntityID()); + } + ChildListCache::UpdateChildList(entity); + } + + GameObject GameObject::GetChild(int index) + { + // Validity Checks + if (!valid) + throw gcnew System::NullReferenceException(); + auto node = GetSceneNode(); + if (!node) + throw gcnew System::NullReferenceException(); + + auto child = node->GetChild(index); + return child ? GameObject(child->GetEntityID()) : GameObject(); + } + + System::Collections::Generic::IEnumerable^ GameObject::GetChildren() + { + return ChildListCache::GetChildList(entity); + } + + int GameObject::GetSiblingIndex() + { + throw gcnew System::NotImplementedException(); + } + + bool GameObject::IsChildOf(GameObject gameObj) + { + // Search parents recursively + auto node = GetSceneNode(); + while (node != nullptr) + { + if (node->GetEntityID() == gameObj.entity) + return true; + + // Go up higher + node = node->GetParent(); + } + return false; + } + + void GameObject::SetAsFirstSibling() + { + throw gcnew System::NotImplementedException(); + } + + void GameObject::SetAsLastSibling() + { + throw gcnew System::NotImplementedException(); + } + + void GameObject::SetSiblingIndex(int index) + { + throw gcnew System::NotImplementedException(); + } + + /*---------------------------------------------------------------------------------*/ + /* Operator Overloads */ + /*---------------------------------------------------------------------------------*/ + GameObject::operator bool(GameObject gameObj) + { + return gameObj.valid; + } + /*---------------------------------------------------------------------------------*/ /* Constructors */ /*---------------------------------------------------------------------------------*/ @@ -245,11 +341,15 @@ namespace SHADE } /*---------------------------------------------------------------------------------*/ - /* Operator Overloads */ + /* Helper Functions */ /*---------------------------------------------------------------------------------*/ - GameObject::operator bool(GameObject gameObj) + SHSceneNode* GameObject::GetSceneNode() { - return gameObj.valid; + const auto& SCENE_GRAPH = SHSceneManager::GetCurrentSceneGraph(); + const auto* ROOT = SCENE_GRAPH.GetRoot(); + if (!ROOT) + return nullptr; + return SCENE_GRAPH.GetNode(entity); } /*---------------------------------------------------------------------------------*/ diff --git a/SHADE_Managed/src/Engine/GameObject.hxx b/SHADE_Managed/src/Engine/GameObject.hxx index 030b917c..ec75f9d9 100644 --- a/SHADE_Managed/src/Engine/GameObject.hxx +++ b/SHADE_Managed/src/Engine/GameObject.hxx @@ -20,7 +20,7 @@ of DigiPen Institute of Technology is prohibited. namespace SHADE { /*---------------------------------------------------------------------------------*/ - /* Forward Declarations */ + /* Forward Declarations */ /*---------------------------------------------------------------------------------*/ ref class Script; ref class BaseComponent; @@ -97,10 +97,17 @@ namespace SHADE /// /// The parent entity for this GameObject. /// - property GameObject^ Parent + property GameObject Parent { - GameObject^ get(); - void set(GameObject^); + GameObject get(); + void set(GameObject); + } + /// + /// Number of Children held by this GameObject + /// + property int ChildCount + { + int get(); } /*-----------------------------------------------------------------------------*/ @@ -120,8 +127,7 @@ namespace SHADE /// /// Whether to activate or deactivate this GameObject. /// - void SetActive(bool active); - + void SetActive(bool active); /*-----------------------------------------------------------------------------*/ /* Component Access Functions */ @@ -214,6 +220,82 @@ namespace SHADE generic where T : ref class, Script void RemoveScript(); + /*-----------------------------------------------------------------------------*/ + /* Scene Graph Functions */ + /*-----------------------------------------------------------------------------*/ + /// + /// Unparents all children. Useful if you want to destroy the root of a hierarchy + /// without destroying the children. + /// + void DetachChildren(); + /// + /// Returns a child by index. + /// + /// Index of the child GameObject to retrieve. + /// + /// Handle to the GameObject if the index is valid. Invalid GameObject otherwise. + /// + GameObject GetChild(int index); + /// + /// Returns a cached enumerable container of child GameObjects of this + /// GameObject. + /// + /// + /// Enumerable container of child GameObjects of this GameObject. Null if + /// ChildCount is 0. + /// + System::Collections::Generic::IEnumerable^ GetChildren(); + /// + /// Gets the sibling index. Use GetSiblingIndex to find out the GameObject’s + /// place in this hierarchy. When the sibling index of a GameObject is changed, + /// its order in the Hierarchy window will also change. + /// + /// + /// Index of this GameObject among the parent GameObject's children. + /// + [System::ObsoleteAttribute("Not yet implemented.", true)] + int GetSiblingIndex(); + /// + /// Checks if this GameObject a direct or indirect child of the specified + /// GameObject. + /// + /// + /// True if this GameObject is a child, deep child (child of a child) or + /// identical to this GameObject, otherwise false. + /// + bool IsChildOf(GameObject gameObj); + /// + /// Move the GameObject to the start of the parent GameObject's children list. + /// + [System::ObsoleteAttribute("Not yet implemented.", true)] + void SetAsFirstSibling(); + /// + /// Move the GameObject to the end of the parent GameObject's children list. + /// + [System::ObsoleteAttribute("Not yet implemented.", true)] + void SetAsLastSibling(); + /// + /// Move the GameObject to the specified position in the parent GameObject's + /// children list. An existing object at that position if any, will be pushed + /// to the next index (existing element will be at index + 1). + /// + /// + /// Position to place this GameObject at in the hierarchy. Clamped to between + /// [0, parent.ChildCount]. + /// + [System::ObsoleteAttribute("Not yet implemented.", true)] + void SetSiblingIndex(int index); + + /*-----------------------------------------------------------------------------*/ + /* Operator Overloads */ + /*-----------------------------------------------------------------------------*/ + /// + /// Implicit conversion operator to enable checking if a GameObject is valid. + /// + /// GameObjects to check. + /// True if the GameObject is valid. + static operator bool(GameObject gameObj); + internal: /*-----------------------------------------------------------------------------*/ /* Constructors */ @@ -249,13 +331,13 @@ namespace SHADE SHEntity& GetNativeEntity(); /*-----------------------------------------------------------------------------*/ - /* Operator Overloads */ + /* Helper Functions */ /*-----------------------------------------------------------------------------*/ /// - /// Implicit conversion operator to enable checking if a GameObject is valid. + /// Retrieves the SceneNode for this GameObject's referenced entity. /// - /// GameObjects to check. - static operator bool(GameObject gameObj); + /// Pointer to the SceneNode for this GameObject.. + SHSceneNode* GetSceneNode(); private: /*-----------------------------------------------------------------------------*/ From 8512c658f9503acc83dca3a5e0ad9c6ba8089732 Mon Sep 17 00:00:00 2001 From: Kah Wei Date: Tue, 8 Nov 2022 18:32:47 +0800 Subject: [PATCH 2/7] Made GameObject an IEnumerable to follow Unity's Transform interface for iterating through children --- SHADE_Managed/src/Engine/GameObject.cxx | 20 ++++++++++++++++++++ SHADE_Managed/src/Engine/GameObject.hxx | 11 ++++++++++- TempScriptsFolder/RaccoonShowcase.cs | 5 +++++ 3 files changed, 35 insertions(+), 1 deletion(-) diff --git a/SHADE_Managed/src/Engine/GameObject.cxx b/SHADE_Managed/src/Engine/GameObject.cxx index ece163f9..e7cc4445 100644 --- a/SHADE_Managed/src/Engine/GameObject.cxx +++ b/SHADE_Managed/src/Engine/GameObject.cxx @@ -265,6 +265,9 @@ namespace SHADE System::Collections::Generic::IEnumerable^ GameObject::GetChildren() { + // Validity Checks + if (!valid) + throw gcnew System::NullReferenceException(); return ChildListCache::GetChildList(entity); } @@ -390,4 +393,21 @@ namespace SHADE { return !(lhs == rhs); } + + /*---------------------------------------------------------------------------------*/ + /* IEnummerable */ + /*---------------------------------------------------------------------------------*/ + System::Collections::Generic::IEnumerator^ GameObject::GetEnumerator() + { + System::Collections::Generic::IEnumerable^ childList = GetChildren(); + if (childList == nullptr) + return System::Linq::Enumerable::Empty()->GetEnumerator(); + else + return childList->GetEnumerator(); + } + + System::Collections::IEnumerator^ GameObject::GetEnumeratorNonGeneric() + { + return GetEnumerator(); + } } diff --git a/SHADE_Managed/src/Engine/GameObject.hxx b/SHADE_Managed/src/Engine/GameObject.hxx index ec75f9d9..ce1dd3eb 100644 --- a/SHADE_Managed/src/Engine/GameObject.hxx +++ b/SHADE_Managed/src/Engine/GameObject.hxx @@ -32,8 +32,9 @@ namespace SHADE /// Lightweight object for an Entity that allows for easy access to Component and /// Script operations. /// Can be set to a invalid/null GameObject by default construction. + /// Can also be iterated to access children. /// - public value class GameObject : public System::IEquatable + public value class GameObject : public System::IEquatable, public System::Collections::Generic::IEnumerable { public: /*-----------------------------------------------------------------------------*/ @@ -386,6 +387,14 @@ namespace SHADE /// Another GameObject to check with. /// True if both Components are different. static bool operator!=(GameObject lhs, GameObject rhs); + + /*-----------------------------------------------------------------------------*/ + /* IEnummerable */ + /*-----------------------------------------------------------------------------*/ + /// + System::Collections::Generic::IEnumerator^ GetEnumerator() override; + /// + System::Collections::IEnumerator^ GetEnumeratorNonGeneric() override = System::Collections::IEnumerable::GetEnumerator; }; } diff --git a/TempScriptsFolder/RaccoonShowcase.cs b/TempScriptsFolder/RaccoonShowcase.cs index da0b89d2..2fc8104d 100644 --- a/TempScriptsFolder/RaccoonShowcase.cs +++ b/TempScriptsFolder/RaccoonShowcase.cs @@ -27,6 +27,11 @@ public class RaccoonShowcase : Script Debug.LogError("Transform is NULL!"); } + foreach (var child in Owner) + { + Debug.Log(child.Name); + } + originalScale = Transform.LocalScale.z; } protected override void update() From d93ea8e49bb36ae96c33e71497b1d3697487fe89 Mon Sep 17 00:00:00 2001 From: Kah Wei Date: Thu, 10 Nov 2022 23:34:53 +0800 Subject: [PATCH 3/7] Child caches are now updated when scene hierarchy changes --- SHADE_Engine/src/Scripting/SHScriptEngine.cpp | 38 ++++++++++++++++++- SHADE_Engine/src/Scripting/SHScriptEngine.h | 3 ++ SHADE_Managed/src/Engine/GameObject.cxx | 3 +- 3 files changed, 40 insertions(+), 4 deletions(-) diff --git a/SHADE_Engine/src/Scripting/SHScriptEngine.cpp b/SHADE_Engine/src/Scripting/SHScriptEngine.cpp index 276eeb24..f279bec1 100644 --- a/SHADE_Engine/src/Scripting/SHScriptEngine.cpp +++ b/SHADE_Engine/src/Scripting/SHScriptEngine.cpp @@ -318,6 +318,20 @@ namespace SHADE return eventData->handle; } + SHEventHandle SHScriptEngine::onSceneNodeChildrenAdded(SHEventPtr eventPtr) + { + auto eventData = reinterpret_cast*>(eventPtr.get()); + csSceneNodeChildrenChanged(eventData->data->parent->GetEntityID()); + return eventData->handle; + } + + SHEventHandle SHScriptEngine::onSceneNodeChildrenRemoved(SHEventPtr eventPtr) + { + auto eventData = reinterpret_cast*>(eventPtr.get()); + csSceneNodeChildrenChanged(eventData->data->parent->GetEntityID()); + return eventData->handle; + } + /*-----------------------------------------------------------------------------------*/ /* Helper Functions */ /*-----------------------------------------------------------------------------------*/ @@ -442,6 +456,12 @@ namespace SHADE DEFAULT_CSHARP_NAMESPACE + ".Collider", "OnCollisionShapeRemoved" ); + csSceneNodeChildrenChanged = dotNet.GetFunctionPtr + ( + DEFAULT_CSHARP_LIB_NAME, + DEFAULT_CSHARP_NAMESPACE + ".ChildListCache", + "OnChildrenChanged" + ); csEditorRenderScripts = dotNet.GetFunctionPtr ( DEFAULT_CSHARP_LIB_NAME, @@ -464,6 +484,7 @@ namespace SHADE void SHScriptEngine::registerEvents() { + /* Entity */ // Register for entity destroyed event std::shared_ptr> destroyedEventReceiver { @@ -471,26 +492,39 @@ namespace SHADE }; SHEventManager::SubscribeTo(SH_ENTITY_DESTROYED_EVENT, std::dynamic_pointer_cast(destroyedEventReceiver)); + /* Colliders */ // Register for collider added event std::shared_ptr> addedColliderEventReceiver { std::make_shared>(this, &SHScriptEngine::onColliderAdded) }; SHEventManager::SubscribeTo(SH_PHYSICS_COLLIDER_ADDED_EVENT, std::dynamic_pointer_cast(addedColliderEventReceiver)); - // Register for collider removed event std::shared_ptr> removedColliderEventReceiver { std::make_shared>(this, &SHScriptEngine::onColliderRemoved) }; SHEventManager::SubscribeTo(SH_PHYSICS_COLLIDER_REMOVED_EVENT, std::dynamic_pointer_cast(removedColliderEventReceiver)); - // Register for collider component removed event std::shared_ptr> removedColliderComponentEventReceiver { std::make_shared>(this, &SHScriptEngine::onColliderComponentRemoved) }; SHEventManager::SubscribeTo(SH_COMPONENT_REMOVED_EVENT, std::dynamic_pointer_cast(removedColliderComponentEventReceiver)); + + /* SceneGraph */ + // Register for SceneNode child added event + std::shared_ptr> addChildEventReceiver + { + std::make_shared>(this, &SHScriptEngine::onSceneNodeChildrenAdded) + }; + SHEventManager::SubscribeTo(SH_SCENEGRAPH_ADD_CHILD_EVENT, std::dynamic_pointer_cast(addChildEventReceiver)); + // Register for SceneNode child removed event + std::shared_ptr> removeChildEventReceiver + { + std::make_shared>(this, &SHScriptEngine::onSceneNodeChildrenRemoved) + }; + SHEventManager::SubscribeTo(SH_SCENEGRAPH_REMOVE_CHILD_EVENT, std::dynamic_pointer_cast(removeChildEventReceiver)); } void SHScriptEngine::dumpBuildLog(const std::string_view& buildLogPath) diff --git a/SHADE_Engine/src/Scripting/SHScriptEngine.h b/SHADE_Engine/src/Scripting/SHScriptEngine.h index 7d83606e..ef778627 100644 --- a/SHADE_Engine/src/Scripting/SHScriptEngine.h +++ b/SHADE_Engine/src/Scripting/SHScriptEngine.h @@ -267,6 +267,7 @@ namespace SHADE // - Events CsEventRelayFuncPtr csColliderOnListChanged = nullptr; CsEventRelayFuncPtr csColliderOnRemoved = nullptr; + CsEventRelayFuncPtr csSceneNodeChildrenChanged = nullptr; // - Editor CsScriptEditorFuncPtr csEditorRenderScripts = nullptr; CsFuncPtr csEditorUndo = nullptr; @@ -279,6 +280,8 @@ namespace SHADE SHEventHandle onColliderAdded(SHEventPtr eventPtr); SHEventHandle onColliderRemoved(SHEventPtr eventPtr); SHEventHandle onColliderComponentRemoved(SHEventPtr eventPtr); + SHEventHandle onSceneNodeChildrenAdded(SHEventPtr eventPtr); + SHEventHandle onSceneNodeChildrenRemoved(SHEventPtr eventPtr); /*-----------------------------------------------------------------------------*/ /* Helper Functions */ diff --git a/SHADE_Managed/src/Engine/GameObject.cxx b/SHADE_Managed/src/Engine/GameObject.cxx index 79960ff6..017366fe 100644 --- a/SHADE_Managed/src/Engine/GameObject.cxx +++ b/SHADE_Managed/src/Engine/GameObject.cxx @@ -244,8 +244,7 @@ namespace SHADE // Unparent all children to the root for (auto child : node->GetChildren()) { - child->SetParent(nullptr); - ChildListCache::UpdateChildList(child->GetEntityID()); + SHSceneManager::GetCurrentSceneGraph().SetParent(child->GetEntityID(), nullptr); } ChildListCache::UpdateChildList(entity); } From e824c174056f008cf4fe56f9bc08a29e4fb23c85 Mon Sep 17 00:00:00 2001 From: Xiao Qi Date: Fri, 11 Nov 2022 10:06:26 +0800 Subject: [PATCH 4/7] Fixed asset type conversion bug when loading from meta files Removed compile all function Set parent id to 0 for non sub assets --- SHADE_Engine/src/Assets/SHAssetMacros.h | 3 +- SHADE_Engine/src/Assets/SHAssetManager.cpp | 45 ------------------- SHADE_Engine/src/Assets/SHAssetManager.h | 2 - .../src/Assets/SHAssetMetaHandler.cpp | 1 + 4 files changed, 3 insertions(+), 48 deletions(-) diff --git a/SHADE_Engine/src/Assets/SHAssetMacros.h b/SHADE_Engine/src/Assets/SHAssetMacros.h index e0551262..7ffdb5f1 100644 --- a/SHADE_Engine/src/Assets/SHAssetMacros.h +++ b/SHADE_Engine/src/Assets/SHAssetMacros.h @@ -51,8 +51,8 @@ enum class AssetType : AssetTypeMeta SCENE, PREFAB, MATERIAL, - SCRIPT, MESH, + SCRIPT, MAX_COUNT }; constexpr size_t TYPE_COUNT{ static_cast(AssetType::MAX_COUNT) }; @@ -97,6 +97,7 @@ constexpr std::string_view EXTENSIONS[] = { SCENE_EXTENSION, PREFAB_EXTENSION, MATERIAL_EXTENSION, + "dummy", SCRIPT_EXTENSION, AUDIO_WAV_EXTENSION, }; diff --git a/SHADE_Engine/src/Assets/SHAssetManager.cpp b/SHADE_Engine/src/Assets/SHAssetManager.cpp index f4727417..5a1bd5db 100644 --- a/SHADE_Engine/src/Assets/SHAssetManager.cpp +++ b/SHADE_Engine/src/Assets/SHAssetManager.cpp @@ -405,51 +405,6 @@ namespace SHADE return result; } - void SHAssetManager::CompileAll() noexcept - { - std::vector paths; - - for (auto const& dir : std::filesystem::recursive_directory_iterator{ ASSET_ROOT }) - { - if (dir.is_regular_file()) - { - for (auto const& ext : EXTERNALS) - { - if (dir.path().extension().string() == ext.data()) - { - paths.push_back(dir.path()); - } - } - } - } - - for (auto const& path : paths) - { - AssetPath newPath; - auto const ext{ path.extension().string() }; - if (ext == GLSL_EXTENSION.data()) - { - newPath = SHShaderSourceCompiler::LoadAndCompileShader(path).value(); - } - else if (ext == DDS_EXTENSION.data()) - { - newPath = SHTextureCompiler::CompileTextureAsset(path).value(); - } - else if (ext == GLTF_EXTENSION.data() || ext == FBX_EXTENSION.data()) - { - std::string command = MODEL_COMPILER_EXE.data(); - command += " " + path.string(); - std::system(command.c_str()); - - std::string modelPath = path.string().substr(0, path.string().find_last_of('.')); - modelPath += MODEL_EXTENSION; - newPath = modelPath; - } - - GenerateNewMeta(newPath); - } - } - bool SHAssetManager::DeleteLocalFile(AssetPath path) noexcept { //TODO Move this to dedicated library diff --git a/SHADE_Engine/src/Assets/SHAssetManager.h b/SHADE_Engine/src/Assets/SHAssetManager.h index a891ec23..5af648e4 100644 --- a/SHADE_Engine/src/Assets/SHAssetManager.h +++ b/SHADE_Engine/src/Assets/SHAssetManager.h @@ -107,8 +107,6 @@ namespace SHADE static SHAsset CreateAssetFromPath(AssetPath path) noexcept; - static void CompileAll() noexcept; - static bool DeleteLocalFile(AssetPath path) noexcept; //TODO use this function to create asset data internall at all calls to generate id diff --git a/SHADE_Engine/src/Assets/SHAssetMetaHandler.cpp b/SHADE_Engine/src/Assets/SHAssetMetaHandler.cpp index 9ae8cde2..b75ee1ad 100644 --- a/SHADE_Engine/src/Assets/SHAssetMetaHandler.cpp +++ b/SHADE_Engine/src/Assets/SHAssetMetaHandler.cpp @@ -98,6 +98,7 @@ namespace SHADE meta.type = static_cast(type); meta.isSubAsset = false; + meta.parent = 0; // Burn Line if (std::getline(metaFile, line)) From 6df3f3d4174c7ae4f5e5e4b3c2f50eeb7c46ef5a Mon Sep 17 00:00:00 2001 From: Xiao Qi Date: Fri, 11 Nov 2022 10:47:03 +0800 Subject: [PATCH 5/7] Fixed get type from extension bug in asset handler --- SHADE_Engine/src/Assets/SHAssetMacros.h | 2 ++ SHADE_Engine/src/Assets/SHAssetMetaHandler.cpp | 2 +- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/SHADE_Engine/src/Assets/SHAssetMacros.h b/SHADE_Engine/src/Assets/SHAssetMacros.h index 7ffdb5f1..7e5befab 100644 --- a/SHADE_Engine/src/Assets/SHAssetMacros.h +++ b/SHADE_Engine/src/Assets/SHAssetMacros.h @@ -102,6 +102,8 @@ constexpr std::string_view EXTENSIONS[] = { AUDIO_WAV_EXTENSION, }; +constexpr size_t EXTENSIONS_COUNT{ 11 }; + // EXTERNAL EXTENSIONS constexpr std::string_view GLSL_EXTENSION{ ".glsl" }; constexpr std::string_view DDS_EXTENSION{ ".dds" }; diff --git a/SHADE_Engine/src/Assets/SHAssetMetaHandler.cpp b/SHADE_Engine/src/Assets/SHAssetMetaHandler.cpp index b75ee1ad..f3b24ed1 100644 --- a/SHADE_Engine/src/Assets/SHAssetMetaHandler.cpp +++ b/SHADE_Engine/src/Assets/SHAssetMetaHandler.cpp @@ -38,7 +38,7 @@ namespace SHADE ****************************************************************************/ AssetType SHAssetMetaHandler::GetTypeFromExtension(AssetExtension ext) noexcept { - for (int i{0}; i < EXTENSIONS->size(); ++i) + for (auto i{0}; i < EXTENSIONS_COUNT; ++i) { if (strcmp(ext.c_str(), EXTENSIONS[i].data()) == 0) { From 9fe5dc385bf6f55b1e91f461cead7743877882b0 Mon Sep 17 00:00:00 2001 From: Xiao Qi Date: Fri, 11 Nov 2022 10:52:57 +0800 Subject: [PATCH 6/7] Implemented check for raw asset if compiled --- SHADE_Engine/src/Filesystem/SHFileSystem.cpp | 63 +++++++++++++++++++- SHADE_Engine/src/Filesystem/SHFileSystem.h | 1 + SHADE_Engine/src/Filesystem/SHFolder.h | 3 +- 3 files changed, 64 insertions(+), 3 deletions(-) diff --git a/SHADE_Engine/src/Filesystem/SHFileSystem.cpp b/SHADE_Engine/src/Filesystem/SHFileSystem.cpp index c4bcc5dc..9144b0d8 100644 --- a/SHADE_Engine/src/Filesystem/SHFileSystem.cpp +++ b/SHADE_Engine/src/Filesystem/SHFileSystem.cpp @@ -38,6 +38,41 @@ namespace SHADE return false; } + bool SHFileSystem::MatchExtention(FileExt raw, FileExt compiled) noexcept + { + if (raw == GLSL_EXTENSION) + { + if (compiled == SHADER_EXTENSION || + compiled == SHADER_BUILT_IN_EXTENSION) + { + return true; + } + } + else if (raw == DDS_EXTENSION) + { + if (compiled == TEXTURE_EXTENSION) + { + return true; + } + } + else if (raw == FBX_EXTENSION) + { + if (compiled == MODEL_EXTENSION) + { + return true; + } + } + else if (raw == GLTF_EXTENSION) + { + if (compiled == MODEL_EXTENSION) + { + return true; + } + } + + return false; + } + void SHFileSystem::BuildDirectory(FolderPath path, FolderPointer& root, std::unordered_map& assetCollection) noexcept { std::stack folderStack; @@ -52,6 +87,7 @@ namespace SHADE std::vector assets; + // Get all subfolders/files in this current folder for (auto& dirEntry : std::filesystem::directory_iterator(folder->path)) { auto path = dirEntry.path(); @@ -60,8 +96,6 @@ namespace SHADE { if (path.extension().string() == META_EXTENSION) { - //auto asset = SHAssetMetaHandler::RetrieveMetaData(path); - //assetCollection.insert({ asset.id, asset }); assets.push_back(SHAssetMetaHandler::RetrieveMetaData(path)); } else @@ -77,6 +111,7 @@ namespace SHADE continue; } + // If item is folder auto newFolder{ folder->CreateSubFolderHere(path.stem().string()) }; folderStack.push(newFolder); } @@ -97,6 +132,30 @@ namespace SHADE } } } + + for (auto i {0}; i < folder->files.size(); ++i) + { + auto& file = folder->files[i]; + if (file.compilable) + { + for (auto j{ 0 }; j < folder->files.size(); ++j) + { + auto& check = folder->files[j]; + if (i == j || check.compilable) + { + continue; + } + + if (file.name == check.name) + { + if (MatchExtention(file.ext, check.ext)) + { + file.compiled = true; + } + } + } + } + } } } diff --git a/SHADE_Engine/src/Filesystem/SHFileSystem.h b/SHADE_Engine/src/Filesystem/SHFileSystem.h index 87d13f42..d30f2164 100644 --- a/SHADE_Engine/src/Filesystem/SHFileSystem.h +++ b/SHADE_Engine/src/Filesystem/SHFileSystem.h @@ -24,5 +24,6 @@ namespace SHADE private: static bool DeleteFolder(FolderPointer location) noexcept; static bool IsCompilable(std::string ext) noexcept; + static bool MatchExtention(FileExt raw, FileExt compiled) noexcept; }; } \ No newline at end of file diff --git a/SHADE_Engine/src/Filesystem/SHFolder.h b/SHADE_Engine/src/Filesystem/SHFolder.h index 5c702b51..234e6f19 100644 --- a/SHADE_Engine/src/Filesystem/SHFolder.h +++ b/SHADE_Engine/src/Filesystem/SHFolder.h @@ -33,7 +33,8 @@ namespace SHADE FilePath path; FileExt ext; SHAsset const* assetMeta; - bool compilable; + bool compilable; + bool compiled; }; class SHFolder From 94b64e92dd51ac837aadaa266d696f93dc49a9a8 Mon Sep 17 00:00:00 2001 From: Xiao Qi Date: Fri, 11 Nov 2022 10:55:19 +0800 Subject: [PATCH 7/7] Initialise files to not compiled --- SHADE_Engine/src/Filesystem/SHFileSystem.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/SHADE_Engine/src/Filesystem/SHFileSystem.cpp b/SHADE_Engine/src/Filesystem/SHFileSystem.cpp index 9144b0d8..9aaf72a4 100644 --- a/SHADE_Engine/src/Filesystem/SHFileSystem.cpp +++ b/SHADE_Engine/src/Filesystem/SHFileSystem.cpp @@ -105,7 +105,8 @@ namespace SHADE path.string(), path.extension().string(), nullptr, - IsCompilable(path.extension().string()) + IsCompilable(path.extension().string()), + false ); } continue;