Created
December 23, 2008 18:55
-
-
Save aberant/39419 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment