Skip to content

Commit

Permalink
More type safety
Browse files Browse the repository at this point in the history
  • Loading branch information
aantropov committed Aug 17, 2024
1 parent 6170d5a commit 8fe7f6f
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 23 deletions.
26 changes: 24 additions & 2 deletions Editor/Utility/EngineTypes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -33,7 +37,11 @@ public class Vec3Property : Property<SailorEditor.Vec3> { }
public class Vec2Property : Property<SailorEditor.Vec2> { }
public class FileIdProperty : Property<FileId> { }
public class InstanceIdProperty : Property<InstanceId> { }
public class ObjectPtrProperty : PropertyBase { }
public class ObjectPtrProperty : PropertyBase
{
public string ElementTypename { get; set; } = "";
}

public class EnumProperty : Property<string> { }
public partial class ObjectPtr : ObservableObject, ICloneable, IComparable<ObjectPtr>
{
Expand Down Expand Up @@ -142,6 +150,20 @@ class EngineTypes
public Dictionary<string, ComponentType> Components { get; private set; } = [];
public Dictionary<string, List<string>> 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()
Expand Down Expand Up @@ -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}")
Expand Down
3 changes: 2 additions & 1 deletion Editor/Utility/Templates.cs
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ public static View TextureEditor<TBindingContext>(Expression<Func<TBindingContex
return stackLayout;
}

public static View FileIdEditor<TBindingContext>(object bindingContext, string bindingPath, Expression<Func<TBindingContext, FileId>> getter, Action<TBindingContext, FileId> setter)
public static View FileIdEditor<TBindingContext>(object bindingContext, string bindingPath, Expression<Func<TBindingContext, FileId>> getter, Action<TBindingContext, FileId> setter, Type supportedType = null)
{
var clearButton = new Button { Text = "Clear" };
clearButton.Clicked += async (sender, e) => setter((TBindingContext)bindingContext, new FileId());
Expand All @@ -182,6 +182,7 @@ public static View FileIdEditor<TBindingContext>(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));
Expand Down
47 changes: 27 additions & 20 deletions Editor/Views/InspectorView/ComponentTemplate.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -44,30 +46,35 @@ public ComponentTemplate()
}
else
{
propertyEditor = property.Value switch
if (component.Typename.Properties[property.Key] is ObjectPtrProperty objectPtr)
{
Observable<float> 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<FileId> observableFileId => Templates.FileIdEditor(component.OverrideProperties[property.Key], nameof(Observable<FileId>.Value), (Observable<FileId> 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<float> 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<FileId> observableFileId => Templates.FileIdEditor(component.OverrideProperties[property.Key], nameof(Observable<FileId>.Value), (Observable<FileId> vm) => vm.Value, (vm, value) => vm.Value = value),
_ => new Label { Text = "Unsupported property type" }
};
}
Templates.AddGridRowWithLabel(props, property.Key, propertyEditor, GridLength.Auto);
Expand Down

0 comments on commit 8fe7f6f

Please sign in to comment.