Example #1
0
        /// <summary>
        /// Creates the default resource for the given type name..
        /// </summary>
        /// <typeparam name="T">The type for which the generic resource should be created.</typeparam>
        internal static T CreateDefaultResource <T>()
            where T : Resource
        {
            Type resourceType = typeof(T);
            T    result       = null;

            // Try to create default resources
            if (resourceType == typeof(MaterialResource))
            {
                result = new SimpleColoredMaterialResource() as T;
            }
            else if (resourceType == typeof(TextureResource))
            {
#if DESKTOP
                result = new LinearGradientTextureResource(
                    Color4.White,
                    Color4.LightGray,
                    GradientDirection.Directional,
                    32, 32) as T;
#else
                result = new StandardTextureResource(
                    new AssemblyResourceLink(
                        typeof(ResourceDictionary),
                        "SeeingSharp.Multimedia.Resources.Textures.Blank_16x16.png")) as T;
#endif
            }
            else if (resourceType == typeof(GeometryResource))
            {
                VertexStructure dummyStructure = new VertexStructure();
                dummyStructure.FirstSurface.BuildCube24V(
                    Vector3.Zero,
                    new Vector3(1f, 1f, 1f),
                    Color4.White);
                result = new GeometryResource(dummyStructure) as T;
            }

            //Try to create the resource using the standard constructor
            if (result == null)
            {
#if DESKTOP
                ConstructorInfo standardConstructor = resourceType.GetConstructor(
                    BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance,
                    null, Type.EmptyTypes, null);
#else
                ConstructorInfo standardConstructor =
                    resourceType.GetTypeInfo().DeclaredConstructors
                    .FirstOrDefault((actConstructor) => actConstructor.GetParameters().Length <= 0);
#endif
                if (standardConstructor != null)
                {
                    result = Activator.CreateInstance(resourceType) as T;
                }
            }

            if (result == null)
            {
                throw new SeeingSharpGraphicsException("Unable to create default resource for resource type " + resourceType.FullName);
            }
            return(result);
        }
        /// <summary>
        /// Creates the default resource for the given type name..
        /// </summary>
        /// <typeparam name="T">The type for which the generic resource should be created.</typeparam>
        internal static T CreateDefaultResource <T>()
            where T : Resource
        {
            var resourceType = typeof(T);
            T?  result       = null;

            // Try to create default resources
            if (resourceType == typeof(MaterialResource))
            {
                result = new StandardMaterialResource() as T;
            }
            else if (resourceType == typeof(TextureResource))
            {
                result = new StandardTextureResource(
                    new AssemblyResourceLink(
                        typeof(ResourceDictionary),
                        "SeeingSharp.Resources.Textures.Blank_16x16.png")) as T;
            }
            else if (resourceType == typeof(GeometryResource))
            {
                var dummyGeometry = new Geometry();
                dummyGeometry.FirstSurface.BuildCube(
                    Vector3.Zero,
                    new Vector3(1f, 1f, 1f))
                .SetVertexColor(Color4.White);
                result = new GeometryResource(dummyGeometry) as T;
            }

            //Try to create the resource using the standard constructor
            if (result == null)
            {
                var standardConstructor =
                    resourceType.GetTypeInfo().DeclaredConstructors
                    .FirstOrDefault(actConstructor => actConstructor.GetParameters().Length <= 0);
                if (standardConstructor != null)
                {
                    result = Activator.CreateInstance(resourceType) as T;
                }
            }

            if (result == null)
            {
                throw new SeeingSharpGraphicsException("Unable to create default resource for resource type " + resourceType.FullName);
            }
            return(result);
        }