summaryrefslogtreecommitdiff
path: root/MapEd/Form1.cs
diff options
context:
space:
mode:
authorIan C <ianc@noddybox.co.uk>2018-09-29 18:01:34 +0000
committerIan C <ianc@noddybox.co.uk>2018-09-29 18:01:34 +0000
commit8ed524392ae4b3b88ab5e9fafdce7620a0467623 (patch)
tree3e982bd04490063c3c98fb1c3cb8406030bab036 /MapEd/Form1.cs
parentcd56676ac2b8757a31d01eb28b13691e350f9d8d (diff)
Initial version
Diffstat (limited to 'MapEd/Form1.cs')
-rw-r--r--MapEd/Form1.cs217
1 files changed, 217 insertions, 0 deletions
diff --git a/MapEd/Form1.cs b/MapEd/Form1.cs
new file mode 100644
index 0000000..c990bcc
--- /dev/null
+++ b/MapEd/Form1.cs
@@ -0,0 +1,217 @@
+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;
+using Newtonsoft.Json;
+using System.IO;
+
+namespace MapEd
+{
+ public partial class Form1 : Form
+ {
+ private TileStrip tiles;
+ private string filename;
+
+ public Form1()
+ {
+ InitializeComponent();
+
+ drawingMode.SelectedIndex = 0;
+ }
+
+ private void OnQuit(object sender, EventArgs e)
+ {
+ if (MessageBox.Show("Quit", "Quit", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.OK)
+ {
+ this.Close();
+ }
+ }
+
+ private void EnableControls()
+ {
+ drawingMode.Enabled = true;
+ tileNumber.Enabled = true;
+ dataNumber.Enabled = true;
+ tileMode.Enabled = true;
+ dataMode.Enabled = true;
+ tilePreview.Enabled = true;
+ }
+
+ private void OnNew(object sender, EventArgs e)
+ {
+ try
+ {
+ NewForm newForm = new NewForm();
+
+ if (newForm.ShowDialog() == DialogResult.OK)
+ {
+ if (tiles != null)
+ {
+ tiles.Dispose();
+ tiles = null;
+ }
+
+ tiles = new TileStrip(newForm.TileMap, newForm.TileSize);
+
+ tileNumber.Maximum = tiles.Count - 1;
+ tileNumber.Value = 0;
+ OnTileNumber(tileNumber, new EventArgs());
+
+ Tuple<int, int> size = newForm.MapSize;
+
+ map.SetMap(size.Item2, size.Item1, tiles, newForm.TileSize);
+
+ saveMenuItem.Enabled = false;
+ saveAsMenuItem.Enabled = true;
+
+ EnableControls();
+ }
+ }
+ catch (Exception ex)
+ {
+ MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
+ }
+ }
+
+ private void OnLoad(object sender, EventArgs e)
+ {
+ try
+ {
+ OpenFileDialog fsel = new OpenFileDialog();
+
+ fsel.Filter = "MapEd Files (*.maped)|*.maped|All Files (*.*)|*.*";
+
+ if (fsel.ShowDialog() == DialogResult.OK)
+ {
+ filename = fsel.FileName;
+
+ using(StreamReader file = File.OpenText(filename))
+ {
+ string json = file.ReadToEnd();
+ FileContents contents = JsonConvert.DeserializeObject<FileContents>(json);
+
+ if (tiles != null)
+ {
+ tiles.Dispose();
+ tiles = null;
+ }
+
+ tiles = new TileStrip(contents.Strip, contents.TileSize);
+
+ tileNumber.Maximum = tiles.Count - 1;
+ tileNumber.Value = 0;
+ OnTileNumber(tileNumber, new EventArgs());
+
+ map.SetMap(contents.Width, contents.Height, tiles, contents.TileSize);
+ map.SetMap(contents.Map);
+ map.SetData(contents.Data);
+
+ file.Close();
+ }
+ }
+
+ saveMenuItem.Enabled = true;
+ saveAsMenuItem.Enabled = true;
+ EnableControls();
+ }
+ catch (Exception ex)
+ {
+ MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
+ filename = null;
+ }
+ }
+
+ private void SaveMap()
+ {
+ FileContents contents = new FileContents();
+
+ contents.Strip = tiles.SourceFile;
+ contents.Width = map.MapWidth;
+ contents.Height = map.MapHeight;
+ contents.TileSize = map.TileSize;
+ contents.Map = map.MapTiles;
+ contents.Data = map.MapData;
+
+ string json = JsonConvert.SerializeObject(contents);
+
+ using (StreamWriter file = File.CreateText(filename))
+ {
+ file.Write(json);
+ file.Close();
+ }
+ }
+
+ private void OnSaveAs(object sender, EventArgs e)
+ {
+ try
+ {
+ SaveFileDialog fsel = new SaveFileDialog();
+ fsel.Filter = "MapEd Files (*.maped)|*.maped|All Files (*.*)|*.*";
+
+ if (fsel.ShowDialog() == DialogResult.OK)
+ {
+ filename = fsel.FileName;
+ SaveMap();
+ }
+ }
+ catch (Exception ex)
+ {
+ MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
+ }
+ }
+
+ private void OnSave(object sender, EventArgs e)
+ {
+ try
+ {
+ if (!String.IsNullOrEmpty(filename))
+ {
+ SaveMap();
+ }
+ }
+ catch (Exception ex)
+ {
+ MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
+ }
+ }
+
+ private void OnTileNumber(object sender, EventArgs e)
+ {
+ int no = (int)tileNumber.Value;
+
+ tilePreview.BackgroundImage = tiles.GetImage(no);
+ map.SetTileNumber(no);
+ }
+
+ private void OnDrawingMode(object sender, EventArgs e)
+ {
+ MapControl.DrawMode mode = (MapControl.DrawMode)drawingMode.SelectedIndex;
+ map.MapDrawMode = mode;
+ }
+
+ private void OnDataNumber(object sender, EventArgs e)
+ {
+ map.SetDataNumber((int)dataNumber.Value);
+ }
+
+ private void OnDataOrTileMode(object sender, EventArgs e)
+ {
+ map.SetDataMode(sender == dataMode);
+ }
+
+ private void OnSelectTile(object sender, EventArgs e)
+ {
+ TileSelector selector = new TileSelector(tiles);
+
+ if (selector.ShowDialog() == DialogResult.OK)
+ {
+ tileNumber.Value = selector.SelectedTile;
+ }
+ }
+ }
+}