private void OnAnimationTick(object sender, EventArgs e)
{
// Limit check the animation offset, incase the limits have changed
_animationOffset.X = Math.Min(Math.Max(_animationOffset.X, _limit.X), 0);
_animationOffset.Y = Math.Min(Math.Max(_animationOffset.Y, _limit.Y), 0);
// Find distance half way to the destination
int distanceX = (_animationOffset.X - _offset.X) / 2;
int distanceY = (_animationOffset.Y - _offset.Y) / 2;
// Enfore a minimum distance to move towards destination in order
// to prevent small moves at the end of the animation duration
if (_animationOffset.X < _offset.X)
{
distanceX = Math.Min(distanceX, -_animationMinimum);
_offset.X = Math.Max(_animationOffset.X, _offset.X + distanceX);
}
else
{
distanceX = Math.Max(distanceX, _animationMinimum);
_offset.X = Math.Min(_animationOffset.X, _offset.X + distanceX);
}
if (_animationOffset.Y < _offset.Y)
{
distanceY = Math.Min(distanceY, -_animationMinimum);
_offset.Y = Math.Max(_animationOffset.Y, _offset.Y + distanceY);
}
else
{
distanceY = Math.Max(distanceY, _animationMinimum);
_offset.Y = Math.Min(_animationOffset.Y, _offset.Y + distanceY);
}
// If new offset is same as the target
if ((_offset.X == _animationOffset.X) &&
(_offset.Y == _animationOffset.Y))
{
// Then all done, cancel the timer
_animationTimer.Stop();
}
// Enfore limits against the offset
_offset.X = Math.Min(Math.Max(_offset.X, _limit.X), 0);
_offset.Y = Math.Min(Math.Max(_offset.Y, _limit.Y), 0);
// Request the layout and paint to reflect change
if (AnimateStep != null)
AnimateStep.Invoke(this, EventArgs.Empty);
}