lib/sup/horizontal_selector.rb (1291B) - raw
1 module Redwood
2
3 class HorizontalSelector
4 class UnknownValue < StandardError; end
5
6 attr_accessor :label, :changed_by_user
7
8 def initialize label, vals, labels, base_color=:horizontal_selector_unselected_color, selected_color=:horizontal_selector_selected_color
9 @label = label
10 @vals = vals
11 @labels = labels
12 @base_color = base_color
13 @selected_color = selected_color
14 @selection = 0
15 @changed_by_user = false
16 end
17
18 def set_to val
19 raise UnknownValue, val.inspect unless can_set_to? val
20 @selection = @vals.index(val)
21 end
22
23 def can_set_to? val
24 @vals.include? val
25 end
26
27 def val; @vals[@selection] end
28
29 def line width=nil
30 label =
31 if width
32 sprintf "%#{width}s ", @label
33 else
34 "#{@label} "
35 end
36
37 [[@base_color, label]] +
38 (0 ... @labels.length).inject([]) do |array, i|
39 array + [
40 if i == @selection
41 [@selected_color, @labels[i]]
42 else
43 [@base_color, @labels[i]]
44 end] + [[@base_color, " "]]
45 end + [[@base_color, ""]]
46 end
47
48 def roll_left
49 @selection = (@selection - 1) % @labels.length
50 @changed_by_user = true
51 end
52
53 def roll_right
54 @selection = (@selection + 1) % @labels.length
55 @changed_by_user = true
56 end
57 end
58
59 end