]> git.cworth.org Git - sup/blob - lib/sup/horizontal-selector.rb
aef16d4844e2379d65c910332e4317eb758add19
[sup] / lib / sup / horizontal-selector.rb
1 module Redwood
2
3 class HorizontalSelector
4   attr_accessor :label
5
6   def initialize label, vals, labels, base_color=:horizontal_selector_unselected_color, selected_color=:horizontal_selector_selected_color
7     @label = label
8     @vals = vals
9     @labels = labels
10     @base_color = base_color
11     @selected_color = selected_color
12     @selection = 0
13   end
14
15   def set_to val; @selection = @vals.index(val) end
16
17   def val; @vals[@selection] end
18
19   def line width=nil
20     label =
21       if width
22         sprintf "%#{width}s ", @label
23       else
24         "#{@label} "
25       end
26
27     [[@base_color, label]] + 
28       (0 ... @labels.length).inject([]) do |array, i|
29         array + [
30           if i == @selection
31             [@selected_color, @labels[i]]
32           else
33             [@base_color, @labels[i]]
34           end] + [[@base_color, "  "]]
35       end + [[@base_color, ""]]
36   end
37
38   def roll_left
39     @selection = (@selection - 1) % @labels.length
40   end
41
42   def roll_right
43     @selection = (@selection + 1) % @labels.length
44   end
45 end
46
47 end