From 85bab6a98f4b8ad2c65c52d9dbaa6a7fb8411c95 Mon Sep 17 00:00:00 2001 From: OtherworldBob Date: Mon, 1 Jan 2018 10:49:30 +0000 Subject: [PATCH] Add new function to export all layers of a scene to their own image file. --- ManiacEditor/Editor.cs | 101 ++++++++++++++++----- ManiacEditor/Editor.designer.cs | 112 ++++++++++++++---------- ManiacEditor/Properties/AssemblyInfo.cs | 6 +- README.md | 1 + 4 files changed, 150 insertions(+), 70 deletions(-) diff --git a/ManiacEditor/Editor.cs b/ManiacEditor/Editor.cs index 49a6d5a8..a146cba6 100644 --- a/ManiacEditor/Editor.cs +++ b/ManiacEditor/Editor.cs @@ -1,20 +1,16 @@ using System; using System.Collections.Generic; -using System.ComponentModel; using System.Data; using System.Drawing; +using System.Drawing.Imaging; +using System.IO; using System.Linq; -using System.Text; using System.Windows.Forms; -using System.Drawing.Drawing2D; using ManiacEditor.Actions; -using System.Collections; -using System.Net; -using SharpDX.Direct3D9; +using ManiacEditor.Enums; using RSDKv5; -using System.IO; +using SharpDX.Direct3D9; using Color = System.Drawing.Color; -using ManiacEditor.Enums; namespace ManiacEditor { @@ -128,7 +124,7 @@ private void SetSceneOnlyButtonsState(bool enabled) { saveToolStripMenuItem.Enabled = enabled; saveAsToolStripMenuItem.Enabled = enabled; - saveAspngToolStripMenuItem.Enabled = enabled; + exportToolStripMenuItem.Enabled = enabled; ShowFGHigh.Enabled = enabled && FGHigh != null; ShowFGLow.Enabled = enabled && FGLow != null; @@ -1504,26 +1500,89 @@ private void exitToolStripMenuItem_Click(object sender, EventArgs e) } private void saveAspngToolStripMenuItem_Click(object sender, EventArgs e) { - if (EditorScene != null) + if (EditorScene == null) return; + + SaveFileDialog save = new SaveFileDialog(); + save.Filter = ".png File|*.png"; + save.DefaultExt = "png"; + if (save.ShowDialog() != DialogResult.Cancel) { - SaveFileDialog save = new SaveFileDialog(); - save.Filter = ".png File|*.png"; - save.DefaultExt = "png"; - if (save.ShowDialog() != DialogResult.Cancel) + using (Bitmap bitmap = new Bitmap(SceneWidth, SceneHeight)) + using (Graphics g = Graphics.FromImage(bitmap)) + { + // not all scenes have both a Low and a High foreground + // only attempt to render the ones we actually have + FGLow?.Draw(g); + FGHigh?.Draw(g); + bitmap.Save(save.FileName); + } + } + } + + private void exportEachLayerAspngToolStripMenuItem_Click(object sender, EventArgs e) + { + try + { + if (EditorScene?.Layers == null || !EditorScene.Layers.Any()) return; + + var dialog = new FolderSelectDialog() + { + Title = "Select folder to save each exported layer image to" + }; + + if (!dialog.ShowDialog()) return; + + int fileCount = 0; + + foreach (var editorLayer in EditorScene.AllLayers) { - using (Bitmap bitmap = new Bitmap(SceneWidth, SceneHeight)) + string fileName = Path.Combine(dialog.FileName, editorLayer.Name + ".png"); + + if (!CanWriteFile(fileName)) { - using (Graphics g = Graphics.FromImage(bitmap)) - { - FGLow.Draw(g); - FGHigh.Draw(g); - } - bitmap.Save(save.FileName); + MessageBox.Show($"Layer export aborted. {fileCount} images saved.", "Error!", + MessageBoxButtons.OK, MessageBoxIcon.Error); + return; + } + + using (var bitmap = new Bitmap(editorLayer.Width * EditorLayer.TILE_SIZE, editorLayer.Height * EditorLayer.TILE_SIZE)) + using (var g = Graphics.FromImage(bitmap)) + { + editorLayer.Draw(g); + bitmap.Save(fileName, ImageFormat.Png); + ++fileCount; } } + + MessageBox.Show($"Layer export succeeded. {fileCount} images saved.", "Success!", + MessageBoxButtons.OK, MessageBoxIcon.Information); + } + catch (Exception ex) + { + MessageBox.Show("An error occurred: " + ex.Message, "Error!", + MessageBoxButtons.OK, MessageBoxIcon.Error); } } + private bool CanWriteFile(string fullFilePath) + { + if (!File.Exists(fullFilePath)) return true; + + if (File.GetAttributes(fullFilePath).HasFlag(FileAttributes.ReadOnly)) + { + MessageBox.Show($"The file {fullFilePath} is Read Only.", "File is Read Only.", + MessageBoxButtons.OK, MessageBoxIcon.Error); + return false; + } + + var result = MessageBox.Show($"The file {fullFilePath} already exists. Overwrite?", "Overwrite?", + MessageBoxButtons.YesNo, MessageBoxIcon.Warning); + + if (result == DialogResult.Yes) return true; + + return false; + } + private void splitContainer1_SplitterMoved(object sender, SplitterEventArgs e) { Form1_Resize(null, null); diff --git a/ManiacEditor/Editor.designer.cs b/ManiacEditor/Editor.designer.cs index dd3d041d..5678cb04 100644 --- a/ManiacEditor/Editor.designer.cs +++ b/ManiacEditor/Editor.designer.cs @@ -35,7 +35,6 @@ private void InitializeComponent() this.openToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); this.saveToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); this.saveAsToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); - this.saveAspngToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); this.exitToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); this.editToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); this.undoToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); @@ -53,11 +52,14 @@ private void InitializeComponent() this.importObjectsToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); this.importSoundsToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); this.layerManagerToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); + this.helpToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); + this.aboutToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); this.splitContainer1 = new System.Windows.Forms.SplitContainer(); this.viewPanel = new System.Windows.Forms.Panel(); this.panel3 = new System.Windows.Forms.Panel(); this.hScrollBar1 = new System.Windows.Forms.HScrollBar(); this.vScrollBar1 = new System.Windows.Forms.VScrollBar(); + this.GraphicPanel = new ManiacEditor.DevicePanel(); this.mainPanel = new System.Windows.Forms.Panel(); this.statusStrip1 = new System.Windows.Forms.StatusStrip(); this.toolStripStatusLabel1 = new System.Windows.Forms.ToolStripStatusLabel(); @@ -87,9 +89,9 @@ private void InitializeComponent() this.pointerButton = new System.Windows.Forms.ToolStripButton(); this.selectTool = new System.Windows.Forms.ToolStripButton(); this.placeTilesButton = new System.Windows.Forms.ToolStripButton(); - this.helpToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); - this.aboutToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); - this.GraphicPanel = new ManiacEditor.DevicePanel(); + this.exportToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); + this.exportEachLayerAspngToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); + this.saveAspngToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); this.menuStrip1.SuspendLayout(); ((System.ComponentModel.ISupportInitialize)(this.splitContainer1)).BeginInit(); this.splitContainer1.Panel1.SuspendLayout(); @@ -121,7 +123,7 @@ private void InitializeComponent() this.openToolStripMenuItem, this.saveToolStripMenuItem, this.saveAsToolStripMenuItem, - this.saveAspngToolStripMenuItem, + this.exportToolStripMenuItem, this.exitToolStripMenuItem}); this.fileToolStripMenuItem.Name = "fileToolStripMenuItem"; this.fileToolStripMenuItem.Size = new System.Drawing.Size(37, 20); @@ -162,13 +164,6 @@ private void InitializeComponent() this.saveAsToolStripMenuItem.Text = "Save As..."; this.saveAsToolStripMenuItem.Click += new System.EventHandler(this.saveAsToolStripMenuItem_Click); // - // saveAspngToolStripMenuItem - // - this.saveAspngToolStripMenuItem.Name = "saveAspngToolStripMenuItem"; - this.saveAspngToolStripMenuItem.Size = new System.Drawing.Size(186, 22); - this.saveAspngToolStripMenuItem.Text = "Export as .png.."; - this.saveAspngToolStripMenuItem.Click += new System.EventHandler(this.saveAspngToolStripMenuItem_Click); - // // exitToolStripMenuItem // this.exitToolStripMenuItem.Name = "exitToolStripMenuItem"; @@ -308,6 +303,21 @@ private void InitializeComponent() this.layerManagerToolStripMenuItem.Text = "Layer Manager"; this.layerManagerToolStripMenuItem.Click += new System.EventHandler(this.layerManagerToolStripMenuItem_Click); // + // helpToolStripMenuItem + // + this.helpToolStripMenuItem.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] { + this.aboutToolStripMenuItem}); + this.helpToolStripMenuItem.Name = "helpToolStripMenuItem"; + this.helpToolStripMenuItem.Size = new System.Drawing.Size(44, 20); + this.helpToolStripMenuItem.Text = "Help"; + // + // aboutToolStripMenuItem + // + this.aboutToolStripMenuItem.Name = "aboutToolStripMenuItem"; + this.aboutToolStripMenuItem.Size = new System.Drawing.Size(107, 22); + this.aboutToolStripMenuItem.Text = "About"; + this.aboutToolStripMenuItem.Click += new System.EventHandler(this.aboutToolStripMenuItem_Click); + // // splitContainer1 // this.splitContainer1.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) @@ -381,6 +391,30 @@ private void InitializeComponent() this.vScrollBar1.Scroll += new System.Windows.Forms.ScrollEventHandler(this.vScrollBar1_Scroll); this.vScrollBar1.ValueChanged += new System.EventHandler(this.vScrollBar1_ValueChanged); // + // GraphicPanel + // + this.GraphicPanel.AllowDrop = true; + this.GraphicPanel.DeviceBackColor = System.Drawing.Color.White; + this.GraphicPanel.Location = new System.Drawing.Point(0, 0); + this.GraphicPanel.Margin = new System.Windows.Forms.Padding(0); + this.GraphicPanel.Name = "GraphicPanel"; + this.GraphicPanel.Size = new System.Drawing.Size(882, 482); + this.GraphicPanel.TabIndex = 0; + this.GraphicPanel.OnRender += new ManiacEditor.RenderEventHandler(this.GraphicPanel_OnRender); + this.GraphicPanel.OnCreateDevice += new ManiacEditor.CreateDeviceEventHandler(this.OnResetDevice); + this.GraphicPanel.DragDrop += new System.Windows.Forms.DragEventHandler(this.GraphicPanel_DragDrop); + this.GraphicPanel.DragEnter += new System.Windows.Forms.DragEventHandler(this.GraphicPanel_DragEnter); + this.GraphicPanel.DragOver += new System.Windows.Forms.DragEventHandler(this.GraphicPanel_DragOver); + this.GraphicPanel.DragLeave += new System.EventHandler(this.GraphicPanel_DragLeave); + this.GraphicPanel.KeyDown += new System.Windows.Forms.KeyEventHandler(this.GraphicPanel_OnKeyDown); + this.GraphicPanel.KeyUp += new System.Windows.Forms.KeyEventHandler(this.GraphicPanel_OnKeyUp); + this.GraphicPanel.MouseDoubleClick += new System.Windows.Forms.MouseEventHandler(this.GraphicPanel_OnMouseDoubleClick); + this.GraphicPanel.MouseDown += new System.Windows.Forms.MouseEventHandler(this.GraphicPanel_OnMouseDown); + this.GraphicPanel.MouseEnter += new System.EventHandler(this.GraphicPanel_MouseEnter); + this.GraphicPanel.MouseMove += new System.Windows.Forms.MouseEventHandler(this.GraphicPanel_OnMouseMove); + this.GraphicPanel.MouseUp += new System.Windows.Forms.MouseEventHandler(this.GraphicPanel_OnMouseUp); + this.GraphicPanel.MouseWheel += new System.Windows.Forms.MouseEventHandler(this.GraphicPanel_MouseWheel); + // // mainPanel // this.mainPanel.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) @@ -689,44 +723,28 @@ private void InitializeComponent() this.placeTilesButton.Text = "Place tiles (Right click [+drag] - place, Left click [+drag] - delete)"; this.placeTilesButton.Click += new System.EventHandler(this.placeTilesButton_Click); // - // helpToolStripMenuItem + // exportToolStripMenuItem // - this.helpToolStripMenuItem.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] { - this.aboutToolStripMenuItem}); - this.helpToolStripMenuItem.Name = "helpToolStripMenuItem"; - this.helpToolStripMenuItem.Size = new System.Drawing.Size(44, 20); - this.helpToolStripMenuItem.Text = "Help"; + this.exportToolStripMenuItem.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] { + this.saveAspngToolStripMenuItem, + this.exportEachLayerAspngToolStripMenuItem}); + this.exportToolStripMenuItem.Name = "exportToolStripMenuItem"; + this.exportToolStripMenuItem.Size = new System.Drawing.Size(186, 22); + this.exportToolStripMenuItem.Text = "Export"; // - // aboutToolStripMenuItem + // exportEachLayerAspngToolStripMenuItem // - this.aboutToolStripMenuItem.Name = "aboutToolStripMenuItem"; - this.aboutToolStripMenuItem.Size = new System.Drawing.Size(152, 22); - this.aboutToolStripMenuItem.Text = "About"; - this.aboutToolStripMenuItem.Click += new System.EventHandler(this.aboutToolStripMenuItem_Click); + this.exportEachLayerAspngToolStripMenuItem.Name = "exportEachLayerAspngToolStripMenuItem"; + this.exportEachLayerAspngToolStripMenuItem.Size = new System.Drawing.Size(207, 22); + this.exportEachLayerAspngToolStripMenuItem.Text = "Export Each Layer as .png"; + this.exportEachLayerAspngToolStripMenuItem.Click += new System.EventHandler(this.exportEachLayerAspngToolStripMenuItem_Click); // - // GraphicPanel + // saveAspngToolStripMenuItem // - this.GraphicPanel.AllowDrop = true; - this.GraphicPanel.DeviceBackColor = System.Drawing.Color.White; - this.GraphicPanel.Location = new System.Drawing.Point(0, 0); - this.GraphicPanel.Margin = new System.Windows.Forms.Padding(0); - this.GraphicPanel.Name = "GraphicPanel"; - this.GraphicPanel.Size = new System.Drawing.Size(882, 482); - this.GraphicPanel.TabIndex = 0; - this.GraphicPanel.OnRender += new ManiacEditor.RenderEventHandler(this.GraphicPanel_OnRender); - this.GraphicPanel.OnCreateDevice += new ManiacEditor.CreateDeviceEventHandler(this.OnResetDevice); - this.GraphicPanel.DragDrop += new System.Windows.Forms.DragEventHandler(this.GraphicPanel_DragDrop); - this.GraphicPanel.DragEnter += new System.Windows.Forms.DragEventHandler(this.GraphicPanel_DragEnter); - this.GraphicPanel.DragOver += new System.Windows.Forms.DragEventHandler(this.GraphicPanel_DragOver); - this.GraphicPanel.DragLeave += new System.EventHandler(this.GraphicPanel_DragLeave); - this.GraphicPanel.KeyDown += new System.Windows.Forms.KeyEventHandler(this.GraphicPanel_OnKeyDown); - this.GraphicPanel.KeyUp += new System.Windows.Forms.KeyEventHandler(this.GraphicPanel_OnKeyUp); - this.GraphicPanel.MouseDoubleClick += new System.Windows.Forms.MouseEventHandler(this.GraphicPanel_OnMouseDoubleClick); - this.GraphicPanel.MouseDown += new System.Windows.Forms.MouseEventHandler(this.GraphicPanel_OnMouseDown); - this.GraphicPanel.MouseEnter += new System.EventHandler(this.GraphicPanel_MouseEnter); - this.GraphicPanel.MouseMove += new System.Windows.Forms.MouseEventHandler(this.GraphicPanel_OnMouseMove); - this.GraphicPanel.MouseUp += new System.Windows.Forms.MouseEventHandler(this.GraphicPanel_OnMouseUp); - this.GraphicPanel.MouseWheel += new System.Windows.Forms.MouseEventHandler(this.GraphicPanel_MouseWheel); + this.saveAspngToolStripMenuItem.Name = "saveAspngToolStripMenuItem"; + this.saveAspngToolStripMenuItem.Size = new System.Drawing.Size(207, 22); + this.saveAspngToolStripMenuItem.Text = "Export as .png.."; + this.saveAspngToolStripMenuItem.Click += new System.EventHandler(this.saveAspngToolStripMenuItem_Click); // // Editor // @@ -777,7 +795,6 @@ private void InitializeComponent() private DevicePanel GraphicPanel; private System.Windows.Forms.ToolStripMenuItem saveToolStripMenuItem; private System.Windows.Forms.ToolStripMenuItem newToolStripMenuItem; - private System.Windows.Forms.ToolStripMenuItem saveAspngToolStripMenuItem; private System.Windows.Forms.VScrollBar vScrollBar1; private System.Windows.Forms.HScrollBar hScrollBar1; private System.Windows.Forms.Panel viewPanel; @@ -827,6 +844,9 @@ private void InitializeComponent() private System.Windows.Forms.ToolStripMenuItem layerManagerToolStripMenuItem; private System.Windows.Forms.ToolStripMenuItem helpToolStripMenuItem; private System.Windows.Forms.ToolStripMenuItem aboutToolStripMenuItem; + private System.Windows.Forms.ToolStripMenuItem exportToolStripMenuItem; + private System.Windows.Forms.ToolStripMenuItem saveAspngToolStripMenuItem; + private System.Windows.Forms.ToolStripMenuItem exportEachLayerAspngToolStripMenuItem; } } diff --git a/ManiacEditor/Properties/AssemblyInfo.cs b/ManiacEditor/Properties/AssemblyInfo.cs index 9d850893..b9f3abb1 100644 --- a/ManiacEditor/Properties/AssemblyInfo.cs +++ b/ManiacEditor/Properties/AssemblyInfo.cs @@ -10,7 +10,7 @@ [assembly: AssemblyConfiguration("")] [assembly: AssemblyCompany("")] [assembly: AssemblyProduct("Maniac Editor")] -[assembly: AssemblyCopyright("Copyright © koolkdev, OtherworldBob 2017")] +[assembly: AssemblyCopyright("Copyright © koolkdev, OtherworldBob 2017-2018")] [assembly: AssemblyTrademark("")] [assembly: AssemblyCulture("")] @@ -32,5 +32,5 @@ // You can specify all the values or you can default the Build and Revision Numbers // by using the '*' as shown below: // [assembly: AssemblyVersion("1.0.*")] -[assembly: AssemblyVersion("0.2.21.1")] -[assembly: AssemblyFileVersion("0.2.21.1")] +[assembly: AssemblyVersion("0.2.21.3")] +[assembly: AssemblyFileVersion("0.2.21.3")] diff --git a/README.md b/README.md index 51f53a46..066f2085 100644 --- a/README.md +++ b/README.md @@ -49,5 +49,6 @@ You can choose which properties the placed tiles will have by clicking on the ch * This goes hand-in-hand with importing objects. You'll want to import any applicable sounds too. * Layer reordering, creation, destruction and resizing (too big and the game will crash!); you can also edit some of the magic values assoicated with the layers too. * You'll need to fix-up any BGSwitch entities to account for anything you change though. +* New export function to write each layer of a scene to its own file. * Extra crashes! Save often, backup well. * Non-descript Undo/Redo helpers that don't tell you much most of the time.