/// <summary> /// Returns a new value based on the given values. /// Tweens the weight with the set sub curve. </summary> /// <param name="a"> the start value </param> /// <param name="b"> the end value </param> /// <param name="t"> the weight which lies between 0.0 and 1.0 </param> /// <returns> tweened value </returns> public virtual float Tween(float a, float b, float t) { t = TweenSub(0f, 1f, t); switch (Type) { case CurveType.Instant: return(a); case CurveType.Linear: return(Interpolator.Linear(a, b, t)); case CurveType.Quadratic: return(Interpolator.Quadratic(a, Interpolator.Linear(a, b, Constraints.C1), b, t)); case CurveType.Cubic: return(Interpolator.Cubic(a, Interpolator.Linear(a, b, Constraints.C1), Interpolator.Linear(a, b, Constraints.C2), b, t)); case CurveType.Quartic: return(Interpolator.Quartic(a, Interpolator.Linear(a, b, Constraints.C1), Interpolator.Linear(a, b, Constraints.C2), Interpolator.Linear(a, b, Constraints.C3), b, t)); case CurveType.Quintic: return(Interpolator.Quintic(a, Interpolator.Linear(a, b, Constraints.C1), Interpolator.Linear(a, b, Constraints.C2), Interpolator.Linear(a, b, Constraints.C3), Interpolator.Linear(a, b, Constraints.C4), b, t)); case CurveType.Bezier: float?cubicSolution = Calculator.SolveCubic(3f * (Constraints.C1 - Constraints.C3) + 1f, 3f * (Constraints.C3 - 2f * Constraints.C1), 3f * Constraints.C1, -t); if (cubicSolution == null) { cubicSolution = lastCubicSolution; } else { lastCubicSolution = cubicSolution.Value; } return(Interpolator.Linear(a, b, Interpolator.Bezier(cubicSolution.Value, 0f, Constraints.C2, Constraints.C4, 1f))); default: return(Interpolator.Linear(a, b, t)); } }
protected override void Draw(GameTime gameTime) { _gameDrawPerformance.Begin(); GraphicsDevice.Clear(Color.CornflowerBlue); _spriteBatch.Begin(); for (float i = 0.0f; i < 1.0f; i += StepInterval) { _tweenPerformance.Begin(); float x1 = i, x2 = i + StepInterval; Vector2 start = new Vector2(x1 * GraphicsDevice.Viewport.Width, Interpolator.Step(_pStart.Y, _pEnd.Y, x1, 0.5f, true)); Vector2 end = new Vector2(x2 * GraphicsDevice.Viewport.Width, Interpolator.Step(_pStart.Y, _pEnd.Y, x2, 0.5f, true)); _spriteBatch.DrawLine(Color.Blue, start, end, 3); start = new Vector2(x1 * GraphicsDevice.Viewport.Width, Interpolator.Linear(_pStart.Y, _pEnd.Y, x1, 0.5f, true)); end = new Vector2(x2 * GraphicsDevice.Viewport.Width, Interpolator.Linear(_pStart.Y, _pEnd.Y, x2, 0.5f, true)); _spriteBatch.DrawLine(Color.Yellow, start, end, 2); start = new Vector2(x1 * GraphicsDevice.Viewport.Width, Interpolator.Sine(_pStart.Y, _pEnd.Y, x1, 0.25f, true)); end = new Vector2(x2 * GraphicsDevice.Viewport.Width, Interpolator.Sine(_pStart.Y, _pEnd.Y, x2, 0.25f, true)); _spriteBatch.DrawLine(Color.Pink, start, end, 2); start = new Vector2(x1 * GraphicsDevice.Viewport.Width, Interpolator.SineIn(_pStart.Y, _pEnd.Y, x1, 0.5f, true)); end = new Vector2(x2 * GraphicsDevice.Viewport.Width, Interpolator.SineIn(_pStart.Y, _pEnd.Y, x2, 0.5f, true)); _spriteBatch.DrawLine(Color.Green, start, end, 2); start = new Vector2(x1 * GraphicsDevice.Viewport.Width, Interpolator.SineOut(_pStart.Y, _pEnd.Y, x1, 0.5f, true)); end = new Vector2(x2 * GraphicsDevice.Viewport.Width, Interpolator.SineOut(_pStart.Y, _pEnd.Y, x2, 0.5f, true)); _spriteBatch.DrawLine(Color.Red, start, end, 2); start = new Vector2(x1 * GraphicsDevice.Viewport.Width, Interpolator.SineInOut(_pStart.Y, _pEnd.Y, x1, 0.5f, true)); end = new Vector2(x2 * GraphicsDevice.Viewport.Width, Interpolator.SineInOut(_pStart.Y, _pEnd.Y, x2, 0.5f, true)); _spriteBatch.DrawLine(Color.Purple, start, end, 2); start = new Vector2(x1 * GraphicsDevice.Viewport.Width, Interpolator.Quadratic(_pStart.Y, _pEnd.Y, x1, 0.25f, true)); end = new Vector2(x2 * GraphicsDevice.Viewport.Width, Interpolator.Quadratic(_pStart.Y, _pEnd.Y, x2, 0.25f, true)); _spriteBatch.DrawLine(Color.Pink, start, end, 2); start = new Vector2(x1 * GraphicsDevice.Viewport.Width, Interpolator.QuadraticIn(_pStart.Y, _pEnd.Y, x1, 0.5f, true)); end = new Vector2(x2 * GraphicsDevice.Viewport.Width, Interpolator.QuadraticIn(_pStart.Y, _pEnd.Y, x2, 0.5f, true)); _spriteBatch.DrawLine(Color.Green, start, end, 2); start = new Vector2(x1 * GraphicsDevice.Viewport.Width, Interpolator.QuadraticOut(_pStart.Y, _pEnd.Y, x1, 0.5f, true)); end = new Vector2(x2 * GraphicsDevice.Viewport.Width, Interpolator.QuadraticOut(_pStart.Y, _pEnd.Y, x2, 0.5f, true)); _spriteBatch.DrawLine(Color.Red, start, end, 2); start = new Vector2(x1 * GraphicsDevice.Viewport.Width, Interpolator.QuadraticInOut(_pStart.Y, _pEnd.Y, x1, 0.5f, true)); end = new Vector2(x2 * GraphicsDevice.Viewport.Width, Interpolator.QuadraticInOut(_pStart.Y, _pEnd.Y, x2, 0.5f, true)); _spriteBatch.DrawLine(Color.Purple, start, end, 2); start = new Vector2(x1 * GraphicsDevice.Viewport.Width, Interpolator.Cubic(_pStart.Y, _pEnd.Y, x1, 0.25f, true)); end = new Vector2(x2 * GraphicsDevice.Viewport.Width, Interpolator.Cubic(_pStart.Y, _pEnd.Y, x2, 0.25f, true)); _spriteBatch.DrawLine(Color.Pink, start, end, 2); start = new Vector2(x1 * GraphicsDevice.Viewport.Width, Interpolator.CubicIn(_pStart.Y, _pEnd.Y, x1, 0.5f, true)); end = new Vector2(x2 * GraphicsDevice.Viewport.Width, Interpolator.CubicIn(_pStart.Y, _pEnd.Y, x2, 0.5f, true)); _spriteBatch.DrawLine(Color.Green, start, end, 2); start = new Vector2(x1 * GraphicsDevice.Viewport.Width, Interpolator.CubicOut(_pStart.Y, _pEnd.Y, x1, 0.5f, true)); end = new Vector2(x2 * GraphicsDevice.Viewport.Width, Interpolator.CubicOut(_pStart.Y, _pEnd.Y, x2, 0.5f, true)); _spriteBatch.DrawLine(Color.Red, start, end, 2); start = new Vector2(x1 * GraphicsDevice.Viewport.Width, Interpolator.CubicInOut(_pStart.Y, _pEnd.Y, x1, 0.5f, true)); end = new Vector2(x2 * GraphicsDevice.Viewport.Width, Interpolator.CubicInOut(_pStart.Y, _pEnd.Y, x2, 0.5f, true)); _spriteBatch.DrawLine(Color.Purple, start, end, 2); start = new Vector2(x1 * GraphicsDevice.Viewport.Width, Interpolator.Exponential(_pStart.Y, _pEnd.Y, x1, MathHelper.E, 0.25f, true)); end = new Vector2(x2 * GraphicsDevice.Viewport.Width, Interpolator.Exponential(_pStart.Y, _pEnd.Y, x2, MathHelper.E, 0.25f, true)); _spriteBatch.DrawLine(Color.Pink, start, end, 2); start = new Vector2(x1 * GraphicsDevice.Viewport.Width, Interpolator.ExponentialIn(_pStart.Y, _pEnd.Y, x1, MathHelper.E, 0.5f, true)); end = new Vector2(x2 * GraphicsDevice.Viewport.Width, Interpolator.ExponentialIn(_pStart.Y, _pEnd.Y, x2, MathHelper.E, 0.5f, true)); _spriteBatch.DrawLine(Color.Green, start, end, 2); start = new Vector2(x1 * GraphicsDevice.Viewport.Width, Interpolator.ExponentialOut(_pStart.Y, _pEnd.Y, x1, MathHelper.E, 0.5f, true)); end = new Vector2(x2 * GraphicsDevice.Viewport.Width, Interpolator.ExponentialOut(_pStart.Y, _pEnd.Y, x2, MathHelper.E, 0.5f, true)); _spriteBatch.DrawLine(Color.Red, start, end, 2); start = new Vector2(x1 * GraphicsDevice.Viewport.Width, Interpolator.ExponentialInOut(_pStart.Y, _pEnd.Y, x1, MathHelper.E, 0.5f, true)); end = new Vector2(x2 * GraphicsDevice.Viewport.Width, Interpolator.ExponentialInOut(_pStart.Y, _pEnd.Y, x2, MathHelper.E, 0.5f, true)); _spriteBatch.DrawLine(Color.Purple, start, end, 2); start = new Vector2(x1 * GraphicsDevice.Viewport.Width, Interpolator.Logarithmic(_pStart.Y, _pEnd.Y, x1, MathHelper.E, 0.25f, true)); end = new Vector2(x2 * GraphicsDevice.Viewport.Width, Interpolator.Logarithmic(_pStart.Y, _pEnd.Y, x2, MathHelper.E, 0.25f, true)); _spriteBatch.DrawLine(Color.Pink, start, end, 2); start = new Vector2(x1 * GraphicsDevice.Viewport.Width, Interpolator.LogarithmicIn(_pStart.Y, _pEnd.Y, x1, MathHelper.E, 0.5f, true)); end = new Vector2(x2 * GraphicsDevice.Viewport.Width, Interpolator.LogarithmicIn(_pStart.Y, _pEnd.Y, x2, MathHelper.E, 0.5f, true)); _spriteBatch.DrawLine(Color.Green, start, end, 2); start = new Vector2(x1 * GraphicsDevice.Viewport.Width, Interpolator.LogarithmicOut(_pStart.Y, _pEnd.Y, x1, MathHelper.E, 0.5f, true)); end = new Vector2(x2 * GraphicsDevice.Viewport.Width, Interpolator.LogarithmicOut(_pStart.Y, _pEnd.Y, x2, MathHelper.E, 0.5f, true)); _spriteBatch.DrawLine(Color.Red, start, end, 2); start = new Vector2(x1 * GraphicsDevice.Viewport.Width, Interpolator.LogarithmicInOut(_pStart.Y, _pEnd.Y, x1, MathHelper.E, 0.5f, true)); end = new Vector2(x2 * GraphicsDevice.Viewport.Width, Interpolator.LogarithmicInOut(_pStart.Y, _pEnd.Y, x2, MathHelper.E, 0.5f, true)); _spriteBatch.DrawLine(Color.Purple, start, end, 2); start = new Vector2(x1 * GraphicsDevice.Viewport.Width, Interpolator.Circular(_pStart.Y, _pEnd.Y, x1, 0.25f, true)); end = new Vector2(x2 * GraphicsDevice.Viewport.Width, Interpolator.Circular(_pStart.Y, _pEnd.Y, x2, 0.25f, true)); _spriteBatch.DrawLine(Color.Pink, start, end, 2); start = new Vector2(x1 * GraphicsDevice.Viewport.Width, Interpolator.CircularIn(_pStart.Y, _pEnd.Y, x1, 0.5f, true)); end = new Vector2(x2 * GraphicsDevice.Viewport.Width, Interpolator.CircularIn(_pStart.Y, _pEnd.Y, x2, 0.5f, true)); _spriteBatch.DrawLine(Color.Green, start, end, 2); start = new Vector2(x1 * GraphicsDevice.Viewport.Width, Interpolator.CircularOut(_pStart.Y, _pEnd.Y, x1, 0.5f, true)); end = new Vector2(x2 * GraphicsDevice.Viewport.Width, Interpolator.CircularOut(_pStart.Y, _pEnd.Y, x2, 0.5f, true)); _spriteBatch.DrawLine(Color.Red, start, end, 2); start = new Vector2(x1 * GraphicsDevice.Viewport.Width, Interpolator.CircularInOut(_pStart.Y, _pEnd.Y, x1, 0.5f, true)); end = new Vector2(x2 * GraphicsDevice.Viewport.Width, Interpolator.CircularInOut(_pStart.Y, _pEnd.Y, x2, 0.5f, true)); _spriteBatch.DrawLine(Color.Purple, start, end, 2); _tweenPerformance.End(); } _spriteBatch.End(); base.Draw(gameTime); _gameDrawPerformance.End(); // Refresh here as the end-of-frame PerformanceTimer.FrameRefresh(); _gameDrawPerformance.Refresh(); _tweenPerformance.Refresh(); // Draw the results onto the screen _spriteBatch.Begin(); _spriteBatch.DrawString(_font, "CurFPS: " + PerformanceTimer.CurrentFramerate.ToString("0.00"), Vector2.Zero, Color.White); _spriteBatch.DrawString(_font, "MinFPS: " + PerformanceTimer.MinFramerate.ToString("0.00"), Vector2.UnitY * 20, Color.White); _spriteBatch.DrawString(_font, "MaxFPS: " + PerformanceTimer.MaxFramerate.ToString("0.00"), Vector2.UnitY * 40, Color.White); _spriteBatch.DrawString(_font, "AvgFPS: " + PerformanceTimer.AverageFramerate.ToString("0.00"), Vector2.UnitY * 60, Color.White); _spriteBatch.DrawString(_font, "CurDraw: " + _gameDrawPerformance.Current.ToString("0.00ms"), Vector2.UnitY * 100, Color.Black); _spriteBatch.DrawString(_font, "MinDraw: " + _gameDrawPerformance.Min.ToString("0.00ms"), Vector2.UnitY * 120, Color.Black); _spriteBatch.DrawString(_font, "MaxDraw: " + _gameDrawPerformance.Max.ToString("0.00ms"), Vector2.UnitY * 140, Color.Black); _spriteBatch.DrawString(_font, "AvgDraw: " + _gameDrawPerformance.Average.ToString("0.00ms"), Vector2.UnitY * 160, Color.Black); _spriteBatch.DrawString(_font, "CurTween: " + _tweenPerformance.Current.ToString("0.00ms"), Vector2.UnitY * 200, Color.Black); _spriteBatch.DrawString(_font, "MinTween: " + _tweenPerformance.Min.ToString("0.00ms"), Vector2.UnitY * 220, Color.Black); _spriteBatch.DrawString(_font, "MaxTween: " + _tweenPerformance.Max.ToString("0.00ms"), Vector2.UnitY * 240, Color.Black); _spriteBatch.DrawString(_font, "AvgTween: " + _tweenPerformance.Average.ToString("0.00ms"), Vector2.UnitY * 260, Color.Black); _spriteBatch.DrawDecimal(_font, _tweenPerformance.Average, 2, Vector2.UnitY * 280, Color.Black); _spriteBatch.DrawDecimal(_font, PerformanceTimer.CurrentFramerate, 2, Vector2.UnitY * 300, Color.Black); _spriteBatch.End(); // Let the next 'frame' begin now because Draw() is throttled internally PerformanceTimer.FrameTick(); }