Refactor PhysicalMaterial to use API bindings

This commit is contained in:
Wojtek Figat
2020-12-21 15:39:14 +01:00
parent 41b938b3cd
commit 7798a7f6d5
7 changed files with 24 additions and 89 deletions

View File

@@ -190,13 +190,11 @@ ISerializable* Create()
return New<T>();
}
// Key: managed class typename, Value: unmanaged instance spawner function
Dictionary<String, UnmanagedJsonInstanceCreator> UnmanagedTypes(32);
void InitUnmanagedJsonTypes()
{
ASSERT(UnmanagedTypes.IsEmpty());
// Key: managed class typename, Value: unmanaged instance spawner function
UnmanagedTypes[TEXT("FlaxEngine.PhysicalMaterial")] = &Create<PhysicalMaterial>;
}

View File

@@ -1,46 +0,0 @@
// Copyright (c) 2012-2020 Wojciech Figat. All rights reserved.
namespace FlaxEngine
{
/// <summary>
/// Physical materials are used to define the response of a physical object when interacting dynamically with the world.
/// </summary>
public sealed class PhysicalMaterial
{
/// <summary>
/// The friction value of surface, controls how easily things can slide on this surface.
/// </summary>
[EditorOrder(0), Limit(0), EditorDisplay("Physical Material"), Tooltip("The friction value of surface, controls how easily things can slide on this surface.")]
public float Friction = 0.7f;
/// <summary>
/// The friction combine mode, controls how friction is computed for multiple materials.
/// </summary>
[EditorOrder(1), EditorDisplay("Physical Material"), Tooltip("The friction combine mode, controls how friction is computed for multiple materials.")]
public PhysicsCombineMode FrictionCombineMode = PhysicsCombineMode.Average;
/// <summary>
/// If set we will use the FrictionCombineMode of this material, instead of the FrictionCombineMode found in the Physics settings.
/// </summary>
[HideInEditor]
public bool OverrideFrictionCombineMode = false;
/// <summary>
/// The restitution or 'bounciness' of this surface, between 0 (no bounce) and 1 (outgoing velocity is same as incoming).
/// </summary>
[EditorOrder(3), Range(0, 1), EditorDisplay("Physical Material"), Tooltip("The restitution or \'bounciness\' of this surface, between 0 (no bounce) and 1 (outgoing velocity is same as incoming).")]
public float Restitution = 0.3f;
/// <summary>
/// The restitution combine mode, controls how restitution is computed for multiple materials.
/// </summary>
[EditorOrder(4), EditorDisplay("Physical Material"), Tooltip("The restitution combine mode, controls how restitution is computed for multiple materials.")]
public PhysicsCombineMode RestitutionCombineMode = PhysicsCombineMode.Average;
/// <summary>
/// If set we will use the RestitutionCombineMode of this material, instead of the RestitutionCombineMode found in the Physics settings.
/// </summary>
[HideInEditor]
public bool OverrideRestitutionCombineMode = false;
}
}

View File

@@ -2,7 +2,6 @@
#include "PhysicalMaterial.h"
#include "PhysicsSettings.h"
#include "Engine/Serialization/JsonTools.h"
#include "Physics.h"
#include <ThirdParty/PhysX/PxPhysics.h>
#include <ThirdParty/PhysX/PxMaterial.h>
@@ -51,18 +50,3 @@ void PhysicalMaterial::UpdatePhysXMaterial()
_material->setRestitutionCombineMode(static_cast<PxCombineMode::Enum>(useRestitutionCombineMode));
}
}
void PhysicalMaterial::Serialize(SerializeStream& stream, const void* otherObj)
{
MISSING_CODE("PhysicalMaterial::Serialize is not implemented");
}
void PhysicalMaterial::Deserialize(DeserializeStream& stream, ISerializeModifier* modifier)
{
Friction = JsonTools::GetFloat(stream, "Friction", PhysicalMaterial_Friction);
FrictionCombineMode = JsonTools::GetEnum(stream, "FrictionCombineMode", PhysicalMaterial_FrictionCombineMode);
OverrideFrictionCombineMode = JsonTools::GetBool(stream, "OverrideFrictionCombineMode", PhysicalMaterial_OverrideFrictionCombineMode);
Restitution = JsonTools::GetFloat(stream, "Restitution", PhysicalMaterial_Restitution);
RestitutionCombineMode = JsonTools::GetEnum(stream, "RestitutionCombineMode", PhysicalMaterial_RestitutionCombineMode);
OverrideRestitutionCombineMode = JsonTools::GetBool(stream, "OverrideRestitutionCombineMode", PhysicalMaterial_OverrideRestitutionCombineMode);
}

View File

@@ -5,20 +5,13 @@
#include "Types.h"
#include "Engine/Serialization/ISerializable.h"
// Default values for the physical material
#define PhysicalMaterial_Friction 0.7f
#define PhysicalMaterial_FrictionCombineMode PhysicsCombineMode::Average
#define PhysicalMaterial_OverrideFrictionCombineMode false
#define PhysicalMaterial_Restitution 0.3f
#define PhysicalMaterial_RestitutionCombineMode PhysicsCombineMode::Average
#define PhysicalMaterial_OverrideRestitutionCombineMode false
/// <summary>
/// Physical materials are used to define the response of a physical object when interacting dynamically with the world.
/// </summary>
class FLAXENGINE_API PhysicalMaterial : public ISerializable
API_CLASS() class FLAXENGINE_API PhysicalMaterial final : public ISerializable
{
API_AUTO_SERIALIZATION();
DECLARE_SCRIPTING_TYPE_MINIMAL(PhysicalMaterial);
private:
PxMaterial* _material;
@@ -40,32 +33,38 @@ public:
/// <summary>
/// The friction value of surface, controls how easily things can slide on this surface.
/// </summary>
float Friction = PhysicalMaterial_Friction;
API_FIELD(Attributes="EditorOrder(0), Limit(0), EditorDisplay(\"Physical Material\")")
float Friction = 0.7f;
/// <summary>
/// The friction combine mode, controls how friction is computed for multiple materials.
/// </summary>
PhysicsCombineMode FrictionCombineMode = PhysicalMaterial_FrictionCombineMode;
API_FIELD(Attributes="EditorOrder(1), EditorDisplay(\"Physical Material\")")
PhysicsCombineMode FrictionCombineMode = PhysicsCombineMode::Average;
/// <summary>
/// If set we will use the FrictionCombineMode of this material, instead of the FrictionCombineMode found in the Physics settings.
/// </summary>
bool OverrideFrictionCombineMode = PhysicalMaterial_OverrideFrictionCombineMode;
API_FIELD(Attributes="HideInEditor")
bool OverrideFrictionCombineMode = false;
/// <summary>
/// The restitution or 'bounciness' of this surface, between 0 (no bounce) and 1 (outgoing velocity is same as incoming).
/// </summary>
float Restitution = PhysicalMaterial_Restitution;
API_FIELD(Attributes="EditorOrder(3), Range(0, 1), EditorDisplay(\"Physical Material\")")
float Restitution = 0.3f;
/// <summary>
/// The restitution combine mode, controls how restitution is computed for multiple materials.
/// </summary>
PhysicsCombineMode RestitutionCombineMode = PhysicalMaterial_RestitutionCombineMode;
API_FIELD(Attributes="EditorOrder(4), EditorDisplay(\"Physical Material\")")
PhysicsCombineMode RestitutionCombineMode = PhysicsCombineMode::Average;
/// <summary>
/// If set we will use the RestitutionCombineMode of this material, instead of the RestitutionCombineMode found in the Physics settings.
/// </summary>
bool OverrideRestitutionCombineMode = PhysicalMaterial_OverrideRestitutionCombineMode;
API_FIELD(Attributes="HideInEditor")
bool OverrideRestitutionCombineMode = false;
public:
@@ -79,10 +78,4 @@ public:
/// Updates the PhysX material (after any property change).
/// </summary>
void UpdatePhysXMaterial();
public:
// [ISerializable]
void Serialize(SerializeStream& stream, const void* otherObj) override;
void Deserialize(DeserializeStream& stream, ISerializeModifier* modifier) override;
};

View File

@@ -393,7 +393,7 @@ namespace Flax.Build.Bindings
{
// Write attributes
contents.Append(indent).Append('[').Append(attributes).Append(']').AppendLine();
writeTooltip = !attributes.Contains("Tooltip(");
writeTooltip = !attributes.Contains("Tooltip(") && !attributes.Contains("HideInEditor");
}
if (useUnmanaged)
@@ -462,7 +462,7 @@ namespace Flax.Build.Bindings
else if (classInfo.IsAbstract)
contents.Append("abstract ");
contents.Append("unsafe partial class ").Append(classInfo.Name);
if (classInfo.BaseType != null && classInfo.BaseTypeInheritance != AccessLevel.Private)
if (classInfo.BaseType != null && !classInfo.IsBaseTypeHidden)
contents.Append(" : ").Append(GenerateCSharpNativeToManaged(buildData, classInfo.BaseType, classInfo));
contents.AppendLine();
contents.Append(indent + "{");

View File

@@ -949,6 +949,8 @@ namespace Flax.Build.Bindings
var baseType = classInfo?.BaseType ?? structureInfo?.BaseType;
if (baseType != null && baseType.Type == "ISerializable")
baseType = null;
else if (classInfo != null && classInfo.IsBaseTypeHidden)
baseType = null;
CppAutoSerializeFields.Clear();
CppAutoSerializeProperties.Clear();
CppIncludeFiles.Add("Engine/Serialization/Serialization.h");

View File

@@ -25,6 +25,7 @@ namespace Flax.Build.Bindings
public AccessLevel Access;
public TypeInfo BaseType;
public AccessLevel BaseTypeInheritance;
public bool IsBaseTypeHidden;
public bool IsStatic;
public bool IsSealed;
public bool IsAbstract;
@@ -50,6 +51,9 @@ namespace Flax.Build.Bindings
{
base.Init(buildData);
// Internal base types are usually hidden from bindings (used in core-only internally)
IsBaseTypeHidden = BaseTypeInheritance == AccessLevel.Private || BaseType.Type == "ISerializable";
// Cache if it it Scripting Object type
if (InBuildScriptingObjectTypes.Contains(Name))
_isScriptingObject = true;