1
0
mirror of https://github.com/ppy/osu.git synced 2024-12-14 18:42:56 +08:00

Get destination index using binarysearch

This commit is contained in:
Kelvin 2017-08-29 00:39:17 -07:00
parent 458c3a355f
commit 1eb31afd14

View File

@ -166,7 +166,7 @@ namespace osu.Game.Overlays.Music
protected override bool OnDrag(InputState state)
{
int src = (int)Parent.Depth;
int dst = MathHelper.Clamp((int)((state.Mouse.Position.Y + Parent.Position.Y) / Parent.Height), 0, playlist.Count - 1);
int dst = getIndex(state.Mouse.Position.Y + Parent.Position.Y);
if (src == dst)
return true;
@ -185,6 +185,24 @@ namespace osu.Game.Overlays.Music
playlist.ChangeChildDepth(Parent as PlaylistItem, dst);
return true;
}
private int getIndex(float position) {
IReadOnlyList<PlaylistItem> items = playlist.Children;
// Binary Search without matching exact
int min = 0;
int max = items.Count - 1;
while (min <= max)
{
int m = (min + max) / 2;
if (items[m].Y < position)
min = m + 1;
else if (items[m].Y > position)
max = m - 1;
}
return (int)items[min - 1].Depth;
}
}
}
}