class Fidgit::Slider

Attributes

handle[R]
range[R]
value[R]

Public Class Methods

new(options = {}, &block) click to toggle source

@param (see Composite#initialize)

@option (see Composite#initialize) @option options [Range] :range (0.0..1.0) @option options [Range] :value (minimum of :range)

Calls superclass method Fidgit::Composite::new
# File lib/fidgit/elements/slider.rb, line 54
def initialize(options = {}, &block)
  options = {
    range: 0.0..1.0,
    height: 25,
    background_color: default(:background_color),
    border_color: default(:border_color),
    groove_color: default(:groove_color),
    handle_color: default(:handle_color),
    groove_thickness: 5,
  }.merge! options

  @range = options[:range].dup
  @groove_color = options[:groove_color].dup
  @groove_thickness = options[:groove_thickness]
  @continuous = @range.min.is_a?(Float) || @range.max.is_a?(Float)

  super(options)

  @handle = Handle.new(parent: self, width: (height / 2 - padding_left), height: height - padding_top + padding_bottom,
                       background_color: options[:handle_color])

  self.value = options.has_key?(:value) ? options[:value] : @range.min
end

Public Instance Methods

handle_dragged_to(x, y) click to toggle source
# File lib/fidgit/elements/slider.rb, line 100
def handle_dragged_to(x, y)
  # In this case, x is the left-hand side fo the handle.
  self.value = ((x - self.x) / (width - @handle.width)) * (@range.max - @range.min) + @range.min
end
left_mouse_button(sender, x, y) click to toggle source
# File lib/fidgit/elements/slider.rb, line 92
def left_mouse_button(sender, x, y)
  # In this case, x should be the centre of the handle after it has moved.
  self.value = ((x - (@handle.width / 2) - self.x) / (width - @handle.width)) * (@range.max - @range.min) + @range.min
  @mouse_down = true

  nil
end
tip() click to toggle source
Calls superclass method
# File lib/fidgit/elements/slider.rb, line 87
def tip
  tip = super
  tip.empty? ? @value.to_s : "#{tip}: #{@value}"
end
value=(value) click to toggle source
# File lib/fidgit/elements/slider.rb, line 78
def value=(value)
  @value = @continuous ? value.to_f : value.round
  @value = [[@value, @range.min].max, @range.max].min
  @handle.x = x + padding_left + ((width - @handle.width) * (@value - @range.min) / (@range.max - @range.min).to_f)
  publish :changed, @value

  @value
end

Protected Instance Methods

draw_background() click to toggle source
Calls superclass method
# File lib/fidgit/elements/slider.rb, line 112
def draw_background
  super
  # Draw a groove for the handle to move along.
  draw_rect x + (@handle.width / 2), y + (height - @groove_thickness) / 2, width - @handle.width, @groove_thickness, z, @groove_color
  nil
end
layout() click to toggle source

Prevent standard packing layout change.

# File lib/fidgit/elements/slider.rb, line 107
def layout
  nil
end
post_init_block(&block) click to toggle source

Use block as an event handler.

# File lib/fidgit/elements/slider.rb, line 121
def post_init_block(&block)
  subscribe :changed, &block
end