示例#1
0
        /// <summary>
        /// Function to load a dependency file.
        /// </summary>
        /// <param name="dependency">The dependency to load.</param>
        /// <param name="stream">Stream containing the dependency file.</param>
        /// <returns>
        /// The result of the load operation.  If the dependency loaded correctly, then the developer should return a successful result.  If the dependency
        /// is not vital to the content, then the developer can return a continue result, otherwise the developer should return fatal result and the content will
        /// not continue loading.
        /// </returns>
        protected override DependencyLoadResult OnLoadDependencyFile(Dependency dependency, Stream stream)
        {
            if (!string.Equals(dependency.Type, TextureDependencyType, StringComparison.OrdinalIgnoreCase))
            {
                return(new DependencyLoadResult(DependencyLoadState.ErrorContinue,
                                                string.Format(Resources.GORSPR_ERR_UNKNOWN_DEPENDENCY, dependency.Type)));
            }

            using (IImageEditorContent imageContent = ImageEditor.ImportContent(dependency.EditorFile, stream))
            {
                if (imageContent.Image.Settings.ImageType != ImageType.Image2D)
                {
                    return(new DependencyLoadResult(DependencyLoadState.ErrorContinue, string.Format(Resources.GORSPR_ERR_2D_TEXTURE_ONLY)));
                }

                if (imageContent.Image == null)
                {
                    return(new DependencyLoadResult(DependencyLoadState.ErrorContinue, string.Format(Resources.GORSPR_WARN_NO_TEXTURE, Name, dependency.EditorFile.FilePath)));
                }

                if (!Dependencies.Contains(dependency.EditorFile, dependency.Type))
                {
                    Dependencies[dependency.EditorFile, dependency.Type] = dependency.Clone();
                }

                Dependencies.CacheDependencyObject(dependency.EditorFile,
                                                   dependency.Type,
                                                   Graphics.Textures.CreateTexture <GorgonTexture2D>(dependency.EditorFile.FilePath, imageContent.Image));

                return(new DependencyLoadResult(DependencyLoadState.Successful, null));
            }
        }
示例#2
0
        /// <summary>
        /// Function to load a dependency file.
        /// </summary>
        /// <param name="dependency">The dependency to load.</param>
        /// <param name="stream">Stream containing the dependency file.</param>
        /// <returns>
        /// The result of the load operation.  If the dependency loaded correctly, then the developer should return NoError.  If the dependency
        /// is not vital to the content, then the developer can return CanContinue, otherwise the developer should return FatalError and the content will
        /// not continue loading.
        /// </returns>
        protected override DependencyLoadResult OnLoadDependencyFile(Dependency dependency, Stream stream)
        {
            if (ImageEditor == null)
            {
                return(new DependencyLoadResult(DependencyLoadState.ErrorContinue, Resources.GORFNT_ERR_EXTERN_IMAGE_EDITOR_MISSING));
            }

            if ((!string.Equals(dependency.Type, TextureBrushTextureType, StringComparison.OrdinalIgnoreCase)) &&
                (!string.Equals(dependency.Type, GlyphTextureType, StringComparison.OrdinalIgnoreCase)))
            {
                return(new DependencyLoadResult(DependencyLoadState.ErrorContinue,
                                                string.Format(Resources.GORFNT_ERR_DEPENDENCY_UNKNOWN_TYPE, dependency.Type)));
            }

            IImageEditorContent imageContent = null;

            try
            {
                Size newSize = Size.Empty;

                // We need to load the image as transformed (either clipped or stretched).
                if (string.Equals(dependency.Type, GlyphTextureType, StringComparison.OrdinalIgnoreCase))
                {
                    var converter  = new SizeConverter();
                    var sizeObject = converter.ConvertFromInvariantString(dependency.Properties[GlyphTextureSizeProp].Value);

                    if (!(sizeObject is Size))
                    {
                        return(new DependencyLoadResult(DependencyLoadState.ErrorContinue,
                                                        Resources.GORFNT_ERR_DEPENDENCY_GLYPH_TEXTURE_BAD_TRANSFORM));
                    }

                    newSize = (Size)sizeObject;
                }

                imageContent = ImageEditor.ImportContent(dependency.EditorFile, stream);

                if (newSize.Width == 0)
                {
                    newSize.Width = imageContent.Image.Settings.Width;
                }

                if (newSize.Height == 0)
                {
                    newSize.Height = imageContent.Image.Settings.Height;
                }

                // Clip the image to a new size if necessary.
                if ((newSize.Width != imageContent.Image.Settings.Width) ||
                    (newSize.Height != imageContent.Image.Settings.Height))
                {
                    imageContent.Image.Resize(newSize.Width, newSize.Height, true);
                }

                if (imageContent.Image == null)
                {
                    return(new DependencyLoadResult(DependencyLoadState.ErrorContinue, Resources.GORFNT_ERR_EXTERN_IMAGE_MISSING));
                }

                if (imageContent.Image.Settings.ImageType != ImageType.Image2D)
                {
                    return(new DependencyLoadResult(DependencyLoadState.ErrorContinue,
                                                    string.Format(Resources.GORFNT_ERR_IMAGE_NOT_2D, dependency.EditorFile.FilePath)));
                }

                if (!Dependencies.Contains(dependency.EditorFile, dependency.Type))
                {
                    Dependencies[dependency.EditorFile, dependency.Type] = dependency.Clone();
                }

                Dependencies.CacheDependencyObject(dependency.EditorFile,
                                                   dependency.Type,
                                                   Graphics.Textures.CreateTexture <GorgonTexture2D>(dependency.EditorFile.FilePath, imageContent.Image));

                return(new DependencyLoadResult(DependencyLoadState.Successful, null));
            }
            finally
            {
                if (imageContent != null)
                {
                    imageContent.Dispose();
                }
            }
        }