make it actually work, except for half the export

This commit is contained in:
Chris Forbes
2010-05-11 11:09:26 +12:00
parent c814aaa5de
commit ac75259efd
3 changed files with 606 additions and 19 deletions

View File

@@ -1,21 +1,117 @@
using System.Collections.Generic;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
using System.IO;
using System.Linq;
using System.Windows.Forms;
using System.Xml;
namespace OpenRA.TilesetBuilder
{
public partial class Form1 : Form
{
string srcfile;
public Form1( string src )
{
srcfile = src;
InitializeComponent();
surface1.Image = (Bitmap)Image.FromFile(src);
surface1.TerrainTypes = new int[surface1.Image.Width / 24, surface1.Image.Height / 24]; /* all passable by default */
surface1.Templates = new List<Template>();
surface1.Size = surface1.Image.Size;
/* todo: load stuff from previous session */
Load();
}
public new void Load()
{
try
{
var doc = new XmlDocument();
doc.Load(Path.ChangeExtension(srcfile, "tsx"));
foreach (var e in doc.SelectNodes("//terrain").OfType<XmlElement>())
surface1.TerrainTypes[
int.Parse(e.GetAttribute("x")),
int.Parse(e.GetAttribute("y"))] = int.Parse(e.GetAttribute("t"));
foreach (var e in doc.SelectNodes("//template").OfType<XmlElement>())
surface1.Templates.Add(new Template
{
Cells = e.SelectNodes("./cell").OfType<XmlElement>()
.Select(f => new int2(int.Parse(f.GetAttribute("x")), int.Parse(f.GetAttribute("y"))))
.ToDictionary(a => a, a => true)
});
}
catch { }
}
public void Save()
{
using (var w = XmlWriter.Create(Path.ChangeExtension(srcfile, "tsx"),
new XmlWriterSettings { Indent = true, IndentChars = " " }))
{
w.WriteStartDocument();
w.WriteStartElement("tileset");
for( var i = 0; i <= surface1.TerrainTypes.GetUpperBound(0); i++ )
for( var j = 0; j <= surface1.TerrainTypes.GetUpperBound(1); j++ )
if (surface1.TerrainTypes[i, j] != 0)
{
w.WriteStartElement("terrain");
w.WriteAttributeString("x", i.ToString());
w.WriteAttributeString("y", j.ToString());
w.WriteAttributeString("t", surface1.TerrainTypes[i, j].ToString());
}
foreach (var t in surface1.Templates)
{
w.WriteStartElement("template");
foreach (var c in t.Cells.Keys)
{
w.WriteStartElement("cell");
w.WriteAttributeString("x", c.X.ToString());
w.WriteAttributeString("y", c.Y.ToString());
w.WriteEndElement();
}
w.WriteEndElement();
}
w.WriteEndElement();
w.WriteEndDocument();
}
}
void TerrainTypeSelectorClicked(object sender, EventArgs e)
{
surface1.InputMode = (sender as ToolStripButton).Tag as string;
foreach (var tsb in (sender as ToolStripButton).Owner.Items.OfType<ToolStripButton>())
tsb.Checked = false;
(sender as ToolStripButton).Checked = true;
}
void SaveClicked(object sender, EventArgs e) { Save(); }
void ShowOverlaysClicked(object sender, EventArgs e) { surface1.ShowTerrainTypes ^= true; }
void ExportClicked(object sender, EventArgs e)
{
var dir = Path.Combine(Path.GetDirectoryName(srcfile), "output");
Directory.CreateDirectory(dir);
// export palette (use the embedded palette)
var p = surface1.Image.Palette.Entries.ToList();
while (p.Count < 256) p.Add(Color.Black); // pad the palette out with extra blacks
var paletteData = p.Take(256).SelectMany(c => new byte[] { (byte)(c.R >> 2), (byte)(c.G >> 2), (byte)(c.B >> 2) }).ToArray();
File.WriteAllBytes(Path.Combine(dir, "terrain.pal"), paletteData);
// todo: write out a TMP for each template
// todo: write out a tileset definition
// todo: write out a templates ini
}
}
@@ -29,6 +125,8 @@ namespace OpenRA.TilesetBuilder
public Bitmap Image;
public int[,] TerrainTypes;
public List<Template> Templates = new List<Template>();
public bool ShowTerrainTypes = true;
public string InputMode;
Template CurrentTemplate;
@@ -51,6 +149,15 @@ namespace OpenRA.TilesetBuilder
e.Graphics.DrawImageUnscaled(Image, 0, 0);
/* draw terrain type overlays */
if (ShowTerrainTypes)
for (var i = 0; i <= TerrainTypes.GetUpperBound(0); i++)
for (var j = 0; j <= TerrainTypes.GetUpperBound(1); j++)
if (TerrainTypes[i, j] != 0)
{
e.Graphics.FillRectangle(Brushes.Black, 24 * i + 10, 24 * j + 10, 10, 10);
e.Graphics.DrawString(TerrainTypes[i, j].ToString(),
Font, Brushes.LimeGreen, 24 * i + 10, 24 * j + 10);
}
/* draw template outlines */
foreach (var t in Templates)
@@ -76,19 +183,27 @@ namespace OpenRA.TilesetBuilder
{
var pos = new int2( e.X / 24, e.Y / 24 );
if (e.Button == MouseButtons.Left)
if (InputMode == null)
{
CurrentTemplate = Templates.FirstOrDefault(t => t.Cells.ContainsKey(pos));
if (CurrentTemplate == null)
Templates.Add(CurrentTemplate = new Template { Cells = new Dictionary<int2, bool> { { pos, true } } });
if (e.Button == MouseButtons.Left)
{
CurrentTemplate = Templates.FirstOrDefault(t => t.Cells.ContainsKey(pos));
if (CurrentTemplate == null)
Templates.Add(CurrentTemplate = new Template { Cells = new Dictionary<int2, bool> { { pos, true } } });
Invalidate();
Invalidate();
}
if (e.Button == MouseButtons.Right)
{
Templates.RemoveAll(t => t.Cells.ContainsKey(pos));
CurrentTemplate = null;
Invalidate();
}
}
if (e.Button == MouseButtons.Right)
else
{
Templates.RemoveAll(t => t.Cells.ContainsKey(pos));
CurrentTemplate = null;
TerrainTypes[pos.X, pos.Y] = int.Parse(InputMode);
Invalidate();
}
}
@@ -97,12 +212,15 @@ namespace OpenRA.TilesetBuilder
{
var pos = new int2(e.X / 24, e.Y / 24);
if (e.Button == MouseButtons.Left && CurrentTemplate != null)
if (InputMode == null)
{
if (!CurrentTemplate.Cells.ContainsKey(pos))
if (e.Button == MouseButtons.Left && CurrentTemplate != null)
{
CurrentTemplate.Cells[pos] = true;
Invalidate();
if (!CurrentTemplate.Cells.ContainsKey(pos))
{
CurrentTemplate.Cells[pos] = true;
Invalidate();
}
}
}
}