Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- require 'widgets/slider_widget'
- Shoes.app(:title => "slider test", :width => 200, :height => 250) do
- stack do
- slider do |pos|
- @t.text = "pos: #{pos}"
- end
- end
- stack do
- @t = para "value"
- end
- end
- # put me in widgets/slider_widget
- class Shoes::Slider < Shoes::Widget
- attr_accessor :position
- def initialize opts = {}, &blk
- @position = opts[:position] || 0
- @mouse_down = false
- @height = 248
- @width = 50
- flow :top => self.top, :left => self.left, :width => 50, :fill => black do
- @back = rect :top => self.top,
- :left => self.left,
- :width => 50,
- :height => 248,
- :stroke => black,
- :fill => gray
- @button = rect :top => ( @height - 48 ),
- :left => self.left + 1,
- :width => 48,
- :height => 48,
- :fill => chocolate
- @back.click do
- button, mouse_x, mouse_y = mouse
- slider_move( mouse_y )
- set_position(mouse_y)
- blk.call(@position)
- @mouse_down = true
- motion do |x, y|
- if @mouse_down
- slider_move( y )
- set_position( y )
- blk.call(@position)
- end
- end # motion
- end
- @back.release do
- @mouse_down = false
- end
- end # flow
- end # initialize
- def in_bounds?( y )
- ( y >= self.top + 24 ) && ( y <= self.top + @height - 24 )
- end
- def slider_move( y )
- slider_x = self.left + 1
- slider_y = y - 24
- @button.move(slider_x, slider_y) if in_bounds?(y)
- end
- def set_position( y )
- if y < 24
- @position = 200
- elsif y > self.top + @height - 24
- @position = 0
- elsif
- @position = ( y - 224 ).abs
- end
- end
- end
Add Comment
Please, Sign In to add comment