class Tsuku::Tween

Public Class Methods

new(target, final_property_values, duration_ms, easing = :linear) click to toggle source
# File lib/tsuku/tween.rb, line 3
def initialize(target, final_property_values, duration_ms, easing = :linear)
  @target = target
  @final_property_values = final_property_values
  @duration_ms = duration_ms
  @easing = easing

  @running = false
  @elapsed_ms = 0.0
end

Public Instance Methods

completed?() click to toggle source
# File lib/tsuku/tween.rb, line 48
def completed?
  @elapsed_ms >= @duration_ms
end
pause() click to toggle source
# File lib/tsuku/tween.rb, line 36
def pause
  @running = false
end
reset() click to toggle source
# File lib/tsuku/tween.rb, line 44
def reset
  @elapsed_ms = 0
end
resume() click to toggle source
# File lib/tsuku/tween.rb, line 40
def resume
  @running = true unless completed?
end
start() click to toggle source
# File lib/tsuku/tween.rb, line 13
def start
  @initial_property_values = {}

  @final_property_values.each_key do |k|
    @initial_property_values[k] = @target.send(k)
  end

  @running = true
end
step(delta_ms) click to toggle source
# File lib/tsuku/tween.rb, line 23
def step(delta_ms)    
  return if !@running
  
  @elapsed_ms += delta_ms

  if @elapsed_ms >= @duration_ms
    complete
    return
  end

  advance_property_values
end

Private Instance Methods

advance_property_values() click to toggle source
# File lib/tsuku/tween.rb, line 54
def advance_property_values
  @final_property_values.each_key do |k|
    # Based on Penner's equations
    t = @elapsed_ms
    b = @initial_property_values[k]
    c = @final_property_values[k] - @initial_property_values[k]
    d = @duration_ms
    
    current_value = Easing.method(@easing).call(t, b, c, d)
    
    @target.send("#{k}=", current_value)
  end
end
complete() click to toggle source
# File lib/tsuku/tween.rb, line 68
def complete
  @elapsed_ms = @duration_ms
  
  @final_property_values.each do |k,v|
    @target.send("#{k}=", v)
  end

  @running = false
end