Class: Fidgit::Slider

Inherits:
Composite show all
Defined in:
lib/fidgit/elements/slider.rb

Defined Under Namespace

Classes: Handle

Constant Summary

Constants inherited from Composite

Composite::DEBUG_BORDER_COLOR

Constants inherited from Element

Element::DEFAULT_SCHEMA_FILE, Element::VALID_ALIGN_H, Element::VALID_ALIGN_V

Instance Attribute Summary collapse

Attributes inherited from Packer

#spacing_h, #spacing_v

Attributes inherited from Element

#align_h, #align_v, #background_color, #border_thickness, #font, #padding_bottom, #padding_left, #padding_right, #padding_top, #parent, #z

Instance Method Summary collapse

Methods inherited from Container

#add, #button, #clear, #color_picker, #color_well, #combo_box, #file_browser, #grid, #group, #hit_element, #horizontal, #image_frame, #insert, #label, #list, #radio_button, #remove, #scroll_area, #scroll_window, #slider, #text_area, #to_s, #toggle_button, #update, #vertical, #write_tree, #x=, #y=

Methods inherited from Element

#default, #drag?, #draw, #draw_frame, #draw_rect, #enabled=, #enabled?, #height, #height=, #hit?, #max_height, #max_width, #min_height, #min_width, new, original_new, #outer_height, #outer_width, #recalc, schema, #to_s, #update, #width, #width=, #with, #x, #x=, #y, #y=

Methods included from Event

#events, included, new_event_handlers, #publish, #subscribe, #unsubscribe

Constructor Details

#initialize(options = {}, &block) ⇒ Slider

Returns a new instance of Slider.

Parameters:

  • options (Hash) (defaults to: {})

    a customizable set of options

Options Hash (options):

  • :range (Range) — default: 0.0..1.0
  • :value (Range) — default: minimum of :range


54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# 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

Instance Attribute Details

#handleObject (readonly)

Returns the value of attribute handle.



47
48
49
# File 'lib/fidgit/elements/slider.rb', line 47

def handle
  @handle
end

#rangeObject (readonly)

Returns the value of attribute range.



47
48
49
# File 'lib/fidgit/elements/slider.rb', line 47

def range
  @range
end

#valueObject

Returns the value of attribute value.



47
48
49
# File 'lib/fidgit/elements/slider.rb', line 47

def value
  @value
end

Instance Method Details

#handle_dragged_to(x, y) ⇒ Object



100
101
102
103
# 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) ⇒ Object



92
93
94
95
96
97
98
# 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

#tipObject



87
88
89
90
# File 'lib/fidgit/elements/slider.rb', line 87

def tip
  tip = super
  tip.empty? ? @value.to_s : "#{tip}: #{@value}"
end