/// <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)); } }
/// <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(); } }
/// <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); } }
/// <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(); } } }
/// <summary> /// Handles the Click event of the buttonOpen 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 buttonOpen_Click(object sender, EventArgs e) { DisableNumericLimits(); numericWidth.ValueChanged -= numericX_ValueChanged; numericHeight.ValueChanged -= numericX_ValueChanged; numericX.ValueChanged -= numericX_ValueChanged; numericY.ValueChanged -= numericX_ValueChanged; try { if (!string.IsNullOrWhiteSpace(GorgonFontEditorPlugIn.Settings.LastTextureImportPath)) { imageFileBrowser.StartDirectory = GorgonFontEditorPlugIn.Settings.LastTextureImportPath; } imageFileBrowser.FileTypes.Clear(); imageFileBrowser.FileTypes.Add(ImageEditor.ContentType); imageFileBrowser.FileView = GorgonFontEditorPlugIn.Settings.LastTextureImportDialogView; if (imageFileBrowser.ShowDialog(ParentForm) == DialogResult.OK) { // Don't load the same image. if ((TextureBrushFile != null) && (string.Equals(TextureBrushFile.FilePath, imageFileBrowser.Files[0].FilePath))) { return; } Cursor.Current = Cursors.WaitCursor; // Load the image. using (Stream stream = imageFileBrowser.OpenFile()) { using (IImageEditorContent imageContent = ImageEditor.ImportContent(imageFileBrowser.Files[0], stream)) { if (imageContent.Image.Settings.ImageType != ImageType.Image2D) { GorgonDialogs.ErrorBox(ParentForm, string.Format(Resources.GORFNT_ERR_IMAGE_NOT_2D, imageContent.Name)); return; } if ((imageContent.Image.Settings.Format != BufferFormat.R8G8B8A8_UIntNormal_sRGB) && (imageContent.Image.Settings.Format != BufferFormat.R8G8B8A8_UIntNormal) && (imageContent.Image.Settings.Format != BufferFormat.B8G8R8A8_UIntNormal) && (imageContent.Image.Settings.Format != BufferFormat.B8G8R8A8_UIntNormal_sRGB)) { GorgonDialogs.ErrorBox(ParentForm, string.Format(Resources.GORFNT_BRUSH_IMAGE_WRONG_FORMAT, imageFileBrowser.Files[0].Filename, imageContent.Image.Settings.Format)); return; } // If we've loaded a texture previously and haven't committed it yet, then get rid of it. if (_texture != null) { _texture.Dispose(); } var settings = (GorgonTexture2DSettings)imageContent.Image.Settings.Clone(); _texture = _graphics.Textures.CreateTexture <GorgonTexture2D>(imageContent.Name, imageContent.Image, settings); // Reset the texture brush settings. numericWidth.Value = settings.Width; numericHeight.Value = settings.Height; numericX.Value = 0; numericY.Value = 0; comboWrapMode.Text = Resources.GORFNT_TEXT_TILE; // Function to retrieve the transformation and node point values from the image. InitializeClipper(_texture, new RectangleF(0, 0, settings.Width, settings.Height)); TextureBrushFile = imageFileBrowser.Files[0]; OnBrushChanged(); } } GorgonFontEditorPlugIn.Settings.LastTextureImportPath = Path.GetDirectoryName(imageFileBrowser.Files[0].FilePath).FormatDirectory('/'); } GorgonFontEditorPlugIn.Settings.LastTextureImportDialogView = imageFileBrowser.FileView; } catch (Exception ex) { GorgonDialogs.ErrorBox(ParentForm, ex); } finally { EnableNumericLimits(); numericWidth.ValueChanged += numericX_ValueChanged; numericHeight.ValueChanged += numericX_ValueChanged; numericX.ValueChanged += numericX_ValueChanged; numericY.ValueChanged += numericX_ValueChanged; Cursor.Current = Cursors.Default; UpdateLabelInfo(); ValidateCommands(); } }