Class: WindowBlessing::Widgets::Slider

Inherits:
WindowBlessing::Window show all
Includes:
Evented
Defined in:
lib/window_blessing/widgets/slider.rb

Instance Attribute Summary collapse

Attributes inherited from WindowBlessing::Window

#name

Attributes included from WindowBlessing::Window::Drawing

#buffer, #redraw_areas

Attributes included from WindowBlessing::Window::ParentsAndChildren

#children, #parent

Attributes included from WindowBlessing::Window::Geometry

#area

Attributes included from WindowBlessing::Window::KeyboardFocus

#focused, #focused_child

Instance Method Summary collapse

Methods included from Evented

#event_manager, #handle_event, #on

Methods inherited from WindowBlessing::Window

attr_accessor_with_redraw, #inspect, #route_pointer_event

Methods included from WindowBlessing::Window::Drawing

#add_redraw_area, #clear_redraw_areas, #draw, #draw_internal, #redraw, #redraw_requested?, #request_redraw, #request_redraw_internal

Methods included from WindowBlessing::Window::ParentsAndChildren

#add_child, #each_child, #each_child_with_index, #parent_path, #path, #remove_child

Methods included from WindowBlessing::Window::Geometry

#internal_area, #loc, #loc=, #move_onscreen, #pointer_inside?, #size, #size=

Methods included from WindowBlessing::Window::KeyboardFocus

#blur, #blurred?, #focus, #focused?, #route_keyboard_event

Methods included from Tools

#buffer, #clone_value, #color, #fill_line, #gen_array2d, #gray_screen_color, #log, #overlapping_span, #overlay2d, #overlay_span, #range_length, #resize2d, #rgb_screen_color, #subarray2d, #window

Constructor Details

#initialize(rect, evented_value, options = {}) ⇒ Slider

options

:key_press_step => 0.1


10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/window_blessing/widgets/slider.rb', line 10

def initialize(rect, evented_value, options={})
  rect.size.y = 1
  super rect
  @evented_value = evented_value = case evented_value
  when EventedVariable then evented_value
  when Float then EventedVariable.new(evented_value)
  else raise ArgumentError.new "invalid text type #{evented_value.inspect}(#{evented_value.class})"
  end
  self.bg = gray_screen_color(0.25)
  @key_press_step = options[:key_press_step] || 0.1

  on :pointer do |event|
    x = event[:loc].x
    evented_value.set bound(0.0, x / screen_value_range, 1.0)
  end

  evented_value.on :refresh do |event|
    request_redraw_internal
  end

  on :key_press do |event|
    case event[:key]
    when :home      then self.value = 0
    when :end       then self.value = 1
    when :left      then
      self.value = max(0.0, self.value - @key_press_step) if @key_press_step
    when :right     then
      self.value = min(1.0, self.value + @key_press_step) if @key_press_step
    end
  end
end

Instance Attribute Details

#backgroundObject (readonly)

Returns the value of attribute background.



5
6
7
# File 'lib/window_blessing/widgets/slider.rb', line 5

def background
  @background
end

#evented_valueObject (readonly)

Returns the value of attribute evented_value.



5
6
7
# File 'lib/window_blessing/widgets/slider.rb', line 5

def evented_value
  @evented_value
end

#key_press_stepObject

Returns the value of attribute key_press_step.



6
7
8
# File 'lib/window_blessing/widgets/slider.rb', line 6

def key_press_step
  @key_press_step
end

Instance Method Details

#draw_backgroundObject



50
51
52
53
# File 'lib/window_blessing/widgets/slider.rb', line 50

def draw_background
  super
  draw_handle
end

#draw_handleObject



46
47
48
# File 'lib/window_blessing/widgets/slider.rb', line 46

def draw_handle
  buffer.fill :area => handle_area, :string => "+"
end

#handle_areaObject



42
43
44
# File 'lib/window_blessing/widgets/slider.rb', line 42

def handle_area
  rect(point(handle_x,0),point(1,1))
end

#handle_xObject



55
# File 'lib/window_blessing/widgets/slider.rb', line 55

def handle_x;           (value * screen_value_range).to_i end

#screen_value_rangeObject



56
# File 'lib/window_blessing/widgets/slider.rb', line 56

def screen_value_range; size.x - 1.0 end

#valueObject



57
# File 'lib/window_blessing/widgets/slider.rb', line 57

def value;              evented_value.get end

#value=(v) ⇒ Object



58
# File 'lib/window_blessing/widgets/slider.rb', line 58

def value=(v)           evented_value.set(v) end