From 9733b3ea13e2618fe43f6bc4889fed3c2e1c26f9 Mon Sep 17 00:00:00 2001 From: codeimp Date: Thu, 15 Nov 2007 23:38:09 +0000 Subject: [PATCH] --- Source/Editing/ClassicMode.cs | 17 +++++++++ Source/Editing/DragVerticesMode.cs | 59 ++++++++++++++++++++++++------ Source/Editing/VerticesMode.cs | 2 +- Source/Interface/MainForm.cs | 2 + 4 files changed, 68 insertions(+), 12 deletions(-) diff --git a/Source/Editing/ClassicMode.cs b/Source/Editing/ClassicMode.cs index c8cb42f3..69070395 100644 --- a/Source/Editing/ClassicMode.cs +++ b/Source/Editing/ClassicMode.cs @@ -147,6 +147,9 @@ namespace CodeImp.DoomBuilder.Editing { // Scroll now renderer.PositionView(renderer.OffsetX + deltax, renderer.OffsetY + deltay); + this.ViewChanged(); + + // Redraw General.MainWindow.RedrawDisplay(); // Determine new unprojected mouse coordinates @@ -189,6 +192,9 @@ namespace CodeImp.DoomBuilder.Editing // Zoom now renderer.PositionView(renderer.OffsetX - diff.x, renderer.OffsetY + diff.y); renderer.ScaleView(newscale); + this.ViewChanged(); + + // Redraw //General.Map.Map.Update(); General.MainWindow.RedrawDisplay(); @@ -201,6 +207,9 @@ namespace CodeImp.DoomBuilder.Editing { // Zoom now renderer.ScaleView(newscale); + this.ViewChanged(); + + // Redraw //General.Map.Map.Update(); General.MainWindow.RedrawDisplay(); @@ -240,12 +249,20 @@ namespace CodeImp.DoomBuilder.Editing // Change the view to see the whole map renderer.ScaleView(scale); renderer.PositionView(left + (right - left) * 0.5f, top + (bottom - top) * 0.5f); + this.ViewChanged(); + + // Redraw //General.Map.Map.Update(); General.MainWindow.RedrawDisplay(); // Give a new mousemove event to update coordinates if(mouseinside) MouseMove(new MouseEventArgs(mousebuttons, 0, (int)mousepos.x, (int)mousepos.y, 0)); } + + // This is called when the view changes (scroll/zoom) + protected virtual void ViewChanged() + { + } #endregion diff --git a/Source/Editing/DragVerticesMode.cs b/Source/Editing/DragVerticesMode.cs index 12c2513d..7885aef2 100644 --- a/Source/Editing/DragVerticesMode.cs +++ b/Source/Editing/DragVerticesMode.cs @@ -56,8 +56,12 @@ namespace CodeImp.DoomBuilder.Editing // List of old vertex positions private List oldpositions; + // List of non-selected items + private List others; + // Options - private bool snaptogrid; + private bool snaptogrid; // SHIFT to disable + private bool snaptonearest; // CTRL to enable #endregion @@ -81,6 +85,11 @@ namespace CodeImp.DoomBuilder.Editing // Also keep old position of the dragged item dragitemposition = dragitem.Position; + + // Make list of non-selected vertices + // This will be used for snapping to nearest items + others = new List(General.Map.Map.Vertices.Count); + foreach(Vertex v in General.Map.Map.Vertices) if(v.Selected == 0) others.Add(v); // We have no destructor GC.SuppressFinalize(this); @@ -102,16 +111,35 @@ namespace CodeImp.DoomBuilder.Editing #endregion #region ================== Methods - + // This moves the selected geometry relatively // Returns true when geometry has actually moved - private bool MoveGeometryRelative(Vector2D offset, bool snap) + private bool MoveGeometryRelative(Vector2D offset, bool snapgrid, bool snapnearest) { Vector2D oldpos = dragitem.Position; + Vertex nearest; int i = 0; + // Snap to nearest? + if(snapnearest) + { + // Find nearest unselected item within selection range + nearest = MapSet.NearestVertexSquareRange(others, mousemappos, VerticesMode.VERTEX_HIGHLIGHT_RANGE / renderer.Scale); + 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; + } + } + // Snap to grid? - if(snap) + if(snapgrid) { // Move the dragged item dragitem.Move(dragitemposition + offset); @@ -124,12 +152,13 @@ namespace CodeImp.DoomBuilder.Editing } // Drag item moved? - if(!snap || (dragitem.Position != oldpos)) + if(!snapgrid || (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 + // Move vertex from old position relative to the + // mouse position change since drag start v.Move(oldpositions[i] + offset); // Next @@ -150,7 +179,7 @@ namespace CodeImp.DoomBuilder.Editing public override void Cancel() { // Move geometry back to original position - MoveGeometryRelative(new Vector2D(0f, 0f), false); + MoveGeometryRelative(new Vector2D(0f, 0f), false, false); // Update cached values General.Map.Map.Update(); @@ -181,13 +210,13 @@ namespace CodeImp.DoomBuilder.Editing if(!cancelled) { // Move geometry back to original position - MoveGeometryRelative(new Vector2D(0f, 0f), false); + MoveGeometryRelative(new Vector2D(0f, 0f), false, false); // Make undo General.Map.UndoRedo.CreateUndo("drag vertices", UndoGroup.None, 0, false); // Move selected geometry to final position - MoveGeometryRelative(mousemappos - dragstartmappos, snaptogrid); + MoveGeometryRelative(mousemappos - dragstartmappos, snaptogrid, snaptonearest); // TODO: Merge geometry @@ -222,6 +251,11 @@ namespace CodeImp.DoomBuilder.Editing renderer.RenderLinedefSet(General.Map.Map.Linedefs); renderer.RenderVerticesSet(General.Map.Map.Vertices); + // 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); + // Done renderer.FinishRendering(); } @@ -231,9 +265,10 @@ namespace CodeImp.DoomBuilder.Editing private void Update() { snaptogrid = !General.MainWindow.ShiftState; - + snaptonearest = General.MainWindow.CtrlState; + // Move selected geometry - if(MoveGeometryRelative(mousemappos - dragstartmappos, snaptogrid)) + if(MoveGeometryRelative(mousemappos - dragstartmappos, snaptogrid, snaptonearest)) { // Update cached values General.Map.Map.Update(); @@ -268,6 +303,7 @@ namespace CodeImp.DoomBuilder.Editing { base.KeyUp(e); if(snaptogrid != !General.MainWindow.ShiftState) Update(); + if(snaptonearest != General.MainWindow.CtrlState) Update(); } // When a key is pressed @@ -275,6 +311,7 @@ namespace CodeImp.DoomBuilder.Editing { base.KeyDown(e); if(snaptogrid != !General.MainWindow.ShiftState) Update(); + if(snaptonearest != General.MainWindow.CtrlState) Update(); } #endregion diff --git a/Source/Editing/VerticesMode.cs b/Source/Editing/VerticesMode.cs index dcfd18bf..348aad6a 100644 --- a/Source/Editing/VerticesMode.cs +++ b/Source/Editing/VerticesMode.cs @@ -38,7 +38,7 @@ namespace CodeImp.DoomBuilder.Editing { #region ================== Constants - protected const float VERTEX_HIGHLIGHT_RANGE = 20f; + public const float VERTEX_HIGHLIGHT_RANGE = 20f; #endregion diff --git a/Source/Interface/MainForm.cs b/Source/Interface/MainForm.cs index 256ce33c..6a3a53f5 100644 --- a/Source/Interface/MainForm.cs +++ b/Source/Interface/MainForm.cs @@ -587,12 +587,14 @@ namespace CodeImp.DoomBuilder.Interface if(e.Delta > 0) { // Invoke actions for scrollwheel + //for(int i = 0; i < e.Delta; i += 120) General.Actions.InvokeByKey(mod | (int)SpecialKeys.MScrollUp); } // Scrollwheel down? else if(e.Delta < 0) { // Invoke actions for scrollwheel + //for(int i = 0; i > e.Delta; i -= 120) General.Actions.InvokeByKey(mod | (int)SpecialKeys.MScrollDown); }