UltimateZoneBuilder/Source/BuilderModes/VerticesMode/DragVerticesMode.cs

388 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;
2008-01-02 21:49:43 +00:00
using CodeImp.DoomBuilder.Editing;
2007-11-07 21:14:27 +00:00
#endregion
2008-01-02 21:49:43 +00:00
namespace CodeImp.DoomBuilder.BuilderModes.Editing
2007-11-07 21:14:27 +00:00
{
2008-01-04 00:16:58 +00:00
// No action or button for this mode, it is automatic.
// The EditMode attribute does not have to be specified unless the
// mode must be activated by class name rather than direct instance.
// In that case, just specifying the attribute like this is enough:
2008-01-02 21:49:43 +00:00
[EditMode]
2008-01-04 00:16:58 +00:00
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-12-26 00:31:32 +00:00
// Mode to return to
private EditMode basemode;
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
// Keep track of view changes
private float lastoffsetx;
private float lastoffsety;
private float lastscale;
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
2008-01-02 21:49:43 +00:00
// Just keep the vertices mode button checked
public override string EditModeButtonName { get { return typeof(VerticesMode).Name; } }
2007-11-07 21:14:27 +00:00
#endregion
#region ================== Constructor / Disposer
// Constructor to start dragging immediately
2007-12-26 00:31:32 +00:00
public DragVerticesMode(EditMode basemode, 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-12-26 00:31:32 +00:00
this.basemode = basemode;
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
// Keep view information
lastoffsetx = renderer.OffsetX;
lastoffsety = renderer.OffsetY;
lastscale = renderer.Scale;
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);
}
2008-01-02 21:49:43 +00:00
// Disposer
2007-11-07 21:14:27 +00:00
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
2007-12-26 00:31:32 +00:00
General.Map.ChangeMode(basemode);
2007-11-10 01:58:08 +00:00
}
2007-11-10 19:24:52 +00:00
// Mode engages
public override void Engage()
{
base.Engage();
}
2007-11-10 01:58:08 +00:00
// Disenagaging
public override void Disengage()
{
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-26 00:31:32 +00:00
// Stitch geometry
General.Map.Map.StitchGeometry(selectedverts, unselectedverts);
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-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
2008-01-02 21:49:43 +00:00
General.Interface.HideInfo();
2007-11-10 19:24:52 +00:00
2008-01-02 21:49:43 +00:00
// Done
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
{
bool viewchanged = false;
// View changed?
if(renderer.OffsetX != lastoffsetx) viewchanged = true;
if(renderer.OffsetY != lastoffsety) viewchanged = true;
if(renderer.Scale != lastscale) viewchanged = true;
2007-11-21 12:50:56 +00:00
// Start rendering
if(renderer.Start(true, viewchanged))
2007-11-10 19:24:52 +00:00
{
2008-01-13 21:23:59 +00:00
// Uncomment this to see triangulation
/*
foreach(Sector s in General.Map.Map.Sectors)
{
for(int i = 0; i < s.Triangles.Count; i += 3)
{
renderer.RenderLine(s.Triangles[i + 0], s.Triangles[i + 1], General.Colors.Selection);
renderer.RenderLine(s.Triangles[i + 1], s.Triangles[i + 2], General.Colors.Selection);
renderer.RenderLine(s.Triangles[i + 2], s.Triangles[i + 0], General.Colors.Selection);
}
}
*/
// Redraw things when view changed
if(viewchanged)
{
renderer.SetThingsRenderOrder(false);
renderer.RenderThingSet(General.Map.Map.Things);
// Keep view information
lastoffsetx = renderer.OffsetX;
lastoffsety = renderer.OffsetY;
lastscale = renderer.Scale;
}
2008-01-13 21:23:59 +00:00
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
{
2008-01-02 21:49:43 +00:00
snaptogrid = General.Interface.ShiftState ^ General.Interface.SnapToGrid;
snaptonearest = General.Interface.CtrlState;
2007-11-15 23:38:09 +00:00
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
2008-01-13 21:23:59 +00:00
//General.Map.Map.Update(true, false);
2007-11-13 09:06:15 +00:00
General.Map.Map.Update();
2007-11-10 19:24:52 +00:00
2007-11-13 09:06:15 +00:00
// Redraw
2008-01-02 21:49:43 +00:00
General.Interface.RedrawDisplay();
2007-11-13 09:06:15 +00:00
}
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.
2007-12-26 00:31:32 +00:00
General.Map.ChangeMode(basemode);
2007-11-10 01:58:08 +00:00
}
}
2007-11-14 22:53:48 +00:00
// When a key is released
public override void KeyUp(KeyEventArgs e)
{
base.KeyUp(e);
2008-01-02 21:49:43 +00:00
if(snaptogrid != General.Interface.ShiftState ^ General.Interface.SnapToGrid) Update();
if(snaptonearest != General.Interface.CtrlState) Update();
2007-11-14 22:53:48 +00:00
}
// When a key is pressed
public override void KeyDown(KeyEventArgs e)
{
base.KeyDown(e);
2008-01-02 21:49:43 +00:00
if(snaptogrid != General.Interface.ShiftState ^ General.Interface.SnapToGrid) Update();
if(snaptonearest != General.Interface.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
}
}