Class: RETerm::Components::RevealingLabel

Inherits:
RETerm::Component show all
Defined in:
lib/reterm/components/revealing_label.rb

Overview

Text that is revealed incrementally over time

Constant Summary

Constants included from LogHelpers

LogHelpers::LOG_FILE

Instance Attribute Summary collapse

Attributes inherited from RETerm::Component

#activatable, #activate_focus, #highlight_focus, #window

Instance Method Summary collapse

Methods inherited from RETerm::Component

#activatable?, #activate!, #activate_focus?, #cdk?, #colored?, #colors=, #deactivate!, #deactivate?, #distance_from, #extra_padding, #finalize!, #highlight_focus?, #reactivate!, #resize, #sync_getch

Methods included from KeyBindings

#bind_key, #invoke_key_bindings, #key_bindings, #key_bound?

Methods included from LogHelpers

#logger

Methods included from EventDispatcher

#dispatch, #handle

Constructor Details

#initialize(args = {}) ⇒ RevealingLabel

Initialize the RevealingLabel component

Options Hash (args):

  • :text (String)

    text of the label



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/reterm/components/revealing_label.rb', line 14

def initialize(args={})
  activate_sync!

  super
  self.text      = args[:text] || ""

  # time between characters being revealed
  @char_time = args[:char_time] || 0.01

  # time wait after a line is displayed before starting new
  @line_wait = args[:line_wait] || 3

  @draw_next = false
  @timestamp = nil
end

Instance Attribute Details

#textObject

Returns the value of attribute text.



8
9
10
# File 'lib/reterm/components/revealing_label.rb', line 8

def text
  @text
end

Instance Method Details

#draw!Object



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/reterm/components/revealing_label.rb', line 79

def draw!
  @current_line  ||= 0
  @current_index ||= 0

  return unless @draw_next
  @draw_next = false

  current = ((lines.size > @current_line) &&
             (lines[@current_line].size > @current_index)) ?
              lines[@current_line][@current_index] :
              ""

  dispatch :start_reveal if @current_line  == 0 &&
                            @current_index == 0

  @timestamp = DateTime.now
  window.mvaddstr(@current_line  + 1,
                  @current_index + 1,
                  current)

  dispatch :revealed_char, current

  set_next
end

#empty?Boolean



37
38
39
# File 'lib/reterm/components/revealing_label.rb', line 37

def empty?
  @text.nil? || @text.length == 0
end

#eraseObject



119
120
121
122
123
# File 'lib/reterm/components/revealing_label.rb', line 119

def erase
  lines.each_with_index { |l, i|
    window.mvaddstr(i+1, 1, " " * l.size)
  }
end

#linesObject



41
42
43
# File 'lib/reterm/components/revealing_label.rb', line 41

def lines
  @lines ||= text.split("\n")
end

#requested_colsObject



49
50
51
52
# File 'lib/reterm/components/revealing_label.rb', line 49

def requested_cols
  empty? ? 1 :
  lines.max { |l1, l2| l1.size <=> l2.size }.size + 1
end

#requested_rowsObject



45
46
47
# File 'lib/reterm/components/revealing_label.rb', line 45

def requested_rows
  lines.size + 1
end

#reset!Object



74
75
76
77
# File 'lib/reterm/components/revealing_label.rb', line 74

def reset!
  @current_line  = 0
  @current_index = 0
end

#set_nextObject



104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/reterm/components/revealing_label.rb', line 104

def set_next
  return if empty?
  @current_index += 1

  if @current_index >= lines[@current_line].size
    @current_index = 0
    @current_line += 1
  end

  if @current_line >= lines.size
    @current_line = 0
    dispatch :revealed
  end
end

#sync!Object



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/reterm/components/revealing_label.rb', line 54

def sync!
  if @timestamp.nil?
    @draw_next = true

  else
    if @current_index == 0
      if (DateTime.now - @timestamp) * 24 * 60 * 60 > @line_wait
        @draw_next = true
      end

    else
      if (DateTime.now - @timestamp) * 24 * 60 * 60 > @char_time
        @draw_next = true
      end
    end
  end

  draw!
end