/// <summary> /// Handles when the mouse cursor moves while this <see cref="ITransBox"/> is selected. /// </summary> /// <param name="cursorWorldPos">The amount the cursor has moved.</param> public void CursorMoved(Vector2 cursorWorldPos) { _position = cursorWorldPos - (Size / 2f); var delta = cursorWorldPos - _selectPos; // Handle move, which is easy enough if (_type == TransBoxType.Move) { _spatial.TryMove(Align(_spatialInitPos + delta)); return; } // Handle resizing, which is the harder part var resizeVector = GetResizeVector(_type); var newSize = _spatialInitSize + (delta * resizeVector); newSize = Align(_spatialInitPos + newSize) - _spatialInitPos; newSize = Vector2.Max(Vector2.One, newSize); var sizeDelta = newSize - _spatialInitSize; var moveVector = sizeDelta * Vector2.Min(resizeVector, Vector2.Zero); var newPos = _spatialInitPos + moveVector; // When resizing with a negative vector (so resizing to the left or up), offset the position so that the // right/bottom side remains in the same place. If the resize vector is 0, ensure it does not resize. var alignPos = Align(newPos); if (resizeVector.X < 0) { newSize.X += newPos.X - alignPos.X; newPos.X = alignPos.X; } else if (resizeVector.X == 0) { newSize.X = _spatialInitSize.X; } if (resizeVector.Y < 0) { newSize.Y += newPos.Y - alignPos.Y; newPos.Y = alignPos.Y; } else if (resizeVector.Y == 0) { newSize.Y = _spatialInitSize.Y; } // Apply the new size and position if (!_spatial.TryResize(newSize)) { return; } _spatial.TryMove(newPos); }
/// <summary> /// Fits an <see cref="ISpatial"/> into the grid so that all sides are snapped to the grid. /// The <paramref name="spatial"/> must support both <see cref="ISpatial.SupportsMove"/> and /// <see cref="ISpatial.SupportsResize"/>. /// </summary> /// <param name="forceAlign">When true, aligning to the grid will be forced.</param> /// <param name="spatial">The <see cref="ISpatial"/>.</param> public void Fit(ISpatial spatial, bool forceAlign = false) { if (spatial == null) { return; } if (!WillAlign && !forceAlign) { return; } if (!spatial.SupportsMove || !spatial.SupportsResize) { return; } var pos = Align(spatial, forceAlign); if (!spatial.TryMove(pos)) { return; } var size = Resize(spatial, forceAlign); spatial.TryResize(size); }
/// <summary> /// Fits an <see cref="ISpatial"/> into the grid so that all sides are snapped to the grid. /// The <paramref name="spatial"/> must support both <see cref="ISpatial.SupportsMove"/> and /// <see cref="ISpatial.SupportsResize"/>. /// </summary> /// <param name="forceAlign">When true, aligning to the grid will be forced.</param> /// <param name="spatial">The <see cref="ISpatial"/>.</param> public void Fit(ISpatial spatial, bool forceAlign = false) { if (spatial == null) return; if (!WillAlign && !forceAlign) return; if (!spatial.SupportsMove || !spatial.SupportsResize) return; var pos = Align(spatial, forceAlign); if (!spatial.TryMove(pos)) return; var size = Resize(spatial, forceAlign); spatial.TryResize(size); }