Class: Rain::Stream

Inherits:
Object
  • Object
show all
Includes:
Observers
Defined in:
lib/matrix/stream.rb

Constant Summary collapse

ARROW =
['', ''].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(index:, config:, event_tree:) ⇒ Stream

Returns a new instance of Stream.



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/matrix/stream.rb', line 15

def initialize(index:, config:, event_tree:)
  @index = index
  @config = config
  @event_tree = event_tree

  @inputs = []
  @delays = []
  @colors = []
  @outputs = []

  @event_cursor = 0
  @head_cursor = Cursor.new
  @tail_cursor = Cursor.new
  @tail_cursor.index = 0

  @show_cursor = Cursor.new
  @hide_cursor = Cursor.new

  observe event_tree
end

Instance Attribute Details

#colorsObject (readonly)

Returns the value of attribute colors.



11
12
13
# File 'lib/matrix/stream.rb', line 11

def colors
  @colors
end

#delaysObject (readonly)

Returns the value of attribute delays.



11
12
13
# File 'lib/matrix/stream.rb', line 11

def delays
  @delays
end

#head_cursorObject (readonly)

Returns the value of attribute head_cursor.



11
12
13
# File 'lib/matrix/stream.rb', line 11

def head_cursor
  @head_cursor
end

#indexObject (readonly)

Returns the value of attribute index.



11
12
13
# File 'lib/matrix/stream.rb', line 11

def index
  @index
end

#inputsObject (readonly)

Returns the value of attribute inputs.



11
12
13
# File 'lib/matrix/stream.rb', line 11

def inputs
  @inputs
end

#outputsObject (readonly)

Returns the value of attribute outputs.



11
12
13
# File 'lib/matrix/stream.rb', line 11

def outputs
  @outputs
end

#tail_cursorObject (readonly)

Returns the value of attribute tail_cursor.



11
12
13
# File 'lib/matrix/stream.rb', line 11

def tail_cursor
  @tail_cursor
end

Instance Method Details

#branch(event: Low::Events::BranchEvent) ⇒ Object

TODO: Use “on :branch do |event|” syntax.



37
38
39
# File 'lib/matrix/stream.rb', line 37

def branch(event: Low::Events::BranchEvent) # rubocop:disable Lint/UnusedMethodArgument
  redraw(cell_count: @inputs.count)
end

#redraw(cell_count:) ⇒ Object

Draw event names onto the current amount of cells in a stream, using cursors. Called when there’s a new event.

INPUT DELAY OUTPUT

┌─────┬─────┬─────┐│ R │ 75 │ │ ◀── 2. Tail Cursor begins at index zero or a random starting index. │ e │ 75 │ │ A Head Cursor that wraps around will push the Tail Cursor down to be just beneath it. │ q │ 75 │ │ The Show Cursor will start at the Tail Cursor index. │ u │ 75 │ ││ │ 75 │ │ ◀── 1. Head Cursor begins at index zero or a random starting index. │ │ 75 │ │ It populates input for every character in an event name. │ │ 75 │ │ Then sets a “75” delay for the Show Cursor. └─────┴─────┴─────┘



53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/matrix/stream.rb', line 53

def redraw(cell_count:)
  randomize_start_index if first_cell_redraw? && @config.start_row == :random
  resize_cells(cell_count:) if cell_count != @inputs.count

  (@event_cursor...@event_tree.sequence.count).each do |event_index|
    current_event = @event_tree.sequence[event_index]
    past_event = @event_tree.sequence[event_index - 1]

    redraw_event(current_event:, past_event:)
    @event_cursor += 1
  end
end

#render(duration: nil) ⇒ Object

Render a cell’s input as output after a delay, using cursors. Called on every frame.

INPUT DELAY OUTPUT

┌─────┬─────┬─────┐│ │ 0 │ │ ◀── 2. Hide Cursor moves the input to the output after a delay. │ │ 250 │ e │ The nil input replaces the previous output of “R”. │ │ 250 │ q ││ │ 250 │ u │ ◀── 1. Show Cursor moves the input to the output after a delay. │ e │ 75 │ │ Leaving behind nil input. │ s │ 75 │ │ Then sets a “250” delay for the Hide Cursor. │ t │ 75 │ │└─────┴─────┴─────┘

TODO: Refactor “@colors” into an Effect that happens dynamically on render rather than stored as a column of data.

This will reduce the "Metrics/AbcSize" complexity.


81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/matrix/stream.rb', line 81

def render(duration: nil) # rubocop:disable Metrics/AbcSize
  @show_cursor.increment(delays:, inputs:, duration:) do |index|
    prev_index = index.zero? ? @inputs.count - 1 : index - 1
    next_index = index + 1 >= @inputs.count ? 0 : index + 1

    if @inputs[index]
      @outputs[index] = @inputs[index]
      @delays[index] = @config.fade_delay
      @colors[prev_index] = @config.cell_color if @colors[prev_index]
      @colors[index] = @outputs[next_index] ? @config.cell_color : @config.lead_color
      @inputs[index] = nil
    end
  end

  fade(duration:) if @config.fade
end