public ClockState(ClockProgressState progressState, double progress, double iteration, TimeSpan previousTick, TimeSpan nextTick) { this.ProgressState = progressState; this.Progress = progress; this.Iteration = iteration; this.PreviousTick = previousTick; this.NextTick = nextTick; }
public ClockState Tick(TimeSpan time) { double iteration = time.Divide(iterationDuration); ClockProgressState progressState = time < FirstTick ? ClockProgressState.BeforeStarted : (time >= LastTick ? ClockProgressState.AfterEnded : ClockProgressState.Active); iteration = Math.Min(Math.Max(iteration, 0), iterationsCount); double iterationRemainder = iteration - Math.Floor(iteration); TimeSpan currentIterationStart; TimeSpan currentIterationTime; if (progressState == ClockProgressState.AfterEnded && iterationRemainder == 0) { currentIterationStart = iterationDuration.Scale(iteration - 1); currentIterationTime = iterationDuration; } else { currentIterationStart = iterationDuration.Scale(iteration - iterationRemainder); currentIterationTime = iterationDuration.Scale(iterationRemainder); } ClockState state = clock.Tick(currentIterationTime); TimeSpan previousTick; TimeSpan nextTick; if (time < FirstTick) { previousTick = Granular.Compatibility.TimeSpan.MinValue; nextTick = FirstTick; } else if (time >= LastTick) { previousTick = LastTick; nextTick = Granular.Compatibility.TimeSpan.MaxValue; } else { if (currentIterationTime > clock.FirstTick || Math.Floor(iteration) == 0) { previousTick = (currentIterationStart + state.PreviousTick).Max(FirstTick); } else { previousTick = (currentIterationStart - iterationDuration + clock.LastTick).Max(FirstTick); } if (currentIterationTime < clock.LastTick || Math.Floor(iteration) == Math.Floor(iterationsCount)) { nextTick = (currentIterationStart + state.NextTick).Min(LastTick); } else { nextTick = (currentIterationStart + iterationDuration + clock.FirstTick).Min(LastTick); } } if (progressState == ClockProgressState.Active) { progressState = state.ProgressState; } return(new ClockState(progressState, state.Progress, iteration, previousTick, nextTick)); }