UltimateZoneBuilder/Source/Map/Vertex.cs

292 lines
7 KiB
C#
Raw Normal View History

2007-06-13 19:39:38 +00:00
2007-06-14 23:31:57 +00:00
#region ================== Copyright (c) 2007 Pascal vd Heiden
2007-06-13 19:39:38 +00:00
/*
* Copyright (c) 2007 Pascal vd Heiden, www.codeimp.com
* This program is released under GNU General Public License
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
*/
2007-06-14 23:31:57 +00:00
#endregion
#region ================== Namespaces
2007-06-13 19:39:38 +00:00
using System;
using System.Collections;
using System.Collections.Generic;
using System.Globalization;
using System.Text;
using CodeImp.DoomBuilder.Geometry;
using CodeImp.DoomBuilder.Rendering;
using SlimDX.Direct3D9;
using System.Drawing;
using CodeImp.DoomBuilder.IO;
2007-06-13 19:39:38 +00:00
2007-06-14 23:31:57 +00:00
#endregion
2007-06-13 19:39:38 +00:00
namespace CodeImp.DoomBuilder.Map
{
public sealed class Vertex : SelectableElement
2007-06-13 19:39:38 +00:00
{
#region ================== Constants
public const int BUFFERVERTICES = 1;
public const int RENDERPRIMITIVES = 1;
2007-06-13 19:39:38 +00:00
#endregion
#region ================== Variables
2007-06-14 12:37:46 +00:00
// Map
private MapSet map;
2007-06-14 12:37:46 +00:00
// List items
private LinkedListNode<Vertex> mainlistitem;
private LinkedListNode<Vertex> selecteditem;
2007-06-14 12:37:46 +00:00
2007-06-13 19:39:38 +00:00
// Position
private Vector2D pos;
// References
2007-06-14 12:37:46 +00:00
private LinkedList<Linedef> linedefs;
2007-11-23 09:33:56 +00:00
// Cloning
private Vertex clone;
private int serializedindex;
2007-06-13 19:39:38 +00:00
#endregion
#region ================== Properties
public MapSet Map { get { return map; } }
2007-06-14 12:37:46 +00:00
public ICollection<Linedef> Linedefs { get { return linedefs; } }
2007-06-13 19:39:38 +00:00
public Vector2D Position { get { return pos; } }
internal Vertex Clone { get { return clone; } set { clone = value; } }
internal int SerializedIndex { get { return serializedindex; } set { serializedindex = value; } }
2007-06-13 19:39:38 +00:00
#endregion
#region ================== Constructor / Disposer
// Constructor
internal Vertex(MapSet map, LinkedListNode<Vertex> listitem, Vector2D pos)
2007-06-13 19:39:38 +00:00
{
// Initialize
2007-06-14 12:37:46 +00:00
this.map = map;
this.linedefs = new LinkedList<Linedef>();
this.mainlistitem = listitem;
this.pos = pos;
2007-06-13 19:39:38 +00:00
// We have no destructor
GC.SuppressFinalize(this);
}
// Constructor
internal Vertex(MapSet map, LinkedListNode<Vertex> listitem, IReadWriteStream stream)
{
// Initialize
this.map = map;
this.linedefs = new LinkedList<Linedef>();
this.mainlistitem = listitem;
ReadWrite(stream);
// We have no destructor
GC.SuppressFinalize(this);
}
2008-01-02 21:49:43 +00:00
// Disposer
public override void Dispose()
2007-06-13 19:39:38 +00:00
{
// Not already disposed?
if(!isdisposed)
{
2007-06-14 12:37:46 +00:00
// Already set isdisposed so that changes can be prohibited
isdisposed = true;
// Remove from main list
mainlistitem.List.Remove(mainlistitem);
2007-06-13 19:39:38 +00:00
// Dispose the lines that are attached to this vertex
// because a linedef cannot exist without 2 vertices.
foreach(Linedef ld in linedefs) ld.Dispose();
2007-06-13 19:39:38 +00:00
// Clean up
linedefs = null;
2007-06-14 12:37:46 +00:00
mainlistitem = null;
map = null;
// Dispose base
base.Dispose();
2007-06-13 19:39:38 +00:00
}
}
#endregion
2007-06-14 12:37:46 +00:00
#region ================== Management
2007-06-13 19:39:38 +00:00
2007-06-14 12:37:46 +00:00
// This attaches a linedef and returns the listitem
public LinkedListNode<Linedef> AttachLinedef(Linedef l) { return linedefs.AddLast(l); }
2007-06-13 19:39:38 +00:00
// This detaches a linedef
2007-06-14 12:37:46 +00:00
public void DetachLinedef(LinkedListNode<Linedef> l)
{
// Not disposing?
if(!isdisposed)
{
// Remove linedef
linedefs.Remove(l);
// No more linedefs left?
if(linedefs.Count == 0)
{
// This vertex is now useless, dispose it
this.Dispose();
}
}
}
// Serialize / deserialize
internal void ReadWrite(IReadWriteStream s)
{
base.ReadWrite(s);
s.rwVector2D(ref pos);
}
// Selected
protected override void DoSelect()
{
base.DoSelect();
selecteditem = map.SelectedVertices.AddLast(this);
}
// Deselect
protected override void DoUnselect()
{
base.DoUnselect();
if(selecteditem.List != null) selecteditem.List.Remove(selecteditem);
selecteditem = null;
}
2008-01-25 19:12:34 +00:00
#endregion
2007-06-24 18:56:43 +00:00
#region ================== Methods
2007-12-26 00:31:32 +00:00
/// <summary>
/// Returns the index of this vertex. This is a O(n) operation.
/// </summary>
public int GetIndex()
{
return map.GetIndexForVertex(this);
}
2008-01-25 19:12:34 +00:00
// This copies all properties to another thing
public void CopyPropertiesTo(Vertex v)
{
// Copy properties
v.pos = pos;
base.CopyPropertiesTo(v);
2008-01-25 19:12:34 +00:00
}
2007-06-14 12:37:46 +00:00
// This returns the distance from given coordinates
public float DistanceToSq(Vector2D p)
{
2008-04-19 15:44:05 +00:00
return (p.x - pos.x) * (p.x - pos.x) + (p.y - pos.y) * (p.y - pos.y);
2007-06-14 12:37:46 +00:00
}
// This returns the distance from given coordinates
public float DistanceTo(Vector2D p)
{
return Vector2D.Distance(p, pos);
2007-06-14 12:37:46 +00:00
}
2007-06-24 18:56:43 +00:00
// This finds the line closest to the specified position
public Linedef NearestLinedef(Vector2D pos) { return MapSet.NearestLinedef(linedefs, pos); }
2007-11-10 19:24:52 +00:00
// This moves the vertex
public void Move(Vector2D newpos)
{
2009-01-03 22:18:59 +00:00
// Do we actually move?
if(newpos != pos)
{
2009-01-03 22:18:59 +00:00
// Change position
pos = newpos;
#if DEBUG
if(float.IsNaN(pos.x) || float.IsNaN(pos.y) ||
float.IsInfinity(pos.x) || float.IsInfinity(pos.y))
{
General.Fail("Invalid vertex position! The given vertex coordinates cannot be NaN or Infinite.");
}
#endif
// Let all lines know they need an update
foreach(Linedef l in linedefs) l.NeedUpdate();
General.Map.IsChanged = true;
}
2007-06-24 18:56:43 +00:00
}
2007-06-14 12:37:46 +00:00
// This snaps the vertex to the map format accuracy
public void SnapToAccuracy()
{
// Round the coordinates
Vector2D newpos = new Vector2D((float)Math.Round(pos.x, General.Map.FormatInterface.VertexDecimals),
(float)Math.Round(pos.y, General.Map.FormatInterface.VertexDecimals));
this.Move(newpos);
}
2007-11-13 09:06:15 +00:00
// This snaps the vertex to the grid
public void SnapToGrid()
{
// Calculate nearest grid coordinates
this.Move(General.Map.Grid.SnappedToGrid(pos));
}
2007-12-01 01:32:56 +00:00
// This joins another vertex
// Which means this vertex is removed and the other is kept!
public void Join(Vertex other)
{
// If either of the two vertices was selected, keep the other selected
if(this.Selected) other.Selected = true;
if(this.marked) other.marked = true;
// Any linedefs to move?
if(linedefs.Count > 0)
{
// Detach all linedefs and attach them to the other
// This will automatically dispose this vertex
while(linedefs != null)
{
// Move the line to the other vertex
if(linedefs.First.Value.Start == this)
linedefs.First.Value.SetStartVertex(other);
else
linedefs.First.Value.SetEndVertex(other);
}
}
else
{
// No lines attached
// Dispose manually
this.Dispose();
}
2009-01-03 22:18:59 +00:00
General.Map.IsChanged = true;
2007-12-01 01:32:56 +00:00
}
// String representation
public override string ToString()
{
return "Vertex (" + pos + ")";
}
2007-06-14 12:37:46 +00:00
#endregion
2007-06-13 19:39:38 +00:00
}
}