Exemplo n.º 1
0
 /// <summary>Initializes a new instance of the <see cref="ExtractorService"/> class.</summary>
 /// <param name="renderer">The application 2D renderer.</param>
 /// <param name="fileManager">The file manager for the project files.</param>
 /// <param name="defaultCodec">The default sprite codec.</param>
 public ExtractorService(Gorgon2D renderer, IContentFileManager fileManager, IGorgonSpriteCodec defaultCodec)
 {
     _renderer     = renderer;
     _graphics     = renderer.Graphics;
     _fileManager  = fileManager;
     _defaultCodec = defaultCodec;
 }
Exemplo n.º 2
0
        /// <summary>
        /// Function to create a new image codec object.
        /// </summary>
        /// <param name="codec">The name of the codec to look up within the plug in.</param>
        /// <param name="renderer">The renderer used to retrieve sprite textures.</param>
        /// <returns>A new instance of a <see cref="IGorgonSpriteCodec"/>.</returns>
        /// <exception cref="ArgumentNullException">Thrown when the <paramref name="codec"/>, or the <paramref name="renderer"/> parameter is <b>null</b>.</exception>
        /// <exception cref="ArgumentEmptyException">Thrown when the <paramref name="codec"/> parameter is empty.</exception>
        /// <exception cref="KeyNotFoundException">Thrown when the <paramref name="codec"/> was not found in this plug in.</exception>
        /// <remarks>
        /// <para>
        /// If the <paramref name="codec"/> is not found within the plug in, then an exception will be thrown. To determine whether the plug in has the desired <paramref name="codec"/>, check the
        /// <see cref="Codecs"/> property on the plug in to locate the plug in name.
        /// </para>
        /// </remarks>
        public IGorgonSpriteCodec CreateCodec(string codec, Gorgon2D renderer)
        {
            if (codec == null)
            {
                throw new ArgumentNullException(nameof(codec));
            }

            if (string.IsNullOrWhiteSpace(codec))
            {
                throw new ArgumentEmptyException(nameof(codec));
            }

            if (renderer == null)
            {
                throw new ArgumentNullException(nameof(renderer));
            }

            if (!Codecs.Any(item => string.Equals(codec, item.Name, StringComparison.OrdinalIgnoreCase)))
            {
                throw new KeyNotFoundException(string.Format(Resources.GOR2DIO_ERR_CODEC_NOT_IN_PLUGIN, codec));
            }

            IGorgonSpriteCodec result = OnCreateCodec(codec, renderer);

            if (result == null)
            {
                throw new KeyNotFoundException(string.Format(Resources.GOR2DIO_ERR_CODEC_NOT_IN_PLUGIN, codec));
            }

            return(result);
        }
Exemplo n.º 3
0
 /// <summary>Initializes a new instance of the <see cref="T:Gorgon.Editor.SpriteEditor.Services.GorgonSpriteImporter"/> class.</summary>
 /// <param name="sourceFile">The file being imported.</param>
 /// <param name="codec">The original codec for the file.</param>
 /// <param name="renderer">The renderer used to locate the image linked to the sprite.</param>
 /// <param name="fileSystem">The file system containing the file being imported.</param>
 /// <param name="log">The log used for logging debug messages.</param>
 public GorgonSpriteImporter(FileInfo sourceFile, IGorgonSpriteCodec codec, Gorgon2D renderer, IGorgonFileSystem fileSystem, IGorgonLog log)
 {
     _log        = log ?? GorgonLog.NullLog;
     SourceFile  = sourceFile;
     _codec      = codec;
     _renderer   = renderer;
     _fileSystem = fileSystem;
 }
Exemplo n.º 4
0
        /// <summary>Initializes a new instance of the <see cref="ResourceManagement"/> class.</summary>
        /// <param name="renderer">The renderer for the application.</param>
        /// <param name="plugIns>The plugin service used to load file system providers.</param>
        public ResourceManagement(Gorgon2D renderer, IGorgonPlugInService plugIns)
        {
            _renderer = renderer;
            _graphics = renderer.Graphics;
            _plugIns  = plugIns;

            _spriteCodec = new GorgonV3SpriteBinaryCodec(renderer);
        }
Exemplo n.º 5
0
        /// <summary>Function to provide initialization for the plugin.</summary>
        /// <remarks>This method is only called when the plugin is loaded at startup.</remarks>
        protected override void OnInitialize()
        {
            _defaultImageCodec  = new GorgonCodecDds();
            _defaultSpriteCodec = new GorgonV3SpriteBinaryCodec(GraphicsContext.Renderer2D);

            _button = new ToolPlugInRibbonButton(Resources.GORTAG_TEXT_BUTTON, Resources.texture_atlas_48x48, Resources.texture_atlas_16x16, Resources.GORTAG_GROUP_BUTTON)
            {
                Description = Resources.GORTAG_DESC_BUTTON
            };
            _button.ValidateButton();
        }
 /// <summary>Initializes a new instance of the <see cref="T:Gorgon.Editor.SpriteEditor.SpriteContentParameters"/> class.</summary>
 /// <param name="factory">The factory for building sprite content data.</param>
 /// <param name="spriteFile">The sprite file.</param>
 /// <param name="spriteTextureFile">The sprite texture file.</param>
 /// <param name="fileManager">The file manager used to access external content.</param>
 /// <param name="textureService">The texture service to use when reading sprite texture data.</param>
 /// <param name="sprite">The sprite being edited.</param>
 /// <param name="codec">The codec to use when reading/writing sprite data.</param>
 /// <param name="manualRectEdit">The manual rectangle editor view model.</param>
 /// <param name="manualVertexEdit">The manual vertex editor view model.</param>
 /// <param name="spritePickMaskEditor">The sprite picker mask color editor.</param>
 /// <param name="colorEditor">The color editor for the sprite.</param>
 /// <param name="anchorEditor">The anchor editor for the sprite.</param>
 /// <param name="wrapEditor">The texture wrapping state editor for the sprite.</param>
 /// <param name="settings">The plug in settings view model.</param>
 /// <param name="undoService">The undo service.</param>
 /// <param name="scratchArea">The file system used to write out temporary working data.</param>
 /// <param name="commonServices">Common application services.</param>
 public SpriteContentParameters(
     ISpriteContentFactory factory,
     IContentFile spriteFile,
     IContentFile spriteTextureFile,
     IContentFileManager fileManager,
     ISpriteTextureService textureService,
     GorgonSprite sprite,
     IGorgonSpriteCodec codec,
     IManualRectangleEditor manualRectEdit,
     IManualVertexEditor manualVertexEdit,
     ISpritePickMaskEditor spritePickMaskEditor,
     ISpriteColorEdit colorEditor,
     ISpriteAnchorEdit anchorEditor,
     ISpriteWrappingEditor wrapEditor,
     ISamplerBuildService samplerBuilder,
     IEditorPlugInSettings settings,
     IUndoService undoService,
     IGorgonFileSystemWriter <Stream> scratchArea,
     IViewModelInjection commonServices)
     : base(spriteFile, commonServices)
 {
     Factory               = factory ?? throw new ArgumentNullException(nameof(factory));
     Sprite                = sprite ?? throw new ArgumentNullException(nameof(sprite));
     UndoService           = undoService ?? throw new ArgumentNullException(nameof(undoService));
     ContentFileManager    = fileManager ?? throw new ArgumentNullException(nameof(fileManager));
     ScratchArea           = scratchArea ?? throw new ArgumentNullException(nameof(scratchArea));
     TextureService        = textureService ?? throw new ArgumentNullException(nameof(textureService));
     SpriteCodec           = codec ?? throw new ArgumentNullException(nameof(codec));
     ManualRectangleEditor = manualRectEdit ?? throw new ArgumentNullException(nameof(manualRectEdit));
     ManualVertexEditor    = manualVertexEdit ?? throw new ArgumentNullException(nameof(manualVertexEdit));
     SpritePickMaskEditor  = spritePickMaskEditor ?? throw new ArgumentNullException(nameof(spritePickMaskEditor));
     Settings              = settings ?? throw new ArgumentNullException(nameof(settings));
     ColorEditor           = colorEditor ?? throw new ArgumentNullException(nameof(colorEditor));
     AnchorEditor          = anchorEditor ?? throw new ArgumentNullException(nameof(anchorEditor));
     SpriteWrappingEditor  = wrapEditor ?? throw new ArgumentNullException(nameof(wrapEditor));
     SamplerBuilder        = samplerBuilder ?? throw new ArgumentNullException(nameof(samplerBuilder));
     SpriteTextureFile     = spriteTextureFile;
 }
Exemplo n.º 7
0
        /// <summary>Function to inject dependencies for the view model.</summary>
        /// <param name="injectionParameters">The parameters to inject.</param>
        /// <remarks>
        /// Applications should call this when setting up the view model for complex operations and/or dependency injection. The constructor should only be used for simple set up and initialization of objects.
        /// </remarks>
        protected override void OnInitialize(ImportPlugInSettingsParameters injectionParameters)
        {
            _messageDisplay  = injectionParameters.MessageDisplay ?? throw new ArgumentMissingException(nameof(injectionParameters.MessageDisplay), nameof(injectionParameters));
            _settings        = injectionParameters.Settings ?? throw new ArgumentMissingException(nameof(injectionParameters.Settings), nameof(injectionParameters));
            _plugInService   = injectionParameters.ContentPlugInService ?? throw new ArgumentMissingException(nameof(injectionParameters.ContentPlugInService), nameof(injectionParameters));
            _codecs          = injectionParameters.Codecs ?? throw new ArgumentMissingException(nameof(injectionParameters.Codecs), nameof(injectionParameters));
            _openCodecDialog = injectionParameters.OpenCodecDialog ?? throw new ArgumentMissingException(nameof(injectionParameters.OpenCodecDialog), nameof(injectionParameters));
            _busyService     = injectionParameters.BusyService ?? throw new ArgumentMissingException(nameof(injectionParameters.BusyService), nameof(injectionParameters));

            foreach (GorgonSpriteCodecPlugIn plugin in _codecs.CodecPlugIns)
            {
                foreach (GorgonSpriteCodecDescription desc in plugin.Codecs)
                {
                    IGorgonSpriteCodec codec = _codecs.Codecs.FirstOrDefault(item => string.Equals(item.GetType().FullName, desc.Name, StringComparison.OrdinalIgnoreCase));

                    if (codec == null)
                    {
                        continue;
                    }

                    CodecPlugInPaths.Add(new CodecSetting(codec.CodecDescription, plugin, desc));
                }
            }
        }
        /// <summary>
        /// Function to load a <see cref="GorgonSprite"/> from a <see cref="GorgonFileSystem"/>.
        /// </summary>
        /// <param name="fileSystem">The file system to load the sprite from.</param>
        /// <param name="renderer">The renderer for the sprite.</param>
        /// <param name="path">The path to the sprite file in the file system.</param>
        /// <param name="textureOptions">[Optional] Options for the texture loaded associated the sprite.</param>
        /// <param name="spriteCodecs">[Optional] The list of sprite codecs to try and load the sprite with.</param>
        /// <param name="imageCodecs">[Optional] The list of image codecs to try and load the sprite texture with.</param>
        /// <returns>The sprite data in the file as a <see cref="GorgonSprite"/>.</returns>
        /// <exception cref="ArgumentNullException">Thrown when the <paramref name="fileSystem"/>, <paramref name="renderer"/>, or <paramref name="path"/> parameter is <b>null</b>.</exception>
        /// <exception cref="ArgumentEmptyException">Thrown when the <paramref name="path"/> parameter is empty.</exception>
        /// <exception cref="FileNotFoundException">Thrown if the file in the <paramref name="path"/> was not found.</exception>
        /// <exception cref="GorgonException">Thrown if the sprite data in the file system could not be loaded because a suitable codec was not found.</exception>
        /// <remarks>
        /// <para>
        /// This method extends a <see cref="GorgonFileSystem"/> so that sprites can be loaded by calling a method on the file system object itself. This negates the need for users to create complex code
        /// for loading a sprite.
        /// </para>
        /// <para>
        /// When loading a sprite, the method will attempt to locate the <see cref="GorgonTexture2DView"/> associated with the sprite (if it exists). When loading, it will check:
        /// <list type="number">
        ///     <item>
        ///         <description>For a texture resource with the same name that is already loaded into memory.</description>
        ///     </item>
        ///     <item>
        ///         <description>Use the local <see cref="IGorgonVirtualDirectory"/> for the sprite file and search for the texture in that directory.</description>
        ///     </item>
        ///     <item>
        ///         <description>Check the entire <paramref name="fileSystem"/> for a file if the texture name contains path information (this is done by the GorgonEditor from v2).</description>
        ///     </item>
        /// </list>
        /// If the file is found, and can be loaded by one of the <paramref name="imageCodecs"/>, then it is loaded and assigned to the sprite.
        /// </para>
        /// <para>
        /// The <paramref name="spriteCodecs"/> is a list of codecs for loading sprite data. If the user specifies this parameter, the only the codecs provided will be used for determining if a sprite can
        /// be read. If it is not supplied, then all built-in (i.e. not plug in based) sprite codecs will be used.
        /// </para>
        /// <para>
        /// The <paramref name="imageCodecs"/> is a list of codecs for loading image data. If the user specifies this parameter, the only the codecs provided will be used for determining if an image can be
        /// read. If it is not supplied, then all built-in (i.e. not plug in based) image codecs will be used.
        /// </para>
        /// </remarks>
        /// <seealso cref="GorgonFileSystem"/>
        /// <seealso cref="GorgonTexture2DView"/>
        /// <seealso cref="GorgonSprite"/>
        public static GorgonSprite LoadSpriteFromFileSystem(this IGorgonFileSystem fileSystem,
                                                            Gorgon2D renderer,
                                                            string path,
                                                            GorgonTexture2DLoadOptions textureOptions     = null,
                                                            IEnumerable <IGorgonSpriteCodec> spriteCodecs = null,
                                                            IEnumerable <IGorgonImageCodec> imageCodecs   = null)
        {
            if (fileSystem == null)
            {
                throw new ArgumentNullException(nameof(fileSystem));
            }

            IGorgonVirtualFile file = fileSystem.GetFile(path);

            if (file == null)
            {
                throw new FileNotFoundException(string.Format(Resources.GOR2DIO_ERR_FILE_NOT_FOUND, path));
            }

            if ((imageCodecs == null) || (!imageCodecs.Any()))
            {
                // If we don't specify any codecs, then use the built in ones.
                imageCodecs = new IGorgonImageCodec[]
                {
                    new GorgonCodecPng(),
                    new GorgonCodecBmp(),
                    new GorgonCodecDds(),
                    new GorgonCodecGif(),
                    new GorgonCodecJpeg(),
                    new GorgonCodecTga(),
                };
            }
            else
            {
                // Only use codecs that can decode image data.
                imageCodecs = imageCodecs.Where(item => item.CanDecode);
            }

            if ((spriteCodecs == null) || (!spriteCodecs.Any()))
            {
                // Use all built-in codecs if we haven't asked for any.
                spriteCodecs = new IGorgonSpriteCodec[]
                {
                    new GorgonV3SpriteBinaryCodec(renderer),
                    new GorgonV3SpriteJsonCodec(renderer),
                    new GorgonV2SpriteCodec(renderer),
                    new GorgonV1SpriteBinaryCodec(renderer),
                };
            }
            else
            {
                // Only use codecs that can decode sprite data.
                spriteCodecs = spriteCodecs.Where(item => item.CanDecode);
            }

            // We need to copy the sprite data into a memory stream since the underlying stream may not be seekable.
            Stream spriteStream = file.OpenStream();

            try
            {
                if (!spriteStream.CanSeek)
                {
                    Stream newStream = new DataStream((int)spriteStream.Length, true, true);
                    spriteStream.CopyTo(newStream);
                    spriteStream.Dispose();
                    newStream.Position = 0;
                    spriteStream       = newStream;
                }

                IGorgonSpriteCodec spriteCodec = GetSpriteCodec(spriteStream, spriteCodecs);

                if (spriteCodec == null)
                {
                    throw new GorgonException(GorgonResult.CannotRead, string.Format(Resources.GOR2DIO_ERR_NO_SUITABLE_SPRITE_CODEC_FOUND, path));
                }

                // Try to locate the texture.
                string textureName = spriteCodec.GetAssociatedTextureName(spriteStream);

                GorgonTexture2DView textureForSprite = null;

                // Let's try and load the texture into memory.
                // This does this by:
                // 1. Checking to see if a texture resource with the name specified is already available in memory.
                // 2. Checking the local directory of the file to see if the texture is there.
                // 3. A file system wide search.

                // ReSharper disable once InvertIf
                if (!string.IsNullOrWhiteSpace(textureName))
                {
                    (IGorgonImageCodec codec, IGorgonVirtualFile textureFile, bool loaded) =
                        LocateTextureCodecAndFile(fileSystem, file.Directory, renderer, textureName, imageCodecs);

                    // We have not loaded the texture yet.  Do so now.
                    // ReSharper disable once InvertIf
                    if ((!loaded) && (textureFile != null) && (codec != null))
                    {
                        using (Stream textureStream = textureFile.OpenStream())
                        {
                            textureForSprite = GorgonTexture2DView.FromStream(renderer.Graphics,
                                                                              textureStream,
                                                                              codec,
                                                                              textureFile.Size,
                                                                              GetTextureOptions(textureFile.FullPath, textureOptions));
                        }
                    }
                }

                return(spriteCodec.FromStream(spriteStream, textureForSprite, (int)file.Size));
            }
            finally
            {
                spriteStream?.Dispose();
            }
        }
Exemplo n.º 9
0
 /// <summary>Initializes a new instance of the <see cref="FileIOService"/> class.</summary>
 /// <param name="fileSystem">The file system.</param>
 /// <param name="imageCodec">The default image codec to use.</param>
 /// <param name="spriteCodec">The default sprite codec to use.</param>
 public FileIOService(IContentFileManager fileSystem, IGorgonImageCodec imageCodec, IGorgonSpriteCodec spriteCodec)
 {
     _fileSystem         = fileSystem;
     _defaultImageCodec  = imageCodec;
     _defaultSpriteCodec = spriteCodec;
 }
Exemplo n.º 10
0
        /// <summary>
        /// Function to load in a plug in assembly.
        /// </summary>
        private void DoLoadPlugInAssembly()
        {
            try
            {
                string lastCodecPath = _settings.LastCodecPlugInPath.FormatDirectory(Path.DirectorySeparatorChar);

                if ((string.IsNullOrWhiteSpace(lastCodecPath)) || (!Directory.Exists(lastCodecPath)))
                {
                    lastCodecPath = AppDomain.CurrentDomain.BaseDirectory;
                }

                _openCodecDialog.DialogTitle      = Resources.GORSPR_CAPTION_SELECT_CODEC_DLL;
                _openCodecDialog.FileFilter       = Resources.GORSPR_FILTER_SELECT_CODEC;
                _openCodecDialog.InitialDirectory = new DirectoryInfo(lastCodecPath);

                string path = _openCodecDialog.GetFilename();

                if ((string.IsNullOrWhiteSpace(path)) || (!File.Exists(path)))
                {
                    return;
                }

                _busyService.SetBusy();
                IReadOnlyList <GorgonSpriteCodecPlugIn> codecs = _codecs.AddCodecPlugIn(path, out IReadOnlyList <string> errors);

                if (errors.Count > 0)
                {
                    _messageDisplay.ShowError(Resources.GORSPR_ERR_CODEC_LOAD_ERRORS_PRESENT, details: string.Join("\n\n", errors.Select((item, index) => $"Error #{index + 1}\n--------------\n{item}")));

                    if (codecs.Count == 0)
                    {
                        return;
                    }
                }

                foreach (GorgonSpriteCodecPlugIn plugin in codecs)
                {
                    foreach (GorgonSpriteCodecDescription desc in plugin.Codecs)
                    {
                        IGorgonSpriteCodec codec = _codecs.Codecs.FirstOrDefault(item => string.Equals(item.GetType().FullName, desc.Name, StringComparison.OrdinalIgnoreCase));

                        if (codec == null)
                        {
                            continue;
                        }

                        CodecPlugInPaths.Add(new CodecSetting(codec.CodecDescription, plugin, desc));
                    }
                }

                _settings.LastCodecPlugInPath = Path.GetDirectoryName(path).FormatDirectory(Path.DirectorySeparatorChar);

                // Store the settings now.
                DoWriteSettings();
            }
            catch (Exception ex)
            {
                _messageDisplay.ShowError(ex, Resources.GORSPR_ERR_CANNOT_LOAD_CODEC);
            }
            finally
            {
                _busyService.SetIdle();
            }
        }