summaryrefslogtreecommitdiff
path: root/neschr/MainForm.cs
diff options
context:
space:
mode:
Diffstat (limited to 'neschr/MainForm.cs')
-rw-r--r--neschr/MainForm.cs465
1 files changed, 465 insertions, 0 deletions
diff --git a/neschr/MainForm.cs b/neschr/MainForm.cs
new file mode 100644
index 0000000..317d2fd
--- /dev/null
+++ b/neschr/MainForm.cs
@@ -0,0 +1,465 @@
+using System;
+using System.Collections.Generic;
+using System.ComponentModel;
+using System.Data;
+using System.Drawing;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+using System.Windows.Forms;
+
+namespace UCTest
+{
+ public partial class MainForm : Form
+ {
+ EditControl editControl;
+ PaletteControl[] paletteControl;
+ IndexedPalette[] palette;
+ int currentPalette;
+
+ NesTileset tiles;
+ int currentTile;
+ bool metaMode;
+
+ string filePath;
+
+ public MainForm()
+ {
+ InitializeComponent();
+
+ tiles = new NesTileset();
+
+ palette = new IndexedPalette[4];
+
+ for(int f = 0; f < 4; f++)
+ {
+ palette[f] = new IndexedPalette();
+ }
+
+ LoadNesTilePalette();
+
+ editControl = new EditControl(palette[0], previewPanel);
+
+ paletteControl = new PaletteControl[4];
+
+ for(int f = 0; f < 4; f++)
+ {
+ paletteControl[f] = new PaletteControl(palette[f]);
+ paletteControl[f].Tag = f;
+ paletteControl[f].SelectionChanged += OnPaletteSelection;
+ paletteControl[f].SelectedForeground = f == 0 ? 1 : -1;
+ }
+
+ editPanel.Controls.Add(editControl);
+ pal1Panel.Controls.Add(paletteControl[0]);
+ pal2Panel.Controls.Add(paletteControl[1]);
+ pal3Panel.Controls.Add(paletteControl[2]);
+ pal4Panel.Controls.Add(paletteControl[3]);
+
+ currentPalette = 0;
+ currentTile = 0;
+ metaMode = false;
+
+ OnPaletteSelection(paletteControl[0], null);
+
+ editControl.ContentChanged += OnContentChanged;
+
+ modeCombo.SelectedIndex = 0;
+ }
+
+ private void LoadNesTilePalette()
+ {
+ for(int f = 0; f < 4; f++)
+ {
+ for(int n = 0; n < 4; n++)
+ {
+ palette[f].Set(n, NesPalette.Palette[tiles.GetPalette(f * 4 + n)]);
+ }
+ }
+
+ }
+
+ private void OnCheck16(object sender, EventArgs e)
+ {
+ if (check16.Checked)
+ {
+ SaveCurrentTile();
+ metaMode = true;
+ editControl.SetGridSize(16, 16);
+ LoadCurrentTile();
+ }
+ else
+ {
+ SaveCurrentTile();
+ metaMode = false;
+ editControl.SetGridSize(8, 8);
+ LoadCurrentTile();
+ }
+ }
+
+ private void OnPaletteSelection(object sender, EventArgs e)
+ {
+ PaletteControl c = (PaletteControl)sender;
+ int i = (int)c.Tag;
+
+ if (i != currentPalette)
+ {
+ paletteControl[currentPalette].SelectedForeground = -1;
+ currentPalette = i;
+ editControl.SetPalette(palette[currentPalette]);
+ }
+
+ editControl.Foreground = paletteControl[currentPalette].SelectedForeground;
+ }
+
+ private void OnContentChanged(object sender, EventArgs e)
+ {
+ undoButton.Enabled = editControl.UndoAvailable;
+ redoButton.Enabled = editControl.RedoAvailable;
+ }
+
+ private void OnUndo(object sender, EventArgs e)
+ {
+ editControl.Undo();
+ }
+
+ private void OnRedo(object sender, EventArgs e)
+ {
+ editControl.Redo();
+ }
+
+ private void OnModeCombo(object sender, EventArgs e)
+ {
+ switch(modeCombo.SelectedIndex)
+ {
+ case 0:
+ editControl.DrawingMode = EditControl.Mode.Plot;
+ break;
+
+ case 1:
+ editControl.DrawingMode = EditControl.Mode.Line;
+ break;
+
+ case 2:
+ editControl.DrawingMode = EditControl.Mode.Fill;
+ break;
+
+ case 3:
+ editControl.DrawingMode = EditControl.Mode.Rect;
+ break;
+
+ case 4:
+ editControl.DrawingMode = EditControl.Mode.FilledRect;
+ break;
+ }
+ }
+
+ private void OnHorizontalFlip(object sender, EventArgs e)
+ {
+ editControl.HorizontalFlip();
+ }
+
+ private void OnVerticalFlip(object sender, EventArgs e)
+ {
+ editControl.VerticalFlip();
+ }
+
+ private void OnRotateRight(object sender, EventArgs e)
+ {
+ editControl.RotateRight();
+ }
+
+ private void OnRotateLeft(object sender, EventArgs e)
+ {
+ editControl.RotateLeft();
+ }
+
+ private void OnColourClick(object sender, EventArgs e)
+ {
+ NesPalette selector = new NesPalette();
+
+ selector.ShowDialog();
+
+ if (selector.ColourWasSelected)
+ {
+ int i = paletteControl[currentPalette].SelectedForeground;
+
+ if (i == 0)
+ {
+ for(int f = 0; f < 4; f++)
+ {
+ palette[f].Set(0, selector.SelectedColour);
+ tiles.SetPalette(f * 4, selector.SelectedNumber);
+ }
+ }
+ else
+ {
+ palette[currentPalette].Set(i, selector.SelectedColour);
+ tiles.SetPalette(currentPalette * 4 + i, selector.SelectedNumber);
+ }
+ }
+ }
+
+ private void ExtractChar(int[,] from, int[,] to, int x, int y)
+ {
+ for(int dx = 0; dx < 8; dx++)
+ {
+ for(int dy = 0; dy < 8; dy++)
+ {
+ to[dx, dy] = from[x + dx, y + dy];
+ }
+ }
+ }
+
+ private void PlaceChar(int[,] from, int[,] to, int x, int y)
+ {
+ for(int dx = 0; dx < 8; dx++)
+ {
+ for(int dy = 0; dy < 8; dy++)
+ {
+ to[x + dx, y + dy] = from[dx, dy];
+ }
+ }
+ }
+
+ private void SaveCurrentTile()
+ {
+ if (metaMode)
+ {
+ int[,] data = editControl.GridData;
+ int[,] copy = new int[8,8];
+
+ ExtractChar(data, copy, 0, 0);
+ tiles.SetChar(currentTile, copy);
+
+ ExtractChar(data, copy, 8, 0);
+ tiles.SetChar(currentTile + 1, copy);
+
+ ExtractChar(data, copy, 0, 8);
+ tiles.SetChar(currentTile + 2, copy);
+
+ ExtractChar(data, copy, 8, 8);
+ tiles.SetChar(currentTile + 3, copy);
+ }
+ else
+ {
+ tiles.SetChar(currentTile, editControl.GridData);
+ }
+ }
+
+ private void LoadCurrentTile()
+ {
+ if (metaMode)
+ {
+ int[,] copy = new int[16,16];
+
+ PlaceChar(tiles.GetChar(currentTile), copy, 0, 0);
+ PlaceChar(tiles.GetChar(currentTile + 1), copy, 8, 0);
+ PlaceChar(tiles.GetChar(currentTile + 2), copy, 0, 8);
+ PlaceChar(tiles.GetChar(currentTile + 3), copy, 8, 8);
+
+ editControl.GridData = copy;
+ }
+ else
+ {
+ editControl.GridData = tiles.GetChar(currentTile);
+ }
+ }
+
+ private void OnTileNumber(object sender, EventArgs e)
+ {
+ SaveCurrentTile();
+ currentTile = Convert.ToInt32(tileNumber.Value);
+ LoadCurrentTile();
+ }
+
+ private void OnRevert(object sender, EventArgs e)
+ {
+ LoadCurrentTile();
+ }
+
+ private void OnShift(object sender, EventArgs e)
+ {
+ if (shiftRightButton.Equals(sender))
+ {
+ editControl.Shift(1, 0);
+ }
+ else if (shiftLeftButton.Equals(sender))
+ {
+ editControl.Shift(-1, 0);
+ }
+ else if (shiftUpButton.Equals(sender))
+ {
+ editControl.Shift(0, -1);
+ }
+ else if (shiftDownButton.Equals(sender))
+ {
+ editControl.Shift(0, 1);
+ }
+ }
+
+ private void OnClosing(object sender, FormClosingEventArgs e)
+ {
+ if (e.CloseReason == CloseReason.UserClosing)
+ {
+ e.Cancel = (MessageBox.Show(this, "Quit?", "Confirmation", MessageBoxButtons.YesNo) == DialogResult.No);
+ }
+ }
+
+ private string FileSelectorOpen(string action, string ext, string filter)
+ {
+ OpenFileDialog fsel = new OpenFileDialog();
+
+ fsel.Title = "Select file to " + action;
+ fsel.DefaultExt = ext;
+ fsel.Filter = filter;
+
+ if (fsel.ShowDialog() == DialogResult.OK)
+ {
+ return fsel.FileName;
+ }
+ else
+ {
+ return null;
+ }
+ }
+
+ private string FileSelectorSave(string action, string ext, string filter)
+ {
+ SaveFileDialog fsel = new SaveFileDialog();
+
+ fsel.Title = "Select file to " + action;
+ fsel.DefaultExt = ext;
+ fsel.Filter = filter;
+
+ if (fsel.ShowDialog() == DialogResult.OK)
+ {
+ return fsel.FileName;
+ }
+ else
+ {
+ return null;
+ }
+ }
+
+ private void Error(string err)
+ {
+ MessageBox.Show(this, err, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
+ }
+
+ private void OnLoad(object sender, EventArgs e)
+ {
+ string path = FileSelectorOpen("open", "neschr", "NES CHR files (*.neschr)|*.neschr|All files (*.*)|*.*");
+
+ if (path != null)
+ {
+ if (!tiles.Load(path))
+ {
+ Error("Failed to load " + path);
+ }
+ else
+ {
+ filePath = path;
+ LoadNesTilePalette();
+ LoadCurrentTile();
+ }
+ }
+ }
+
+ private void OnSave(object sender, EventArgs e)
+ {
+ string path = filePath ?? FileSelectorSave("save", "neschr", "NES CHR files (*.neschr)|*.neschr|All files (*.*)|*.*");
+
+ if (path != null)
+ {
+ SaveCurrentTile();
+
+ if (!tiles.Save(path))
+ {
+ Error("Failed to save " + path);
+ }
+ }
+ }
+
+ private void OnSaveAs(object sender, EventArgs e)
+ {
+ string path = FileSelectorSave("save", "neschr", "NES CHR files (*.neschr)|*.neschr|All files (*.*)|*.*");
+
+ if (path != null)
+ {
+ SaveCurrentTile();
+
+ if (!tiles.Save(path))
+ {
+ Error("Failed to save " + path);
+ }
+ else
+ {
+ filePath = path;
+ }
+ }
+ }
+
+ private void OnExportPalette(object sender, EventArgs e)
+ {
+ string path = FileSelectorSave("export", String.Empty, "All files (*.*)|*.*");
+
+ if (path != null)
+ {
+ if (!tiles.ExportPalette(path))
+ {
+ Error("Failed to export " + path);
+ }
+ }
+ }
+
+ private void OnExportCHR(object sender, EventArgs e)
+ {
+ string path = FileSelectorSave("export", String.Empty, "All files (*.*)|*.*");
+
+ if (path != null)
+ {
+ SaveCurrentTile();
+
+ if (!tiles.ExportCHR(path))
+ {
+ Error("Failed to export " + path);
+ }
+ }
+ }
+
+ private void OnImportPalettte(object sender, EventArgs e)
+ {
+ string path = FileSelectorOpen("import", String.Empty, "All files (*.*)|*.*");
+
+ if (path != null)
+ {
+ if (!tiles.ImportPalette(path))
+ {
+ Error("Failed to import " + path);
+ }
+ else
+ {
+ LoadNesTilePalette();
+ }
+ }
+ }
+
+ private void OnImportCHR(object sender, EventArgs e)
+ {
+ string path = FileSelectorOpen("import", String.Empty, "All files (*.*)|*.*");
+
+ if (path != null)
+ {
+ if (!tiles.ImportCHR(path))
+ {
+ Error("Failed to import " + path);
+ }
+ else
+ {
+ LoadCurrentTile();
+ }
+ }
+ }
+ }
+}