UltimateZoneBuilder/Source/Map/Vertex.cs

226 lines
5.2 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.Direct3D;
using System.Drawing;
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
{
2007-11-12 22:43:01 +00:00
public sealed class Vertex : IDisposable
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;
2007-06-13 19:39:38 +00:00
// Position
private int x, y;
2007-06-13 19:39:38 +00:00
private Vector2D pos;
// References
2007-06-14 12:37:46 +00:00
private LinkedList<Linedef> linedefs;
// Selections
2007-12-01 18:29:58 +00:00
private bool selected;
2007-11-23 09:33:56 +00:00
// Cloning
private Vertex clone;
2007-06-13 19:39:38 +00:00
// Disposing
private bool isdisposed = false;
#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; } }
public int X { get { return x; } }
public int Y { get { return y; } }
2007-06-13 19:39:38 +00:00
public bool IsDisposed { get { return isdisposed; } }
2007-12-01 18:29:58 +00:00
public bool Selected { get { return selected; } set { selected = value; } }
2007-11-23 09:33:56 +00:00
public Vertex Clone { get { return clone; } set { clone = value; } }
2007-06-13 19:39:38 +00:00
#endregion
#region ================== Constructor / Disposer
// Constructor
public Vertex(MapSet map, LinkedListNode<Vertex> listitem, int x, int y)
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 = new Vector2D(x, y);
this.x = x;
this.y = y;
2007-06-13 19:39:38 +00:00
// We have no destructor
GC.SuppressFinalize(this);
}
// Diposer
public void Dispose()
{
// 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 l in linedefs) l.Dispose();
// Clean up
linedefs = null;
2007-06-14 12:37:46 +00:00
mainlistitem = null;
map = null;
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();
}
}
}
2007-06-13 19:39:38 +00:00
#endregion
2007-06-14 12:37:46 +00:00
2007-06-24 18:56:43 +00:00
#region ================== Methods
2007-12-26 00:31:32 +00:00
2007-06-14 12:37:46 +00:00
// This returns the distance from given coordinates
public float DistanceToSq(Vector2D p)
{
Vector2D delta = p - pos;
return delta.GetLengthSq();
}
// This returns the distance from given coordinates
public float DistanceTo(Vector2D p)
{
Vector2D delta = p - pos;
return delta.GetLength();
}
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)
{
// Change position
this.Move((int)Math.Round(newpos.x), (int)Math.Round(newpos.y));
}
2007-06-24 18:56:43 +00:00
// This moves the vertex
public void Move(int newx, int newy)
2007-06-24 18:56:43 +00:00
{
// Change position
x = newx;
y = newy;
pos = new Vector2D(newx, newy);
// Let all lines know they need an update
foreach(Linedef l in linedefs) l.NeedUpdate();
2007-06-24 18:56:43 +00:00
}
2007-06-14 12:37:46 +00:00
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)
{
LinkedListNode<Linedef> previous;
LinkedListNode<Linedef> current;
// Go for all lines
current = linedefs.Last;
while(current != null)
{
// Get previous
previous = current.Previous;
// Move the start to the other vertex
if(current.Value.Start == this)
current.Value.SetStartVertex(other);
// Move the end to the other vertex
if(current.Value.End == this)
current.Value.SetEndVertex(other);
// Go back one
current = previous;
}
// Remove this vertex
this.Dispose();
}
2007-06-14 12:37:46 +00:00
#endregion
2007-06-13 19:39:38 +00:00
}
}