Class: Luck::TextBox

Inherits:
Label show all
Defined in:
lib/luck/textbox.rb

Direct Known Subclasses

CommandBox

Instance Attribute Summary collapse

Attributes inherited from Label

#alignment

Attributes inherited from Control

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

Instance Method Summary collapse

Methods inherited from Label

#align, #align_text

Methods inherited from Control

#focus!, #height, #width

Constructor Details

#initialize(*args) ⇒ TextBox

Returns a new instance of TextBox.



5
6
7
8
9
10
11
# File 'lib/luck/textbox.rb', line 5

def initialize *args
  @label = ''
  @text = 'Input box'
  @index = 0
  
  super
end

Instance Attribute Details

#handlerObject

Returns the value of attribute handler.



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

def handler
  @handler
end

#indexObject

Returns the value of attribute index.



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

def index
  @index
end

#labelObject

Returns the value of attribute label.



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

def label
  @label
end

#maskObject

Returns the value of attribute mask.



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

def mask
  @mask
end

#multilineObject

Returns the value of attribute multiline.



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

def multiline
  @multiline
end

Instance Method Details

#handle_char(char) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/luck/textbox.rb', line 49

def handle_char char
  if char == "\n" && !@multiline
    handler.call self, @text if handler
    self.value = ''
  elsif char == :backspace
    if @index > 0
      @index -= 1
      @text.slice! @index, 1
    end
  elsif char == :delete
    if @index < @text.size
      @text.slice! @index, 1
    end
  elsif char == :left
    @index -= 1 if @index > 0
  elsif char == :right
    @index += 1 if @index < @text.size
  elsif char == :home
    @index = 0
  elsif char == :end
    @index = @text.size
  elsif char.is_a? String
    if text.size < width
      @text.insert @index, char
      @index += 1
    end
  end
  redraw
end

#on_submit(&blck) ⇒ Object



27
28
29
# File 'lib/luck/textbox.rb', line 27

def on_submit &blck
  @handler = blck
end

#redrawObject



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

def redraw
  super
  
  case @alignment
    when :center
      text.center width
    when :right
      text.rjust width
    else
      @display.driver.set_cursor y1, x1 + text.size - @text.size + @index if @display.active_control == self
  end
  @display.driver.cursor = true if @display.active_control == self
end

#textObject



39
40
41
42
43
# File 'lib/luck/textbox.rb', line 39

def text
  text = @mask ? (@mask * value.size) : value
  text = "#{label}: #{text}" unless label.empty?
  text
end

#valueObject



35
36
37
# File 'lib/luck/textbox.rb', line 35

def value
  @text
end

#value=(val) ⇒ Object



31
32
33
34
# File 'lib/luck/textbox.rb', line 31

def value= val
  @text = val
  @index = @text.size if @index > @text.size
end