1
0
mirror of https://github.com/ppy/osu.git synced 2025-01-28 16:52:55 +08:00

Remove re-instantiation of clock in EditorClockTestCase

This commit is contained in:
smoogipoo 2018-04-06 18:38:44 +09:00
parent b238130fe4
commit e59124962c
2 changed files with 31 additions and 26 deletions

View File

@ -12,18 +12,20 @@ namespace osu.Game.Screens.Edit
{ {
public class EditorClock : DecoupleableInterpolatingFramedClock public class EditorClock : DecoupleableInterpolatingFramedClock
{ {
private readonly ControlPointInfo controlPointInfo; public ControlPointInfo ControlPointInfo;
private readonly BindableBeatDivisor beatDivisor; private readonly BindableBeatDivisor beatDivisor;
public EditorClock(ControlPointInfo controlPointInfo, BindableBeatDivisor beatDivisor) public EditorClock(ControlPointInfo controlPointInfo, BindableBeatDivisor beatDivisor)
{ {
this.controlPointInfo = controlPointInfo;
this.beatDivisor = beatDivisor; this.beatDivisor = beatDivisor;
ControlPointInfo = controlPointInfo;
} }
public bool SeekSnapped(double position) public bool SeekSnapped(double position)
{ {
var timingPoint = controlPointInfo.TimingPointAt(position); var timingPoint = ControlPointInfo.TimingPointAt(position);
double beatSnapLength = timingPoint.BeatLength / beatDivisor; double beatSnapLength = timingPoint.BeatLength / beatDivisor;
// We will be snapping to beats within the timing point // We will be snapping to beats within the timing point
@ -35,7 +37,7 @@ namespace osu.Game.Screens.Edit
// Depending on beatSnapLength, we may snap to a beat that is beyond timingPoint's end time, but we want to instead snap to // Depending on beatSnapLength, we may snap to a beat that is beyond timingPoint's end time, but we want to instead snap to
// the next timing point's start time // the next timing point's start time
var nextTimingPoint = controlPointInfo.TimingPoints.FirstOrDefault(t => t.Time > timingPoint.Time); var nextTimingPoint = ControlPointInfo.TimingPoints.FirstOrDefault(t => t.Time > timingPoint.Time);
if (position > nextTimingPoint?.Time) if (position > nextTimingPoint?.Time)
position = nextTimingPoint.Time; position = nextTimingPoint.Time;
@ -56,19 +58,19 @@ namespace osu.Game.Screens.Edit
private void seek(int direction, bool snapped) private void seek(int direction, bool snapped)
{ {
var timingPoint = controlPointInfo.TimingPointAt(CurrentTime); var timingPoint = ControlPointInfo.TimingPointAt(CurrentTime);
if (direction < 0 && timingPoint.Time == CurrentTime) if (direction < 0 && timingPoint.Time == CurrentTime)
{ {
// When going backwards and we're at the boundary of two timing points, we compute the seek distance with the timing point which we are seeking into // When going backwards and we're at the boundary of two timing points, we compute the seek distance with the timing point which we are seeking into
int activeIndex = controlPointInfo.TimingPoints.IndexOf(timingPoint); int activeIndex = ControlPointInfo.TimingPoints.IndexOf(timingPoint);
while (activeIndex > 0 && CurrentTime == timingPoint.Time) while (activeIndex > 0 && CurrentTime == timingPoint.Time)
timingPoint = controlPointInfo.TimingPoints[--activeIndex]; timingPoint = ControlPointInfo.TimingPoints[--activeIndex];
} }
double seekAmount = timingPoint.BeatLength / beatDivisor; double seekAmount = timingPoint.BeatLength / beatDivisor;
double seekTime = CurrentTime + seekAmount * direction; double seekTime = CurrentTime + seekAmount * direction;
if (!snapped || controlPointInfo.TimingPoints.Count == 0) if (!snapped || ControlPointInfo.TimingPoints.Count == 0)
{ {
Seek(seekTime); Seek(seekTime);
return; return;
@ -94,10 +96,10 @@ namespace osu.Game.Screens.Edit
seekTime = timingPoint.Time + closestBeat * seekAmount; seekTime = timingPoint.Time + closestBeat * seekAmount;
} }
if (seekTime < timingPoint.Time && timingPoint != controlPointInfo.TimingPoints.First()) if (seekTime < timingPoint.Time && timingPoint != ControlPointInfo.TimingPoints.First())
seekTime = timingPoint.Time; seekTime = timingPoint.Time;
var nextTimingPoint = controlPointInfo.TimingPoints.FirstOrDefault(t => t.Time > timingPoint.Time); var nextTimingPoint = ControlPointInfo.TimingPoints.FirstOrDefault(t => t.Time > timingPoint.Time);
if (seekTime > nextTimingPoint?.Time) if (seekTime > nextTimingPoint?.Time)
seekTime = nextTimingPoint.Time; seekTime = nextTimingPoint.Time;

View File

@ -5,6 +5,7 @@ using osu.Framework.Allocation;
using osu.Framework.Input; using osu.Framework.Input;
using osu.Framework.Timing; using osu.Framework.Timing;
using osu.Game.Beatmaps; using osu.Game.Beatmaps;
using osu.Game.Beatmaps.ControlPoints;
using osu.Game.Screens.Edit; using osu.Game.Screens.Edit;
using osu.Game.Screens.Edit.Screens.Compose; using osu.Game.Screens.Edit.Screens.Compose;
@ -17,7 +18,7 @@ namespace osu.Game.Tests.Visual
public abstract class EditorClockTestCase : OsuTestCase public abstract class EditorClockTestCase : OsuTestCase
{ {
protected readonly BindableBeatDivisor BeatDivisor = new BindableBeatDivisor(); protected readonly BindableBeatDivisor BeatDivisor = new BindableBeatDivisor();
protected EditorClock Clock { get; private set; } protected readonly EditorClock Clock;
private DependencyContainer dependencies; private DependencyContainer dependencies;
@ -26,31 +27,26 @@ namespace osu.Game.Tests.Visual
private OsuGameBase osuGame; private OsuGameBase osuGame;
protected EditorClockTestCase()
{
Clock = new EditorClock(new ControlPointInfo(), BeatDivisor) { IsCoupled = false };
}
[BackgroundDependencyLoader] [BackgroundDependencyLoader]
private void load(OsuGameBase osuGame) private void load(OsuGameBase osuGame)
{ {
this.osuGame = osuGame; this.osuGame = osuGame;
dependencies.Cache(BeatDivisor); dependencies.Cache(BeatDivisor);
osuGame.Beatmap.ValueChanged += reinitializeClock;
reinitializeClock(osuGame.Beatmap.Value);
}
protected override void Dispose(bool isDisposing)
{
osuGame.Beatmap.ValueChanged -= reinitializeClock;
base.Dispose(isDisposing);
}
private void reinitializeClock(WorkingBeatmap working)
{
Clock = new EditorClock(working.Beatmap.ControlPointInfo, BeatDivisor) { IsCoupled = false };
dependencies.CacheAs<IFrameBasedClock>(Clock); dependencies.CacheAs<IFrameBasedClock>(Clock);
dependencies.CacheAs<IAdjustableClock>(Clock); dependencies.CacheAs<IAdjustableClock>(Clock);
osuGame.Beatmap.ValueChanged += beatmapChanged;
beatmapChanged(osuGame.Beatmap.Value);
} }
private void beatmapChanged(WorkingBeatmap working) => Clock.ControlPointInfo = working.Beatmap.ControlPointInfo;
protected override bool OnWheel(InputState state) protected override bool OnWheel(InputState state)
{ {
if (state.Mouse.WheelDelta > 0) if (state.Mouse.WheelDelta > 0)
@ -60,5 +56,12 @@ namespace osu.Game.Tests.Visual
return true; return true;
} }
protected override void Dispose(bool isDisposing)
{
osuGame.Beatmap.ValueChanged -= beatmapChanged;
base.Dispose(isDisposing);
}
} }
} }