start splitting up tsb; fix a wtf in the save code

This commit is contained in:
Chris Forbes
2010-09-17 18:59:14 +12:00
parent 1b66635670
commit 5593b9ffdd
5 changed files with 1265 additions and 1414 deletions

View File

@@ -24,6 +24,7 @@ namespace OpenRA.TilesetBuilder
{
string srcfile;
int size;
public Form1( string src, int size )
{
srcfile = src;
@@ -31,11 +32,11 @@ namespace OpenRA.TilesetBuilder
InitializeComponent();
surface1.TileSize = size;
surface1.Image = (Bitmap)Image.FromFile(src);
surface1.Image.SetResolution(96, 96); // people keep being noobs about DPI, and GDI+ cares.
surface1.TerrainTypes = new int[surface1.Image.Width / size, surface1.Image.Height / size]; /* all passable by default */
surface1.Templates = new List<Template>();
surface1.Size = surface1.Image.Size;
/* todo: load stuff from previous session */
Load();
}
@@ -78,6 +79,7 @@ namespace OpenRA.TilesetBuilder
w.WriteAttributeString("x", i.ToString());
w.WriteAttributeString("y", j.ToString());
w.WriteAttributeString("t", surface1.TerrainTypes[i, j].ToString());
w.WriteEndElement();
}
foreach (var t in surface1.Templates)
@@ -235,7 +237,7 @@ namespace OpenRA.TilesetBuilder
}
else
for (var x = 0; x < TileSize * TileSize; x++)
bw.Write((byte)0); /* todo: don't fill with air */
bw.Write((byte)0); /* todo: don't fill with air */
}
}
@@ -273,125 +275,4 @@ namespace OpenRA.TilesetBuilder
return filename;
}
}
class Template
{
public Dictionary<int2, bool> Cells = new Dictionary<int2, bool>();
public int Left { get { return Cells.Keys.Min(c => c.X); } }
public int Top { get { return Cells.Keys.Min(c => c.Y); } }
public int Right { get { return Cells.Keys.Max(c => c.X) + 1; } }
public int Bottom { get { return Cells.Keys.Max(c => c.Y) + 1; } }
public int Width { get { return Right - Left; } }
public int Height { get { return Bottom - Top; } }
}
class Surface : Control
{
public Bitmap Image;
public int[,] TerrainTypes;
public List<Template> Templates = new List<Template>();
public bool ShowTerrainTypes = true;
public string InputMode;
public int TileSize;
Template CurrentTemplate;
public Surface()
{
SetStyle(ControlStyles.AllPaintingInWmPaint, true);
SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
SetStyle(ControlStyles.ResizeRedraw, true);
UpdateStyles();
}
Brush currentBrush = new SolidBrush(Color.FromArgb(60, Color.White));
protected override void OnPaint(PaintEventArgs e)
{
if (Image == null || TerrainTypes == null || Templates == null)
return;
/* draw the background */
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, TileSize * i + 10, TileSize * j + 10, 10, 10);
e.Graphics.DrawString(TerrainTypes[i, j].ToString(),
Font, Brushes.LimeGreen, TileSize * i + 10, TileSize * j + 10);
}
/* draw template outlines */
foreach (var t in Templates)
{
foreach (var c in t.Cells.Keys)
{
if (CurrentTemplate == t)
e.Graphics.FillRectangle(currentBrush, TileSize * c.X, TileSize * c.Y, TileSize, TileSize);
if (!t.Cells.ContainsKey(c + new int2(-1, 0)))
e.Graphics.DrawLine(Pens.Red, (TileSize * c).ToPoint(), (TileSize * (c + new int2(0, 1))).ToPoint());
if (!t.Cells.ContainsKey(c + new int2(+1, 0)))
e.Graphics.DrawLine(Pens.Red, (TileSize * (c + new int2(1, 0))).ToPoint(), (TileSize * (c + new int2(1, 1))).ToPoint());
if (!t.Cells.ContainsKey(c + new int2(0, +1)))
e.Graphics.DrawLine(Pens.Red, (TileSize * (c + new int2(0, 1))).ToPoint(), (TileSize * (c + new int2(1, 1))).ToPoint());
if (!t.Cells.ContainsKey(c + new int2(0, -1)))
e.Graphics.DrawLine(Pens.Red, (TileSize * c).ToPoint(), (TileSize * (c + new int2(1, 0))).ToPoint());
}
}
}
protected override void OnMouseDown(MouseEventArgs e)
{
var pos = new int2( e.X / TileSize, e.Y / TileSize );
if (InputMode == null)
{
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();
}
if (e.Button == MouseButtons.Right)
{
Templates.RemoveAll(t => t.Cells.ContainsKey(pos));
CurrentTemplate = null;
Invalidate();
}
}
else
{
TerrainTypes[pos.X, pos.Y] = int.Parse(InputMode);
Invalidate();
}
}
protected override void OnMouseMove(MouseEventArgs e)
{
var pos = new int2(e.X / TileSize, e.Y / TileSize);
if (InputMode == null)
{
if (e.Button == MouseButtons.Left && CurrentTemplate != null)
{
if (!CurrentTemplate.Cells.ContainsKey(pos))
{
CurrentTemplate.Cells[pos] = true;
Invalidate();
}
}
}
}
}
}

View File

@@ -80,6 +80,10 @@
<DependentUpon>Settings.settings</DependentUpon>
<DesignTimeSharedInput>True</DesignTimeSharedInput>
</Compile>
<Compile Include="Surface.cs">
<SubType>Component</SubType>
</Compile>
<Compile Include="Template.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\OpenRA.FileFormats\OpenRA.FileFormats.csproj">

View File

@@ -0,0 +1,114 @@
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;
namespace OpenRA.TilesetBuilder
{
class Surface : Control
{
public Bitmap Image;
public int[,] TerrainTypes;
public List<Template> Templates = new List<Template>();
public bool ShowTerrainTypes = true;
public string InputMode;
public int TileSize;
Template CurrentTemplate;
public Surface()
{
SetStyle(ControlStyles.AllPaintingInWmPaint, true);
SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
SetStyle(ControlStyles.ResizeRedraw, true);
UpdateStyles();
}
Brush currentBrush = new SolidBrush(Color.FromArgb(60, Color.White));
protected override void OnPaint(PaintEventArgs e)
{
if (Image == null || TerrainTypes == null || Templates == null)
return;
/* draw the background */
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, TileSize * i + 10, TileSize * j + 10, 10, 10);
e.Graphics.DrawString(TerrainTypes[i, j].ToString(),
Font, Brushes.LimeGreen, TileSize * i + 10, TileSize * j + 10);
}
/* draw template outlines */
foreach (var t in Templates)
{
foreach (var c in t.Cells.Keys)
{
if (CurrentTemplate == t)
e.Graphics.FillRectangle(currentBrush, TileSize * c.X, TileSize * c.Y, TileSize, TileSize);
if (!t.Cells.ContainsKey(c + new int2(-1, 0)))
e.Graphics.DrawLine(Pens.Red, (TileSize * c).ToPoint(), (TileSize * (c + new int2(0, 1))).ToPoint());
if (!t.Cells.ContainsKey(c + new int2(+1, 0)))
e.Graphics.DrawLine(Pens.Red, (TileSize * (c + new int2(1, 0))).ToPoint(), (TileSize * (c + new int2(1, 1))).ToPoint());
if (!t.Cells.ContainsKey(c + new int2(0, +1)))
e.Graphics.DrawLine(Pens.Red, (TileSize * (c + new int2(0, 1))).ToPoint(), (TileSize * (c + new int2(1, 1))).ToPoint());
if (!t.Cells.ContainsKey(c + new int2(0, -1)))
e.Graphics.DrawLine(Pens.Red, (TileSize * c).ToPoint(), (TileSize * (c + new int2(1, 0))).ToPoint());
}
}
}
protected override void OnMouseDown(MouseEventArgs e)
{
var pos = new int2(e.X / TileSize, e.Y / TileSize);
if (InputMode == null)
{
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();
}
if (e.Button == MouseButtons.Right)
{
Templates.RemoveAll(t => t.Cells.ContainsKey(pos));
CurrentTemplate = null;
Invalidate();
}
}
else
{
TerrainTypes[pos.X, pos.Y] = int.Parse(InputMode);
Invalidate();
}
}
protected override void OnMouseMove(MouseEventArgs e)
{
var pos = new int2(e.X / TileSize, e.Y / TileSize);
if (InputMode == null)
{
if (e.Button == MouseButtons.Left && CurrentTemplate != null)
{
if (!CurrentTemplate.Cells.ContainsKey(pos))
{
CurrentTemplate.Cells[pos] = true;
Invalidate();
}
}
}
}
}
}

View File

@@ -0,0 +1,19 @@
using System.Collections.Generic;
using System.Linq;
namespace OpenRA.TilesetBuilder
{
class Template
{
public Dictionary<int2, bool> Cells = new Dictionary<int2, bool>();
public int Left { get { return Cells.Keys.Min(c => c.X); } }
public int Top { get { return Cells.Keys.Min(c => c.Y); } }
public int Right { get { return Cells.Keys.Max(c => c.X) + 1; } }
public int Bottom { get { return Cells.Keys.Max(c => c.Y) + 1; } }
public int Width { get { return Right - Left; } }
public int Height { get { return Bottom - Top; } }
}
}

File diff suppressed because it is too large Load Diff