class RISlider
Attributes
actions[RW]
active[RW]
color[RW]
font[RW]
id[RW]
length[RW]
line_color[RW]
onchange[RW]
size[RW]
square[RW]
square_color[RW]
square_size[RW]
ticks[RW]
x[RW]
y[RW]
Public Class Methods
new(opts = [:length, :x, :y, :square_size, :ticks, :actions])
click to toggle source
# File lib/RIUI/RISlider.rb, line 5 def initialize(opts = [:length, :x, :y, :square_size, :ticks, :actions]) ### Initialize all variables and start functionality extend Ruby2D::DSL @x = opts[:x] || 0 @y = opts[:y] || 0 @length = opts[:length] || 100 @square_size = opts[:square_size] || 10 @ticks = opts[:ticks] || 10 @label_font = opts[:label_font] || '' @label_size = opts[:label_size] || 20 @label_color = opts[:label_color] || 'black' @line_color = opts[:line_color] || 'black' @square_color = opts[:square_color] || 'red' @actions = opts[:actions] @value = 0 @line = Line.new(x1: @x, y1: @y, x2: @x + @length, y2: y, color: @line_color) self.square = Square.new(size: @square_size, x: x - (@square_size/2), y: y - (@square_size/2), color: @square_color) setState(active: false) #@actions.add(self) # start_update end
Public Instance Methods
mouse_down_actions(x, y)
click to toggle source
# File lib/RIUI/RISlider.rb, line 50 def mouse_down_actions(x, y) if self.square.contains?(x, y) setState(active: true) end end
mouse_move_actions(x, y)
click to toggle source
# File lib/RIUI/RISlider.rb, line 58 def mouse_move_actions(x, y); end
mouse_up_actions()
click to toggle source
# File lib/RIUI/RISlider.rb, line 55 def mouse_up_actions setState(active: false) end
onChange(opts = [:onchange])
click to toggle source
# File lib/RIUI/RISlider.rb, line 45 def onChange(opts = [:onchange]) @onChange = opts[:onchange] end
reset()
click to toggle source
# File lib/RIUI/RISlider.rb, line 41 def reset self.square.x = @x - (@square_size/2) @value = 0 end
setColors(opts = [:square_color, :line_color])
click to toggle source
# File lib/RIUI/RISlider.rb, line 26 def setColors(opts = [:square_color, :line_color]) ### Sets the colors of the line and square @line_color = opts[:line_color] @square_color = opts[:square_color] @line.color = @line_color self.square.color = @square_color puts 'colors set' end
setLabel(opts = [:size, :font, :color])
click to toggle source
# File lib/RIUI/RISlider.rb, line 34 def setLabel(opts = [:size, :font, :color]) ### Creates a label for the slider @label_size = opts[:size] @label_font = opts[:font] @label_color = opts[:color] @label = Text.new(x: @x + @length + 15, y: @y, color: @label_color, font: @label_font, size: @label_size, text: '0') puts 'label set' end
update_actions()
click to toggle source
# File lib/RIUI/RISlider.rb, line 59 def update_actions check update_label changed = check_for_change(@old_value, @value) if changed @onChange.call end end
value()
click to toggle source
# File lib/RIUI/RISlider.rb, line 48 def value; @value; end
Private Instance Methods
check()
click to toggle source
# File lib/RIUI/RISlider.rb, line 86 def check ### Checks and updates the square's position while keeping it in bounds of the line @max = @x + @length @min = @x - self.square.size/2 @old_value = @value extend Ruby2D::DSL if self.active puts 'active' mouse_x = get :mouse_x if mouse_x < @max && mouse_x > @min self.square.x = mouse_x puts 'moved square' elsif mouse_x > @max self.square.x = @max elsif mouse_x < @min self.square.x = @min end end @value = ((self.square.x.to_f - @min.to_f) / @length.to_f) * @ticks @value = (@value).ceil - 1 if @value < 0; @value = 0; end if @value > @ticks; @value = ticks; end end
check_for_change(old_value, new_value)
click to toggle source
# File lib/RIUI/RISlider.rb, line 109 def check_for_change(old_value, new_value) if old_value == new_value false else true end end
setState(opts=[:active])
click to toggle source
# File lib/RIUI/RISlider.rb, line 69 def setState(opts=[:active]) self.active = opts[:active] end
start_update()
click to toggle source
# File lib/RIUI/RISlider.rb, line 121 def start_update ### Starts update function to be run every frame extend Ruby2D::DSL update do check update_label end end
update_label()
click to toggle source
# File lib/RIUI/RISlider.rb, line 117 def update_label ### Updates the slider's label when the value changes @label.text = @value end