class CyberarmEngine::Element::Slider

Attributes

range[R]
step_size[R]
value[R]

Public Class Methods

new(options = {}, block = nil) click to toggle source
Calls superclass method CyberarmEngine::Element::Container::new
# File lib/cyberarm_engine/ui/elements/slider.rb, line 38
def initialize(options = {}, block = nil)
  super(options, block)

  @range     = @options[:range] || (0.0..1.0)
  @step_size = @options[:step] || 0.1
  @value     = @options[:value] || (@range.first + @range.last) / 2

  @handle = Handle.new("", parent: self, width: 8, height: 1.0) { close }
  add(@handle)
end

Public Instance Methods

draw() click to toggle source
Calls superclass method CyberarmEngine::Element#draw
# File lib/cyberarm_engine/ui/elements/slider.rb, line 70
def draw
  super

  @handle.draw
end
handle_dragged_to(x, _y) click to toggle source
# File lib/cyberarm_engine/ui/elements/slider.rb, line 89
def handle_dragged_to(x, _y)
  @ratio = ((x - @handle.width / 2) - @x) / (content_width - @handle.outer_width)

  self.value = @ratio.clamp(0.0, 1.0) * (@range.max - @range.min) + @range.min
end
holding_left_mouse_button(_sender, x, y) click to toggle source
# File lib/cyberarm_engine/ui/elements/slider.rb, line 83
def holding_left_mouse_button(_sender, x, y)
  handle_dragged_to(x, y)

  :handled
end
position_handle() click to toggle source
# File lib/cyberarm_engine/ui/elements/slider.rb, line 63
def position_handle
  @handle.x = @x + @style.padding_left + @style.border_thickness_left +
              ((content_width - @handle.outer_width) * (@value - @range.min) / (@range.max - @range.min).to_f)

  @handle.y = @y + @style.border_thickness_top + @style.padding_top
end
recalculate() click to toggle source
# File lib/cyberarm_engine/ui/elements/slider.rb, line 49
def recalculate
  _width = dimensional_size(@style.width, :width)
  _height = dimensional_size(@style.height, :height)

  @width  = _width
  @height = _height

  position_handle
  @handle.recalculate
  @handle.update_background

  update_background
end
update() click to toggle source
# File lib/cyberarm_engine/ui/elements/slider.rb, line 76
def update
  super

  @tip = value.to_s
  @handle.tip = @tip
end
value=(n) click to toggle source
# File lib/cyberarm_engine/ui/elements/slider.rb, line 95
def value=(n)
  @value = n
  position_handle
  @handle.recalculate

  publish(:changed, @value)
end