diff --git a/Editor/Utility/EngineTypes.cs b/Editor/Utility/EngineTypes.cs index 9d2c35ac..361e4618 100644 --- a/Editor/Utility/EngineTypes.cs +++ b/Editor/Utility/EngineTypes.cs @@ -11,6 +11,10 @@ using YamlDotNet.Serialization.NamingConventions; using SailorEngine; using System.Xml.Linq; +using System.Text.RegularExpressions; +using SailorEditor.Helpers; +using SailorEditor.Utility; +using System.ComponentModel; namespace SailorEngine { @@ -33,7 +37,11 @@ public class Vec3Property : Property { } public class Vec2Property : Property { } public class FileIdProperty : Property { } public class InstanceIdProperty : Property { } - public class ObjectPtrProperty : PropertyBase { } + public class ObjectPtrProperty : PropertyBase + { + public string ElementTypename { get; set; } = ""; + } + public class EnumProperty : Property { } public partial class ObjectPtr : ObservableObject, ICloneable, IComparable { @@ -142,6 +150,20 @@ class EngineTypes public Dictionary Components { get; private set; } = []; public Dictionary> Enums { get; private set; } = []; + public static string GetEditorType(string engineType) + { + string editorType = engineType switch + { + "class Sailor::Model" => typeof(ModelFile).FullName, + "class Sailor::Texture" => typeof(TextureFile).FullName, + "class Sailor::Material" => typeof(MaterialFile).FullName, + "class Sailor::Shader" => typeof(ShaderFile).FullName, + _ => engineType + }; + + return editorType; + } + public static EngineTypes FromYaml(string yamlContent) { var deserializer = new DeserializerBuilder() @@ -171,7 +193,7 @@ public static EngineTypes FromYaml(string yamlContent) "struct glm::vec<3,float,0>" => new Vec3Property(), "struct glm::vec<4,float,0>" => new Vec4Property(), "float" => new FloatProperty(), - var value when value.StartsWith("class Sailor::TObjectPtr") => new ObjectPtrProperty(), + var value when value.StartsWith("class Sailor::TObjectPtr") => new ObjectPtrProperty() { ElementTypename = GetEditorType(Regex.Match(value, @"<(.+?)>").Groups[1].Value) }, var value when value.Contains("TObjectPtr") => new InstanceIdProperty(), var value when value.StartsWith("enum") => new EnumProperty() { Typename = value }, _ => throw new InvalidOperationException($"Unexpected property type: {property.Value}") diff --git a/Editor/Utility/Templates.cs b/Editor/Utility/Templates.cs index f7984e0a..c816fe40 100644 --- a/Editor/Utility/Templates.cs +++ b/Editor/Utility/Templates.cs @@ -161,7 +161,7 @@ public static View TextureEditor(Expression(object bindingContext, string bindingPath, Expression> getter, Action setter) + public static View FileIdEditor(object bindingContext, string bindingPath, Expression> getter, Action setter, Type supportedType = null) { var clearButton = new Button { Text = "Clear" }; clearButton.Clicked += async (sender, e) => setter((TBindingContext)bindingContext, new FileId()); @@ -182,6 +182,7 @@ public static View FileIdEditor(object bindingContext, string b var dragAndDropBehaviour = new FileIdDragAndDropBehaviour(); dragAndDropBehaviour.SetBinding(FileIdDragAndDropBehaviour.BoundPropertyProperty, new Binding(bindingPath)); + dragAndDropBehaviour.SupportedType = supportedType; var selectBehavior = new FileIdSelectBehavior(); selectBehavior.SetBinding(FileIdSelectBehavior.BoundPropertyProperty, new Binding(bindingPath)); diff --git a/Editor/Views/InspectorView/ComponentTemplate.cs b/Editor/Views/InspectorView/ComponentTemplate.cs index 42fcf22e..8262392b 100644 --- a/Editor/Views/InspectorView/ComponentTemplate.cs +++ b/Editor/Views/InspectorView/ComponentTemplate.cs @@ -5,6 +5,8 @@ using SailorEditor.ViewModels; using SailorEditor.Views; using SailorEngine; +using Windows.Foundation.Collections; +using YamlDotNet.Core.Tokens; namespace SailorEditor; public class ComponentTemplate : DataTemplate @@ -44,30 +46,35 @@ public ComponentTemplate() } else { - propertyEditor = property.Value switch + if (component.Typename.Properties[property.Key] is ObjectPtrProperty objectPtr) { - Observable observableFloat => Templates.FloatEditor((Component vm) => observableFloat.Value, (vm, value) => observableFloat.Value = value), - Rotation quat => Templates.RotationEditor((Component vm) => quat), - Vec4 vec4 => Templates.Vec4Editor((Component vm) => vec4), - Vec3 vec3 => Templates.Vec3Editor((Component vm) => vec3), - Vec2 vec2 => Templates.Vec2Editor((Component vm) => vec2), - Observable observableFileId => Templates.FileIdEditor(component.OverrideProperties[property.Key], nameof(Observable.Value), (Observable vm) => vm.Value, (vm, value) => vm.Value = value), - _ => new Label { Text = "Unsupported property type" } - }; - - if (property.Value is ObjectPtr ptr) - { - if (!ptr.FileId.IsEmpty()) - { - propertyEditor = Templates.FileIdEditor(ptr, - nameof(ObjectPtr.FileId), (ObjectPtr p) => p.FileId, (p, value) => p.FileId = value); - } - else if (!ptr.InstanceId.IsEmpty()) + if (property.Value is ObjectPtr ptr) { - propertyEditor = Templates.InstanceIdEditor(ptr, - nameof(ObjectPtr.InstanceId), (ObjectPtr vm) => ptr.InstanceId, (p, value) => p.InstanceId = value); + if (!ptr.FileId.IsEmpty()) + { + Type type = Type.GetType(objectPtr.ElementTypename); + + propertyEditor = Templates.FileIdEditor(ptr, + nameof(ObjectPtr.FileId), (ObjectPtr p) => p.FileId, (p, value) => p.FileId = value, type); + } + else if (!ptr.InstanceId.IsEmpty()) + { + propertyEditor = Templates.InstanceIdEditor(ptr, + nameof(ObjectPtr.InstanceId), (ObjectPtr vm) => ptr.InstanceId, (p, value) => p.InstanceId = value); + } } } + else + propertyEditor = property.Value switch + { + Observable observableFloat => Templates.FloatEditor((Component vm) => observableFloat.Value, (vm, value) => observableFloat.Value = value), + Rotation quat => Templates.RotationEditor((Component vm) => quat), + Vec4 vec4 => Templates.Vec4Editor((Component vm) => vec4), + Vec3 vec3 => Templates.Vec3Editor((Component vm) => vec3), + Vec2 vec2 => Templates.Vec2Editor((Component vm) => vec2), + Observable observableFileId => Templates.FileIdEditor(component.OverrideProperties[property.Key], nameof(Observable.Value), (Observable vm) => vm.Value, (vm, value) => vm.Value = value), + _ => new Label { Text = "Unsupported property type" } + }; } Templates.AddGridRowWithLabel(props, property.Key, propertyEditor, GridLength.Auto);