Added Transform Component C# interface

This commit is contained in:
Kah Wei 2022-09-23 19:57:29 +08:00
parent 6848672899
commit 187142d5c3
8 changed files with 297 additions and 17 deletions

View File

@ -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
{

View File

@ -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);
}
}

View File

@ -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
{
/// <summary>
/// CLR version of the the SHADE Engine's TransformComponent.
/// </summary>
public ref class Transform : public Component<SHTransformComponent>
{
internal:
/*-----------------------------------------------------------------------------*/
/* Constructors */
/*-----------------------------------------------------------------------------*/
/// <summary>
/// Constructs a Transform Component that represents a native Transform component
/// tied to the specified Entity.
/// </summary>
/// <param name="entity">Entity that this Component will be tied to.</param>
Transform(Entity entity);
public:
/*-----------------------------------------------------------------------------*/
/* Properties */
/*-----------------------------------------------------------------------------*/
/// <summary>
/// Local position stored by this Transform.
/// </summary>
property Vector3 LocalPosition
{
Vector3 get();
void set(Vector3 val);
}
/// <summary>
/// Local Z-axis rotation angle stored by this Transform in Radians.
/// </summary>
property Vector3 LocalRotation
{
Vector3 get();
void set(Vector3 val);
}
/// <summary>
/// Local scale stored by this Transform.
/// </summary>
property Vector3 LocalScale
{
Vector3 get();
void set(Vector3 val);
}
/// <summary>
/// Global position stored by this Transform.
/// </summary>
property Vector3 GlobalPosition
{
Vector3 get();
void set(Vector3 val);
}
/// <summary>
/// Global Z-axis rotation angle stored by this Transform in Radians.
/// </summary>
property Vector3 GlobalRotation
{
Vector3 get();
void set(Vector3 val);
}
/// <summary>
/// Global scale stored by this Transform.
/// Note that this operation is expensive.
/// </summary>
property Vector3 GlobalScale
{
Vector3 get();
void set(Vector3 val);
}
/// <summary>
/// Parent Transform that affects this Transform.
/// </summary>
property Transform^ Parent
{
Transform^ get();
}
/*-----------------------------------------------------------------------------*/
/* Usage Functions */
/*-----------------------------------------------------------------------------*/
/// <summary>
/// Sets the parent of this Transform component.
/// </summary>
/// <param name="parent">
/// Entity that contains the Transform component that this Transform will be
/// parented to. If null, unparenting will occur.
/// </param>
/// <param name="worldPositionStays">
/// 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.
/// </param>
void SetParent(Transform^ parent, bool worldPositionStays);
};
}

View File

@ -21,12 +21,14 @@ of DigiPen Institute of Technology is prohibited.
#include <msclr\marshal_cppstd.h>
// 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<Transform, Transform>());
componentMap.Add(createComponentSet<SHTransformComponent, Transform>());
}
/*---------------------------------------------------------------------------------*/

View File

@ -28,15 +28,8 @@ namespace SHADE
template <typename NativeComponent>
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<NativeComponent>(nativeEntity);
NativeComponent* component = SHComponentManager::GetComponent_s<NativeComponent>(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<NativeType>,
SHComponentManager::EnsureComponent<NativeType>,
SHComponentManager::HasComponent<NativeType>,
SHComponentManager::RemoveComponent<NativeType>
};

View File

@ -99,8 +99,8 @@ namespace SHADE
/// <summary>
/// Pointer to a function for Component manipulation operations.
/// </summary>
using ComponentFunc = void(*)(const EntityID&);
using ComponentHasFunc = bool(*)(const EntityID&);
using ComponentFunc = void(*)(EntityID) noexcept;
using ComponentHasFunc = bool(*)(EntityID) noexcept;
/// <summary>
/// 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;
};
/*-----------------------------------------------------------------------------*/

View File

@ -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 <msclr/marshal_cppstd.h>
#include "ECS_Base/Managers/SHEntityManager.h"
namespace SHADE
{
@ -30,6 +30,32 @@ namespace SHADE
return static_cast<Entity>(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 */
/*---------------------------------------------------------------------------------*/

View File

@ -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
/// <returns>Managed representation of the specified Entity.</returns>
static Entity ToCLI(SHEntity entity);
/*-----------------------------------------------------------------------------*/
/* Math Conversions */
/*-----------------------------------------------------------------------------*/
/// <summary>
/// Converts from a managed Vector3 to a native Vector3.
/// </summary>
/// <param name="vec">The managed Vector3 to convert from.</param>
/// <returns>Native copy of a managed Vector3.</returns>
static SHVec3 ToNative(Vector3 vec);
/// <summary>
/// Converts from a native Vector3 to a managed Vector3.
/// </summary>
/// <param name="vec">The native Vector3 to convert from.</param>
/// <returns>Managed copy of a native Vector3.</returns>
static Vector3 ToCLI(const SHVec3& vec);
/// <summary>
/// Converts from a managed Vector2 to a native Vector2.
/// </summary>
/// <param name="vec">The managed Vector2 to convert from.</param>
/// <returns>Native copy of a managed Vector2.</returns>
static SHVec2 ToNative(Vector2 vec);
/// <summary>
/// Converts from a native Vector2 to a managed Vector2.
/// </summary>
/// <param name="vec">The native Vector2 to convert from.</param>
/// <returns>Managed copy of a native Vector2.</returns>
static Vector2 ToCLI(const SHVec2& vec);
/*-----------------------------------------------------------------------------*/
/* String Conversions */
/*-----------------------------------------------------------------------------*/