New: Add interface-filtered object references
Add native hard and soft reference types for scene objects that implement a scripting interface: - ScriptingObjectInterfaceReference<T> - SoftObjectInterfaceReference<T>
This commit is contained in:
@@ -58,11 +58,13 @@ namespace FlaxEditor.CustomEditors
|
||||
if (targetType.Type == typeof(object) && values.Count > 0 && values[0] != null && !values.HasDifferentTypes)
|
||||
return CreateEditor(TypeUtils.GetObjectType(values[0]), canUseRefPicker);
|
||||
|
||||
// Special case if property is interface but the value is implemented as Scripting Object that should use reference picker
|
||||
if (targetType.IsInterface && canUseRefPicker && values.Count > 0 && values[0] is FlaxEngine.Object)
|
||||
return new DummyEditor();
|
||||
|
||||
// Use editor for the property type
|
||||
if (canUseRefPicker &&
|
||||
targetType.IsInterface &&
|
||||
values.GetAttributes().Any(x => x is ScriptingObjectInterfaceReferenceAttribute || x is SoftObjectInterfaceReferenceAttribute))
|
||||
{
|
||||
return new FlaxObjectRefEditor();
|
||||
}
|
||||
return CreateEditor(targetType, canUseRefPicker);
|
||||
}
|
||||
|
||||
|
||||
@@ -48,7 +48,7 @@ namespace FlaxEditor.CustomEditors.Editors
|
||||
public IPresenterOwner PresenterContext;
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the allowed objects type (given type and all subclasses). Must be <see cref="Object"/> type of any subclass.
|
||||
/// Gets or sets the allowed objects type (given type and all subclasses). Must be <see cref="Object"/> type of any subclass or a scripting interface.
|
||||
/// </summary>
|
||||
public ScriptType Type
|
||||
{
|
||||
@@ -57,11 +57,12 @@ namespace FlaxEditor.CustomEditors.Editors
|
||||
{
|
||||
if (_type == value)
|
||||
return;
|
||||
if (value == ScriptType.Null || (value.Type != typeof(Object) && !value.IsSubclassOf(ScriptType.Object)))
|
||||
if (value == ScriptType.Null || (!value.IsInterface && value.Type != typeof(Object) && !value.IsSubclassOf(ScriptType.Object)))
|
||||
throw new ArgumentException(string.Format("Invalid type for FlaxObjectRefEditor. Input type: {0}", value != ScriptType.Null ? value.TypeName : "null"));
|
||||
|
||||
_type = value;
|
||||
_supportsPickDropDown = new ScriptType(typeof(Actor)).IsAssignableFrom(value) ||
|
||||
_supportsPickDropDown = value.IsInterface ||
|
||||
new ScriptType(typeof(Actor)).IsAssignableFrom(value) ||
|
||||
new ScriptType(typeof(Script)).IsAssignableFrom(value);
|
||||
|
||||
// Deselect value if it's not valid now
|
||||
@@ -149,13 +150,22 @@ namespace FlaxEditor.CustomEditors.Editors
|
||||
protected virtual bool IsValid(Object obj)
|
||||
{
|
||||
var type = TypeUtils.GetObjectType(obj);
|
||||
return obj == null || _type.IsAssignableFrom(type) && (CheckValid == null || CheckValid(obj, type));
|
||||
return obj == null || (!_type.IsInterface || obj is SceneObject) && _type.IsAssignableFrom(type) && (CheckValid == null || CheckValid(obj, type));
|
||||
}
|
||||
|
||||
private void ShowDropDownMenu()
|
||||
{
|
||||
Focus();
|
||||
if (new ScriptType(typeof(Actor)).IsAssignableFrom(_type))
|
||||
if (_type.IsInterface)
|
||||
{
|
||||
SceneObjectSearchPopup.Show(this, new Float2(0, Height), IsValid, obj =>
|
||||
{
|
||||
Value = obj;
|
||||
RootWindow.Focus();
|
||||
Focus();
|
||||
}, PresenterContext);
|
||||
}
|
||||
else if (new ScriptType(typeof(Actor)).IsAssignableFrom(_type))
|
||||
{
|
||||
ActorSearchPopup.Show(this, new Float2(0, Height), IsValid, actor =>
|
||||
{
|
||||
|
||||
@@ -0,0 +1,146 @@
|
||||
// Copyright (c) Wojciech Figat. All rights reserved.
|
||||
|
||||
using System;
|
||||
using FlaxEditor.Windows;
|
||||
using FlaxEditor.Windows.Assets;
|
||||
using FlaxEngine;
|
||||
using FlaxEngine.GUI;
|
||||
using FlaxEngine.Utilities;
|
||||
|
||||
namespace FlaxEditor.GUI
|
||||
{
|
||||
/// <summary>
|
||||
/// Popup that shows the list of scene objects to pick. Supports searching and basic type filtering.
|
||||
/// </summary>
|
||||
/// <seealso cref="FlaxEditor.GUI.ItemsListContextMenu" />
|
||||
public class SceneObjectSearchPopup : ItemsListContextMenu
|
||||
{
|
||||
/// <summary>
|
||||
/// The scene object item.
|
||||
/// </summary>
|
||||
/// <seealso cref="FlaxEditor.GUI.ItemsListContextMenu.Item" />
|
||||
public class SceneObjectItemView : Item
|
||||
{
|
||||
private SceneObject _object;
|
||||
|
||||
/// <summary>
|
||||
/// Gets the scene object.
|
||||
/// </summary>
|
||||
public SceneObject Object => _object;
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="SceneObjectItemView"/> class.
|
||||
/// </summary>
|
||||
/// <param name="obj">The object.</param>
|
||||
public SceneObjectItemView(SceneObject obj)
|
||||
{
|
||||
_object = obj;
|
||||
Category = obj is Actor ? "Actors" : "Scripts";
|
||||
if (obj is Script script)
|
||||
{
|
||||
var type = TypeUtils.GetObjectType(script);
|
||||
Name = script.Actor ? $"{type.Name} ({script.Actor.Name})" : type.Name;
|
||||
}
|
||||
else if (obj is Actor actor)
|
||||
{
|
||||
Name = actor.Name;
|
||||
}
|
||||
else
|
||||
{
|
||||
Name = obj.ToString();
|
||||
}
|
||||
TooltipText = Utilities.Utils.GetTooltip(obj);
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public override void OnDestroy()
|
||||
{
|
||||
_object = null;
|
||||
base.OnDestroy();
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Validates if the given scene object item can be used to pick it.
|
||||
/// </summary>
|
||||
/// <param name="obj">The scene object.</param>
|
||||
/// <returns>True if is valid.</returns>
|
||||
public delegate bool IsValidDelegate(SceneObject obj);
|
||||
|
||||
private IsValidDelegate _isValid;
|
||||
private Action<SceneObject> _selected;
|
||||
|
||||
private SceneObjectSearchPopup(IsValidDelegate isValid, Action<SceneObject> selected, CustomEditors.IPresenterOwner context)
|
||||
{
|
||||
_isValid = isValid;
|
||||
_selected = selected;
|
||||
|
||||
ItemClicked += OnItemClicked;
|
||||
|
||||
if (context is PropertiesWindow || context == null)
|
||||
{
|
||||
// TODO: use async thread to search scenes
|
||||
for (int i = 0; i < Level.ScenesCount; i++)
|
||||
{
|
||||
Find(Level.GetScene(i));
|
||||
}
|
||||
}
|
||||
else if (context is PrefabWindow prefabWindow)
|
||||
{
|
||||
Find(prefabWindow.Graph.MainActor);
|
||||
}
|
||||
|
||||
SortItems();
|
||||
}
|
||||
|
||||
private void OnItemClicked(Item item)
|
||||
{
|
||||
_selected(((SceneObjectItemView)item).Object);
|
||||
}
|
||||
|
||||
private void Find(Actor actor)
|
||||
{
|
||||
if (!actor)
|
||||
return;
|
||||
|
||||
if (_isValid(actor))
|
||||
AddItem(new SceneObjectItemView(actor));
|
||||
|
||||
for (int i = 0; i < actor.ScriptsCount; i++)
|
||||
{
|
||||
var script = actor.GetScript(i);
|
||||
if (_isValid(script))
|
||||
AddItem(new SceneObjectItemView(script));
|
||||
}
|
||||
|
||||
for (int i = 0; i < actor.ChildrenCount; i++)
|
||||
{
|
||||
Find(actor.GetChild(i));
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Shows the popup.
|
||||
/// </summary>
|
||||
/// <param name="showTarget">The show target.</param>
|
||||
/// <param name="showTargetLocation">The show target location.</param>
|
||||
/// <param name="isValid">Event called to check if a given scene object item is valid to be used.</param>
|
||||
/// <param name="selected">Event called on scene object item pick.</param>
|
||||
/// <param name="context">The presenter owner context (i.e. PrefabWindow, PropertiesWindow).</param>
|
||||
/// <returns>The dialog.</returns>
|
||||
public static SceneObjectSearchPopup Show(Control showTarget, Float2 showTargetLocation, IsValidDelegate isValid, Action<SceneObject> selected, CustomEditors.IPresenterOwner context)
|
||||
{
|
||||
var popup = new SceneObjectSearchPopup(isValid, selected, context);
|
||||
popup.Show(showTarget, showTargetLocation);
|
||||
return popup;
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public override void OnDestroy()
|
||||
{
|
||||
_isValid = null;
|
||||
_selected = null;
|
||||
base.OnDestroy();
|
||||
}
|
||||
}
|
||||
}
|
||||
+14
@@ -0,0 +1,14 @@
|
||||
// Copyright (c) Wojciech Figat. All rights reserved.
|
||||
|
||||
using System;
|
||||
|
||||
namespace FlaxEngine
|
||||
{
|
||||
/// <summary>
|
||||
/// Marks a generated interface property as a native scripting object interface reference.
|
||||
/// </summary>
|
||||
[AttributeUsage(AttributeTargets.Field | AttributeTargets.Property)]
|
||||
public sealed class ScriptingObjectInterfaceReferenceAttribute : Attribute
|
||||
{
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
// Copyright (c) Wojciech Figat. All rights reserved.
|
||||
|
||||
using System;
|
||||
|
||||
namespace FlaxEngine
|
||||
{
|
||||
/// <summary>
|
||||
/// Marks a generated interface property as a native soft object interface reference.
|
||||
/// </summary>
|
||||
[AttributeUsage(AttributeTargets.Field | AttributeTargets.Property)]
|
||||
public sealed class SoftObjectInterfaceReferenceAttribute : Attribute
|
||||
{
|
||||
}
|
||||
}
|
||||
@@ -37,7 +37,7 @@ struct FLAXENGINE_API VTableFunctionInjector
|
||||
|
||||
#if USE_NETCORE
|
||||
#define ADD_INTERNAL_CALL(fullName, method)
|
||||
#define DEFINE_INTERNAL_CALL(returnType) extern "C" DLLEXPORT returnType
|
||||
#define DEFINE_INTERNAL_CALL(returnType) extern "C" DLLEXPORT USED returnType
|
||||
#else
|
||||
extern "C" FLAXENGINE_API void mono_add_internal_call(const char* name, const void* method);
|
||||
#define ADD_INTERNAL_CALL(fullName, method) mono_add_internal_call(fullName, (const void*)method)
|
||||
|
||||
@@ -278,6 +278,10 @@ struct MConverter<T, typename TEnableIf<TIsBaseOf<class ScriptingObject, T>::Val
|
||||
// Converter for ScriptingObject References.
|
||||
template<typename T>
|
||||
class ScriptingObjectReference;
|
||||
template<typename T>
|
||||
class ScriptingObjectInterfaceReference;
|
||||
template<typename T>
|
||||
class SoftObjectInterfaceReference;
|
||||
|
||||
template<typename T>
|
||||
struct MConverter<ScriptingObjectReference<T>>
|
||||
@@ -311,6 +315,50 @@ struct MConverter<ScriptingObjectReference<T>>
|
||||
}
|
||||
};
|
||||
|
||||
template<typename TReference, typename TInterface>
|
||||
struct MInterfaceReferenceConverter
|
||||
{
|
||||
MObject* Box(const TReference& data, const MClass* klass)
|
||||
{
|
||||
return data.GetManagedInstance();
|
||||
}
|
||||
|
||||
void Unbox(TReference& result, MObject* data)
|
||||
{
|
||||
result = ScriptingObject::ToInterface<TInterface>(ScriptingObject::ToNative(data));
|
||||
}
|
||||
|
||||
void ToManagedArray(MArray* result, const Span<TReference>& data)
|
||||
{
|
||||
if (data.Length() == 0)
|
||||
return;
|
||||
MObject** objects = (MObject**)Allocator::Allocate(data.Length() * sizeof(MObject*));
|
||||
for (int32 i = 0; i < data.Length(); i++)
|
||||
objects[i] = data[i].GetManagedInstance();
|
||||
MCore::GC::WriteArrayRef(result, Span<MObject*>(objects, data.Length()));
|
||||
Allocator::Free(objects);
|
||||
}
|
||||
|
||||
void ToNativeArray(Span<TReference>& result, const MArray* data)
|
||||
{
|
||||
MObject** dataPtr = MCore::Array::GetAddress<MObject*>(data);
|
||||
for (int32 i = 0; i < result.Length(); i++)
|
||||
result.Get()[i] = ScriptingObject::ToInterface<TInterface>(ScriptingObject::ToNative(dataPtr[i]));
|
||||
}
|
||||
};
|
||||
|
||||
// Converter for Scripting Interface References.
|
||||
template<typename T>
|
||||
struct MConverter<ScriptingObjectInterfaceReference<T>> : MInterfaceReferenceConverter<ScriptingObjectInterfaceReference<T>, T>
|
||||
{
|
||||
};
|
||||
|
||||
// Converter for Soft Object Interface References.
|
||||
template<typename T>
|
||||
struct MConverter<SoftObjectInterfaceReference<T>> : MInterfaceReferenceConverter<SoftObjectInterfaceReference<T>, T>
|
||||
{
|
||||
};
|
||||
|
||||
// Converter for Asset References.
|
||||
template<typename T>
|
||||
class AssetReference;
|
||||
|
||||
@@ -0,0 +1,195 @@
|
||||
// Copyright (c) Wojciech Figat. All rights reserved.
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "Engine/Scripting/ScriptingObjectInterfaceReferenceUtils.h"
|
||||
|
||||
/// <summary>
|
||||
/// The scene object interface reference.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The type of the scripting interface.</typeparam>
|
||||
template<typename T>
|
||||
API_CLASS(InBuild) class ScriptingObjectInterfaceReference : public ScriptingObjectReferenceBase
|
||||
{
|
||||
typedef ScriptingObjectInterfaceReferenceHelper<T> Helper;
|
||||
|
||||
public:
|
||||
typedef ScriptingObjectInterfaceReference<T> Type;
|
||||
|
||||
public:
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="ScriptingObjectInterfaceReference"/> class.
|
||||
/// </summary>
|
||||
ScriptingObjectInterfaceReference()
|
||||
{
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="ScriptingObjectInterfaceReference"/> class.
|
||||
/// </summary>
|
||||
/// <param name="obj">The object to link.</param>
|
||||
ScriptingObjectInterfaceReference(SceneObject* obj)
|
||||
: ScriptingObjectReferenceBase(Helper::IsValidObject(obj) ? obj : nullptr)
|
||||
{
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="ScriptingObjectInterfaceReference"/> class.
|
||||
/// </summary>
|
||||
/// <param name="interfaceObj">The interface object to link.</param>
|
||||
ScriptingObjectInterfaceReference(T* interfaceObj)
|
||||
: ScriptingObjectReferenceBase(Helper::GetSceneObject(interfaceObj))
|
||||
{
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="ScriptingObjectInterfaceReference"/> class.
|
||||
/// </summary>
|
||||
/// <param name="other">The other property.</param>
|
||||
ScriptingObjectInterfaceReference(const ScriptingObjectInterfaceReference& other)
|
||||
: ScriptingObjectReferenceBase(other._object)
|
||||
{
|
||||
}
|
||||
|
||||
ScriptingObjectInterfaceReference(ScriptingObjectInterfaceReference&& other) noexcept
|
||||
: ScriptingObjectReferenceBase(MoveTemp(other))
|
||||
{
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Finalizes an instance of the <see cref="ScriptingObjectInterfaceReference"/> class.
|
||||
/// </summary>
|
||||
~ScriptingObjectInterfaceReference()
|
||||
{
|
||||
}
|
||||
|
||||
public:
|
||||
FORCE_INLINE bool operator==(SceneObject* other) const
|
||||
{
|
||||
return _object == other;
|
||||
}
|
||||
|
||||
FORCE_INLINE bool operator!=(SceneObject* other) const
|
||||
{
|
||||
return _object != other;
|
||||
}
|
||||
|
||||
FORCE_INLINE bool operator==(T* other) const
|
||||
{
|
||||
return Get() == other;
|
||||
}
|
||||
|
||||
FORCE_INLINE bool operator!=(T* other) const
|
||||
{
|
||||
return Get() != other;
|
||||
}
|
||||
|
||||
FORCE_INLINE bool operator==(const ScriptingObjectInterfaceReference& other) const
|
||||
{
|
||||
return _object == other._object;
|
||||
}
|
||||
|
||||
FORCE_INLINE bool operator!=(const ScriptingObjectInterfaceReference& other) const
|
||||
{
|
||||
return _object != other._object;
|
||||
}
|
||||
|
||||
FORCE_INLINE ScriptingObjectInterfaceReference& operator=(SceneObject* other)
|
||||
{
|
||||
OnSet(Helper::IsValidObject(other) ? other : nullptr);
|
||||
return *this;
|
||||
}
|
||||
|
||||
FORCE_INLINE ScriptingObjectInterfaceReference& operator=(T* other)
|
||||
{
|
||||
OnSet(Helper::GetSceneObject(other));
|
||||
return *this;
|
||||
}
|
||||
|
||||
ScriptingObjectInterfaceReference& operator=(const ScriptingObjectInterfaceReference& other)
|
||||
{
|
||||
OnSet(other._object);
|
||||
return *this;
|
||||
}
|
||||
|
||||
ScriptingObjectInterfaceReference& operator=(ScriptingObjectInterfaceReference&& other) noexcept
|
||||
{
|
||||
ScriptingObjectReferenceBase::operator=(MoveTemp(other));
|
||||
return *this;
|
||||
}
|
||||
|
||||
FORCE_INLINE ScriptingObjectInterfaceReference& operator=(const Guid& id)
|
||||
{
|
||||
OnSet(Helper::FindSceneObject(id));
|
||||
return *this;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Implicit conversion to the interface.
|
||||
/// </summary>
|
||||
FORCE_INLINE operator T*() const
|
||||
{
|
||||
return Get();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Implicit conversion to boolean value.
|
||||
/// </summary>
|
||||
FORCE_INLINE operator bool() const
|
||||
{
|
||||
return _object != nullptr;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Interface accessor.
|
||||
/// </summary>
|
||||
FORCE_INLINE T* operator->() const
|
||||
{
|
||||
return Get();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the interface pointer.
|
||||
/// </summary>
|
||||
FORCE_INLINE T* Get() const
|
||||
{
|
||||
return ScriptingObject::ToInterface<T>(_object);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the referenced object.
|
||||
/// </summary>
|
||||
FORCE_INLINE SceneObject* GetObject() const
|
||||
{
|
||||
return static_cast<SceneObject*>(_object);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Copies the object ID into the raw storage.
|
||||
/// </summary>
|
||||
FORCE_INLINE void CopyID(uint32 id[4]) const
|
||||
{
|
||||
memset(id, 0, sizeof(uint32) * 4);
|
||||
if (_object)
|
||||
{
|
||||
const Guid value = GetID();
|
||||
memcpy(id, &value, sizeof(uint32) * 4);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the object as a given type (static cast).
|
||||
/// </summary>
|
||||
template<typename U>
|
||||
FORCE_INLINE U* As() const
|
||||
{
|
||||
return static_cast<U*>(_object);
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
template<typename T>
|
||||
uint32 GetHash(const ScriptingObjectInterfaceReference<T>& key)
|
||||
{
|
||||
return GetHash(key.GetID());
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
// Copyright (c) Wojciech Figat. All rights reserved.
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "Engine/Scripting/ScriptingObjectReference.h"
|
||||
#include "Engine/Level/SceneObject.h"
|
||||
|
||||
/// <summary>
|
||||
/// Utility methods for scene object interface references.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The type of the scripting interface.</typeparam>
|
||||
template<typename T>
|
||||
struct ScriptingObjectInterfaceReferenceHelper
|
||||
{
|
||||
FORCE_INLINE static bool IsValidObject(const SceneObject* obj)
|
||||
{
|
||||
return !obj || obj->GetType().GetInterface(T::TypeInitializer) != nullptr;
|
||||
}
|
||||
|
||||
FORCE_INLINE static SceneObject* GetSceneObject(T* interfaceObj)
|
||||
{
|
||||
return ScriptingObject::Cast<SceneObject>(ScriptingObject::FromInterface<T>(interfaceObj));
|
||||
}
|
||||
|
||||
FORCE_INLINE static SceneObject* FindSceneObject(const Guid& id)
|
||||
{
|
||||
SceneObject* obj = static_cast<SceneObject*>(FindObject(id, SceneObject::GetStaticClass()));
|
||||
return IsValidObject(obj) ? obj : nullptr;
|
||||
}
|
||||
};
|
||||
@@ -0,0 +1,258 @@
|
||||
// Copyright (c) Wojciech Figat. All rights reserved.
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "Engine/Scripting/SoftObjectReference.h"
|
||||
#include "Engine/Scripting/ScriptingObjectInterfaceReferenceUtils.h"
|
||||
|
||||
/// <summary>
|
||||
/// The scene object soft interface reference. Objects gets referenced on use (ID reference is resolving it).
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The type of the scripting interface.</typeparam>
|
||||
template<typename T>
|
||||
API_CLASS(InBuild) class SoftObjectInterfaceReference : public SoftObjectReferenceBase
|
||||
{
|
||||
typedef ScriptingObjectInterfaceReferenceHelper<T> Helper;
|
||||
|
||||
public:
|
||||
typedef SoftObjectInterfaceReference<T> Type;
|
||||
|
||||
public:
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="SoftObjectInterfaceReference"/> class.
|
||||
/// </summary>
|
||||
SoftObjectInterfaceReference()
|
||||
{
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="SoftObjectInterfaceReference"/> class.
|
||||
/// </summary>
|
||||
/// <param name="obj">The object to link.</param>
|
||||
SoftObjectInterfaceReference(SceneObject* obj)
|
||||
{
|
||||
OnSet(Helper::IsValidObject(obj) ? obj : nullptr);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="SoftObjectInterfaceReference"/> class.
|
||||
/// </summary>
|
||||
/// <param name="interfaceObj">The interface object to link.</param>
|
||||
SoftObjectInterfaceReference(T* interfaceObj)
|
||||
{
|
||||
OnSet(Helper::GetSceneObject(interfaceObj));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="SoftObjectInterfaceReference"/> class.
|
||||
/// </summary>
|
||||
/// <param name="other">The other property.</param>
|
||||
SoftObjectInterfaceReference(const SoftObjectInterfaceReference& other)
|
||||
{
|
||||
OnSet(other.GetID());
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="SoftObjectInterfaceReference"/> class.
|
||||
/// </summary>
|
||||
/// <param name="other">The other property.</param>
|
||||
SoftObjectInterfaceReference(SoftObjectInterfaceReference&& other)
|
||||
{
|
||||
OnSet(other.GetID());
|
||||
other.OnSet(nullptr);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Finalizes an instance of the <see cref="SoftObjectInterfaceReference"/> class.
|
||||
/// </summary>
|
||||
~SoftObjectInterfaceReference()
|
||||
{
|
||||
}
|
||||
|
||||
public:
|
||||
FORCE_INLINE bool operator==(SceneObject* other)
|
||||
{
|
||||
return GetObject() == other;
|
||||
}
|
||||
|
||||
FORCE_INLINE bool operator!=(SceneObject* other)
|
||||
{
|
||||
return GetObject() != other;
|
||||
}
|
||||
|
||||
FORCE_INLINE bool operator==(T* other)
|
||||
{
|
||||
return Get() == other;
|
||||
}
|
||||
|
||||
FORCE_INLINE bool operator!=(T* other)
|
||||
{
|
||||
return Get() != other;
|
||||
}
|
||||
|
||||
FORCE_INLINE bool operator==(const SoftObjectInterfaceReference& other)
|
||||
{
|
||||
return GetID() == other.GetID();
|
||||
}
|
||||
|
||||
FORCE_INLINE bool operator!=(const SoftObjectInterfaceReference& other)
|
||||
{
|
||||
return GetID() != other.GetID();
|
||||
}
|
||||
|
||||
SoftObjectInterfaceReference& operator=(const SoftObjectInterfaceReference& other)
|
||||
{
|
||||
if (this != &other)
|
||||
OnSet(other.GetID());
|
||||
return *this;
|
||||
}
|
||||
|
||||
SoftObjectInterfaceReference& operator=(SoftObjectInterfaceReference&& other)
|
||||
{
|
||||
if (this != &other)
|
||||
{
|
||||
OnSet(other.GetID());
|
||||
other.OnSet(nullptr);
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
|
||||
FORCE_INLINE SoftObjectInterfaceReference& operator=(SceneObject* other)
|
||||
{
|
||||
OnSet(Helper::IsValidObject(other) ? other : nullptr);
|
||||
return *this;
|
||||
}
|
||||
|
||||
FORCE_INLINE SoftObjectInterfaceReference& operator=(T* other)
|
||||
{
|
||||
OnSet(Helper::GetSceneObject(other));
|
||||
return *this;
|
||||
}
|
||||
|
||||
FORCE_INLINE SoftObjectInterfaceReference& operator=(const Guid& id)
|
||||
{
|
||||
OnSet(id);
|
||||
return *this;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Implicit conversion to the interface.
|
||||
/// </summary>
|
||||
FORCE_INLINE operator T*() const
|
||||
{
|
||||
return Get();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Implicit conversion to boolean value.
|
||||
/// </summary>
|
||||
FORCE_INLINE operator bool() const
|
||||
{
|
||||
return Get() != nullptr;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Interface accessor.
|
||||
/// </summary>
|
||||
FORCE_INLINE T* operator->() const
|
||||
{
|
||||
return Get();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the object as a given type (static cast).
|
||||
/// </summary>
|
||||
template<typename U>
|
||||
FORCE_INLINE U* As() const
|
||||
{
|
||||
return static_cast<U*>(GetObject());
|
||||
}
|
||||
|
||||
public:
|
||||
/// <summary>
|
||||
/// Gets the interface pointer.
|
||||
/// </summary>
|
||||
FORCE_INLINE T* Get() const
|
||||
{
|
||||
return ScriptingObject::ToInterface<T>(GetObject());
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the referenced object.
|
||||
/// </summary>
|
||||
SceneObject* GetObject() const
|
||||
{
|
||||
if (!_object)
|
||||
const_cast<SoftObjectInterfaceReference*>(this)->OnResolve(SceneObject::GetStaticClass());
|
||||
return Helper::IsValidObject(static_cast<SceneObject*>(_object)) ? static_cast<SceneObject*>(_object) : nullptr;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets managed instance object (or null if no object linked).
|
||||
/// </summary>
|
||||
MObject* GetManagedInstance() const
|
||||
{
|
||||
auto object = GetObject();
|
||||
return object ? object->GetOrCreateManagedInstance() : nullptr;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Determines whether object is assigned and managed instance of the object is alive.
|
||||
/// </summary>
|
||||
bool HasManagedInstance() const
|
||||
{
|
||||
auto object = GetObject();
|
||||
return object && object->HasManagedInstance();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the managed instance object or creates it if missing or null if not assigned.
|
||||
/// </summary>
|
||||
MObject* GetOrCreateManagedInstance() const
|
||||
{
|
||||
auto object = GetObject();
|
||||
return object ? object->GetOrCreateManagedInstance() : nullptr;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Copies the object ID into the raw storage.
|
||||
/// </summary>
|
||||
FORCE_INLINE void CopyID(uint32 id[4]) const
|
||||
{
|
||||
const Guid value = GetID();
|
||||
memcpy(id, &value, sizeof(uint32) * 4);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sets the object.
|
||||
/// </summary>
|
||||
/// <param name="id">The object ID. Uses Scripting to find the registered object of the given ID.</param>
|
||||
FORCE_INLINE void Set(const Guid& id)
|
||||
{
|
||||
OnSet(id);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sets the object.
|
||||
/// </summary>
|
||||
/// <param name="object">The object.</param>
|
||||
FORCE_INLINE void Set(SceneObject* object)
|
||||
{
|
||||
OnSet(Helper::IsValidObject(object) ? object : nullptr);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sets the object.
|
||||
/// </summary>
|
||||
/// <param name="interfaceObj">The interface object.</param>
|
||||
FORCE_INLINE void Set(T* interfaceObj)
|
||||
{
|
||||
OnSet(Helper::GetSceneObject(interfaceObj));
|
||||
}
|
||||
};
|
||||
|
||||
template<typename T>
|
||||
uint32 GetHash(const SoftObjectInterfaceReference<T>& key)
|
||||
{
|
||||
return GetHash(key.GetID());
|
||||
}
|
||||
@@ -133,6 +133,14 @@ public:
|
||||
v = ptr;
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
FORCE_INLINE void Read(ScriptingObjectInterfaceReference<T>& v)
|
||||
{
|
||||
uint32 id[4];
|
||||
ReadBytes(id, sizeof(id));
|
||||
v = *(Guid*)id;
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
FORCE_INLINE void Read(SoftObjectReference<T>& v)
|
||||
{
|
||||
@@ -141,6 +149,14 @@ public:
|
||||
v.Set(*(Guid*)id);
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
FORCE_INLINE void Read(SoftObjectInterfaceReference<T>& v)
|
||||
{
|
||||
uint32 id[4];
|
||||
ReadBytes(id, sizeof(id));
|
||||
v.Set(*(Guid*)id);
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
FORCE_INLINE void Read(AssetReference<T>& v)
|
||||
{
|
||||
|
||||
@@ -14,8 +14,12 @@ struct VariantType;
|
||||
template<typename T>
|
||||
class ScriptingObjectReference;
|
||||
template<typename T>
|
||||
class ScriptingObjectInterfaceReference;
|
||||
template<typename T>
|
||||
class SoftObjectReference;
|
||||
template<typename T>
|
||||
class SoftObjectInterfaceReference;
|
||||
template<typename T>
|
||||
class AssetReference;
|
||||
template<typename T>
|
||||
class WeakAssetReference;
|
||||
@@ -454,7 +458,6 @@ namespace Serialization
|
||||
}
|
||||
|
||||
FLAXENGINE_API bool ShouldSerializeRef(const SceneObject* v, const SceneObject* other);
|
||||
|
||||
template<typename T>
|
||||
inline typename TEnableIf<TAnd<TIsBaseOf<ScriptingObject, T>, TNot<TIsBaseOf<SceneObject, T>>>::Value, bool>::Type ShouldSerialize(const T* v, const void* otherObj)
|
||||
{
|
||||
@@ -470,7 +473,7 @@ namespace Serialization
|
||||
{
|
||||
Guid id;
|
||||
Deserialize(stream, id, modifier);
|
||||
modifier->IdsMapping.TryGet(id, id);
|
||||
modifier->IdsMapping.TryGet(id, id);
|
||||
v = (T*)::FindObject(id, T::GetStaticClass());
|
||||
}
|
||||
|
||||
@@ -497,7 +500,28 @@ namespace Serialization
|
||||
{
|
||||
Guid id;
|
||||
Deserialize(stream, id, modifier);
|
||||
modifier->IdsMapping.TryGet(id, id);
|
||||
modifier->IdsMapping.TryGet(id, id);
|
||||
v = id;
|
||||
}
|
||||
|
||||
// Scripting Interface Reference
|
||||
|
||||
template<typename T>
|
||||
inline bool ShouldSerialize(const ScriptingObjectInterfaceReference<T>& v, const void* otherObj)
|
||||
{
|
||||
return !otherObj || ShouldSerializeRef(v.GetObject(), ((ScriptingObjectInterfaceReference<T>*)otherObj)->GetObject());
|
||||
}
|
||||
template<typename T>
|
||||
inline void Serialize(ISerializable::SerializeStream& stream, const ScriptingObjectInterfaceReference<T>& v, const void* otherObj)
|
||||
{
|
||||
stream.Guid(v.GetID());
|
||||
}
|
||||
template<typename T>
|
||||
inline void Deserialize(ISerializable::DeserializeStream& stream, ScriptingObjectInterfaceReference<T>& v, ISerializeModifier* modifier)
|
||||
{
|
||||
Guid id;
|
||||
Deserialize(stream, id, modifier);
|
||||
modifier->IdsMapping.TryGet(id, id);
|
||||
v = id;
|
||||
}
|
||||
|
||||
@@ -518,7 +542,28 @@ namespace Serialization
|
||||
{
|
||||
Guid id;
|
||||
Deserialize(stream, id, modifier);
|
||||
modifier->IdsMapping.TryGet(id, id);
|
||||
modifier->IdsMapping.TryGet(id, id);
|
||||
v = id;
|
||||
}
|
||||
|
||||
// Soft Object Interface Reference
|
||||
|
||||
template<typename T>
|
||||
inline bool ShouldSerialize(const SoftObjectInterfaceReference<T>& v, const void* otherObj)
|
||||
{
|
||||
return !otherObj || ShouldSerializeRef(v.GetObject(), ((SoftObjectInterfaceReference<T>*)otherObj)->GetObject());
|
||||
}
|
||||
template<typename T>
|
||||
inline void Serialize(ISerializable::SerializeStream& stream, const SoftObjectInterfaceReference<T>& v, const void* otherObj)
|
||||
{
|
||||
stream.Guid(v.GetID());
|
||||
}
|
||||
template<typename T>
|
||||
inline void Deserialize(ISerializable::DeserializeStream& stream, SoftObjectInterfaceReference<T>& v, ISerializeModifier* modifier)
|
||||
{
|
||||
Guid id;
|
||||
Deserialize(stream, id, modifier);
|
||||
modifier->IdsMapping.TryGet(id, id);
|
||||
v = id;
|
||||
}
|
||||
|
||||
|
||||
@@ -17,8 +17,12 @@ class ScriptingObject;
|
||||
template<typename T>
|
||||
class ScriptingObjectReference;
|
||||
template<typename T>
|
||||
class ScriptingObjectInterfaceReference;
|
||||
template<typename T>
|
||||
class SoftObjectReference;
|
||||
template<typename T>
|
||||
class SoftObjectInterfaceReference;
|
||||
template<typename T>
|
||||
class AssetReference;
|
||||
template<typename T>
|
||||
class WeakAssetReference;
|
||||
|
||||
@@ -156,11 +156,29 @@ public:
|
||||
{
|
||||
Write(v.Get());
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
FORCE_INLINE void Write(const ScriptingObjectInterfaceReference<T>& v)
|
||||
{
|
||||
uint32 id[4];
|
||||
v.CopyID(id);
|
||||
WriteBytes(id, sizeof(id));
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
FORCE_INLINE void Write(const SoftObjectReference<T>& v)
|
||||
{
|
||||
Write(v.Get());
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
FORCE_INLINE void Write(const SoftObjectInterfaceReference<T>& v)
|
||||
{
|
||||
uint32 id[4];
|
||||
v.CopyID(id);
|
||||
WriteBytes(id, sizeof(id));
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
FORCE_INLINE void Write(const AssetReference<T>& v)
|
||||
{
|
||||
|
||||
@@ -6,6 +6,8 @@
|
||||
#include "Engine/Core/Math/Vector3.h"
|
||||
#include "Engine/Core/Collections/Array.h"
|
||||
#include "Engine/Scripting/ScriptingObject.h"
|
||||
#include "Engine/Scripting/ScriptingObjectInterfaceReference.h"
|
||||
#include "Engine/Scripting/SoftObjectInterfaceReference.h"
|
||||
#include "Engine/Scripting/SerializableScriptingObject.h"
|
||||
#include "Engine/Scripting/SoftTypeReference.h"
|
||||
#include "Engine/Content/SceneReference.h"
|
||||
@@ -177,6 +179,10 @@ public:
|
||||
|
||||
// Test struct
|
||||
API_FIELD() TestStruct SimpleStruct;
|
||||
// Test interface reference
|
||||
API_FIELD() ScriptingObjectInterfaceReference<ITestInterface> InterfaceRef;
|
||||
// Test soft interface reference
|
||||
API_FIELD() SoftObjectInterfaceReference<ITestInterface> SoftInterfaceRef;
|
||||
|
||||
// Test event
|
||||
API_EVENT() Delegate<int32, Float3, const String&, String&, TestStruct&, const Array<TestStruct>&, Array<TestStruct>&> SimpleEvent;
|
||||
|
||||
@@ -108,7 +108,7 @@ namespace Flax.Build.Bindings
|
||||
if (attribute && valueType != null && !valueType.IsArray)
|
||||
{
|
||||
//if (valueType.Type == "")
|
||||
//ScriptingObjectReference
|
||||
//ScriptingObjectReference, ScriptingObjectInterfaceReference, SoftObjectInterfaceReference
|
||||
apiType = FindApiTypeInfo(buildData, valueType, caller);
|
||||
|
||||
// Object reference
|
||||
@@ -350,6 +350,10 @@ namespace Flax.Build.Bindings
|
||||
if (CSharpNativeToManagedDefault.TryGetValue(typeInfo.Type, out result))
|
||||
return result;
|
||||
|
||||
// Interface reference property
|
||||
if (typeInfo.IsInterfaceRef)
|
||||
return marshalling ? "IntPtr" : GenerateCSharpNativeToManaged(buildData, typeInfo.GenericArgs[0], caller, marshalling);
|
||||
|
||||
// Object reference property
|
||||
if (typeInfo.IsObjectRef)
|
||||
return GenerateCSharpNativeToManaged(buildData, typeInfo.GenericArgs[0], caller, marshalling);
|
||||
@@ -556,6 +560,10 @@ namespace Flax.Build.Bindings
|
||||
}
|
||||
return string.Empty;
|
||||
default:
|
||||
// Interface reference property
|
||||
if (typeInfo.IsInterfaceRef)
|
||||
return string.Format("FlaxEngine.Object.GetUnmanagedInterface({{0}}, typeof({0}))", GenerateCSharpNativeToManaged(buildData, typeInfo.GenericArgs[0], caller));
|
||||
|
||||
var apiType = FindApiTypeInfo(buildData, typeInfo, caller);
|
||||
if (apiType != null)
|
||||
{
|
||||
@@ -778,9 +786,17 @@ namespace Flax.Build.Bindings
|
||||
}
|
||||
}
|
||||
#endif
|
||||
const string interfaceResultName = "__interfaceResult";
|
||||
|
||||
var returnInterfaceRef = !functionInfo.Glue.UseReferenceForResult && functionInfo.ReturnType.IsInterfaceRef;
|
||||
|
||||
if (functionInfo.Glue.UseReferenceForResult)
|
||||
{
|
||||
}
|
||||
else if (returnInterfaceRef)
|
||||
{
|
||||
contents.Append("var ").Append(interfaceResultName).Append(" = ");
|
||||
}
|
||||
else if (!functionInfo.ReturnType.IsVoid)
|
||||
{
|
||||
contents.Append("return ");
|
||||
@@ -851,6 +867,11 @@ namespace Flax.Build.Bindings
|
||||
}
|
||||
|
||||
contents.Append(')');
|
||||
if (returnInterfaceRef)
|
||||
{
|
||||
var managedType = GenerateCSharpNativeToManaged(buildData, functionInfo.ReturnType.GenericArgs[0], caller);
|
||||
contents.Append("; return ").Append(interfaceResultName).Append(" != IntPtr.Zero ? Unsafe.As<").Append(managedType).Append(">(ManagedHandle.FromIntPtr(").Append(interfaceResultName).Append(").Target) : null");
|
||||
}
|
||||
if ((functionInfo.ReturnType.Type == "Array" || functionInfo.ReturnType.Type == "Span" || functionInfo.ReturnType.Type == "DataContainer") && functionInfo.ReturnType.GenericArgs != null)
|
||||
{
|
||||
// Convert array that uses different type for marshalling
|
||||
@@ -987,6 +1008,12 @@ namespace Flax.Build.Bindings
|
||||
private static void GenerateCSharpAttributes(BuildData buildData, StringBuilder contents, string indent, ApiTypeInfo apiTypeInfo, MemberInfo memberInfo, bool useUnmanaged, string defaultValue = null, TypeInfo defaultValueType = null)
|
||||
{
|
||||
GenerateCSharpAttributes(buildData, contents, indent, apiTypeInfo, memberInfo.Attributes, memberInfo.Comment, true, useUnmanaged, defaultValue, memberInfo.DeprecatedMessage, defaultValueType);
|
||||
var memberType = (memberInfo as FieldInfo)?.Type ?? (memberInfo as PropertyInfo)?.Type;
|
||||
if (memberType != null && memberType.IsInterfaceRef)
|
||||
{
|
||||
var attribute = memberType.Type == "SoftObjectInterfaceReference" ? "SoftObjectInterfaceReference" : "ScriptingObjectInterfaceReference";
|
||||
contents.Append(indent).Append("[FlaxEngine.").Append(attribute).AppendLine("]");
|
||||
}
|
||||
}
|
||||
|
||||
private static bool GenerateCSharpStructureUseDefaultInitialize(BuildData buildData, StructureInfo structureInfo)
|
||||
|
||||
@@ -167,6 +167,8 @@ namespace Flax.Build.Bindings
|
||||
return $"Variant(StringView({value}))";
|
||||
if (typeInfo.Type == "StringAnsi")
|
||||
return $"Variant(StringAnsiView({value}))";
|
||||
if (typeInfo.IsInterfaceRef)
|
||||
return $"Variant({value}.GetObject())";
|
||||
if (typeInfo.IsObjectRef)
|
||||
return $"Variant({value}.Get())";
|
||||
if (typeInfo.Type == "SoftTypeReference")
|
||||
@@ -307,6 +309,8 @@ namespace Flax.Build.Bindings
|
||||
return $"((StringView){value}).GetText()"; // (StringView)Variant, if not empty, is guaranteed to point to a null-terminated buffer.
|
||||
if (typeInfo.Type == "ScriptingObjectReference" || typeInfo.Type == "SoftObjectReference")
|
||||
return $"ScriptingObject::Cast<{typeInfo.GenericArgs[0].Type}>((ScriptingObject*){value})";
|
||||
if (typeInfo.IsInterfaceRef)
|
||||
return $"ScriptingObject::ToInterface<{typeInfo.GenericArgs[0].Type}>((ScriptingObject*){value})";
|
||||
if (typeInfo.IsObjectRef)
|
||||
return $"ScriptingObject::Cast<{typeInfo.GenericArgs[0].Type}>((Asset*){value})";
|
||||
if (typeInfo.Type == "SoftTypeReference")
|
||||
@@ -797,6 +801,19 @@ namespace Flax.Build.Bindings
|
||||
type = "MObject*";
|
||||
return "MUtils::ToNative({0})";
|
||||
default:
|
||||
// Interface reference property
|
||||
if (typeInfo.IsInterfaceRef)
|
||||
{
|
||||
if (CppNonPodTypesConvertingGeneration)
|
||||
{
|
||||
type = "MObject*";
|
||||
return "ScriptingObject::ToInterface<" + typeInfo.GenericArgs[0].Type + ">(ScriptingObject::ToNative({0}))";
|
||||
}
|
||||
|
||||
type = typeInfo.GenericArgs[0].Type + '*';
|
||||
return string.Empty;
|
||||
}
|
||||
|
||||
// Object reference property
|
||||
if (typeInfo.IsObjectRef)
|
||||
{
|
||||
|
||||
@@ -18,6 +18,8 @@ namespace Flax.Build.Bindings
|
||||
"ManagedScriptingObject",
|
||||
"PersistentScriptingObject",
|
||||
"ScriptingObjectReference",
|
||||
"ScriptingObjectInterfaceReference",
|
||||
"SoftObjectInterfaceReference",
|
||||
"AssetReference",
|
||||
"BinaryAsset",
|
||||
"SceneObject",
|
||||
|
||||
@@ -38,10 +38,18 @@ namespace Flax.Build.Bindings
|
||||
/// Gets a value indicating whether this type is a reference to another object.
|
||||
/// </summary>
|
||||
public bool IsObjectRef => (Type == "ScriptingObjectReference" ||
|
||||
Type == "ScriptingObjectInterfaceReference" ||
|
||||
Type == "AssetReference" ||
|
||||
Type == "WeakAssetReference" ||
|
||||
Type == "SoftAssetReference" ||
|
||||
Type == "SoftObjectReference") && GenericArgs != null;
|
||||
Type == "SoftObjectReference" ||
|
||||
Type == "SoftObjectInterfaceReference") && GenericArgs != null;
|
||||
|
||||
/// <summary>
|
||||
/// Gets a value indicating whether this type is a reference to another object filtered by interface.
|
||||
/// </summary>
|
||||
public bool IsInterfaceRef => (Type == "ScriptingObjectInterfaceReference" ||
|
||||
Type == "SoftObjectInterfaceReference") && GenericArgs != null;
|
||||
|
||||
public TypeInfo()
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user