Class: Rain::Stream

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

Constant Summary collapse

ARROW =
['', '']

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Stream.



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/matrix/stream.rb', line 11

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

  @event_tree = event_tree
  @event_cursor = 0
  @redraw_cursor = 0

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

  @head_cursor = Cursor.new
  @tail_cursor = Cursor.new
end

Instance Attribute Details

#colorsObject (readonly)

Returns the value of attribute colors.



7
8
9
# File 'lib/matrix/stream.rb', line 7

def colors
  @colors
end

#indexObject (readonly)

Returns the value of attribute index.



7
8
9
# File 'lib/matrix/stream.rb', line 7

def index
  @index
end

#outputsObject (readonly)

Returns the value of attribute outputs.



7
8
9
# File 'lib/matrix/stream.rb', line 7

def outputs
  @outputs
end

Instance Method Details

#redraw(cell_count:) ⇒ Object

Layout cells to fit the current cell count. Called on matrix initialization, screen size changes and on new events.



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/matrix/stream.rb', line 29

def redraw(cell_count:)
  @inputs.fill(nil, 0...cell_count)[0...cell_count]
  @delays.fill(@config.min_delay, 0...cell_count)[0...cell_count]
  @colors.fill(@config.cell_color, 0...cell_count)[0...cell_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

  @inputs
end

#render(duration: nil) ⇒ Object

Render a cell’s input as output after a delay, using cursors. Called on every frame. ┌─┐│R│ <– Each cell is represented as an input, delay and output. │e││q│ <– The head cursor outputs the cell’s input after a delay and colors the leading cell white. │ ││ │ <– The tail cursor does the same thing but the input (and therefore output) will be empty. └─┘ The tail cursor can be before or after the head cursor depending on whether events wrap around.

Unit tests use “duration” to skip forwards in time, while matrix spec and the real world use old fashioned linear time.



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/matrix/stream.rb', line 55

def render(duration: nil)
  @head_cursor.increment(delays:, inputs:, duration:) do |index|
    prev_index = (index - 1).clamp(0, nil)
    next_index = index >= @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