Class: Stagecraft::Animation::Track

Inherits:
Object
  • Object
show all
Defined in:
lib/stagecraft/animation/track.rb

Constant Summary collapse

PATH_COMPONENTS =
{ translation: 3, rotation: 4, scale: 3 }.freeze
INTERPOLATIONS =
%i[linear step cubicspline].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(times:, values:, target:, target_path:, interpolation: :linear, value_size: nil) ⇒ Track

Returns a new instance of Track.

Raises:

  • (ArgumentError)


11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/stagecraft/animation/track.rb', line 11

def initialize(times:, values:, target:, target_path:, interpolation: :linear, value_size: nil)
  @times = unpack(times)
  @values = unpack(values)
  @target = target
  @target_path = target_path.to_sym
  @interpolation = interpolation.to_sym
  raise ArgumentError, "unsupported interpolation #{interpolation.inspect}" unless INTERPOLATIONS.include?(@interpolation)
  raise ArgumentError, "animation times cannot be empty" if @times.empty?
  raise ArgumentError, "animation times must be sorted" unless @times.each_cons(2).all? { |left, right| left <= right }

  @value_size = value_size || infer_value_size
  expected = @times.length * @value_size * (cubicspline? ? 3 : 1)
  raise ArgumentError, "animation value count #{values_count} does not match expected #{expected}" unless values_count == expected
end

Instance Attribute Details

#interpolationObject (readonly)

Returns the value of attribute interpolation.



9
10
11
# File 'lib/stagecraft/animation/track.rb', line 9

def interpolation
  @interpolation
end

#targetObject (readonly)

Returns the value of attribute target.



9
10
11
# File 'lib/stagecraft/animation/track.rb', line 9

def target
  @target
end

#target_pathObject (readonly)

Returns the value of attribute target_path.



9
10
11
# File 'lib/stagecraft/animation/track.rb', line 9

def target_path
  @target_path
end

#timesObject (readonly)

Returns the value of attribute times.



9
10
11
# File 'lib/stagecraft/animation/track.rb', line 9

def times
  @times
end

#value_sizeObject (readonly)

Returns the value of attribute value_size.



9
10
11
# File 'lib/stagecraft/animation/track.rb', line 9

def value_size
  @value_size
end

#valuesObject (readonly)

Returns the value of attribute values.



9
10
11
# File 'lib/stagecraft/animation/track.rb', line 9

def values
  @values
end

Instance Method Details

#durationObject



26
27
28
# File 'lib/stagecraft/animation/track.rb', line 26

def duration
  times.last
end

#sample(time, out = nil) ⇒ Object



30
31
32
33
34
# File 'lib/stagecraft/animation/track.rb', line 30

def sample(time, out = nil)
  value = sample_value(Float(time))
  assign(out, value) if out
  value
end