Skip to content

Commit

Permalink
Add new function to export all layers of a scene to their own image f…
Browse files Browse the repository at this point in the history
…ile.
  • Loading branch information
OtherworldBob committed Jan 1, 2018
1 parent f4e6f70 commit 85bab6a
Show file tree
Hide file tree
Showing 4 changed files with 150 additions and 70 deletions.
101 changes: 80 additions & 21 deletions ManiacEditor/Editor.cs
Original file line number Diff line number Diff line change
@@ -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
{
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
112 changes: 66 additions & 46 deletions ManiacEditor/Editor.designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions ManiacEditor/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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("")]

Expand All @@ -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")]
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

0 comments on commit 85bab6a

Please sign in to comment.