IceDragon200

[RUBY]Tween-easers

Jul 12th, 2012
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 5.76 KB | None | 0 0
  1. #-inject gen_class_header 'Tween::Easer'
  2. class Tween
  3.   class Easer
  4.     attr_accessor :name,:symbol
  5.     def initialize name=nil,&function
  6.       @name = name || ".Easer"
  7.       @symbol = :__easer
  8.       @function = function
  9.     end
  10.     def _ease *args,&block
  11.       @function.call *args,&block
  12.     end
  13.     def ease et, sv, ev, t, *args
  14.       _ease et, sv, ev-sv, t, *args
  15.     end
  16.   end
  17.   EASERS = []
  18.   EASER_BY_SYMBOL = {}
  19.   def self.add_easer str,&func
  20.     mod,name = str.split("::")
  21.     (name = mod; mod = nil) unless name
  22.     modu = self
  23.     if mod
  24.       module_eval %Q(module #{mod} ; end) # // Initialize module
  25.       modu = const_get(mod)
  26.     end
  27.     easer = modu.__send__(:const_set,name,Easer.new(str,&func))
  28.     sym   = easer.name.gsub('::',?_).downcase.to_sym
  29.     EASER_BY_SYMBOL[sym] = easer
  30.     EASER_BY_SYMBOL[sym].symbol = sym
  31.     EASERS.push(easer)
  32.   end
  33.   #-// IceDragon
  34.   #-// 01/26/2012
  35.   #-// 01/26/2012
  36.   #-// Null
  37.   add_easer 'Null::In' do |t, st, ch, d|
  38.     st
  39.   end
  40.   add_easer 'Null::Out' do |t, st, ch, d|
  41.     ch + st
  42.   end
  43.   #-// Linear
  44.   add_easer "Linear"  do |t, st, ch, d|
  45.     ch * t / d + st
  46.   end
  47.   #-// Sine
  48.   add_easer "Sine::In"  do |t, st, ch, d|
  49.     -ch * Math.cos(t / d * (Math::PI / 2)) + ch + st
  50.   end
  51.   add_easer "Sine::Out"  do |t, st, ch, d|
  52.     ch * Math.sin(t / d * (Math::PI / 2)) + st
  53.   end
  54.   add_easer "Sine::InOut" do |t, st, ch, d|
  55.     -ch / 2 * (Math.cos(Math::PI * t / d) - 1) + st
  56.   end
  57.   #-// Circ
  58.   add_easer "Circ::In" do |t, st, ch, d|
  59.     -ch * (Math.sqrt(1 - (t/d) * t/d) - 1) + st rescue st
  60.   end
  61.   add_easer "Circ::Out" do |t, st, ch, d|
  62.     t = t/d - 1 ; ch * Math.sqrt(1 - t * t) + st rescue st
  63.   end
  64.   add_easer "Circ::InOut" do |t, st, ch, d|
  65.     (t /= d/2.0) < 1 ?
  66.      -ch / 2 * (Math.sqrt(1 - t*t) - 1) + st :
  67.       ch / 2 * (Math.sqrt(1 - (t -= 2) * t) + 1) + st rescue st
  68.   end
  69.   #-// Bounce
  70.   add_easer "Bounce::Out" do |t, st, ch, d|
  71.     if (t /= d) < (1/2.75)
  72.       ch * (7.5625 * t * t) + st
  73.     elsif t < (2 / 2.75)
  74.       ch * (7.5625 * (t -= (1.5 / 2.75)) * t + 0.75) + st
  75.     elsif t < (2.5 / 2.75)
  76.       ch * (7.5625 * (t -= (2.25 / 2.75)) * t + 0.9375) + st
  77.     else
  78.       ch * (7.5625 * (t -= (2.625 / 2.75)) * t + 0.984375) + st
  79.     end
  80.   end
  81.   add_easer "Bounce::In" do |t, st, ch, d|
  82.     ch - Bounce::Out.ease(d-t, 0, ch, d) + st
  83.   end
  84.   add_easer "Bounce::InOut"  do |t, st, ch, d|
  85.     t < d/2.0 ?
  86.       Bounce::In.ease(t*2.0, 0, ch, d) * 0.5 + st :
  87.       Bounce::Out.ease(t*2.0 - d, 0, ch, d) * 0.5 + ch * 0.5 + st
  88.   end
  89.   #-// Back
  90.   add_easer "Back::In" do |t, st, ch, d, s=1.70158|
  91.     ch * (t/=d) * t * ((s+1) * t - s) + st
  92.   end
  93.   add_easer "Back::Out" do |t, st, ch, d, s=1.70158|
  94.     ch * ((t=t/d-1) * t * ((s+1) * t + s) + 1) + st
  95.   end
  96.   add_easer "Back::InOut" do |t, st, ch, d, s=1.70158|
  97.     (t /= d/2.0) < 1 ?
  98.       ch / 2.0 * (t * t * (((s *= (1.525)) + 1) * t - s)) + st :
  99.       ch / 2.0 * ((t -= 2) * t * (((s *= (1.525)) + 1) * t + s) + 2) + st
  100.   end
  101.   #-// Cubic
  102.   add_easer "Cubic::In" do |t, st, ch, d|
  103.     ch * (t /= d) * t * t + st
  104.   end
  105.   add_easer "Cubic::Out" do |t, st, ch, d|
  106.     ch * ((t = t / d.to_f - 1) * t * t + 1) + st
  107.   end
  108.   add_easer "Cubic::InOut" do |t, st, ch, d|
  109.     (t /= d / 2.0) < 1 ?
  110.       ch / 2.0 * t * t * t + st :
  111.       ch / 2.0 * ((t -= 2) * t * t + 2) + st
  112.   end
  113.   #-// Expo
  114.   add_easer "Expo::In" do |t, st, ch, d|
  115.     t == 0 ? st : ch * (2 ** (10 * (t / d.to_f - 1))) + st
  116.   end
  117.   add_easer "Expo::Out" do |t, st, ch, d|
  118.     t == d ? st + ch : ch * (-(2 ** (-10 * t / d.to_f)) + 1) + st
  119.   end
  120.   add_easer "Expo::InOut" do |t, st, ch, d|
  121.     if t == 0                ; st
  122.     elsif t == d             ; st + ch
  123.     elsif (t /= d / 2.0) < 1 ; ch / 2.0 * (2 ** (10 * (t - 1))) + st
  124.     else                     ; ch / 2.0 * (-(2 ** (-10 * (t -= 1))) + 2) + st
  125.     end
  126.   end
  127.   #-// Quad
  128.   add_easer "Quad::In" do |t, st, ch, d|
  129.     ch * (t /= d.to_f) * t + st
  130.   end
  131.   add_easer "Quad::Out" do |t, st, ch, d|
  132.     -ch * (t /= d.to_f) * (t - 2) + st
  133.   end
  134.   add_easer "Quad::InOut" do |t, st, ch, d|
  135.     (t /= d / 2.0) < 1 ?
  136.       ch / 2.0 * t ** 2 + st :
  137.       -ch / 2.0 * ((t -= 1) * (t - 2) - 1) + st
  138.   end
  139.   #-// Quart
  140.   add_easer "Quart::In" do |t, st, ch, d|
  141.     ch * (t /= d.to_f) * t ** 3 + st
  142.   end
  143.   add_easer "Quart::Out" do |t, st, ch, d|
  144.     -ch * ((t = t / d.to_f - 1) * t ** 3 - 1) + st
  145.   end
  146.   add_easer "Quart::InOut" do |t, st, ch, d|
  147.     (t /= d / 2.0) < 1 ?
  148.       ch / 2.0 * t ** 4 + st :
  149.       -ch / 2.0 * ((t -= 2) * t ** 3 - 2) + st
  150.   end
  151.   #-// Quint
  152.   add_easer "Quint::In" do |t, st, ch, d|
  153.     ch * (t /= d.to_f) * t ** 4 + st
  154.   end
  155.   add_easer "Quint::Out" do |t, st, ch, d|
  156.     ch * ((t = t / d.to_f - 1) * t ** 4 + 1) + st
  157.   end
  158.   add_easer "Quint::InOut" do |t, st, ch, d|
  159.     (t /= d / 2.0) < 1 ?
  160.       ch / 2.0 * t ** 5 + st :
  161.       ch / 2.0 * ((t -= 2) * t ** 4 + 2) + st
  162.   end
  163.   #-// Elastic
  164.   add_easer "Elastic::In" do |t, st, ch, d, a = 5, p = 0|
  165.     s = 0
  166.     if t == 0
  167.       st
  168.     elsif (t /= d.to_f) >= 1
  169.       st + ch
  170.     else
  171.       p = d * 0.3 if p == 0
  172.       if (a == 0) || (a < ch.abs)
  173.         a = ch
  174.         s = p / 4.0
  175.       else
  176.         s = p / (2 * Math::PI) * Math.asin(ch / a.to_f)
  177.       end
  178.       -(a * (2 ** (10 * (t -= 1))) * Math.sin( (t * d - s) * (2 * Math::PI) / p)) + st
  179.     end
  180.   end
  181.   add_easer "Elastic::Out" do |t, st, ch, d, a = 5, p = 0|
  182.     s = 0
  183.     if t == 0
  184.       st
  185.     elsif (t /= d.to_f) >= 1
  186.       st + ch
  187.     else
  188.       p = d * 0.3 if p == 0
  189.       if (a == 0) || (a < ch.abs)
  190.         a = ch
  191.         s = p / 4.0
  192.       else
  193.         s = p / (2 * Math::PI) * Math.asin(ch / a.to_f)
  194.       end
  195.       a * (2 ** (-10 * t)) * Math.sin((t * d - s) * (2 * Math::PI) / p.to_f) + ch + st
  196.     end
  197.   end
  198. end
Advertisement
Add Comment
Please, Sign In to add comment