Class: Luck::Label

Inherits:
Control show all
Defined in:
lib/luck/label.rb

Direct Known Subclasses

Button, TextBox

Instance Attribute Summary collapse

Attributes inherited from Control

#display, #pane, #x1, #x2, #y1, #y2

Instance Method Summary collapse

Methods inherited from Control

#focus!, #height, #width

Constructor Details

#initialize(*args) ⇒ Label

Returns a new instance of Label.



5
6
7
8
9
# File 'lib/luck/label.rb', line 5

def initialize *args
  @alignment = :left
  @text = 'Label'
  super
end

Instance Attribute Details

#alignmentObject

Returns the value of attribute alignment.



3
4
5
# File 'lib/luck/label.rb', line 3

def alignment
  @alignment
end

#textObject

Returns the value of attribute text.



3
4
5
# File 'lib/luck/label.rb', line 3

def text
  @text
end

Instance Method Details

#align(direction) ⇒ Object



11
12
13
# File 'lib/luck/label.rb', line 11

def align direction
  @alignment = direction
end

#align_text(text) ⇒ Object



15
16
17
18
19
20
21
22
23
24
# File 'lib/luck/label.rb', line 15

def align_text text
  case @alignment
    when :center
      text.center width
    when :right
      text.rjust width
    else
      text.ljust width
  end
end

#redrawObject



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/luck/label.rb', line 26

def redraw
  if text.empty?
    @display.place y1, x1, ' ' * width
    return
  end
  
  row = y1
  text.each_line do |line|
    line.chomp!
    length = line.size
    offset = 0
    while offset < width || offset == 0
      @display.place row, x1, align_text(line[offset, width])
      row += 1
      offset += width
      return if row >= y2
    end
  end
end