Class: Luck::Pane

Inherits:
Object
  • Object
show all
Defined in:
lib/luck/pane.rb

Direct Known Subclasses

Alert

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(display, x1, y1, x2, y2, title, controls = {}, &blck) ⇒ Pane

Returns a new instance of Pane.



5
6
7
8
9
10
11
12
13
14
# File 'lib/luck/pane.rb', line 5

def initialize display, x1, y1, x2, y2, title, controls={}, &blck
  @display = display
  @x1, @y1 = x1, y1
  @x2, @y2 = x2, y2
  @title, @controls = title, controls
  @dirty = false
  @visible = true
  
  instance_eval &blck if blck
end

Instance Attribute Details

#controlsObject

Returns the value of attribute controls.



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

def controls
  @controls
end

#dirtyObject Also known as: dirty?

Returns the value of attribute dirty.



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

def dirty
  @dirty
end

#displayObject

Returns the value of attribute display.



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

def display
  @display
end

#handlerObject

Returns the value of attribute handler.



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

def handler
  @handler
end

#titleObject

Returns the value of attribute title.



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

def title
  @title
end

#visibleObject Also known as: visible?

Returns the value of attribute visible.



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

def visible
  @visible
end

#x1Object

Returns the value of attribute x1.



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

def x1
  @x1
end

#x2Object

Returns the value of attribute x2.



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

def x2
  @x2
end

#y1Object

Returns the value of attribute y1.



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

def y1
  @y1
end

#y2Object

Returns the value of attribute y2.



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

def y2
  @y2
end

Instance Method Details

#[](control) ⇒ Object



16
17
18
# File 'lib/luck/pane.rb', line 16

def [] control
  @controls[control]
end

#control(name, type, *args, &blck) ⇒ Object



43
44
45
# File 'lib/luck/pane.rb', line 43

def control name, type, *args, &blck
  @controls[name] = type.new(self, *args, &blck)
end

#control_at(x, y) ⇒ Object



80
81
82
83
84
85
# File 'lib/luck/pane.rb', line 80

def control_at x, y
  @controls.values.each do |control|
    return control if (control.x1..control.x2).include?(x) && (control.y1..control.y2).include?(y)
  end
  nil
end

#dirty!Object



21
22
23
# File 'lib/luck/pane.rb', line 21

def dirty!
  @dirty = true
end

#draw_contentsObject



74
75
76
77
78
# File 'lib/luck/pane.rb', line 74

def draw_contents
  controls.each_value do |control|
    control.redraw
  end
end

#draw_frameObject



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/luck/pane.rb', line 98

def draw_frame
  linebar = 'q' * (width - 1)
  fillerbar = ' ' * (width - 1)

  @display.driver.linedrawing = true
  print @display.color('0;2')
  
  @display.place y1, x1, "n#{linebar}n"
  @display.place y2, x1, "n#{linebar}n"
  #~ @display.place y1, x1, "l#{topbar}k"
  #~ @display.place y2, x1, "m#{bottombar}j"

  (y1 + 1).upto y2 - 1 do |row|
    @display.place row, x1, "x#{fillerbar}x"
  end
  
  @display.driver.linedrawing = false
  print @display.color('0')
end

#draw_titleObject



118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/luck/pane.rb', line 118

def draw_title
  title = " * #{@title} * "
  left = (((width - 1).to_f / 2) - (title.size.to_f / 2)).to_i

  if title.size >= width
    title = @title[0, width - 3]
    left = 0
  end
  
  print @display.color('1;34')
  @display.place y1, x1 + 1 + left, title
  print @display.color('0')
end

#handle_click(button, modifiers, x, y) ⇒ Object



87
88
89
90
91
92
93
94
95
96
# File 'lib/luck/pane.rb', line 87

def handle_click button, modifiers, x, y
  control = control_at x, y
  if control
    if button == :left
      control.focus! if control.respond_to? :handle_char
      control.redraw
    end
    control.handle_click button, modifiers, x, y if control.respond_to?(:handle_click)
  end
end

#heightObject



64
65
66
# File 'lib/luck/pane.rb', line 64

def height
  y2 - y1
end

#hide!Object



27
# File 'lib/luck/pane.rb', line 27

def hide!; @visible = false; end

#on_submit(&blck) ⇒ Object



39
40
41
# File 'lib/luck/pane.rb', line 39

def on_submit &blck
  @handler = blck
end

#redrawObject



68
69
70
71
72
# File 'lib/luck/pane.rb', line 68

def redraw
  draw_frame
  draw_contents
  draw_title
end

#show!Object



26
# File 'lib/luck/pane.rb', line 26

def show!; @visible = true; end

#widthObject



61
62
63
# File 'lib/luck/pane.rb', line 61

def width
  x2 - x1
end

#yank_valuesObject



29
30
31
32
33
34
35
36
37
# File 'lib/luck/pane.rb', line 29

def yank_values
  vals = {}
  @controls.each_pair do |key, control|
    next unless control.respond_to? :value
    vals[key] = control.value
    control.value = ''
  end
  vals
end