Browse Source

Allowed Asset class to have a reference back to ModelRoot

vpenades 2 years ago
parent
commit
2b15aa1685

+ 29 - 6
src/SharpGLTF.Core/Schema2/gltf.Asset.cs

@@ -2,10 +2,12 @@
 using System.Collections.Generic;
 using System.Linq;
 
+using SharpGLTF.Collections;
+
 namespace SharpGLTF.Schema2
 {
     [System.Diagnostics.DebuggerDisplay("{Version} {MinVersion} {Generator} {Copyright}")]
-    public sealed partial class Asset
+    public sealed partial class Asset : Collections.IChildOfList<ModelRoot>
     {
         #region lifecycle
 
@@ -26,9 +28,21 @@ namespace SharpGLTF.Schema2
             };
         }
 
-        #endregion
+        void IChildOfList<ModelRoot>.SetLogicalParent(ModelRoot parent, int index)
+        {
+            _LogicalParent = parent;
+            _LogicalIndex = index;
+        }
+
+        private ModelRoot _LogicalParent;
+        private int _LogicalIndex;
+
+        ModelRoot IChildOfList<ModelRoot>.LogicalParent => _LogicalParent;
+        int IChildOfList<ModelRoot>.LogicalIndex => _LogicalIndex;
+
+        #endregion        
 
-        #region properties
+        #region properties        
 
         public static string AssemblyInformationalVersion
         {
@@ -49,9 +63,9 @@ namespace SharpGLTF.Schema2
         public string Copyright { get => _copyright; set => _copyright = value.AsEmptyNullable(); }
         public string Generator { get => _generator; set => _generator = value.AsEmptyNullable(); }
 
-        public Version Version      => Version.TryParse(   _version, out Version ver) ? ver : ZEROVERSION;
+        public Version Version => Version.TryParse(_version, out Version ver) ? ver : ZEROVERSION;
 
-        public Version MinVersion   => Version.TryParse(_minVersion, out Version ver) ? ver : MINVERSION;
+        public Version MinVersion => Version.TryParse(_minVersion, out Version ver) ? ver : MINVERSION;        
 
         #endregion
 
@@ -76,8 +90,17 @@ namespace SharpGLTF.Schema2
             validate.IsGreaterOrEqual(nameof(Version), Version, MINVERSION);
 
             // if (MinVersion > MAXVERSION) result.AddSemanticError( $"Maximum supported version is {MAXVERSION} but found:{MinVersion}");
-        }
+        }        
 
         #endregion
     }
+
+    partial class ModelRoot
+    {
+        public Asset Asset
+        {
+            get => _asset;
+            set => GetChildSetter(this).SetProperty(ref _asset, value);
+        }
+    }
 }

+ 5 - 0
src/SharpGLTF.Core/Schema2/gltf.ExtraProperties.cs

@@ -57,6 +57,11 @@ namespace SharpGLTF.Schema2
 
         #region API
 
+        protected static Collections.ChildSetter<T> GetChildSetter<T>(T owner) where T:ExtraProperties
+        {
+            return new Collections.ChildSetter<T>(owner);
+        }
+
         public T GetExtension<T>()
             where T : JsonSerializable
         {

+ 2 - 4
src/SharpGLTF.Core/Schema2/gltf.Root.cs

@@ -18,7 +18,7 @@ namespace SharpGLTF.Schema2
         public static ModelRoot CreateModel()
         {
             var root = new ModelRoot();
-            root._asset = Asset.CreateDefault(string.Empty);
+            root.Asset = Asset.CreateDefault(string.Empty);
 
             return root;
         }
@@ -87,9 +87,7 @@ namespace SharpGLTF.Schema2
 
         #endregion
 
-        #region properties
-
-        public Asset Asset => _asset;
+        #region properties        
 
         public IEnumerable<String> ExtensionsUsed               => _extensionsUsed;