lib/savio/Slider.rb in savio-0.1.2 vs lib/savio/Slider.rb in savio-0.1.3

- old
+ new

@@ -15,20 +15,31 @@ @length = args[:length] || 100 @min = args[:min] || 0 @max = args[:max] || 100 + @style = args[:style] || 'knob' + if @style != 'knob' && @style != 'fill' + @style = 'knob' + end + @value = args[:value] || rand(@min..@max) @showValue = args[:showValue] || true @labelColor = args[:labelColor] || '#F5F5F5' @sliderColor = args[:sliderColor] || '#757575' @knobColor = args[:knobColor] || '#5BB36A' build() end + def style=(style) + if style == 'knob' || style == 'fill' + @style = style + rebuild() + end + end def length=(length) @length = length.clamp(1, Window.width-@x) rebuild() end def showValue=(state) @@ -54,19 +65,30 @@ def max=(x) @max = x setValue(@value) end + def renderKnobTo(x) + if x.between?(@x, @x+@length) + case @style + when 'knob' + @knob.x = x + when 'fill' + @knob.width = x - @x + end + end + end + def moveKnob(x) if x.between?(@x, @x+@length) - @knob.x = x + renderKnobTo(x) to_max = @max to_min = @min from_max = @x + @length from_min = @x - pos = @knob.x + pos = x @value = (((to_max - to_min) * (pos - from_min)) / (from_max - from_min) + to_min) if @showValue == true @label.text = @value.round(2).to_s end end @@ -86,11 +108,11 @@ knobX = (((to_max - to_min) * (pos - from_min)) / (from_max - from_min) + to_min) @value = value if @showValue == true @label.text = @value.round(2).to_s end - @knob.x = knobX + renderKnobTo(knobX) else setValue(@min) end end @@ -119,23 +141,35 @@ width: @size, color: @sliderColor, z: @z ) - @knob = Circle.new( - x: @x, y: @y, - radius: @size * 1.2, - color: @knobColor, - z: @z+1 - ) + case @style + when 'knob' + @knob = Circle.new( + x: @x, y: @y, + radius: @size * 1.2, + color: @knobColor, + z: @z+1 + ) + when 'fill' + @knob = Rectangle.new( + x: @x, y: @y, + width: 0 , height: @size, + color: @knobColor, + z: @z+1 + ) + end @label = Text.new( @value.to_s, x: @x + @length + @size, y: @y - @size * 1.75, size: @size * 2.5, color: @labelColor, z: @z+1 ) + + @label.y = @y + @sliderLine.width / 2 - @label.height / 2 @nameLabel = Text.new( @displayName.to_s, x: @x, y: @y - @size * 3 - @size, size: @size * 2.5,