UltimateZoneBuilder/Source/Editing/DragVerticesMode.cs

283 lines
6.5 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;
#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;
// Options
private bool snaptogrid;
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-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
oldpositions = new List<Vector2D>(General.Map.Selection.Vertices.Count);
foreach(Vertex v in General.Map.Selection.Vertices) 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-10 19:24:52 +00:00
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-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
private bool MoveGeometryRelative(Vector2D offset, bool snap)
2007-11-10 01:58:08 +00:00
{
2007-11-13 09:06:15 +00:00
Vector2D oldpos = dragitem.Position;
2007-11-10 19:24:52 +00:00
int i = 0;
2007-11-12 22:43:01 +00:00
2007-11-13 09:06:15 +00:00
// Snap to grid?
if(snap)
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?
if(!snap || (dragitem.Position != oldpos))
{
// Move selected geometry
foreach(Vertex v in General.Map.Selection.Vertices)
{
// Move vertex from old position relative to the mouse position change since drag start
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-13 09:06:15 +00:00
MoveGeometryRelative(new Vector2D(0f, 0f), false);
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-11-10 19:24:52 +00:00
base.Disengage();
2007-11-14 22:53:48 +00:00
Cursor.Current = Cursors.WaitCursor;
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-13 09:06:15 +00:00
MoveGeometryRelative(new Vector2D(0f, 0f), false);
2007-11-13 06:49:13 +00:00
// Make undo
General.Map.UndoRedo.CreateUndo("drag vertices", UndoGroup.None, 0, false);
// Move selected geometry to final position
2007-11-13 09:06:15 +00:00
MoveGeometryRelative(mousemappos - dragstartmappos, snaptogrid);
2007-11-13 06:49:13 +00:00
2007-11-10 01:58:08 +00:00
2007-11-13 06:49:13 +00:00
// TODO: Merge geometry
// 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-10 19:24:52 +00:00
// Start with a clear display
if(renderer.StartRendering(true, true))
{
// Render things
renderer.SetThingsRenderOrder(false);
renderer.RenderThingSet(General.Map.Map.Things);
// Render lines and vertices
renderer.RenderLinedefSet(General.Map.Map.Linedefs);
renderer.RenderVerticesSet(General.Map.Map.Vertices);
// Done
renderer.FinishRendering();
}
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-11-13 09:06:15 +00:00
snaptogrid = !General.MainWindow.ShiftState;
2007-11-14 22:53:48 +00:00
2007-11-10 19:24:52 +00:00
// Move selected geometry
2007-11-13 09:06:15 +00:00
if(MoveGeometryRelative(mousemappos - dragstartmappos, snaptogrid))
{
// 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);
if(snaptogrid != !General.MainWindow.ShiftState) Update();
}
// When a key is pressed
public override void KeyDown(KeyEventArgs e)
{
base.KeyDown(e);
if(snaptogrid != !General.MainWindow.ShiftState) Update();
}
2007-11-10 01:58:08 +00:00
2007-11-07 21:14:27 +00:00
#endregion
}
}