diff --git a/NHSE.WinForms/Subforms/TerrainEditor.cs b/NHSE.WinForms/Subforms/TerrainEditor.cs index 3e8d1c0..6975e44 100644 --- a/NHSE.WinForms/Subforms/TerrainEditor.cs +++ b/NHSE.WinForms/Subforms/TerrainEditor.cs @@ -338,7 +338,7 @@ private void ClickMapAt(MouseEventArgs e, bool skipLagCheck) int mX = e.X; int mY = e.Y; bool centerReticle = CHK_SnapToAcre.Checked; - GetCoordinates(centerReticle, mX, mY, out var x, out var y); + GetViewAnchorCoordinates(mX, mY, out var x, out var y, centerReticle); var acre = TerrainManager.GetAcre(x, y); bool sameAcre = AcreIndex == acre; @@ -369,14 +369,33 @@ private void ClickMapAt(MouseEventArgs e, bool skipLagCheck) CB_Acre.SelectedIndex = acre; } - private static void GetCoordinates(bool centerReticle, int mX, int mY, out int x, out int y) + private static void GetCursorCoordinates(int mX, int mY, out int x, out int y) { - int center = centerReticle ? 0 : GridWidth / 2; + x = mX / MapScale; + y = mY / MapScale; + } + + private static void GetViewAnchorCoordinates(int mX, int mY, out int x, out int y, bool centerReticle) + { + GetCursorCoordinates(mX, mY, out x, out y); + + // Clamp to viewport dimensions, and center to nearest acre if desired. const int maxX = ((TerrainManager.AcreWidth - 1) * GridWidth); const int maxY = ((TerrainManager.AcreHeight - 1) * GridHeight); - x = Math.Max(0, Math.Min((mX / MapScale) - center, maxX)); - y = Math.Max(0, Math.Min((mY / MapScale) - center, maxY)); + // If we aren't snapping the reticle to the nearest acre + // we want to put the middle of the reticle rectangle where the cursor is. + // Adjust the view coordinate + if (!centerReticle) + { + // Reticle size is GridWidth, center = /2 + x -= GridWidth / 2; + y -= GridWidth / 2; + } + + // Clamp to boundaries so that we always have 16x16 to view. + x = Math.Max(0, Math.Min(x, maxX)); + y = Math.Max(0, Math.Min(y, maxY)); } private void PB_Map_MouseMove(object sender, MouseEventArgs e) @@ -389,7 +408,7 @@ private void PB_Map_MouseMove(object sender, MouseEventArgs e) { int mX = e.X; int mY = e.Y; - GetCoordinates(false, mX, mY, out var x, out var y); + GetCursorCoordinates(mX, mY, out var x, out var y); L_Coordinates.Text = $"({x:000},{y:000}) = (0x{x:X2},0x{y:X2})"; } }