Guest User

Untitled

a guest
Feb 21st, 2018
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.94 KB | None | 0 0
  1. require 'widgets/slider_widget'
  2.  
  3. Shoes.app(:title => "slider test", :width => 200, :height => 250) do
  4.  
  5. stack do
  6. slider do |pos|
  7. @t.text = "pos: #{pos}"
  8. end
  9. end
  10. stack do
  11. @t = para "value"
  12. end
  13. end
  14.  
  15.  
  16. # put me in widgets/slider_widget
  17. class Shoes::Slider < Shoes::Widget
  18. attr_accessor :position
  19.  
  20. def initialize opts = {}, &blk
  21. @position = opts[:position] || 0
  22.  
  23. @mouse_down = false
  24.  
  25. @height = 248
  26. @width = 50
  27.  
  28. flow :top => self.top, :left => self.left, :width => 50, :fill => black do
  29. @back = rect :top => self.top,
  30. :left => self.left,
  31. :width => 50,
  32. :height => 248,
  33. :stroke => black,
  34. :fill => gray
  35.  
  36. @button = rect :top => ( @height - 48 ),
  37. :left => self.left + 1,
  38. :width => 48,
  39. :height => 48,
  40. :fill => chocolate
  41. @back.click do
  42. button, mouse_x, mouse_y = mouse
  43.  
  44. slider_move( mouse_y )
  45. set_position(mouse_y)
  46. blk.call(@position)
  47.  
  48. @mouse_down = true
  49.  
  50. motion do |x, y|
  51. if @mouse_down
  52. slider_move( y )
  53. set_position( y )
  54. blk.call(@position)
  55. end
  56. end # motion
  57. end
  58.  
  59. @back.release do
  60. @mouse_down = false
  61. end
  62.  
  63. end # flow
  64. end # initialize
  65.  
  66. def in_bounds?( y )
  67. ( y >= self.top + 24 ) && ( y <= self.top + @height - 24 )
  68. end
  69.  
  70. def slider_move( y )
  71. slider_x = self.left + 1
  72. slider_y = y - 24
  73. @button.move(slider_x, slider_y) if in_bounds?(y)
  74. end
  75.  
  76. def set_position( y )
  77. if y < 24
  78. @position = 200
  79. elsif y > self.top + @height - 24
  80. @position = 0
  81. elsif
  82. @position = ( y - 224 ).abs
  83. end
  84. end
  85. end
Add Comment
Please, Sign In to add comment