@ working on undo/redo system

This commit is contained in:
codeimp 2009-02-20 15:59:55 +00:00
parent 370bdca6fd
commit 5f6d122a67
3 changed files with 128 additions and 6 deletions

View file

@ -27,6 +27,7 @@ using SlimDX.Direct3D9;
using System.Drawing;
using CodeImp.DoomBuilder.Map;
using System.Collections.ObjectModel;
using CodeImp.DoomBuilder.IO;
#endregion
@ -74,8 +75,11 @@ namespace CodeImp.DoomBuilder.Geometry
// along a sidedef, this list contains a null entry for that vertex.
private ReadOnlyCollection<Sidedef> sidedefs;
// Temporary array for the sidedefs deserialization
private int[] sidedefindices;
#endregion
#region ================== Properties
public ReadOnlyCollection<int> IslandVertices { get { return islandvertices; } }
@ -83,7 +87,7 @@ namespace CodeImp.DoomBuilder.Geometry
public ReadOnlyCollection<Sidedef> Sidedefs { get { return sidedefs; } }
#endregion
#region ================== Constructor / Disposer
// Constructor
@ -142,7 +146,72 @@ namespace CodeImp.DoomBuilder.Geometry
}
#endregion
#region ================== Serialization
// Serialize / deserialize
internal void ReadWrite(IReadWriteStream s)
{
if(s.IsWriting)
{
s.wInt(islandvertices.Count);
for(int i = 0; i < islandvertices.Count; i++) s.wInt(islandvertices[i]);
s.wInt(vertices.Count);
for(int i = 0; i < vertices.Count; i++) s.wVector2D(vertices[i]);
s.wInt(sidedefs.Count);
for(int i = 0; i < sidedefs.Count; i++)
{
if(sidedefs[i] != null)
s.wInt(sidedefs[i].SerializedIndex);
else
s.wInt(-1);
}
}
else
{
int c;
s.rInt(out c);
int[] islandverticeslist = new int[c];
for(int i = 0; i < c; i++) s.rInt(out islandverticeslist[i]);
islandvertices = Array.AsReadOnly<int>(islandverticeslist);
s.rInt(out c);
Vector2D[] verticeslist = new Vector2D[c];
for(int i = 0; i < c; i++) s.rVector2D(out verticeslist[i]);
vertices = Array.AsReadOnly<Vector2D>(verticeslist);
s.rInt(out c);
sidedefindices = new int[c];
for(int i = 0; i < c; i++) s.rInt(out sidedefindices[i]);
}
}
// After deserialization we need to find the actual sidedefs back
internal void PostDeserialize(MapSet map)
{
// Find our sidedefs
List<Sidedef> sides = new List<Sidedef>(sidedefindices.Length);
for(int i = 0; i < sidedefindices.Length; i++)
{
if(sidedefindices[i] >= 0)
sides.Add(map.SidedefIndices[sidedefindices[i]]);
else
sides.Add(null);
}
// We don't need this array any longer
sidedefindices = null;
// Keep readonly array
sidedefs = Array.AsReadOnly<Sidedef>(sides.ToArray());
}
#endregion
#region ================== Tracing
// This traces sector lines to create a polygon tree

View file

@ -59,6 +59,9 @@ namespace CodeImp.DoomBuilder.Map
private List<int> indexholes;
private int lastsectorindex;
// Sidedef indexing for (de)serialization
private Sidedef[] sidedefindices;
// Map structures
private LinkedList<Vertex> vertices;
private LinkedList<Linedef> linedefs;
@ -88,6 +91,8 @@ namespace CodeImp.DoomBuilder.Map
public static string VirtualSectorField { get { return VIRTUAL_SECTOR_FIELD; } }
public static UniValue VirtualSectorValue { get { return virtualsectorvalue; } }
internal Sidedef[] SidedefIndices { get { return sidedefindices; } }
#endregion
#region ================== Constructor / Disposer
@ -572,7 +577,7 @@ namespace CodeImp.DoomBuilder.Map
// This serializes the MapSet
internal MemoryStream Serialize()
{
MemoryStream stream = new MemoryStream(512000);
MemoryStream stream = new MemoryStream(20000000); // Yes that is about 20 MB.
SerializerStream serializer = new SerializerStream(stream);
// Index the sidedefs
@ -591,7 +596,10 @@ namespace CodeImp.DoomBuilder.Map
WriteLinedefs(serializer);
WriteSidedefs(serializer);
WriteThings(serializer);
// Reallocate to keep only the used memory
stream.Capacity = stream.Length;
return stream;
}
@ -701,8 +709,17 @@ namespace CodeImp.DoomBuilder.Map
Linedef[] linedefsarray = ReadLinedefs(deserializer, verticesarray);
ReadSidedefs(deserializer, linedefsarray, sectorsarray);
ReadThings(deserializer);
// Make table of sidedef indices
sidedefindices = new Sidedef[sidedefs.Count];
foreach(Sidedef sd in sidedefs)
sidedefindices[sd.SerializedIndex] = sd;
// Call PostDeserialize
foreach(Sector s in sectors)
s.PostDeserialize(this);
}
// This deserializes things
private void ReadThings(DeserializerStream stream)
{

View file

@ -197,6 +197,42 @@ namespace CodeImp.DoomBuilder.Map
s.rwInt(ref effect);
s.rwInt(ref tag);
s.rwInt(ref brightness);
// Use a new triangulator when reading from stream
if(!s.IsWriting && (triangles == null)) triangles = new Triangulation();
triangles.ReadWrite(s);
if(s.IsWriting)
{
s.wInt(labels.Count);
for(int i = 0; i < labels.Count; i++)
{
s.wVector2D(labels[i].position);
s.wFloat(labels[i].radius);
}
}
else
{
int c; s.rInt(out c);
LabelPositionInfo[] labelsarray = new LabelPositionInfo[c];
for(int i = 0; i < c; i++)
{
s.rVector2D(out labelsarray[i].position);
s.rFloat(out labelsarray[i].radius);
}
labels = Array.AsReadOnly<LabelPositionInfo>(labelsarray);
}
}
// After deserialization
internal void PostDeserialize(MapSet map)
{
triangles.PostDeserialize(map);
// We need to rebuild the vertex buffer,
// but the triangulation was deserialized
updateneeded = true;
triangulationneeded = false;
}
// This copies all properties to another sector