UltimateZoneBuilder/Source/Editing/DragVerticesMode.cs

384 lines
10 KiB
C#
Raw Normal View History

2007-11-07 21:14:27 +00:00
#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
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;
2007-12-05 19:39:09 +00:00
using System.Drawing;
2007-11-07 21:14:27 +00:00
#endregion
2007-11-10 01:58:08 +00:00
// This mode if for quickly dragging vertices without a layer.
// The geometry is merged and the mode returns to VerticesMode when the mouse is released.
2007-11-07 21:14:27 +00:00
namespace CodeImp.DoomBuilder.Editing
{
2007-11-10 19:24:52 +00:00
public class DragVerticesMode : ClassicMode
2007-11-07 21:14:27 +00:00
{
#region ================== Constants
#endregion
#region ================== Variables
2007-11-10 19:24:52 +00:00
// Mouse position on map where dragging started
2007-11-13 09:06:15 +00:00
private Vector2D dragstartmappos;
2007-11-10 01:58:08 +00:00
2007-11-10 19:24:52 +00:00
// Item used as reference for snapping to the grid
2007-11-13 09:06:15 +00:00
private Vertex dragitem;
private Vector2D dragitemposition;
2007-11-10 01:58:08 +00:00
2007-11-10 19:24:52 +00:00
// List of old vertex positions
2007-11-13 09:06:15 +00:00
private List<Vector2D> oldpositions;
2007-12-01 18:29:58 +00:00
// List of selected items
private ICollection<Vertex> selectedverts;
2007-11-15 23:38:09 +00:00
// List of non-selected items
2007-12-01 01:32:56 +00:00
private ICollection<Vertex> unselectedverts;
// List of unstable lines
private ICollection<Linedef> unstablelines;
2007-11-15 23:38:09 +00:00
2007-11-13 09:06:15 +00:00
// Options
2007-12-01 01:32:56 +00:00
private bool snaptogrid; // SHIFT to toggle
2007-11-15 23:38:09 +00:00
private bool snaptonearest; // CTRL to enable
2007-11-10 19:24:52 +00:00
2007-11-07 21:14:27 +00:00
#endregion
#region ================== Properties
#endregion
#region ================== Constructor / Disposer
// Constructor to start dragging immediately
2007-11-10 19:24:52 +00:00
public DragVerticesMode(Vertex dragitem, Vector2D dragstartmappos)
2007-11-07 21:14:27 +00:00
{
// Initialize
2007-11-10 01:58:08 +00:00
this.dragitem = dragitem;
2007-11-10 19:24:52 +00:00
this.dragstartmappos = dragstartmappos;
2007-11-07 21:14:27 +00:00
2007-12-01 01:32:56 +00:00
Cursor.Current = Cursors.AppStarting;
2007-12-01 18:29:58 +00:00
// Make list of selected vertices
selectedverts = General.Map.Map.GetVerticesSelection(true);
// Make list of non-selected vertices
// This will be used for snapping to nearest items
unselectedverts = General.Map.Map.GetVerticesSelection(false);
2007-11-10 19:24:52 +00:00
// Make old positions list
// We will use this as reference to move the vertices, or to move them back on cancel
2007-12-01 18:29:58 +00:00
oldpositions = new List<Vector2D>(selectedverts.Count);
foreach(Vertex v in selectedverts) oldpositions.Add(v.Position);
2007-11-13 09:06:15 +00:00
// Also keep old position of the dragged item
dragitemposition = dragitem.Position;
2007-11-15 23:38:09 +00:00
2007-12-01 01:32:56 +00:00
// Make list of unstable lines only
// These will have their length displayed during the drag
2007-12-01 18:29:58 +00:00
unstablelines = General.Map.Map.LinedefsFromSelectedVertices(false, false, true);
2007-12-01 01:32:56 +00:00
Cursor.Current = Cursors.Default;
2007-11-07 21:14:27 +00:00
// We have no destructor
GC.SuppressFinalize(this);
}
// Diposer
public override void Dispose()
{
// Not already disposed?
if(!isdisposed)
{
// Clean up
// Done
isdisposed = true;
}
}
#endregion
#region ================== Methods
2007-11-15 23:38:09 +00:00
2007-11-13 06:49:13 +00:00
// This moves the selected geometry relatively
2007-11-13 09:06:15 +00:00
// Returns true when geometry has actually moved
2007-11-15 23:38:09 +00:00
private bool MoveGeometryRelative(Vector2D offset, bool snapgrid, bool snapnearest)
2007-11-10 01:58:08 +00:00
{
2007-11-13 09:06:15 +00:00
Vector2D oldpos = dragitem.Position;
2007-11-15 23:38:09 +00:00
Vertex nearest;
2007-11-10 19:24:52 +00:00
int i = 0;
2007-11-12 22:43:01 +00:00
2007-11-15 23:38:09 +00:00
// Snap to nearest?
if(snapnearest)
{
// Find nearest unselected item within selection range
2007-12-01 01:32:56 +00:00
nearest = MapSet.NearestVertexSquareRange(unselectedverts, mousemappos, VerticesMode.VERTEX_HIGHLIGHT_RANGE / renderer.Scale);
2007-11-15 23:38:09 +00:00
if(nearest != null)
{
// Move the dragged item
dragitem.Move(nearest.Position);
// Adjust the offset
offset = nearest.Position - dragitemposition;
// Do not snap to grid!
snapgrid = false;
}
}
2007-11-13 09:06:15 +00:00
// Snap to grid?
2007-11-15 23:38:09 +00:00
if(snapgrid)
2007-11-10 19:24:52 +00:00
{
2007-11-13 09:06:15 +00:00
// Move the dragged item
dragitem.Move(dragitemposition + offset);
2007-11-10 01:58:08 +00:00
2007-11-13 09:06:15 +00:00
// Snap item to grid
dragitem.SnapToGrid();
// Adjust the offset
offset += dragitem.Position - (dragitemposition + offset);
}
// Drag item moved?
2007-11-15 23:38:09 +00:00
if(!snapgrid || (dragitem.Position != oldpos))
2007-11-13 09:06:15 +00:00
{
// Move selected geometry
2007-12-01 18:29:58 +00:00
foreach(Vertex v in selectedverts)
2007-11-13 09:06:15 +00:00
{
2007-11-15 23:38:09 +00:00
// Move vertex from old position relative to the
// mouse position change since drag start
2007-11-13 09:06:15 +00:00
v.Move(oldpositions[i] + offset);
// Next
i++;
}
// Moved
return true;
}
else
{
// No changes
return false;
2007-11-10 19:24:52 +00:00
}
2007-11-13 06:49:13 +00:00
}
// Cancelled
public override void Cancel()
{
// Move geometry back to original position
2007-11-15 23:38:09 +00:00
MoveGeometryRelative(new Vector2D(0f, 0f), false, false);
2007-11-10 19:24:52 +00:00
2007-12-01 01:32:56 +00:00
// If only a single vertex was selected, deselect it now
2007-12-01 18:29:58 +00:00
if(selectedverts.Count == 1) General.Map.Map.ClearSelectedVertices();
2007-12-01 01:32:56 +00:00
2007-11-10 19:24:52 +00:00
// Update cached values
General.Map.Map.Update();
// Cancel base class
2007-11-10 01:58:08 +00:00
base.Cancel();
2007-11-10 19:24:52 +00:00
// Return to vertices mode
General.Map.ChangeMode(new VerticesMode());
2007-11-10 01:58:08 +00:00
}
2007-11-10 19:24:52 +00:00
// Mode engages
public override void Engage()
{
base.Engage();
// Check vertices button on main window
General.MainWindow.SetVerticesChecked(true);
}
2007-11-10 01:58:08 +00:00
// Disenagaging
public override void Disengage()
{
2007-12-01 01:32:56 +00:00
ICollection<Linedef> movinglines;
ICollection<Linedef> fixedlines;
2007-12-05 19:39:09 +00:00
Rectangle editarea;
2007-12-01 01:32:56 +00:00
int stitches = 0;
int stitchundo;
2007-11-10 19:24:52 +00:00
base.Disengage();
2007-12-04 19:22:14 +00:00
Cursor.Current = Cursors.AppStarting;
2007-11-10 19:24:52 +00:00
2007-11-10 01:58:08 +00:00
// When not cancelled
if(!cancelled)
{
2007-11-13 06:49:13 +00:00
// Move geometry back to original position
2007-11-15 23:38:09 +00:00
MoveGeometryRelative(new Vector2D(0f, 0f), false, false);
2007-11-13 06:49:13 +00:00
2007-12-01 01:32:56 +00:00
// Make undo for the dragging
2007-11-13 06:49:13 +00:00
General.Map.UndoRedo.CreateUndo("drag vertices", UndoGroup.None, 0, false);
// Move selected geometry to final position
2007-11-15 23:38:09 +00:00
MoveGeometryRelative(mousemappos - dragstartmappos, snaptogrid, snaptonearest);
2007-11-13 06:49:13 +00:00
2007-12-01 01:32:56 +00:00
// ===== BEGIN GEOMETRY STITCHING
if(General.MainWindow.AutoMerge)
{
// Make undo for the stitching
stitchundo = General.Map.UndoRedo.CreateUndo("stitch geometry", UndoGroup.None, 0, false);
2007-12-01 01:32:56 +00:00
// Find lines that moved during the drag
movinglines = General.Map.Map.LinedefsFromSelectedVertices(false, true, true);
2007-12-01 01:32:56 +00:00
// Find all non-moving lines
fixedlines = General.Map.Map.LinedefsFromSelectedVertices(true, false, false);
2007-12-01 01:32:56 +00:00
// Determine area in which we are editing
editarea = MapSet.AreaFromLines(movinglines);
editarea.Inflate((int)Math.Ceiling(General.Settings.StitchDistance),
(int)Math.Ceiling(General.Settings.StitchDistance));
// Join nearby vertices
stitches += MapSet.JoinVertices(unselectedverts, selectedverts, true, General.Settings.StitchDistance);
2007-12-01 01:32:56 +00:00
// Update cached values
General.Map.Map.Update();
2007-12-01 01:32:56 +00:00
// Split moving lines with unselected vertices
unselectedverts = MapSet.FilterArea(unselectedverts, ref editarea);
stitches += MapSet.SplitLinesByVertices(movinglines, unselectedverts, General.Settings.StitchDistance, movinglines);
2007-11-13 06:49:13 +00:00
// Split non-moving lines with selected vertices
fixedlines = MapSet.FilterArea(fixedlines, ref editarea);
stitches += MapSet.SplitLinesByVertices(fixedlines, selectedverts, General.Settings.StitchDistance, movinglines);
2007-11-13 06:49:13 +00:00
// Remove looped linedefs
stitches += MapSet.RemoveLoopedLinedefs(movinglines);
// Join overlapping lines
stitches += MapSet.JoinOverlappingLines(movinglines);
// No stitching done? then withdraw undo
if(stitches == 0) General.Map.UndoRedo.WithdrawUndo(stitchundo);
}
2007-12-01 01:32:56 +00:00
// ===== END GEOMETRY STITCHING
// If only a single vertex was selected, deselect it now
2007-12-01 18:29:58 +00:00
if(selectedverts.Count == 1) General.Map.Map.ClearSelectedVertices();
2007-12-01 01:32:56 +00:00
2007-11-13 06:49:13 +00:00
// Update cached values
General.Map.Map.Update();
2007-11-10 01:58:08 +00:00
// Map is changed
General.Map.IsChanged = true;
}
2007-11-10 19:24:52 +00:00
// Hide highlight info
General.MainWindow.HideInfo();
// Uncheck vertices button on main window
General.MainWindow.SetVerticesChecked(false);
2007-11-14 22:53:48 +00:00
Cursor.Current = Cursors.Default;
2007-11-10 01:58:08 +00:00
}
2007-11-10 19:24:52 +00:00
// This redraws the display
public unsafe override void RedrawDisplay()
2007-11-10 01:58:08 +00:00
{
2007-11-21 12:50:56 +00:00
// Start rendering
if(renderer.Start(true, false))
2007-11-10 19:24:52 +00:00
{
// Render lines and vertices
renderer.RenderLinedefSet(General.Map.Map.Linedefs);
2007-12-01 18:29:58 +00:00
renderer.RenderVerticesSet(unselectedverts);
renderer.RenderVerticesSet(selectedverts);
2007-11-10 19:24:52 +00:00
2007-11-15 23:38:09 +00:00
// Draw the dragged item highlighted
// This is important to know, because this item is used
// for snapping to the grid and snapping to nearest items
renderer.RenderVertex(dragitem, ColorCollection.HIGHLIGHT);
2007-11-10 19:24:52 +00:00
// Done
2007-11-21 12:50:56 +00:00
renderer.Finish();
2007-11-10 19:24:52 +00:00
}
2007-11-10 01:58:08 +00:00
}
2007-11-14 22:53:48 +00:00
// This updates the dragging
private void Update()
2007-11-10 01:58:08 +00:00
{
2007-12-01 01:32:56 +00:00
snaptogrid = General.MainWindow.ShiftState ^ General.MainWindow.SnapToGrid;
2007-11-15 23:38:09 +00:00
snaptonearest = General.MainWindow.CtrlState;
2007-11-10 19:24:52 +00:00
// Move selected geometry
2007-11-15 23:38:09 +00:00
if(MoveGeometryRelative(mousemappos - dragstartmappos, snaptogrid, snaptonearest))
2007-11-13 09:06:15 +00:00
{
// Update cached values
General.Map.Map.Update();
2007-11-10 19:24:52 +00:00
2007-11-13 09:06:15 +00:00
// Redraw
General.MainWindow.RedrawDisplay();
}
2007-11-10 01:58:08 +00:00
}
2007-11-14 22:53:48 +00:00
// Mouse moving
public override void MouseMove(MouseEventArgs e)
{
base.MouseMove(e);
Update();
}
2007-11-13 09:06:15 +00:00
// Mouse button released
2007-11-10 01:58:08 +00:00
public override void MouseUp(MouseEventArgs e)
{
2007-11-10 19:24:52 +00:00
base.MouseUp(e);
2007-11-10 01:58:08 +00:00
// Is the editing button released?
if(e.Button == EditMode.EDIT_BUTTON)
{
// Just return to vertices mode, geometry will be merged on disengage.
General.Map.ChangeMode(new VerticesMode());
}
}
2007-11-14 22:53:48 +00:00
// When a key is released
public override void KeyUp(KeyEventArgs e)
{
base.KeyUp(e);
2007-12-01 01:32:56 +00:00
if(snaptogrid != General.MainWindow.ShiftState ^ General.MainWindow.SnapToGrid) Update();
2007-11-15 23:38:09 +00:00
if(snaptonearest != General.MainWindow.CtrlState) Update();
2007-11-14 22:53:48 +00:00
}
// When a key is pressed
public override void KeyDown(KeyEventArgs e)
{
base.KeyDown(e);
2007-12-01 01:32:56 +00:00
if(snaptogrid != General.MainWindow.ShiftState ^ General.MainWindow.SnapToGrid) Update();
2007-11-15 23:38:09 +00:00
if(snaptonearest != General.MainWindow.CtrlState) Update();
2007-11-14 22:53:48 +00:00
}
2007-11-10 01:58:08 +00:00
2007-11-07 21:14:27 +00:00
#endregion
}
}