PB5.1 : Animation & Motion tween FX

Share your advanced PureBasic knowledge/code with the community.
User avatar
eddy
Addict
Addict
Posts: 1479
Joined: Mon May 26, 2003 3:07 pm
Location: Nantes

PB5.1 : Animation & Motion tween FX

Post by eddy »

I hope my convertion is correct :)
I didn't test all functions.

Tween.pbi

Code: Select all

;-TOP
; Comment : Animation & Motion Tween
; Author  : eddy
; Web     : http://www.purebasic.fr/english/viewtopic.php?f=12&t=53251
; File:   : Tween.pbi
; Version : v1.5
; Credit :
;    Robert Penner - The easing equations we all know and love 
;      (http://robertpenner.com/easing/) [See License.txt for license info]
; 
;    Lee Brimelow - initial port of Penner's equations to WPF 
;      (http://thewpfblog.com/?p=12)
; 
;    Zeh Fernando - additional equations (out/in) from 
;      caurina.transitions.Tweener (http://code.google.com/p/tweener/)
;      [See License.txt for license info] 

EnableExplicit

; *********************
; ANIMATION & MOTION TWEEN
; *********************

;{ region Linear

; <summary>
; Easing equation function for a simple linear tweening, with no easing.
; </summary>
Procedure.f Tween_Linear( t.f, b.f, c.f, d.f )
  
  ProcedureReturn c * t / d + b
EndProcedure

;} endregion

;{ region Expo

; <summary>
; Easing equation function for an exponential (2^t) easing out: 
; decelerating from zero velocity.
; </summary>
Procedure.f Tween_ExpoEaseOut( t.f, b.f, c.f, d.f )
  
  If ( t = d ) : ProcedureReturn  b + c : Else : ProcedureReturn  c * ( -Pow( 2, -10 * t / d ) + 1 ) + b : EndIf
EndProcedure

; <summary>
; Easing equation function for an exponential (2^t) easing in: 
; accelerating from zero velocity.
; </summary>
Procedure.f Tween_ExpoEaseIn( t.f, b.f, c.f, d.f )
  
  If ( t = 0 ) : ProcedureReturn  b : Else : ProcedureReturn  c * Pow( 2, 10 * ( t / d - 1 ) ) + b : EndIf
EndProcedure

; <summary>
; Easing equation function for an exponential (2^t) easing in/out: 
; acceleration until halfway, then deceleration.
; </summary>
Procedure.f Tween_ExpoEaseInOut( t.f, b.f, c.f, d.f )
  
  If ( t = 0 )
    ProcedureReturn b;
  EndIf
  
  If ( t = d )
    ProcedureReturn b + c;
  EndIf
  
  t / (d / 2)
  If ( t < 1 )
    ProcedureReturn c / 2 * Pow( 2, 10 * ( t - 1 ) ) + b;
  EndIf
  
  t-1
  ProcedureReturn c / 2 * ( -Pow( 2, -10 * t ) + 2 ) + b
EndProcedure

; <summary>
; Easing equation function for an exponential (2^t) easing out/in: 
; deceleration until halfway, then acceleration.
; </summary>
Procedure.f Tween_ExpoEaseOutIn( t.f, b.f, c.f, d.f )
  
  If ( t < d / 2 )
    ProcedureReturn Tween_ExpoEaseOut( t * 2, b, c / 2, d );
  EndIf
  
  ProcedureReturn Tween_ExpoEaseIn( ( t * 2 ) - d, b + c / 2, c / 2, d )
EndProcedure

;} endregion

;{ region Circular

; <summary>
; Easing equation function for a circular (sqrt(1-t^2)) easing out: 
; decelerating from zero velocity.
; </summary>
Procedure.f Tween_CircEaseOut( t.f, b.f, c.f, d.f )
  
  t = t / d - 1
  ProcedureReturn c * Sqr( 1 - t * t ) + b
EndProcedure

; <summary>
; Easing equation function for a circular (sqrt(1-t^2)) easing in: 
; accelerating from zero velocity.
; </summary>
Procedure.f Tween_CircEaseIn( t.f, b.f, c.f, d.f )
  
  t / d
  ProcedureReturn -c * ( Sqr( 1 - t * t ) - 1 ) + b
EndProcedure

; <summary>
; Easing equation function for a circular (sqrt(1-t^2)) easing in/out: 
; acceleration until halfway, then deceleration.
; </summary>
Procedure.f Tween_CircEaseInOut( t.f, b.f, c.f, d.f )
  
  t / (d / 2)
  If ( t < 1 )
    ProcedureReturn -c / 2 * ( Sqr( 1 - t * t ) - 1 ) + b;
  EndIf
  
  t - 2
  ProcedureReturn c / 2 * ( Sqr( 1 - t * t ) + 1 ) + b
EndProcedure

; <summary>
; Easing equation function for a circular (sqrt(1-t^2)) easing in/out: 
; acceleration until halfway, then deceleration.
; </summary>
Procedure.f Tween_CircEaseOutIn( t.f, b.f, c.f, d.f )
  
  If ( t < d / 2 )
    ProcedureReturn Tween_CircEaseOut( t * 2, b, c / 2, d );
  EndIf
  
  ProcedureReturn Tween_CircEaseIn( ( t * 2 ) - d, b + c / 2, c / 2, d )
EndProcedure

;} endregion

;{ region Quad

; <summary>
; Easing equation function for a quadratic (t^2) easing out: 
; decelerating from zero velocity.
; </summary>
Procedure.f Tween_QuadEaseOut( t.f, b.f, c.f, d.f )
  
  t / d
  ProcedureReturn -c * t * ( t - 2 ) + b
EndProcedure

; <summary>
; Easing equation function for a quadratic (t^2) easing in: 
; accelerating from zero velocity.
; </summary>
Procedure.f Tween_QuadEaseIn( t.f, b.f, c.f, d.f )
  
  t / d
  ProcedureReturn c * t * t + b
EndProcedure

; <summary>
; Easing equation function for a quadratic (t^2) easing in/out: 
; acceleration until halfway, then deceleration.
; </summary>
Procedure.f Tween_QuadEaseInOut( t.f, b.f, c.f, d.f )
  
  t / (d / 2)
  If ( t < 1 )
    ProcedureReturn c / 2 * t * t + b;
  EndIf
  
  t-1
  ProcedureReturn -c / 2 * ( t * ( t - 2 ) - 1 ) + b
EndProcedure

; <summary>
; Easing equation function for a quadratic (t^2) easing out/in: 
; deceleration until halfway, then acceleration.
; </summary>
Procedure.f Tween_QuadEaseOutIn( t.f, b.f, c.f, d.f )
  
  If ( t < d / 2 )
    ProcedureReturn Tween_QuadEaseOut( t * 2, b, c / 2, d );
  EndIf
  
  ProcedureReturn Tween_QuadEaseIn( ( t * 2 ) - d, b + c / 2, c / 2, d )
EndProcedure

;} endregion

;{ region Sine

; <summary>
; Easing equation function for a sinusoidal (sin(t)) easing out: 
; decelerating from zero velocity.
; </summary>
Procedure.f Tween_SineEaseOut( t.f, b.f, c.f, d.f )
  
  ProcedureReturn c * Sin( t / d * ( #PI / 2 ) ) + b
EndProcedure

; <summary>
; Easing equation function for a sinusoidal (sin(t)) easing in: 
; accelerating from zero velocity.
; </summary>
Procedure.f Tween_SineEaseIn( t.f, b.f, c.f, d.f )
  
  ProcedureReturn -c * Cos( t / d * ( #PI / 2 ) ) + c + b
EndProcedure

; <summary>
; Easing equation function for a sinusoidal (sin(t)) easing in/out: 
; acceleration until halfway, then deceleration.
; </summary>
Procedure.f Tween_SineEaseInOut( t.f, b.f, c.f, d.f )
  
  t / (d / 2)
  If ( t < 1 )
    ProcedureReturn c / 2 * ( Sin( #PI * t / 2 ) ) + b;
  EndIf
  
  t-1
  ProcedureReturn -c / 2 * ( Cos( #PI * t / 2 ) - 2 ) + b
EndProcedure

; <summary>
; Easing equation function for a sinusoidal (sin(t)) easing in/out: 
; deceleration until halfway, then acceleration.
; </summary>
Procedure.f Tween_SineEaseOutIn( t.f, b.f, c.f, d.f )
  
  If ( t < d / 2 )
    ProcedureReturn Tween_SineEaseOut( t * 2, b, c / 2, d );
  EndIf
  
  ProcedureReturn Tween_SineEaseIn( ( t * 2 ) - d, b + c / 2, c / 2, d )
EndProcedure

;} endregion

;{ region Cubic

; <summary>
; Easing equation function for a cubic (t^3) easing out: 
; decelerating from zero velocity.
; </summary>
Procedure.f Tween_CubicEaseOut( t.f, b.f, c.f, d.f )
  
  t = t / d - 1
  ProcedureReturn c * ( t * t * t + 1 ) + b
EndProcedure

; <summary>
; Easing equation function for a cubic (t^3) easing in: 
; accelerating from zero velocity.
; </summary>
Procedure.f Tween_CubicEaseIn( t.f, b.f, c.f, d.f )
  
  t / d
  ProcedureReturn c * t * t * t + b
EndProcedure

; <summary>
; Easing equation function for a cubic (t^3) easing in/out: 
; acceleration until halfway, then deceleration.
; </summary>
Procedure.f Tween_CubicEaseInOut( t.f, b.f, c.f, d.f )
  
  t / (d / 2)
  If ( t < 1 )
    ProcedureReturn c / 2 * t * t * t + b;
  EndIf
  
  t - 2
  ProcedureReturn c / 2 * ( t * t * t + 2 ) + b
EndProcedure

; <summary>
; Easing equation function for a cubic (t^3) easing out/in: 
; deceleration until halfway, then acceleration.
; </summary>
Procedure.f Tween_CubicEaseOutIn( t.f, b.f, c.f, d.f )
  
  If ( t < d / 2 )
    ProcedureReturn Tween_CubicEaseOut( t * 2, b, c / 2, d );
  EndIf
  
  ProcedureReturn Tween_CubicEaseIn( ( t * 2 ) - d, b + c / 2, c / 2, d )
EndProcedure

;} endregion

;{ region Quartic

; <summary>
; Easing equation function for a quartic (t^4) easing out: 
; decelerating from zero velocity.
; </summary>
Procedure.f Tween_QuartEaseOut( t.f, b.f, c.f, d.f )
  
  t = t / d - 1
  ProcedureReturn -c * ( t * t * t * t - 1 ) + b
EndProcedure

; <summary>
; Easing equation function for a quartic (t^4) easing in: 
; accelerating from zero velocity.
; </summary>
Procedure.f Tween_QuartEaseIn( t.f, b.f, c.f, d.f )
  
  t / d
  ProcedureReturn c * t * t * t * t + b
EndProcedure

; <summary>
; Easing equation function for a quartic (t^4) easing in/out: 
; acceleration until halfway, then deceleration.
; </summary>
Procedure.f Tween_QuartEaseInOut( t.f, b.f, c.f, d.f )
  
  t / (d / 2)
  If ( t < 1 )
    ProcedureReturn c / 2 * t * t * t * t + b;
  EndIf
  
  t - 2
  ProcedureReturn -c / 2 * ( t * t * t * t - 2 ) + b
EndProcedure

; <summary>
; Easing equation function for a quartic (t^4) easing out/in: 
; deceleration until halfway, then acceleration.
; </summary>
Procedure.f Tween_QuartEaseOutIn( t.f, b.f, c.f, d.f )
  
  If ( t < d / 2 )
    ProcedureReturn Tween_QuartEaseOut( t * 2, b, c / 2, d );
  EndIf
  
  ProcedureReturn Tween_QuartEaseIn( ( t * 2 ) - d, b + c / 2, c / 2, d )
EndProcedure

;} endregion

;{ region Quintic

; <summary>
; Easing equation function for a quintic (t^5) easing out: 
; decelerating from zero velocity.
; </summary>
Procedure.f Tween_QuintEaseOut( t.f, b.f, c.f, d.f )
  
  t = t / d - 1
  ProcedureReturn c * ( t * t * t * t * t + 1 ) + b
EndProcedure

; <summary>
; Easing equation function for a quintic (t^5) easing in: 
; accelerating from zero velocity.
; </summary>
Procedure.f Tween_QuintEaseIn( t.f, b.f, c.f, d.f )
  
  t / d
  ProcedureReturn c * t * t * t * t * t + b
EndProcedure

; <summary>
; Easing equation function for a quintic (t^5) easing in/out: 
; acceleration until halfway, then deceleration.
; </summary>
Procedure.f Tween_QuintEaseInOut( t.f, b.f, c.f, d.f )
  
  t / (d / 2)
  If ( t < 1 )
    ProcedureReturn c / 2 * t * t * t * t * t + b;
  EndIf
  
  t - 2
  ProcedureReturn c / 2 * ( t * t * t * t * t + 2 ) + b
EndProcedure

; <summary>
; Easing equation function for a quintic (t^5) easing in/out: 
; acceleration until halfway, then deceleration.
; </summary>
Procedure.f Tween_QuintEaseOutIn( t.f, b.f, c.f, d.f )
  
  If ( t < d / 2 )
    ProcedureReturn Tween_QuintEaseOut( t * 2, b, c / 2, d );
  EndIf
  
  ProcedureReturn Tween_QuintEaseIn( ( t * 2 ) - d, b + c / 2, c / 2, d )
EndProcedure

;} endregion

;{ region Elastic

; <summary>
; Easing equation function for an elastic (exponentially decaying sine wave) easing out: 
; decelerating from zero velocity.
; </summary>
Procedure.f Tween_ElasticEaseOut( t.f, b.f, c.f, d.f )
  
  t / d
  If ( t = 1 )
    ProcedureReturn b + c;
  EndIf
  
  Protected p.f = d * 0.3
  Protected s.f = p / 4
  
  ProcedureReturn ( c * Pow( 2, -10 * t ) * Sin( ( t * d - s ) * ( 2 * #PI ) / p ) + c + b )
EndProcedure

; <summary>
; Easing equation function for an elastic (exponentially decaying sine wave) easing in: 
; accelerating from zero velocity.
; </summary>
Procedure.f Tween_ElasticEaseIn( t.f, b.f, c.f, d.f )
  
  t / d
  If ( t = 1 )
    ProcedureReturn b + c;
  EndIf
  
  Protected p.f = d * 0.3
  Protected s.f = p / 4
  
  t - 1
  ProcedureReturn -( c * Pow( 2, 10 * t ) * Sin( ( t * d - s ) * ( 2 * #PI ) / p ) ) + b
EndProcedure

; <summary>
; Easing equation function for an elastic (exponentially decaying sine wave) easing in/out: 
; acceleration until halfway, then deceleration.
; </summary>
Procedure.f Tween_ElasticEaseInOut( t.f, b.f, c.f, d.f )
  
  t / (d / 2)
  If ( t = 2 )
    ProcedureReturn b + c;
  EndIf
  
  Protected p.f = d * ( 0.3 * 1.5 )
  Protected s.f = p / 4
  
  If ( t < 1 )
    t - 1
    ProcedureReturn -0.5 * ( c * Pow( 2, 10 * t ) * Sin( ( t * d - s ) * ( 2 * #PI ) / p ) ) + b;
  EndIf
  
  t - 1
  ProcedureReturn c * Pow( 2, -10 * t ) * Sin( ( t * d - s ) * ( 2 * #PI ) / p ) * 0.5 + c + b
EndProcedure

; <summary>
; Easing equation function for an elastic (exponentially decaying sine wave) easing out/in: 
; deceleration until halfway, then acceleration.
; </summary>
Procedure.f Tween_ElasticEaseOutIn( t.f, b.f, c.f, d.f )
  
  If ( t < d / 2 )
    ProcedureReturn Tween_ElasticEaseOut( t * 2, b, c / 2, d );
  EndIf
  
  ProcedureReturn Tween_ElasticEaseIn( ( t * 2 ) - d, b + c / 2, c / 2, d )
EndProcedure

;} endregion

;{ region Bounce

; <summary>
; Easing equation function for a bounce (exponentially decaying parabolic bounce) easing out: 
; decelerating from zero velocity.
; </summary>
Procedure.f Tween_BounceEaseOut( t.f, b.f, c.f, d.f )
  
  t / d
  If ( t < ( 1 / 2.75 ) )
    ProcedureReturn c * ( 7.5625 * t * t ) + b;
  ElseIf ( t < ( 2 / 2.75 ) )
    t - ( 1.5 / 2.75 )                
    ProcedureReturn c * ( 7.5625 * t * t + 0.75 ) + b;
  ElseIf ( t < ( 2.5 / 2.75 ) )
    t - ( 2.25 / 2.75 )
    ProcedureReturn c * ( 7.5625 * t * t + 0.9375 ) + b;
  Else
    t - ( 2.625 / 2.75 )
    ProcedureReturn c * ( 7.5625 * t * t + 0.984375 ) + b
  EndIf 
EndProcedure

; <summary>
; Easing equation function for a bounce (exponentially decaying parabolic bounce) easing in: 
; accelerating from zero velocity.
; </summary>
Procedure.f Tween_BounceEaseIn( t.f, b.f, c.f, d.f )
  
  ProcedureReturn c - Tween_BounceEaseOut( d - t, 0, c, d ) + b
EndProcedure

; <summary>
; Easing equation function for a bounce (exponentially decaying parabolic bounce) easing in/out: 
; acceleration until halfway, then deceleration.
; </summary>
Procedure.f Tween_BounceEaseInOut( t.f, b.f, c.f, d.f )
  
  If ( t < d / 2 )
    ProcedureReturn Tween_BounceEaseIn( t * 2, 0, c, d ) * 0.5 + b;
  Else
    ProcedureReturn Tween_BounceEaseOut( t * 2 - d, 0, c, d ) * 0.5 + c * 0.5 + b
  EndIf 
EndProcedure

; <summary>
; Easing equation function for a bounce (exponentially decaying parabolic bounce) easing out/in: 
; deceleration until halfway, then acceleration.
; </summary>
Procedure.f Tween_BounceEaseOutIn( t.f, b.f, c.f, d.f )
  
  If ( t < d / 2 )
    ProcedureReturn Tween_BounceEaseOut( t * 2, b, c / 2, d );
  EndIf
  
  ProcedureReturn Tween_BounceEaseIn( ( t * 2 ) - d, b + c / 2, c / 2, d )
EndProcedure

;} endregion

;{ region Back

; <summary>
; Easing equation function for a back (overshooting cubic easing: (s+1)*t^3 - s*t^2) easing out: 
; decelerating from zero velocity.
; </summary>
Procedure.f Tween_BackEaseOut( t.f, b.f, c.f, d.f )
  
  t = t / d - 1
  ProcedureReturn c * ( t * t * ( ( 1.70158 + 1 ) * t + 1.70158 ) + 1 ) + b
EndProcedure

; <summary>
; Easing equation function for a back (overshooting cubic easing: (s+1)*t^3 - s*t^2) easing in: 
; accelerating from zero velocity.
; </summary>
Procedure.f Tween_BackEaseIn( t.f, b.f, c.f, d.f )
  
  t / d
  ProcedureReturn c * t * t * ( ( 1.70158 + 1 ) * t - 1.70158 ) + b
EndProcedure

; <summary>
; Easing equation function for a back (overshooting cubic easing: (s+1)*t^3 - s*t^2) easing in/out: 
; acceleration until halfway, then deceleration.
; </summary>
Procedure.f Tween_BackEaseInOut( t.f, b.f, c.f, d.f )
  
  Protected s.f = 1.70158
  
  t / (d / 2)
  If ( t < 1 )
    s * ( 1.525 )
    ProcedureReturn c / 2 * ( t * t * ( ( s + 1 ) * t - s ) ) + b;
  EndIf
  
  t - 2
  s * ( 1.525 )
  ProcedureReturn c / 2 * ( t * t * ( ( s + 1 ) * t + s ) + 2 ) + b
EndProcedure

; <summary>
; Easing equation function for a back (overshooting cubic easing: (s+1)*t^3 - s*t^2) easing out/in: 
; deceleration until halfway, then acceleration.
; </summary>
Procedure.f Tween_BackEaseOutIn( t.f, b.f, c.f, d.f )
  
  If ( t < d / 2 )
    ProcedureReturn Tween_BackEaseOut( t * 2, b, c / 2, d );
  EndIf
  
  ProcedureReturn Tween_BackEaseIn( ( t * 2 ) - d, b + c / 2, c / 2, d )
EndProcedure

;} endregion

Enumeration    
  ;Tween FX list
  #FX_Linear=0
  
  #FX_QuadEaseOut
  #FX_QuadEaseIn
  #FX_QuadEaseInOut
  #FX_QuadEaseOutIn
  
  #FX_ExpoEaseOut
  #FX_ExpoEaseIn
  #FX_ExpoEaseInOut
  #FX_ExpoEaseOutIn
  
  #FX_CubicEaseOut
  #FX_CubicEaseIn
  #FX_CubicEaseInOut
  #FX_CubicEaseOutIn
  
  #FX_QuartEaseOut
  #FX_QuartEaseIn
  #FX_QuartEaseInOut
  #FX_QuartEaseOutIn
  
  #FX_QuintEaseOut
  #FX_QuintEaseIn
  #FX_QuintEaseInOut
  #FX_QuintEaseOutIn
  
  #FX_CircEaseOut
  #FX_CircEaseIn
  #FX_CircEaseInOut
  #FX_CircEaseOutIn
  
  #FX_SineEaseOut
  #FX_SineEaseIn
  #FX_SineEaseInOut
  #FX_SineEaseOutIn
  
  #FX_ElasticEaseOut
  #FX_ElasticEaseIn
  #FX_ElasticEaseInOut
  #FX_ElasticEaseOutIn
  
  #FX_BounceEaseOut
  #FX_BounceEaseIn
  #FX_BounceEaseInOut
  #FX_BounceEaseOutIn
  
  #FX_BackEaseOut
  #FX_BackEaseIn
  #FX_BackEaseInOut
  #FX_BackEaseOutIn
  
  ;Helpers to select all FX in FOR...NEXT loops
  #FX_FirstValue=#FX_Linear
  #FX_LastValue=#FX_BackEaseOutIn
  
  ;Looping mode
  #FX_Loop_Cycle=0
  #FX_Loop_PingPong
  
  ;Looping count max
  #FX_Loop_MaxCount=$FFFF
  
  ;Helpers to change FastTween Accuracy
  #FX_FastTweenResolution=2048
  #FX_FastTweenPingPong=#FX_FastTweenResolution*2
EndEnumeration

Global Dim FastTweenValues.f(#FX_LastValue,#FX_FastTweenResolution)
Global Dim FastTweenCycles.u(#FX_FastTweenPingPong)

Procedure.f Tween(fx, timer.f, start.f, delta.f, duration.f) 
  Select fx
    Case #FX_Linear: ProcedureReturn Tween_Linear(timer, start, delta, duration)
      
    Case #FX_QuadEaseOut: ProcedureReturn Tween_QuadEaseOut(timer, start, delta, duration)
    Case #FX_QuadEaseIn: ProcedureReturn Tween_QuadEaseIn(timer, start, delta, duration)
    Case #FX_QuadEaseInOut: ProcedureReturn Tween_QuadEaseInOut(timer, start, delta, duration)
    Case #FX_QuadEaseOutIn: ProcedureReturn Tween_QuadEaseOutIn(timer, start, delta, duration)
      
    Case #FX_ExpoEaseOut: ProcedureReturn Tween_ExpoEaseOut(timer, start, delta, duration)
    Case #FX_ExpoEaseIn: ProcedureReturn Tween_ExpoEaseIn(timer, start, delta, duration)
    Case #FX_ExpoEaseInOut: ProcedureReturn Tween_ExpoEaseInOut(timer, start, delta, duration)
    Case #FX_ExpoEaseOutIn: ProcedureReturn Tween_ExpoEaseOutIn(timer, start, delta, duration)
      
    Case #FX_CubicEaseOut: ProcedureReturn Tween_CubicEaseOut(timer, start, delta, duration)
    Case #FX_CubicEaseIn: ProcedureReturn Tween_CubicEaseIn(timer, start, delta, duration)
    Case #FX_CubicEaseInOut: ProcedureReturn Tween_CubicEaseInOut(timer, start, delta, duration)
    Case #FX_CubicEaseOutIn: ProcedureReturn Tween_CubicEaseOutIn(timer, start, delta, duration)
      
    Case #FX_QuartEaseOut: ProcedureReturn Tween_QuartEaseOut(timer, start, delta, duration)
    Case #FX_QuartEaseIn: ProcedureReturn Tween_QuartEaseIn(timer, start, delta, duration)
    Case #FX_QuartEaseInOut: ProcedureReturn Tween_QuartEaseInOut(timer, start, delta, duration)
    Case #FX_QuartEaseOutIn: ProcedureReturn Tween_QuartEaseOutIn(timer, start, delta, duration)
      
    Case #FX_QuintEaseOut: ProcedureReturn Tween_QuintEaseOut(timer, start, delta, duration)
    Case #FX_QuintEaseIn: ProcedureReturn Tween_QuintEaseIn(timer, start, delta, duration)
    Case #FX_QuintEaseInOut: ProcedureReturn Tween_QuintEaseInOut(timer, start, delta, duration)
    Case #FX_QuintEaseOutIn: ProcedureReturn Tween_QuintEaseOutIn(timer, start, delta, duration)
      
    Case #FX_CircEaseOut: ProcedureReturn Tween_CircEaseOut(timer, start, delta, duration)
    Case #FX_CircEaseIn: ProcedureReturn Tween_CircEaseIn(timer, start, delta, duration)
    Case #FX_CircEaseInOut: ProcedureReturn Tween_CircEaseInOut(timer, start, delta, duration)
    Case #FX_CircEaseOutIn: ProcedureReturn Tween_CircEaseOutIn(timer, start, delta, duration)
      
    Case #FX_SineEaseOut: ProcedureReturn Tween_SineEaseOut(timer, start, delta, duration)
    Case #FX_SineEaseIn: ProcedureReturn Tween_SineEaseIn(timer, start, delta, duration)
    Case #FX_SineEaseInOut: ProcedureReturn Tween_SineEaseInOut(timer, start, delta, duration)
    Case #FX_SineEaseOutIn: ProcedureReturn Tween_SineEaseOutIn(timer, start, delta, duration)
      
    Case #FX_ElasticEaseOut: ProcedureReturn Tween_ElasticEaseOut(timer, start, delta, duration)
    Case #FX_ElasticEaseIn: ProcedureReturn Tween_ElasticEaseIn(timer, start, delta, duration)
    Case #FX_ElasticEaseInOut: ProcedureReturn Tween_ElasticEaseInOut(timer, start, delta, duration)
    Case #FX_ElasticEaseOutIn: ProcedureReturn Tween_ElasticEaseOutIn(timer, start, delta, duration)
      
    Case #FX_BounceEaseOut: ProcedureReturn Tween_BounceEaseOut(timer, start, delta, duration)
    Case #FX_BounceEaseIn: ProcedureReturn Tween_BounceEaseIn(timer, start, delta, duration)
    Case #FX_BounceEaseInOut: ProcedureReturn Tween_BounceEaseInOut(timer, start, delta, duration)
    Case #FX_BounceEaseOutIn: ProcedureReturn Tween_BounceEaseOutIn(timer, start, delta, duration)
      
    Case #FX_BackEaseOut: ProcedureReturn Tween_BackEaseOut(timer, start, delta, duration)
    Case #FX_BackEaseIn: ProcedureReturn Tween_BackEaseIn(timer, start, delta, duration)
    Case #FX_BackEaseInOut: ProcedureReturn Tween_BackEaseInOut(timer, start, delta, duration)
    Case #FX_BackEaseOutIn: ProcedureReturn Tween_BackEaseOutIn(timer, start, delta, duration)
  EndSelect 
EndProcedure

Procedure.f FastTween(fx, timer.f, start.f, delta.f, duration.f)    
  Protected tweenIndex=Int(#FX_FastTweenResolution*timer/duration)
  If tweenIndex>#FX_FastTweenResolution
    ProcedureReturn delta+start
  EndIf 
  ProcedureReturn FastTweenValues(fx,tweenIndex)*delta+start
EndProcedure

Procedure.f FastTweenLoop(fx, timer.f, start.f, delta.f, duration.f, loopMode, loopCount.f=#FX_Loop_MaxCount)    
  Protected tweenIndex=Int(#FX_FastTweenResolution*timer/duration)
  Select loopMode
    Case #FX_Loop_Cycle
      If timer>#FX_FastTweenResolution*loopCount
        tweenIndex=#FX_FastTweenResolution*loopCount
      EndIf 
      tweenIndex % (#FX_FastTweenResolution+1)
    Case #FX_Loop_PingPong
      If timer>#FX_FastTweenResolution*loopCount*2
        tweenIndex=#FX_FastTweenResolution*loopCount*2
      EndIf 
      tweenIndex=FastTweenCycles(tweenIndex % (#FX_FastTweenPingPong+1))
  EndSelect
  
  ProcedureReturn FastTweenValues(fx,tweenIndex)*delta+start
EndProcedure

Procedure InitFastTween()
  Protected fx,t
  For fx=#FX_FirstValue To #FX_LastValue
    For t=0 To #FX_FastTweenResolution
      FastTweenValues(fx,t)=Tween(fx,t,0,1,#FX_FastTweenResolution)
    Next 
  Next 
  
  For t=0 To #FX_FastTweenResolution
    FastTweenCycles(t)=t
    FastTweenCycles(#FX_FastTweenPingPong-t)=t
  Next      
EndProcedure 

Procedure.i DeltaMilliseconds()
  Static lastTime.i, previousTime.i, deltaTime.f
  lastTime=ElapsedMilliseconds()
  deltaTime=(lastTime-previousTime)
  previousTime=lastTime  
  ProcedureReturn deltaTime  
EndProcedure 

Procedure.f DeltaSeconds()
  Static lastTime.i, previousTime.i, deltaTime.f
  lastTime=ElapsedMilliseconds()
  deltaTime=(lastTime-previousTime)
  previousTime=lastTime  
  ProcedureReturn deltaTime/1000.0 
EndProcedure 
Last edited by eddy on Fri Jul 26, 2013 1:10 pm, edited 17 times in total.
Imagewin10 x64 5.72 | IDE | PB plugin | Tools | Sprite | JSON | visual tool
User avatar
eddy
Addict
Addict
Posts: 1479
Joined: Mon May 26, 2003 3:07 pm
Location: Nantes

Re: Animation & Motion tween FX

Post by eddy »

[updated]
- add function prefix : Tween_*
- add FX enumeration
- add function: Tween(fx, timer, start, delta, duration)
Imagewin10 x64 5.72 | IDE | PB plugin | Tools | Sprite | JSON | visual tool
User avatar
eddy
Addict
Addict
Posts: 1479
Joined: Mon May 26, 2003 3:07 pm
Location: Nantes

Re: Animation & Motion tween FX

Post by eddy »

- multi graph tracer

Image

Code: Select all

DisableExplicit
Enumeration 
   #Window
   #Grapher
   #ListFX
EndEnumeration
If OpenWindow(#Window, 0, 0, 620, 410, "Tween Function Tester", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
   CanvasGadget(#Grapher, 5, 5, 400, 400)
   ListViewGadget(#ListFX, 410, 5, 200, 400,#PB_ListView_MultiSelect |#PB_ListView_ClickSelect)
   ;{ LIST OF EFFECTS
   AddGadgetItem (#ListFX, -1, "#FX_Linear")
   
   AddGadgetItem (#ListFX, -1, "#FX_QuadEaseOut")
   AddGadgetItem (#ListFX, -1, "#FX_QuadEaseIn")
   AddGadgetItem (#ListFX, -1, "#FX_QuadEaseInOut")
   AddGadgetItem (#ListFX, -1, "#FX_QuadEaseOutIn")
   
   AddGadgetItem (#ListFX, -1, "#FX_ExpoEaseOut")
   AddGadgetItem (#ListFX, -1, "#FX_ExpoEaseIn")
   AddGadgetItem (#ListFX, -1, "#FX_ExpoEaseInOut")
   AddGadgetItem (#ListFX, -1, "#FX_ExpoEaseOutIn")
   
   AddGadgetItem (#ListFX, -1, "#FX_CubicEaseOut")
   AddGadgetItem (#ListFX, -1, "#FX_CubicEaseIn")
   AddGadgetItem (#ListFX, -1, "#FX_CubicEaseInOut")
   AddGadgetItem (#ListFX, -1, "#FX_CubicEaseOutIn")
   
   AddGadgetItem (#ListFX, -1, "#FX_QuartEaseOut")
   AddGadgetItem (#ListFX, -1, "#FX_QuartEaseIn")
   AddGadgetItem (#ListFX, -1, "#FX_QuartEaseInOut")
   AddGadgetItem (#ListFX, -1, "#FX_QuartEaseOutIn")
   
   AddGadgetItem (#ListFX, -1, "#FX_QuintEaseOut")
   AddGadgetItem (#ListFX, -1, "#FX_QuintEaseIn")
   AddGadgetItem (#ListFX, -1, "#FX_QuintEaseInOut")
   AddGadgetItem (#ListFX, -1, "#FX_QuintEaseOutIn")
   
   AddGadgetItem (#ListFX, -1, "#FX_CircEaseOut")
   AddGadgetItem (#ListFX, -1, "#FX_CircEaseIn")
   AddGadgetItem (#ListFX, -1, "#FX_CircEaseInOut")
   AddGadgetItem (#ListFX, -1, "#FX_CircEaseOutIn")
   
   AddGadgetItem (#ListFX, -1, "#FX_SineEaseOut")
   AddGadgetItem (#ListFX, -1, "#FX_SineEaseIn")
   AddGadgetItem (#ListFX, -1, "#FX_SineEaseInOut")
   AddGadgetItem (#ListFX, -1, "#FX_SineEaseOutIn")
   
   AddGadgetItem (#ListFX, -1, "#FX_ElasticEaseOut")
   AddGadgetItem (#ListFX, -1, "#FX_ElasticEaseIn")
   AddGadgetItem (#ListFX, -1, "#FX_ElasticEaseInOut")
   AddGadgetItem (#ListFX, -1, "#FX_ElasticEaseOutIn")
   
   AddGadgetItem (#ListFX, -1, "#FX_BounceEaseOut")
   AddGadgetItem (#ListFX, -1, "#FX_BounceEaseIn")
   AddGadgetItem (#ListFX, -1, "#FX_BounceEaseInOut")
   AddGadgetItem (#ListFX, -1, "#FX_BounceEaseOutIn")
   
   AddGadgetItem (#ListFX, -1, "#FX_BackEaseOut")
   AddGadgetItem (#ListFX, -1, "#FX_BackEaseIn")
   AddGadgetItem (#ListFX, -1, "#FX_BackEaseInOut")
   AddGadgetItem (#ListFX, -1, "#FX_BackEaseOutIn")
   ;}
   countFX=CountGadgetItems(#ListFX)
            
   Repeat
      Event = WaitWindowEvent()
      If Event = #PB_Event_Gadget And EventGadget() = #ListFX And EventType() = #PB_EventType_LeftClick
         
         If StartDrawing(CanvasOutput(#Grapher))
            ;{ GRAPH BACKGROUND IMAGE
            w=OutputWidth()
            h=OutputHeight()
            Box(0,0,w,h,#White)
            DrawingMode(#PB_2DDrawing_Transparent )
            Line(5,5,w-10,1,RGB(200,200,200))
            Line(5,h-5,w-10,1,RGB(200,200,200))
            Line(w-5,5,1,h-10,RGB(200,200,200))
            
            Line(5,5,1,h-10,#Gray)
            Line(5,h/2,w-10,1,#Gray)
            
            Line(5,5,5,5,#Gray)
            Line(5,5,-5,5,#Gray)
            
            Line(5,h-5,5,-5,#Gray)
            Line(5,h-5,-5,-5,#Gray)
            
            Line(w-5,h/2,-5,5,#Gray)
            Line(w-5,h/2,-5,-5,#Gray)
            
            DrawText(12,5,"+1",#Red,0)
            DrawText(12,h-18,"-1",#Red,0)
            DrawText(12,h/2-20,"0",#Red,0)
            DrawText(w-10,h/2-20,"d",#Red,0)
            ;}
            
            Define t,color,x.f,y.f
            Define startX.f,startY.f
            Define finalX.f,finalY.f
            Define moveX.f,moveY.f,prevX.f,prevY.f
            
            startX=5
            startY=h/2
            finalX=h-5
            finalY=5
            moveX=finalX-startX-1
            moveY=finalY-startY
            
            For fx=#FX_FirstValue To #FX_LastValue
               If GetGadgetItemState(#ListFX,fx)
                  ;{ TRACE COLORED GRAPHS
                  prevX=startX
                  prevY=startY
                  color=RGB(50+Random(150),50+Random(150),50+Random(150))
                  For t=0 To 400
                     x.f=Tween(#FX_Linear,t,startX,moveX,400)
                     y.f=Tween(fx,t,startY,moveY,400)
                     LineXY(prevX,prevY,x,y,color)
                     prevX=x
                     prevY=y
                  Next 
                  ;}
               EndIf 
            Next 
            StopDrawing()
         EndIf
      EndIf
      
   Until Event = #PB_Event_CloseWindow
EndIf
Last edited by eddy on Wed Feb 06, 2013 4:45 am, edited 4 times in total.
Imagewin10 x64 5.72 | IDE | PB plugin | Tools | Sprite | JSON | visual tool
eesau
Enthusiast
Enthusiast
Posts: 589
Joined: Fri Apr 27, 2007 12:38 pm
Location: Finland

Re: Animation & Motion tween FX

Post by eesau »

Nice, I'll have a few good uses for this in a short while. Thanks eddy, brilliant work!
User avatar
eddy
Addict
Addict
Posts: 1479
Joined: Mon May 26, 2003 3:07 pm
Location: Nantes

Re: Animation & Motion tween FX

Post by eddy »

[updated]
- fixed : procedure return type (.d => .f)
- added : FastTween() & InitFastTween() (using of precalculated values)
- added : #Tween_FirstFX, #Tween_LastFX
Imagewin10 x64 5.72 | IDE | PB plugin | Tools | Sprite | JSON | visual tool
Kelebrindae
Enthusiast
Enthusiast
Posts: 151
Joined: Tue Apr 01, 2008 3:23 pm

Re: Animation & Motion tween FX

Post by Kelebrindae »

Brillant! Brillant, and most useful!

Thanks a lot, Eddy! :D
User avatar
eddy
Addict
Addict
Posts: 1479
Joined: Mon May 26, 2003 3:07 pm
Location: Nantes

Re: Animation & Motion tween FX

Post by eddy »

As soon as I know how to create a radialGradient sprite3D, I'll add some examples to compare Tween et FastTween.
Imagewin10 x64 5.72 | IDE | PB plugin | Tools | Sprite | JSON | visual tool
User avatar
eddy
Addict
Addict
Posts: 1479
Joined: Mon May 26, 2003 3:07 pm
Location: Nantes

Re: Animation & Motion tween FX

Post by eddy »

Updated
- added : FastTweenLoop( ) with loop count and loop modes (cycle, pingpong)
- changed constant prefix: FX_*

Image

Code: Select all

; *******************************
; EXAMPLE - SPRITE TWEEN ANIMATION
; *******************************

DisableExplicit

UsePNGImageEncoder() ;<--- used by EncodeImage
UsePNGImageDecoder() ;<--- used by CatchSprite

InitSprite() 
InitSprite3D() 
InitFastTween()


Structure FLARE
   x.i
   y.i
   z.i
   dx.i
   dy.i
   dz.i
   effectX.i
   effectY.i
   effectZ.i
   t.i
   durationXY.i
   durationZ.i
EndStructure
Global NewList flares.FLARE()
Procedure.i FlipBuffersAndRetrieveFPS()
   Static fpsCounter
   Static fpsStartTime
   Static fpsValue
   
   If FlipBuffers()
      fpsCounter +1
   EndIf
   
   If (ElapsedMilliseconds()-fpsStartTime) >= 1000
      fpsValue = fpsCounter
      fpsCounter = 0
      fpsStartTime = ElapsedMilliseconds()      
   EndIf
   
   ProcedureReturn fpsValue  
EndProcedure 

win=OpenWindow(#PB_Any, 0, 0, 610, 410, "Tween Sprite Animation",
               #PB_Window_SystemMenu | 
               #PB_Window_ScreenCentered)  
OpenWindowedScreen(WindowID(win),5,5,600,400,0,0,0, #PB_Screen_NoSynchronization)

;// CREATE "FLARE" SPRITE 3D
img=CreateImage(#PB_Any, 200, 200, 32 | #PB_Image_Transparent)  
If StartDrawing(ImageOutput(img))
   DrawingMode(#PB_2DDrawing_Gradient|
               #PB_2DDrawing_AlphaBlend|
               #PB_2DDrawing_Transparent) 
   BackColor(RGBA(255,255,255,255))
   GradientColor(0.3,RGBA(255,100,0,100))
   FrontColor(RGBA(255,0,0,0))      
   CircularGradient(100, 100, 100)   
   Circle(100, 100, 100)
   StopDrawing() 
   
   *imgMemory=EncodeImage(img,#PB_ImagePlugin_PNG,10,32) 
   If *imgMemory
      flare=CatchSprite(#PB_Any,*imgMemory,#PB_Sprite_Texture|#PB_Sprite_AlphaBlending) 
      flare3d=CreateSprite3D(#PB_Any, flare)
   EndIf 
EndIf 

For i=0 To 1000
   AddElement(flares())
   With flares()
      ;// RANDOM POSITION, MOVES & ANIMATIONS
      \x=Int(77*i*i*Cos(i*0.3)) % 600
      \y=Int(66*i*i*Sin(i*0.2)) % 400
      \z=\x % 5
      \dx=300+Cos(\x)*300
      \dy=200+Sin(\x)*200
      \dz=\y % 50
      \effectX=(i+7) % (#FX_LastValue+1)
      \effectY=(i+9) % (#FX_LastValue+1)
      \effectZ=(i+3) % (#FX_LastValue+1)
      \t=0
      \durationXY=((\x*\y*i) % 5000) + 5000
      \durationZ=\durationXY/3
   EndWith
Next 

DeltaMilliseconds()
Repeat
   dt=DeltaMilliseconds()
   
   ClearScreen(#Black)
   If Start3D()
      ForEach flares()
         With flares()
            \t+dt
            x=FastTweenLoop(\effectX,\t,\x,\dx,\durationXY,#FX_Loop_PingPong,2)
            y=FastTweenLoop(\effectY,\t,\y,\dy,\durationXY,#FX_Loop_PingPong,2)
            z=FastTweenLoop(\effectZ,\t,\z,\dz,\durationZ,#FX_Loop_PingPong,10)
            TransformSprite3D(flare3d,-z,-z,z,-z,z,z,-z,z)
            DisplaySprite3D(flare3d, x, y)             
         EndWith 
      Next
      Stop3D() 
   EndIf 
   fps=FlipBuffersAndRetrieveFPS()
   SetWindowTitle(win,"Frame Rate = " + Str(fps))
Until WindowEvent()=#PB_Event_CloseWindow 
Last edited by eddy on Mon Feb 11, 2013 11:35 pm, edited 1 time in total.
Imagewin10 x64 5.72 | IDE | PB plugin | Tools | Sprite | JSON | visual tool
dige
Addict
Addict
Posts: 1410
Joined: Wed Apr 30, 2003 8:15 am
Location: Germany
Contact:

Re: Animation & Motion tween FX

Post by dige »

It's magic! :D thx for sharing!!
"Daddy, I'll run faster, then it is not so far..."
User avatar
djes
Addict
Addict
Posts: 1806
Joined: Sat Feb 19, 2005 2:46 pm
Location: Pas-de-Calais, France

Re: Animation & Motion tween FX

Post by djes »

Really nice ! Thank you very much ! :D
User avatar
eddy
Addict
Addict
Posts: 1479
Joined: Mon May 26, 2003 3:07 pm
Location: Nantes

Re: Animation & Motion tween FX

Post by eddy »

Updated
- fixed FastTween : replace unsigned conversion => Int(...)
- updated FastTweenLoop : "max loop count" default value

Image

Code: Select all

; *******************************
; EXAMPLE - BOILING BUBBLES
; *******************************

DisableExplicit
UsePNGImageEncoder()
UsePNGImageDecoder()

InitSprite() 
InitSprite3D() 
InitFastTween()

Structure BUBBLE
   x.i
   y.i
   z.i
   a.i
   dx.i
   dy.i
   dz.i
   da.i
   t.i
   durationX.i
   durationY.i
   durationZ.i
EndStructure
Global NewList Bubbles.BUBBLE()

win=OpenWindow(#PB_Any, 0, 0, 600, 400, "Bubble Sprites",#PB_Window_SystemMenu |#PB_Window_ScreenCentered)  
OpenWindowedScreen(WindowID(win),0,0,600,400)

;// CREATE "BUBBLE" SPRITE 3D
img=CreateImage(#PB_Any, 200, 200, 32 | #PB_Image_Transparent)  
If StartDrawing(ImageOutput(img))
   DrawingMode(#PB_2DDrawing_Gradient|
               #PB_2DDrawing_AlphaBlend|
               #PB_2DDrawing_Transparent) 
   BackColor(RGBA(26, 96, 186,255))
   GradientColor(0.8,RGBA(35, 108, 203,255))
   FrontColor(RGBA(84, 155, 246,255))      
   CircularGradient(90, 90, 100)   
   Circle(100, 100, 100)
   
   DrawingMode(#PB_2DDrawing_AlphaChannel)
   FrontColor(RGBA(255,255,255,240))
   Circle(95, 95, 90)
   FrontColor(RGBA(255,255,255,200))
   Circle(75, 75, 60)
   
   StopDrawing() 
   
   *imgMemory=EncodeImage(img,#PB_ImagePlugin_PNG,10,32) 
   If *imgMemory
      bubble=CatchSprite(#PB_Any,*imgMemory,#PB_Sprite_Texture|#PB_Sprite_AlphaBlending) 
      bubble3D=CreateSprite3D(#PB_Any, bubble)
   EndIf 
EndIf 

For i=0 To 100
   AddElement(Bubbles())
   With Bubbles()
      ;// RANDOM POSITION, MOVES & ANIMATIONS
      \x=Random(580)+10
      \dx=Random(580)+10-\x
      \y=Random(50)+400
      \dy=-Random(50)-\y
      \z=Random(5,2)
      \dz=Random(20)
      \a=10
      \da=245
      \durationX=Random(15000)+5000
      \durationY=\durationX+1000
      \durationZ=\durationY
      \t=Random(10000)
   EndWith
Next 

t1=ElapsedMilliseconds()
Repeat
   t2=ElapsedMilliseconds()
   dt=t2-t1
   t1=t2
   
   ClearScreen(RGB(44, 82, 131))
   If Start3D()      
      ForEach Bubbles()
         With Bubbles()
            \t+dt
            x.f=FastTweenLoop(#FX_CubicEaseIn,\t,\x,\dx,\durationX,#FX_Loop_PingPong)
            y.f=FastTweenLoop(#FX_CircEaseOut,\t,\y,\dy,\durationY,#FX_Loop_Cycle)
            z.f=FastTweenLoop(#FX_ExpoEaseOut,\t,\z,\dz,\durationZ,#FX_Loop_Cycle)
            a.i=FastTweenLoop(#FX_Linear,\t,\a,\da,3000,#FX_Loop_PingPong)
            TransformSprite3D(bubble3D,-z,-z,z,-z,z,z,-z,z)
            DisplaySprite3D(bubble3D, x, y, a)             
         EndWith 
      Next
      Stop3D() 
   EndIf 
   FlipBuffers()
Until WindowEvent()=#PB_Event_CloseWindow
Imagewin10 x64 5.72 | IDE | PB plugin | Tools | Sprite | JSON | visual tool
User avatar
Kwai chang caine
Always Here
Always Here
Posts: 5494
Joined: Sun Nov 05, 2006 11:42 pm
Location: Lyon - France

Re: Animation & Motion tween FX

Post by Kwai chang caine »

Splendid !!! :shock:
Thanks a lot 8)
ImageThe happiness is a road...
Not a destination
Post Reply