From bd6ce4ae2588b9725b2c6a6e7ee0b8bda2c95ba8 Mon Sep 17 00:00:00 2001 From: Menotdan <32620310+Menotdan@users.noreply.github.com> Date: Sun, 17 Sep 2023 02:03:01 -0400 Subject: [PATCH] Very basic grip snap working. Need to add configuration. --- Source/Editor/Surface/VisjectSurface.Input.cs | 35 +++++++++++++++++-- 1 file changed, 33 insertions(+), 2 deletions(-) diff --git a/Source/Editor/Surface/VisjectSurface.Input.cs b/Source/Editor/Surface/VisjectSurface.Input.cs index 7264321c3..27c8e2a02 100644 --- a/Source/Editor/Surface/VisjectSurface.Input.cs +++ b/Source/Editor/Surface/VisjectSurface.Input.cs @@ -1,5 +1,6 @@ // Copyright (c) 2012-2023 Wojciech Figat. All rights reserved. +using System; using System.Collections.Generic; using System.Linq; using FlaxEditor.Options; @@ -223,16 +224,46 @@ namespace FlaxEditor.Surface // Moving else if (_isMovingSelection) { + bool testOption = true; + float testGridSize = 15f; // Calculate delta (apply view offset) var viewDelta = _rootControl.Location - _movingSelectionViewPos; _movingSelectionViewPos = _rootControl.Location; var delta = location - _leftMouseDownPos - viewDelta; - if (delta.LengthSquared > 0.01f) + var deltaLengthSquared = delta.LengthSquared; + + delta /= _targetScale; + if ((!testOption || Math.Abs(delta.X) >= testGridSize || (Math.Abs(delta.Y) >= testGridSize)) + && deltaLengthSquared > 0.01f) { // Move selected nodes - delta /= _targetScale; + Debug.Log("test " + delta.ToString() + ", " + testGridSize.ToString() + ", " + _targetScale.ToString()); + + + if (testOption) + { + // Round delta to ensure grid snapping. + + Float2 unroundedDelta = delta; + unroundedDelta.X = (float) Math.CopySign(Math.Floor(Math.Abs((double)unroundedDelta.X) / testGridSize) * testGridSize, unroundedDelta.X); + unroundedDelta.Y = (float)Math.CopySign(Math.Floor(Math.Abs((double)unroundedDelta.Y) / testGridSize) * testGridSize, unroundedDelta.Y); + delta = unroundedDelta; + } + foreach (var node in _movingNodes) + { + if (testOption) + { + // Ensure location is snapped if grid snap is on. + + Float2 unroundedLocation = node.Location; + unroundedLocation.X = (float)Math.CopySign(Math.Round(Math.Abs((double)unroundedLocation.X) / testGridSize) * testGridSize, unroundedLocation.X); + unroundedLocation.Y = (float)Math.CopySign(Math.Round(Math.Abs((double)unroundedLocation.Y) / testGridSize) * testGridSize, unroundedLocation.Y); + node.Location = unroundedLocation; + } node.Location += delta; + } + _leftMouseDownPos = location; _movingNodesDelta += delta; Cursor = CursorType.SizeAll;