Class: Vizcore::DSL::TimelineBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/vizcore/dsl/timeline_builder.rb

Overview

Collects ordered timeline scene markers and converts them to transitions.

Defined Under Namespace

Classes: Point

Constant Summary collapse

DEFAULT_BEATS_PER_BAR =
4

Instance Method Summary collapse

Constructor Details

#initialize(beats_per_bar: DEFAULT_BEATS_PER_BAR) ⇒ TimelineBuilder

Returns a new instance of TimelineBuilder.



11
12
13
14
# File 'lib/vizcore/dsl/timeline_builder.rb', line 11

def initialize(beats_per_bar: DEFAULT_BEATS_PER_BAR)
  @beats_per_bar = positive_integer(beats_per_bar, "beats_per_bar")
  @entries = []
end

Instance Method Details

#at(position, scene:) ⇒ Hash

Add a scene marker at a timeline position.

Parameters:

  • position (Numeric, Point)

    seconds by default, or a value from ‘seconds`, `beats`, or `bars`

  • scene (Symbol, String)

    scene to activate at the position

Returns:

  • (Hash)


31
32
33
34
35
36
37
38
39
40
# File 'lib/vizcore/dsl/timeline_builder.rb', line 31

def at(position, scene:)
  point = normalize_position(position)
  entry = {
    at: point.value,
    unit: point.unit,
    scene: scene.to_sym
  }
  @entries << entry
  entry
end

#bars(value, beats_per_bar: nil) ⇒ Point

Parameters:

  • value (Numeric)

    bars from the timeline start

  • beats_per_bar (Integer, nil) (defaults to: nil)

    meter override

Returns:



57
58
59
60
# File 'lib/vizcore/dsl/timeline_builder.rb', line 57

def bars(value, beats_per_bar: nil)
  beats_per_measure = beats_per_bar.nil? ? @beats_per_bar : positive_integer(beats_per_bar, "beats_per_bar")
  beats(non_negative_float(value, "timeline bars") * beats_per_measure)
end

#beats(value) ⇒ Point

Parameters:

  • value (Numeric)

    beats from the timeline start

Returns:



50
51
52
# File 'lib/vizcore/dsl/timeline_builder.rb', line 50

def beats(value)
  Point.new(value: non_negative_float(value, "timeline beats"), unit: :beats)
end

#evaluate { ... } ⇒ Vizcore::DSL::TimelineBuilder

Evaluate a timeline block.

Yields:

  • Timeline marker definitions

Returns:



20
21
22
23
24
# File 'lib/vizcore/dsl/timeline_builder.rb', line 20

def evaluate(&block)
  instance_eval(&block) if block
  validate_entries!
  self
end

#seconds(value) ⇒ Point

Parameters:

  • value (Numeric)

    seconds from the timeline start

Returns:



44
45
46
# File 'lib/vizcore/dsl/timeline_builder.rb', line 44

def seconds(value)
  Point.new(value: non_negative_float(value, "timeline seconds"), unit: :seconds)
end

#to_hArray<Hash>

Returns serialized marker definitions.

Returns:

  • (Array<Hash>)

    serialized marker definitions



63
64
65
# File 'lib/vizcore/dsl/timeline_builder.rb', line 63

def to_h
  @entries.map(&:dup)
end

#transitionsArray<Hash>

Returns generated scene transitions.

Returns:

  • (Array<Hash>)

    generated scene transitions



68
69
70
71
72
73
74
75
76
77
# File 'lib/vizcore/dsl/timeline_builder.rb', line 68

def transitions
  @entries.each_cons(2).map do |from_entry, to_entry|
    delta = to_entry.fetch(:at) - from_entry.fetch(:at)
    {
      from: from_entry.fetch(:scene),
      to: to_entry.fetch(:scene),
      trigger: trigger_for(delta, from_entry.fetch(:unit))
    }
  end
end