Use lookup direction than 2 while loops

This commit is contained in:
iiSaLMaN 2019-07-26 05:11:01 +03:00
parent a08d54eb06
commit b4c93b1777

View File

@ -31,7 +31,9 @@ namespace osu.Game.Screens.Play
set set
{ {
breaks = value; breaks = value;
nearestBreakIndex = 0;
// reset index in case the new breaks list is smaller than last one
currentBreakIndex = 0;
initializeBreaks(); initializeBreaks();
} }
@ -44,7 +46,7 @@ namespace osu.Game.Screens.Play
/// </summary> /// </summary>
public IBindable<bool> IsBreakTime => isBreakTime; public IBindable<bool> IsBreakTime => isBreakTime;
private int nearestBreakIndex; private int currentBreakIndex;
private readonly BindableBool isBreakTime = new BindableBool(); private readonly BindableBool isBreakTime = new BindableBool();
private readonly Container remainingTimeAdjustmentBox; private readonly Container remainingTimeAdjustmentBox;
@ -133,20 +135,31 @@ namespace osu.Game.Screens.Play
if (breaks == null || !breaks.Any()) if (breaks == null || !breaks.Any())
{ {
isBreakTime.Value = false; isBreakTime.Value = false;
currentBreakIndex = 0;
return; return;
} }
while (nearestBreakIndex < breaks.Count - 1 && Clock.CurrentTime > breaks[nearestBreakIndex].EndTime) var lastIndex = currentBreakIndex;
nearestBreakIndex++; var lookupDirection = Clock.CurrentTime > breaks[lastIndex].EndTime ? 1 : (Clock.CurrentTime < breaks[lastIndex].StartTime ? -1 : 0);
while (nearestBreakIndex > 0 && Clock.CurrentTime < breaks[nearestBreakIndex].StartTime) while (Clock.CurrentTime < breaks[currentBreakIndex].StartTime || Clock.CurrentTime > breaks[currentBreakIndex].EndTime)
nearestBreakIndex--; {
currentBreakIndex += lookupDirection;
// restore index if out of bounds
if (currentBreakIndex < 0 || currentBreakIndex >= breaks.Count)
{
isBreakTime.Value = false;
currentBreakIndex = lastIndex;
return;
}
}
// This ensures that IsBreakTime is generally consistent with the overlay's transforms during a break. // This ensures that IsBreakTime is generally consistent with the overlay's transforms during a break.
// If the overlay never shows (break.HasEffect is false), IsBreakTime should be false. // If the current break doesn't have effects, IsBreakTime should be false.
// We also assume that the overlay's fade out transform is "not break time". // We also assume that the overlay's fade out transform is "not break time".
var nearestBreak = breaks[nearestBreakIndex]; var currentBreak = breaks[currentBreakIndex];
isBreakTime.Value = nearestBreak.HasEffect && Clock.CurrentTime >= nearestBreak.StartTime && Clock.CurrentTime <= nearestBreak.EndTime - fade_duration; isBreakTime.Value = currentBreak.HasEffect && Clock.CurrentTime <= currentBreak.EndTime - fade_duration;
} }
private void initializeBreaks() private void initializeBreaks()