From 187142d5c3e67f4ee403ac9032a6f81f582105f4 Mon Sep 17 00:00:00 2001 From: Kah Wei Date: Fri, 23 Sep 2022 19:57:29 +0800 Subject: [PATCH] Added Transform Component C# interface --- SHADE_Engine/src/Math/Transform/SHTransform.h | 4 +- SHADE_Managed/src/Components/Transform.cxx | 107 ++++++++++++++++ SHADE_Managed/src/Components/Transform.hxx | 121 ++++++++++++++++++ SHADE_Managed/src/Engine/ECS.cxx | 5 +- SHADE_Managed/src/Engine/ECS.h++ | 10 +- SHADE_Managed/src/Engine/ECS.hxx | 7 +- SHADE_Managed/src/Utility/Convert.cxx | 28 +++- SHADE_Managed/src/Utility/Convert.hxx | 32 +++++ 8 files changed, 297 insertions(+), 17 deletions(-) create mode 100644 SHADE_Managed/src/Components/Transform.cxx create mode 100644 SHADE_Managed/src/Components/Transform.hxx diff --git a/SHADE_Engine/src/Math/Transform/SHTransform.h b/SHADE_Engine/src/Math/Transform/SHTransform.h index 9edd7fdd..c1a0e565 100644 --- a/SHADE_Engine/src/Math/Transform/SHTransform.h +++ b/SHADE_Engine/src/Math/Transform/SHTransform.h @@ -12,7 +12,9 @@ // Project Headers #include "SH_API.h" -#include "Math/SHMath.h" +#include "Math/Vector/SHVec2.h" +#include "Math/Vector/SHVec3.h" +#include "Math/SHMatrix.h" namespace SHADE { diff --git a/SHADE_Managed/src/Components/Transform.cxx b/SHADE_Managed/src/Components/Transform.cxx new file mode 100644 index 00000000..e065015e --- /dev/null +++ b/SHADE_Managed/src/Components/Transform.cxx @@ -0,0 +1,107 @@ +/************************************************************************************//*! +\file Transform.cxx +\author Tng Kah Wei, kahwei.tng, 390009620 +\par email: kahwei.tng\@digipen.edu +\date Sep 23, 2022 +\brief Contains the definition of the functions of the managed Transform 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 "Transform.hxx" + +namespace SHADE +{ + /*---------------------------------------------------------------------------------*/ + /* Properties */ + /*---------------------------------------------------------------------------------*/ + Vector3 Transform::LocalPosition::get() + { + return Convert::ToCLI(GetNativeComponent()->GetLocalPosition()); + } + void Transform::LocalPosition::set(Vector3 val) + { + GetNativeComponent()->SetLocalPosition(Convert::ToNative(val)); + } + Vector3 Transform::LocalRotation::get() + { + return Convert::ToCLI(GetNativeComponent()->GetLocalRotation()); + } + void Transform::LocalRotation::set(Vector3 val) + { + GetNativeComponent()->SetLocalPosition(Convert::ToNative(val)); + } + Vector3 Transform::LocalScale::get() + { + return Convert::ToCLI(GetNativeComponent()->GetLocalScale()); + + } + void Transform::LocalScale::set(Vector3 val) + { + GetNativeComponent()->SetLocalScale(Convert::ToNative(val)); + } + Vector3 Transform::GlobalPosition::get() + { + return Convert::ToCLI(GetNativeComponent()->GetWorldPosition()); + } + void Transform::GlobalPosition::set(Vector3 val) + { + GetNativeComponent()->SetWorldPosition(Convert::ToNative(val)); + } + Vector3 Transform::GlobalRotation::get() + { + return Convert::ToCLI(GetNativeComponent()->GetWorldRotation()); + } + void Transform::GlobalRotation::set(Vector3 val) + { + GetNativeComponent()->SetWorldRotation(Convert::ToNative(val)); + } + Vector3 Transform::GlobalScale::get() + { + return Convert::ToCLI(GetNativeComponent()->GetWorldScale()); + + } + void Transform::GlobalScale::set(Vector3 val) + { + GetNativeComponent()->SetWorldScale(Convert::ToNative(val)); + } + Transform^ Transform::Parent::get() + { + auto node = SHSceneManager::GetCurrentSceneGraph().GetNode(owner.GetEntity()); + if (!node) + throw gcnew System::InvalidOperationException("[Transform] Unable to retrieve SceneGraphNode for an Entity."); + + const auto PARENT = node->GetParent(); + return PARENT ? gcnew Transform(PARENT->GetEntityID()) : nullptr; + } + + /*---------------------------------------------------------------------------------*/ + /* Constructors */ + /*---------------------------------------------------------------------------------*/ + Transform::Transform(Entity entity) + : Component(entity) + {} + + /*---------------------------------------------------------------------------------*/ + /* Usage Functions */ + /*---------------------------------------------------------------------------------*/ + + void Transform::SetParent(Transform^ parent, bool worldPositionStays) + { + auto& sceneGraph = SHSceneManager::GetCurrentSceneGraph(); + auto node = sceneGraph.GetNode(owner.GetEntity()); + if (!node) + throw gcnew System::InvalidOperationException("[Transform] Unable to retrieve SceneGraphNode for an Entity."); + + if (parent) + node->SetParent(sceneGraph.GetNode(parent->owner.GetEntity())); + else + sceneGraph.SetParent(parent->owner.GetEntity(), nullptr); + } +} diff --git a/SHADE_Managed/src/Components/Transform.hxx b/SHADE_Managed/src/Components/Transform.hxx new file mode 100644 index 00000000..9efd2927 --- /dev/null +++ b/SHADE_Managed/src/Components/Transform.hxx @@ -0,0 +1,121 @@ +/************************************************************************************//*! +\file Transform.hxx +\author Tng Kah Wei, kahwei.tng, 390009620 +\par email: kahwei.tng\@digipen.edu +\date Sep 23, 2022 +\brief Contains the definition of the managed Transform class with the + declaration of functions for working with it. + + 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 "Components/Component.hxx" +#include "Math/Vector3.hxx" +#include "Utility/Convert.hxx" +// External Dependencies +#include "Math/Transform/SHTransformComponent.h" + +namespace SHADE +{ + /// + /// CLR version of the the SHADE Engine's TransformComponent. + /// + public ref class Transform : public Component + { + internal: + /*-----------------------------------------------------------------------------*/ + /* Constructors */ + /*-----------------------------------------------------------------------------*/ + /// + /// Constructs a Transform Component that represents a native Transform component + /// tied to the specified Entity. + /// + /// Entity that this Component will be tied to. + Transform(Entity entity); + + public: + /*-----------------------------------------------------------------------------*/ + /* Properties */ + /*-----------------------------------------------------------------------------*/ + /// + /// Local position stored by this Transform. + /// + property Vector3 LocalPosition + { + Vector3 get(); + void set(Vector3 val); + } + /// + /// Local Z-axis rotation angle stored by this Transform in Radians. + /// + property Vector3 LocalRotation + { + Vector3 get(); + void set(Vector3 val); + } + /// + /// Local scale stored by this Transform. + /// + property Vector3 LocalScale + { + Vector3 get(); + void set(Vector3 val); + } + /// + /// Global position stored by this Transform. + /// + property Vector3 GlobalPosition + { + Vector3 get(); + void set(Vector3 val); + } + /// + /// Global Z-axis rotation angle stored by this Transform in Radians. + /// + property Vector3 GlobalRotation + { + Vector3 get(); + void set(Vector3 val); + } + /// + /// Global scale stored by this Transform. + /// Note that this operation is expensive. + /// + property Vector3 GlobalScale + { + Vector3 get(); + void set(Vector3 val); + } + /// + /// Parent Transform that affects this Transform. + /// + property Transform^ Parent + { + Transform^ get(); + } + + /*-----------------------------------------------------------------------------*/ + /* Usage Functions */ + /*-----------------------------------------------------------------------------*/ + /// + /// Sets the parent of this Transform component. + /// + /// + /// Entity that contains the Transform component that this Transform will be + /// parented to. If null, unparenting will occur. + /// + /// + /// If true, the transform values of this Transform component will retain their + /// pre-parent-change global transforms. The local transform values will be + /// modified to ensure that the global transforms do not change. + /// + void SetParent(Transform^ parent, bool worldPositionStays); + }; +} + diff --git a/SHADE_Managed/src/Engine/ECS.cxx b/SHADE_Managed/src/Engine/ECS.cxx index 36bef851..4d62f643 100644 --- a/SHADE_Managed/src/Engine/ECS.cxx +++ b/SHADE_Managed/src/Engine/ECS.cxx @@ -21,12 +21,14 @@ of DigiPen Institute of Technology is prohibited. #include // External Dependencies #include "ECS_Base/Managers/SHEntityManager.h" +#include "Math/Transform/SHTransformComponent.h" #include "Scene/SHSceneManager.h" #include "Scene/SHSceneGraph.h" #include "Tools/SHLog.h" // Project Headers #include "Utility/Convert.hxx" #include "Utility/Debug.hxx" +#include "Components/Transform.hxx" namespace SHADE { @@ -239,8 +241,7 @@ namespace SHADE /*---------------------------------------------------------------------------------*/ static ECS::ECS() { - // TODO - // componentMap.Add(createComponentSet()); + componentMap.Add(createComponentSet()); } /*---------------------------------------------------------------------------------*/ diff --git a/SHADE_Managed/src/Engine/ECS.h++ b/SHADE_Managed/src/Engine/ECS.h++ index daaa859f..f2588294 100644 --- a/SHADE_Managed/src/Engine/ECS.h++ +++ b/SHADE_Managed/src/Engine/ECS.h++ @@ -28,15 +28,8 @@ namespace SHADE template NativeComponent* ECS::GetNativeComponent(Entity entity) { - // Get native Entity - SHEntity* nativeEntity = SHEntityManager::GetEntityByID(entity); - - // Entity Validity Check - if (nativeEntity == nullptr) - throw gcnew System::InvalidOperationException("Attempted to get native Component to an invalid Entity."); - // Null Check - NativeComponent* component = SHComponentManager::GetComponent_s(nativeEntity); + NativeComponent* component = SHComponentManager::GetComponent_s(entity); if (component == nullptr) throw gcnew System::NullReferenceException("Attempted to get a native Component that does not exist."); @@ -52,7 +45,6 @@ namespace SHADE { ManagedType::typeid, SHComponentManager::AddComponent, - SHComponentManager::EnsureComponent, SHComponentManager::HasComponent, SHComponentManager::RemoveComponent }; diff --git a/SHADE_Managed/src/Engine/ECS.hxx b/SHADE_Managed/src/Engine/ECS.hxx index da73f14c..0563f678 100644 --- a/SHADE_Managed/src/Engine/ECS.hxx +++ b/SHADE_Managed/src/Engine/ECS.hxx @@ -99,8 +99,8 @@ namespace SHADE /// /// Pointer to a function for Component manipulation operations. /// - using ComponentFunc = void(*)(const EntityID&); - using ComponentHasFunc = bool(*)(const EntityID&); + using ComponentFunc = void(*)(EntityID) noexcept; + using ComponentHasFunc = bool(*)(EntityID) noexcept; /// /// Contains a set of Component related data used for resolving operations for /// each Component. @@ -108,11 +108,10 @@ namespace SHADE value struct ComponentSet { public: - System::Type^ Type; + System::Type^ Type; ComponentFunc AddFunction; ComponentHasFunc HasFunction; ComponentFunc RemoveFunction; - }; /*-----------------------------------------------------------------------------*/ diff --git a/SHADE_Managed/src/Utility/Convert.cxx b/SHADE_Managed/src/Utility/Convert.cxx index d222fbb3..661eb3e4 100644 --- a/SHADE_Managed/src/Utility/Convert.cxx +++ b/SHADE_Managed/src/Utility/Convert.cxx @@ -17,8 +17,8 @@ of DigiPen Institute of Technology is prohibited. // Primary Header #include "Convert.hxx" // External Dependencies -#include "ECS_Base/Managers/SHEntityManager.h" #include +#include "ECS_Base/Managers/SHEntityManager.h" namespace SHADE { @@ -30,6 +30,32 @@ namespace SHADE return static_cast(entity.GetEID()); } + /*---------------------------------------------------------------------------------*/ + /* Math Conversions */ + /*---------------------------------------------------------------------------------*/ + SHVec3 Convert::ToNative(Vector3 vec) + { + const double X = vec.x; + const double Y = vec.y; + const double Z = vec.z; + return SHVec3(X, Y, Z); + } + Vector3 Convert::ToCLI(const SHVec3& vec) + { + return Vector3(vec.x, vec.y, vec.z); + } + SHVec2 Convert::ToNative(Vector2 vec) + { + const double X = vec.x; + const double Y = vec.y; + return SHVec2(X, Y); + } + + Vector2 Convert::ToCLI(const SHVec2& vec) + { + return Vector2(vec.x, vec.y); + } + /*---------------------------------------------------------------------------------*/ /* String Conversions */ /*---------------------------------------------------------------------------------*/ diff --git a/SHADE_Managed/src/Utility/Convert.hxx b/SHADE_Managed/src/Utility/Convert.hxx index 241e5863..b41ffef4 100644 --- a/SHADE_Managed/src/Utility/Convert.hxx +++ b/SHADE_Managed/src/Utility/Convert.hxx @@ -16,8 +16,12 @@ of DigiPen Institute of Technology is prohibited. // External Dependencies #include "ECS_Base/Entity/SHEntity.h" +#include "Math/Vector/SHVec2.h" +#include "Math/Vector/SHVec3.h" // Project Includes #include "Engine/Entity.hxx" +#include "Math/Vector2.hxx" +#include "Math/Vector3.hxx" namespace SHADE { @@ -43,6 +47,34 @@ namespace SHADE /// Managed representation of the specified Entity. static Entity ToCLI(SHEntity entity); + /*-----------------------------------------------------------------------------*/ + /* Math Conversions */ + /*-----------------------------------------------------------------------------*/ + /// + /// Converts from a managed Vector3 to a native Vector3. + /// + /// The managed Vector3 to convert from. + /// Native copy of a managed Vector3. + static SHVec3 ToNative(Vector3 vec); + /// + /// Converts from a native Vector3 to a managed Vector3. + /// + /// The native Vector3 to convert from. + /// Managed copy of a native Vector3. + static Vector3 ToCLI(const SHVec3& vec); + /// + /// Converts from a managed Vector2 to a native Vector2. + /// + /// The managed Vector2 to convert from. + /// Native copy of a managed Vector2. + static SHVec2 ToNative(Vector2 vec); + /// + /// Converts from a native Vector2 to a managed Vector2. + /// + /// The native Vector2 to convert from. + /// Managed copy of a native Vector2. + static Vector2 ToCLI(const SHVec2& vec); + /*-----------------------------------------------------------------------------*/ /* String Conversions */ /*-----------------------------------------------------------------------------*/