UltimateZoneBuilder/Source/BuilderModes/ClassicModes/DrawGeometryMode.cs

787 lines
23 KiB
C#
Raw Normal View History

#region ================== Copyright (c) 2007 Pascal vd Heiden
/*
* 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.
*
*/
#endregion
#region ================== Namespaces
2008-02-24 21:52:18 +00:00
using System;
using System.Collections;
using System.Collections.Generic;
using System.Globalization;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Reflection;
using CodeImp.DoomBuilder.Interface;
using CodeImp.DoomBuilder.IO;
using CodeImp.DoomBuilder.Map;
using CodeImp.DoomBuilder.Rendering;
using CodeImp.DoomBuilder.Geometry;
using System.Drawing;
using CodeImp.DoomBuilder.Editing;
using CodeImp.DoomBuilder.Controls;
#endregion
namespace CodeImp.DoomBuilder.BuilderModes.Editing
2008-02-24 21:52:18 +00:00
{
[EditMode(SwitchAction = "drawlinesmode", Volatile = true)]
public class DrawGeometryMode : ClassicMode
2008-02-24 21:52:18 +00:00
{
#region ================== Structures
private struct DrawnVertex
{
public Vector2D pos;
public bool stitch;
}
#endregion
2008-02-24 21:52:18 +00:00
#region ================== Constants
2008-04-29 20:59:59 +00:00
private const float LINE_THICKNESS = 0.8f;
2008-02-24 21:52:18 +00:00
#endregion
#region ================== Variables
// Mode to return to
private EditMode basemode;
2008-02-24 21:52:18 +00:00
// Drawing points
private List<DrawnVertex> points;
// Keep track of view changes
private float lastoffsetx;
private float lastoffsety;
private float lastscale;
// Options
private bool snaptogrid; // SHIFT to toggle
private bool snaptonearest; // CTRL to enable
2008-02-24 21:52:18 +00:00
#endregion
#region ================== Properties
// Just keep the base mode button checked
public override string EditModeButtonName { get { return basemode.GetType().Name; } }
2008-02-24 21:52:18 +00:00
#endregion
#region ================== Constructor / Disposer
// Constructor
public DrawGeometryMode()
{
// Initialize
this.basemode = General.Map.Mode;
points = new List<DrawnVertex>();
// No selection in this mode
General.Map.Map.ClearAllSelected();
General.Map.Map.ClearAllMarks();
2008-02-24 21:52:18 +00:00
// We have no destructor
GC.SuppressFinalize(this);
}
// Disposer
public override void Dispose()
2008-02-24 21:52:18 +00:00
{
// Not already disposed?
if(!isdisposed)
{
// Clean up
// Done
base.Dispose();
2008-02-24 21:52:18 +00:00
}
}
#endregion
#region ================== Methods
// Cancelled
public override void Cancel()
{
// Cancel base class
base.Cancel();
// Return to original mode
Type t = basemode.GetType();
basemode = (EditMode)Activator.CreateInstance(t);
General.Map.ChangeMode(basemode);
}
// Disenagaging
public override void Disengage()
{
List<Vertex> newverts = new List<Vertex>();
List<Vertex> intersectverts = new List<Vertex>();
List<Linedef> newlines = new List<Linedef>();
2008-05-08 12:04:20 +00:00
List<Linedef> oldlines = new List<Linedef>(General.Map.Map.Linedefs);
2008-05-08 13:44:48 +00:00
List<Sidedef> insidesides = new List<Sidedef>();
List<Vertex> mergeverts = new List<Vertex>();
List<Vertex> nonmergeverts = new List<Vertex>(General.Map.Map.Vertices);
2008-05-05 21:38:52 +00:00
MapSet map = General.Map.Map;
base.Disengage();
Cursor.Current = Cursors.AppStarting;
General.Settings.FindDefaultDrawSettings();
// When not cancelled and points have been drawn
if(!cancelled && (points.Count > 0))
{
// Make undo for the draw
General.Map.UndoRedo.CreateUndo("line draw", UndoGroup.None, 0);
2008-05-05 21:38:52 +00:00
/***************************************************\
STEP 1: Create the new geometry
\***************************************************/
// Make first vertex
Vertex v1 = map.CreateVertex(points[0].pos);
v1.Marked = true;
// Keep references
newverts.Add(v1);
if(points[0].stitch) mergeverts.Add(v1); else nonmergeverts.Add(v1);
// Go for all other points
for(int i = 1; i < points.Count; i++)
{
// Create vertex for point
Vertex v2 = map.CreateVertex(points[i].pos);
v2.Marked = true;
// Keep references
newverts.Add(v2);
if(points[i].stitch) mergeverts.Add(v2); else nonmergeverts.Add(v2);
// Create line between point and previous
Linedef ld = map.CreateLinedef(v1, v2);
ld.Marked = true;
ld.Selected = true;
2008-05-08 05:45:30 +00:00
ld.ApplySidedFlags();
ld.UpdateCache();
2008-05-08 05:45:30 +00:00
newlines.Add(ld);
// Should we split this line to merge with intersecting lines?
2008-05-08 12:04:20 +00:00
if(points[i - 1].stitch && points[i].stitch)
{
// Check if any other lines intersect this line
List<float> intersections = new List<float>();
Line2D measureline = ld.Line;
foreach(Linedef ld2 in map.Linedefs)
{
// Intersecting?
// We only keep the unit length from the start of the line and
// do the real splitting later, when all intersections are known
float u;
if(ld2.Line.GetIntersection(measureline, out u))
{
if(!float.IsNaN(u) && (u > 0.0f) && (u < 1.0f) && (ld2 != ld))
intersections.Add(u);
}
}
// Sort the intersections
intersections.Sort();
// Go for all found intersections
Linedef splitline = ld;
foreach(float u in intersections)
{
// Calculate exact coordinates where to split
// We use measureline for this, because the original line
// may already have changed in length due to a previous split
Vector2D splitpoint = measureline.GetCoordinatesAt(u);
// Make the vertex
Vertex splitvertex = map.CreateVertex(splitpoint);
splitvertex.Marked = true;
newverts.Add(splitvertex);
mergeverts.Add(splitvertex); // <-- add to merge?
intersectverts.Add(splitvertex);
// The Split method ties the end of the original line to the given
// vertex and starts a new line at the given vertex, so continue
// splitting with the new line, because the intersections are sorted
// from low to high (beginning at the original line start)
splitline = splitline.Split(splitvertex);
2008-05-08 05:45:30 +00:00
splitline.ApplySidedFlags();
newlines.Add(splitline);
}
}
// Next
v1 = v2;
}
// Join merge vertices so that overlapping vertices in the draw become one.
MapSet.JoinVertices(mergeverts, mergeverts, false, MapSet.STITCH_DISTANCE);
2008-05-08 12:04:20 +00:00
// We prefer a closed polygon, because then we can determine the interior properly
// Check if the two ends of the polygon are closed
bool drawingclosed = false;
if(newlines.Count > 0)
{
// When not closed, we will try to find a path to close it
Linedef firstline = newlines[0];
Linedef lastline = newlines[newlines.Count - 1];
drawingclosed = (firstline.Start == lastline.End);
if(!drawingclosed)
{
// First and last vertex stitch with geometry?
if(points[0].stitch && points[points.Count - 1].stitch)
{
// Find out where they will stitch
Linedef l1 = MapSet.NearestLinedefRange(oldlines, firstline.Start.Position, MapSet.STITCH_DISTANCE);
Linedef l2 = MapSet.NearestLinedefRange(oldlines, lastline.End.Position, MapSet.STITCH_DISTANCE);
if((l1 != null) && (l2 != null))
{
List<LinedefSide> shortestpath = null;
// Same line?
if(l1 == l2)
{
// Then just connect the two
shortestpath = new List<LinedefSide>();
shortestpath.Add(new LinedefSide(l1, true));
}
else
{
// Find the shortest, closest path between these lines
List<List<LinedefSide>> paths = new List<List<LinedefSide>>(8);
paths.Add(SectorTools.FindClosestPath(l1, true, l2, true, true));
paths.Add(SectorTools.FindClosestPath(l1, true, l2, false, true));
paths.Add(SectorTools.FindClosestPath(l1, false, l2, true, true));
paths.Add(SectorTools.FindClosestPath(l1, false, l2, false, true));
paths.Add(SectorTools.FindClosestPath(l2, true, l1, true, true));
paths.Add(SectorTools.FindClosestPath(l2, true, l1, false, true));
paths.Add(SectorTools.FindClosestPath(l2, false, l1, true, true));
paths.Add(SectorTools.FindClosestPath(l2, false, l1, false, true));
foreach(List<LinedefSide> p in paths)
if((p != null) && ((shortestpath == null) || (p.Count < shortestpath.Count))) shortestpath = p;
}
// Found a path?
if(shortestpath != null)
{
// Go for all vertices in the path to make additional lines
v1 = firstline.Start;
for(int i = 1; i < shortestpath.Count; i++)
{
// Get the next position
Vector2D v2pos = shortestpath[i].Front ? shortestpath[i].Line.Start.Position : shortestpath[i].Line.End.Position;
// Make the new vertex
Vertex v2 = map.CreateVertex(v2pos);
v2.Marked = true;
mergeverts.Add(v2);
// Make the line
Linedef ld = map.CreateLinedef(v1, v2);
ld.Marked = true;
ld.Selected = true;
ld.ApplySidedFlags();
ld.UpdateCache();
newlines.Add(ld);
// Next
v1 = v2;
}
// Make the final line
Linedef lld = map.CreateLinedef(v1, lastline.End);
lld.Marked = true;
lld.Selected = true;
lld.ApplySidedFlags();
lld.UpdateCache();
newlines.Add(lld);
// Drawing is now closed
drawingclosed = true;
// Join merge vertices so that overlapping vertices in the draw become one.
MapSet.JoinVertices(mergeverts, mergeverts, false, MapSet.STITCH_DISTANCE);
}
}
}
}
}
// Merge intersetion vertices with the new lines. This completes the
// self intersections for which splits were made above.
map.Update(true, false);
MapSet.SplitLinesByVertices(newlines, intersectverts, MapSet.STITCH_DISTANCE, null);
2008-05-08 05:45:30 +00:00
MapSet.SplitLinesByVertices(newlines, mergeverts, MapSet.STITCH_DISTANCE, null);
2008-05-08 12:04:20 +00:00
2008-05-05 21:38:52 +00:00
/***************************************************\
STEP 2: Merge the new geometry
\***************************************************/
// In step 3 we will make sectors on the front sides and join sectors on the
// back sides, but because the user could have drawn counterclockwise or just
// some weird polygon this could result in problems. The following code adjusts
// the direction of all new lines so that their front (right) side is facing
// the interior of the new drawn polygon.
map.Update(true, false);
foreach(Linedef ld in newlines)
{
// Find closest path starting with the front of this linedef
2008-05-08 05:45:30 +00:00
List<LinedefSide> pathlines = SectorTools.FindClosestPath(ld, true, true);
if(pathlines != null)
{
// Make polygon
LinedefTracePath tracepath = new LinedefTracePath(pathlines);
Polygon pathpoly = tracepath.MakePolygon();
2008-05-08 05:45:30 +00:00
// Check if the front of the line is outside the polygon
if(!pathpoly.Intersect(ld.GetSidePoint(true)))
{
// Now trace from the back side of the line to see if
// the back side lies in the interior. I don't want to
// flip the line if it is not helping.
// Find closest path starting with the back of this linedef
2008-05-08 05:45:30 +00:00
pathlines = SectorTools.FindClosestPath(ld, false, true);
if(pathlines != null)
{
// Make polygon
tracepath = new LinedefTracePath(pathlines);
pathpoly = tracepath.MakePolygon();
// Check if the back of the line is inside the polygon
if(pathpoly.Intersect(ld.GetSidePoint(false)))
{
// We must flip this linedef to face the interior
ld.FlipVertices();
ld.FlipSidedefs();
2008-05-08 05:45:30 +00:00
ld.UpdateCache();
}
}
}
}
}
// Before this point, the new geometry is not linked with the existing geometry.
// Now perform standard geometry stitching to merge the new geometry with the rest
// of the map. The marked vertices indicate the new geometry.
map.StitchGeometry();
map.Update(true, false);
2008-05-05 21:38:52 +00:00
// Find our new lines again, because they have been merged with the other geometry
// but their Marked property is copied where they have joined!
newlines = map.GetMarkedLinedefs(true);
2008-05-05 21:38:52 +00:00
/***************************************************\
STEP 3: Join and create new sectors
\***************************************************/
2008-05-08 12:04:20 +00:00
// The code below atempts to create sectors on the front sides of the drawn
// geometry and joins sectors on the back sides of the drawn geometry.
// This code does not change any geometry, it only makes/updates sidedefs.
bool[] frontsdone = new bool[newlines.Count];
bool[] backsdone = new bool[newlines.Count];
for(int i = 0; i < newlines.Count; i++)
{
Linedef ld = newlines[i];
// Front not marked as done?
if(!frontsdone[i])
{
2008-05-05 21:38:52 +00:00
// Find a way to create a sector here
List<LinedefSide> sectorlines = SectorTools.FindPotentialSectorAt(ld, true);
if(sectorlines != null)
{
2008-05-05 21:38:52 +00:00
// Make the new sector
Sector newsector = SectorTools.MakeSector(sectorlines);
// Go for all sidedefs in this new sector
foreach(Sidedef sd in newsector.Sidedefs)
{
2008-05-08 13:44:48 +00:00
// Keep list of sides inside created sectors
insidesides.Add(sd);
// Side matches with a side of our new lines?
int lineindex = newlines.IndexOf(sd.Line);
if(lineindex > -1)
{
// Mark this side as done
if(sd.IsFront)
frontsdone[lineindex] = true;
else
backsdone[lineindex] = true;
}
}
}
}
// Back not marked as done?
if(!backsdone[i])
{
2008-05-05 21:38:52 +00:00
// Find a way to create a sector here
List<LinedefSide> sectorlines = SectorTools.FindPotentialSectorAt(ld, false);
if(sectorlines != null)
{
2008-05-05 21:38:52 +00:00
// We don't always want to create a new sector on the back sides
// So first check if any of the surrounding lines originally have sidedefs
Sidedef joinsidedef = null;
foreach(LinedefSide ls in sectorlines)
{
2008-05-05 21:38:52 +00:00
if(ls.Front && (ls.Line.Front != null))
{
2008-05-05 21:38:52 +00:00
joinsidedef = ls.Line.Front;
break;
}
else if(!ls.Front && (ls.Line.Back != null))
{
joinsidedef = ls.Line.Back;
break;
}
}
// Join?
if(joinsidedef != null)
{
// Join the new sector
Sector newsector = SectorTools.JoinSector(sectorlines, joinsidedef);
// Go for all sidedefs in this new sector
foreach(Sidedef sd in newsector.Sidedefs)
{
// Side matches with a side of our new lines?
int lineindex = newlines.IndexOf(sd.Line);
if(lineindex > -1)
{
// Mark this side as done
if(sd.IsFront)
frontsdone[lineindex] = true;
else
backsdone[lineindex] = true;
}
}
}
}
}
}
// Make corrections for backward linedefs
MapSet.FlipBackwardLinedefs(newlines);
2008-05-08 13:44:48 +00:00
// Remove all unneeded textures
foreach(Linedef ld in newlines)
{
if(ld.Front != null) ld.Front.RemoveUnneededTextures(true);
if(ld.Back != null) ld.Back.RemoveUnneededTextures(true);
}
foreach(Sidedef sd in insidesides)
{
sd.RemoveUnneededTextures(true);
}
// Snap to map format accuracy
General.Map.Map.SnapAllToAccuracy();
// Update cached values
map.Update();
// Map is changed
General.Map.IsChanged = true;
}
// Hide highlight info
General.Interface.HideInfo();
// Done
Cursor.Current = Cursors.Default;
}
// This checks if the view offset/zoom changed and updates the check
protected bool CheckViewChanged()
{
bool viewchanged = false;
// View changed?
if(renderer.OffsetX != lastoffsetx) viewchanged = true;
if(renderer.OffsetY != lastoffsety) viewchanged = true;
if(renderer.Scale != lastscale) viewchanged = true;
// Keep view information
lastoffsetx = renderer.OffsetX;
lastoffsety = renderer.OffsetY;
lastscale = renderer.Scale;
// Return result
return viewchanged;
}
// This redraws the display
public override void RedrawDisplay()
{
// Render lines
if(renderer.StartPlotter(true))
{
renderer.PlotLinedefSet(General.Map.Map.Linedefs);
renderer.PlotVerticesSet(General.Map.Map.Vertices);
renderer.Finish();
}
// Render things
if(renderer.StartThings(true))
{
renderer.SetThingsRenderOrder(false);
renderer.RenderThingSet(General.Map.Map.Things);
renderer.Finish();
}
// Normal update
Update();
}
// This updates the dragging
private void Update()
{
PixelColor color;
snaptogrid = General.Interface.ShiftState ^ General.Interface.SnapToGrid;
snaptonearest = General.Interface.CtrlState ^ General.Interface.AutoMerge;
DrawnVertex lastp = new DrawnVertex();
DrawnVertex curp = GetCurrentPosition();
float vsize = ((float)renderer.VertexSize + 1.0f) / renderer.Scale;
// Render drawing lines
if(renderer.StartOverlay(true))
{
// Go for all points to draw lines
if(points.Count > 0)
{
// Render lines
lastp = points[0];
for(int i = 1; i < points.Count; i++)
{
// Determine line color
if(lastp.stitch || points[i].stitch)
color = General.Colors.Highlight;
else
color = General.Colors.Selection;
// Render line
renderer.RenderLine(lastp.pos, points[i].pos, LINE_THICKNESS, color, true);
lastp = points[i];
}
// Determine line color
if(lastp.stitch || snaptonearest)
color = General.Colors.Highlight;
else
color = General.Colors.Selection;
// Render line to cursor
renderer.RenderLine(lastp.pos, curp.pos, LINE_THICKNESS, color, true);
// Render vertices
for(int i = 0; i < points.Count; i++)
{
if(points[i].stitch)
{
renderer.RenderRectangleFilled(new RectangleF(points[i].pos.x - vsize, points[i].pos.y - vsize, vsize * 2.0f, vsize * 2.0f), General.Colors.Highlight, true);
}
else
{
renderer.RenderRectangleFilled(new RectangleF(points[i].pos.x - vsize, points[i].pos.y - vsize, vsize * 2.0f, vsize * 2.0f), General.Colors.Selection, true);
}
}
}
// Determine point color
if(snaptonearest) color = General.Colors.Highlight;
else color = General.Colors.Selection;
// Render vertex at cursor
renderer.RenderRectangleFilled(new RectangleF(curp.pos.x - vsize, curp.pos.y - vsize, vsize * 2.0f, vsize * 2.0f), color, true);
// Done
renderer.Finish();
}
// Done
renderer.Present();
}
// This gets the aligned and snapped draw position
private DrawnVertex GetCurrentPosition()
{
DrawnVertex p = new DrawnVertex();
// Snap to nearest?
if(snaptonearest)
{
float vrange = VerticesMode.VERTEX_HIGHLIGHT_RANGE / renderer.Scale;
// Go for all drawn points
foreach(DrawnVertex v in points)
{
Vector2D delta = mousemappos - v.pos;
if(delta.GetLengthSq() < (vrange * vrange))
{
p.pos = v.pos;
p.stitch = true;
return p;
}
}
// Try the nearest vertex
Vertex nv = General.Map.Map.NearestVertexSquareRange(mousemappos, vrange);
if(nv != null)
{
p.pos = nv.Position;
p.stitch = true;
return p;
}
// Try the nearest linedef
Linedef nl = General.Map.Map.NearestLinedefRange(mousemappos, LinedefsMode.LINEDEF_HIGHLIGHT_RANGE / renderer.Scale);
if(nl != null)
{
// Snap to grid?
if(snaptogrid)
{
// Get grid intersection coordinates
List<Vector2D> coords = nl.GetGridIntersections();
// Find nearest grid intersection
float found_distance = float.MaxValue;
Vector2D found_coord = new Vector2D();
foreach(Vector2D v in coords)
{
Vector2D delta = mousemappos - v;
if(delta.GetLengthSq() < found_distance)
{
found_distance = delta.GetLengthSq();
found_coord = v;
}
}
// Align to the closest grid intersection
p.pos = found_coord;
p.stitch = true;
return p;
}
else
{
// Aligned to line
p.pos = nl.NearestOnLine(mousemappos);
p.stitch = true;
return p;
}
}
}
// Snap to grid?
if(snaptogrid)
{
// Aligned to grid
p.pos = General.Map.Grid.SnappedToGrid(mousemappos);
p.stitch = snaptonearest;
return p;
}
else
{
// Normal position
p.pos = mousemappos;
p.stitch = snaptonearest;
return p;
}
}
// Mouse moving
public override void MouseMove(MouseEventArgs e)
{
base.MouseMove(e);
Update();
}
// Drawing a point
[BeginAction("drawpoint")]
public void DrawPoint()
{
// Mouse inside window?
if(General.Interface.MouseInDisplay)
{
points.Add(GetCurrentPosition());
Update();
// Check if point stitches with the first
if((points.Count > 1) && (points[points.Count - 1].stitch))
{
Vector2D p1 = points[0].pos;
Vector2D p2 = points[points.Count - 1].pos;
Vector2D delta = p1 - p2;
if((Math.Abs(delta.x) <= 0.001f) && (Math.Abs(delta.y) <= 0.001f))
{
// Finish drawing
FinishDraw();
}
}
}
}
// Remove a point
[BeginAction("removepoint")]
public void RemovePoint()
{
if(points.Count > 0) points.RemoveAt(points.Count - 1);
Update();
}
// Finish drawing
[BeginAction("finishdraw")]
public void FinishDraw()
{
// Just return to base mode, Disengage will be called automatically.
General.Map.ChangeMode(basemode);
}
// When a key is released
public override void KeyUp(KeyEventArgs e)
{
base.KeyUp(e);
if((snaptogrid != (General.Interface.ShiftState ^ General.Interface.SnapToGrid)) ||
(snaptonearest != (General.Interface.CtrlState ^ General.Interface.AutoMerge))) Update();
}
// When a key is pressed
public override void KeyDown(KeyEventArgs e)
{
base.KeyDown(e);
if((snaptogrid != (General.Interface.ShiftState ^ General.Interface.SnapToGrid)) ||
(snaptonearest != (General.Interface.CtrlState ^ General.Interface.AutoMerge))) Update();
}
2008-02-24 21:52:18 +00:00
#endregion
}
}