Exemplo n.º 1
0
        /// <summary>
        /// Handles the Click event of the buttonSelectTexture control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param>
        private void buttonSelectTexture_Click(object sender, EventArgs e)
        {
            EditorFileDialog editorDialog = null;

            try
            {
                editorDialog = new EditorFileDialog
                {
                    Text           = Resources.GORSPR_DLG_SELECT_TEXTURE,
                    StartDirectory = GorgonSpriteEditorPlugIn.Settings.LastTexturePath,
                    FileView       = FileViews.Large
                };

                editorDialog.FileTypes.Add(ImageEditor.ContentType);

                if (editorDialog.ShowDialog(this) == DialogResult.Cancel)
                {
                    return;
                }

                if (Texture != null)
                {
                    Texture.Dispose();
                }

                // Read the new texture.
                using (Stream file = editorDialog.OpenFile())
                {
                    using (IImageEditorContent image = ImageEditor.ImportContent(editorDialog.Files[0], file))
                    {
                        if (image.Image.Settings.ImageType != ImageType.Image2D)
                        {
                            throw new GorgonException(GorgonResult.CannotRead, Resources.GORSPR_ERR_2D_TEXTURE_ONLY);
                        }

                        Texture = ContentObject.Graphics.Textures.CreateTexture <GorgonTexture2D>(editorDialog.Filename, image.Image);

                        // Create the new dependency to pass back to the initialization.
                        Dependency = new Dependency(editorDialog.Files[0], GorgonSpriteContent.TextureDependencyType);
                    }

                    labelTexturePath.Text = editorDialog.Filename;
                }
            }
            catch (Exception ex)
            {
                GorgonDialogs.ErrorBox(this, ex);
            }
            finally
            {
                if (editorDialog != null)
                {
                    editorDialog.Dispose();
                }

                Cursor = Cursors.Default;
                ValidateControls();
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// Edits the specified object's value using the editor style indicated by the <see cref="M:System.Drawing.Design.UITypeEditor.GetEditStyle" /> method.
        /// </summary>
        /// <param name="context">An <see cref="T:System.ComponentModel.ITypeDescriptorContext" /> that can be used to gain additional context information.</param>
        /// <param name="provider">An <see cref="T:System.IServiceProvider" /> that this editor can use to obtain services.</param>
        /// <param name="value">The object to edit.</param>
        /// <returns>
        /// The new value of the object. If the value of the object has not changed, this should return the same object it was passed.
        /// </returns>
        public override object EditValue(System.ComponentModel.ITypeDescriptorContext context, IServiceProvider provider, object value)
        {
            var             sprite         = (GorgonSpriteContent)((ContentTypeDescriptor)context.Instance).Content;
            GorgonTexture2D currentTexture = sprite.Texture;
            string          textureName    = currentTexture != null ? currentTexture.Name : string.Empty;
            Dependency      dependency     = sprite.Dependencies.SingleOrDefault(item => string.Equals(item.Type, GorgonSpriteContent.TextureDependencyType));

            using (var editorDialog = new EditorFileDialog
            {
                Text = Resources.GORSPR_DLG_SELECT_TEXTURE,
                Filename = textureName,
                StartDirectory = GorgonSpriteEditorPlugIn.Settings.LastTexturePath,
                FileView = FileViews.Large
            })
            {
                editorDialog.FileTypes.Add(sprite.ImageEditor.ContentType);

                if ((editorDialog.ShowDialog() == DialogResult.Cancel) ||
                    (string.Equals(editorDialog.Filename, textureName, StringComparison.OrdinalIgnoreCase)))
                {
                    // Resharper is just plain wrong here.  EditValue can most certainly return NULL.
                    // ReSharper disable once AssignNullToNotNullAttribute
                    return(currentTexture);
                }

                GorgonTexture2D texture;

                // Read the new texture.
                using (Stream file = editorDialog.OpenFile())
                {
                    using (IImageEditorContent image = sprite.ImageEditor.ImportContent(editorDialog.Files[0], file))
                    {
                        if (image.Image.Settings.ImageType != ImageType.Image2D)
                        {
                            throw new GorgonException(GorgonResult.CannotRead, Resources.GORSPR_ERR_2D_TEXTURE_ONLY);
                        }

                        texture = ContentObject.Graphics.Textures.CreateTexture <GorgonTexture2D>(editorDialog.Filename, image.Image);
                    }
                }

                // Update the dependency list for this sprite.
                if (dependency != null)
                {
                    if (sprite.Dependencies.Contains(dependency.EditorFile, dependency.Type))
                    {
                        sprite.Dependencies.Remove(dependency.EditorFile, dependency.Type);
                    }
                }
                sprite.Dependencies[editorDialog.Files[0], GorgonSpriteContent.TextureDependencyType] = new Dependency(editorDialog.Files[0], GorgonSpriteContent.TextureDependencyType);
                sprite.Dependencies.CacheDependencyObject(editorDialog.Files[0], GorgonSpriteContent.TextureDependencyType, texture);

                GorgonSpriteEditorPlugIn.Settings.LastTexturePath = Path.GetDirectoryName(editorDialog.Filename).FormatDirectory('/');

                return(texture);
            }
        }