1
0
mirror of https://github.com/ppy/osu.git synced 2025-02-22 06:02:54 +08:00

Refactor and rename getMinSliderMargin to getSliderBoundingBox.

This commit is contained in:
Henry Lin 2021-07-01 10:06:14 +08:00
parent ac7b9b9663
commit 3c1f0452a2

View File

@ -4,7 +4,7 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using osu.Framework.Graphics; using osu.Framework.Graphics.Primitives;
using osu.Framework.Utils; using osu.Framework.Utils;
using osu.Game.Beatmaps; using osu.Game.Beatmaps;
using osu.Game.Rulesets.Mods; using osu.Game.Rulesets.Mods;
@ -148,19 +148,14 @@ namespace osu.Game.Rulesets.Osu.Mods
/// <returns>The <see cref="Vector2"/> that this slider has been shifted by.</returns> /// <returns>The <see cref="Vector2"/> that this slider has been shifted by.</returns>
private Vector2 moveSliderIntoPlayfield(Slider slider, RandomObjectInfo currentObjectInfo) private Vector2 moveSliderIntoPlayfield(Slider slider, RandomObjectInfo currentObjectInfo)
{ {
var minMargin = getMinSliderMargin(slider); var minMargin = getSliderBoundingBox(slider);
var prevPosition = slider.Position; var prevPosition = slider.Position;
var newX = minMargin.Left + minMargin.Right > OsuPlayfield.BASE_SIZE.X slider.Position = new Vector2(
? currentObjectInfo.PositionOriginal.X Math.Clamp(slider.Position.X, minMargin.Left, minMargin.Right),
: Math.Clamp(slider.Position.X, minMargin.Left, OsuPlayfield.BASE_SIZE.X - minMargin.Right); Math.Clamp(slider.Position.Y, minMargin.Top, minMargin.Bottom)
);
var newY = minMargin.Top + minMargin.Bottom > OsuPlayfield.BASE_SIZE.Y
? currentObjectInfo.PositionOriginal.Y
: Math.Clamp(slider.Position.Y, minMargin.Top, OsuPlayfield.BASE_SIZE.Y - minMargin.Bottom);
slider.Position = new Vector2(newX, newY);
currentObjectInfo.PositionRandomised = slider.Position; currentObjectInfo.PositionRandomised = slider.Position;
currentObjectInfo.EndPositionRandomised = slider.EndPosition; currentObjectInfo.EndPositionRandomised = slider.EndPosition;
@ -190,34 +185,44 @@ namespace osu.Game.Rulesets.Osu.Mods
} }
/// <summary> /// <summary>
/// Calculates the min. distances from the <see cref="Slider"/>'s position to the playfield border for the slider to be fully inside of the playfield. /// Calculates the bounding box of a <see cref="Slider"/>'s position for the slider to be fully inside of the playfield.
/// </summary> /// </summary>
private MarginPadding getMinSliderMargin(Slider slider) private RectangleF getSliderBoundingBox(Slider slider)
{ {
var pathPositions = new List<Vector2>(); var pathPositions = new List<Vector2>();
slider.Path.GetPathToProgress(pathPositions, 0, 1); slider.Path.GetPathToProgress(pathPositions, 0, 1);
var minMargin = new MarginPadding(); var box = new RectangleF();
foreach (var pos in pathPositions) foreach (var pos in pathPositions)
{ {
minMargin.Left = Math.Max(minMargin.Left, -pos.X); box.X = Math.Max(box.X, -pos.X);
minMargin.Right = Math.Max(minMargin.Right, pos.X); box.Y = Math.Max(box.Y, -pos.Y);
minMargin.Top = Math.Max(minMargin.Top, -pos.Y); box.Width = Math.Min(box.Width, OsuPlayfield.BASE_SIZE.X - pos.X - box.X);
minMargin.Bottom = Math.Max(minMargin.Bottom, pos.Y); box.Height = Math.Min(box.Height, OsuPlayfield.BASE_SIZE.Y - pos.Y - box.Y);
} }
minMargin.Left = Math.Min(minMargin.Left, OsuPlayfield.BASE_SIZE.X - minMargin.Right);
minMargin.Top = Math.Min(minMargin.Top, OsuPlayfield.BASE_SIZE.Y - minMargin.Bottom);
var radius = (float)slider.Radius; var radius = (float)slider.Radius;
minMargin.Left += radius; box.X += radius;
minMargin.Right += radius; box.Y += radius;
minMargin.Top += radius; box.Width -= radius * 2;
minMargin.Bottom += radius; box.Height -= radius * 2;
return minMargin; // If the slider is larger than the playfield, force the slider to stay at its original position
if (box.Width < 0)
{
box.Width = 0;
box.X = slider.Position.X;
}
if (box.Height < 0)
{
box.Height = 0;
box.Y = slider.Position.Y;
}
return box;
} }
/// <summary> /// <summary>