Guest User

Untitled

a guest
Feb 19th, 2018
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.82 KB | None | 0 0
  1. class LookLookSee < Shoes
  2. url '/', :index
  3.  
  4. # TODO Use actual Shoes::Color objects for these values?
  5. BACKGROUND = [20,20,20]
  6. START_COLOR = [24,24,24]
  7. HOVER_COLOR = [36,36,36]
  8. PIXEL_SIZE = 20
  9. ANIMATING = { }
  10. PAINTED = { }
  11. @@painting = false
  12. @@paint_color = [240,240,240]
  13.  
  14. # A pixel-painting app for Shoes
  15. def index
  16. nostroke
  17. background rgb(*BACKGROUND)
  18. release { @@painting = false }
  19.  
  20. 500.times do
  21. # TODO Subclass widget with a Pixel class
  22. stack :width => PIXEL_SIZE, :height => PIXEL_SIZE, :margin_left => 1, :margin_bottom => 1 do
  23. background rgb(*START_COLOR)
  24. hover { |p| handle_hover(p) unless PAINTED[p] }
  25. leave { |p| handle_leave(p) unless PAINTED[p] }
  26. click { @@painting = true }
  27. end
  28. end
  29.  
  30. button("Reset") do
  31. @@painting = false
  32. PAINTED.keys.each do |p|
  33. PAINTED.delete(p)
  34. handle_leave(p)
  35. end
  36. end
  37.  
  38. button("Color") do
  39. m = ask_color("Pick a Color").inspect.match(/rgb\((\d+), (\d+), (\d+)\)/)
  40. @@paint_color = [m[1], m[2], m[3]].map { |p| p.to_i }
  41. end
  42.  
  43. animate(60) do
  44. ANIMATING.each { |p, c| fade(p, c) }
  45. end
  46. end
  47.  
  48. private
  49.  
  50. def handle_hover(pixel)
  51. @@painting ? paint(pixel) : pixel.background(rgb(*HOVER_COLOR))
  52. end
  53.  
  54. def handle_leave(pixel)
  55. ANIMATING[pixel] = HOVER_COLOR.first unless @@painting
  56. end
  57.  
  58. def fade(pixel, color)
  59. ANIMATING.delete(pixel) and return if color <= START_COLOR.first
  60. ANIMATING[pixel] = color -= 2
  61. pixel.background rgb(color,color,color)
  62. end
  63.  
  64. def paint(pixel)
  65. return if PAINTED[pixel]
  66. pixel.background rgb(*@@paint_color)
  67. PAINTED[pixel] = true
  68. end
  69. end
  70.  
  71. Shoes.app :height => 436, :width => 500, :title => 'looklooksee'
Add Comment
Please, Sign In to add comment