Guest User

Untitled

a guest
Feb 20th, 2018
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.84 KB | None | 0 0
  1. # change the line below ("image" or "stack")
  2. $meth = "image"
  3.  
  4. class Drawer
  5. def self.drawit_in_stack(w, h)
  6. $app.stroke gray(0.2)
  7. 1000.times do |i|
  8. $app.fill gray(rand*0.6)
  9. $app.rect rand(w), rand(h), 10, 10
  10. $app.inscription "foo", :top => rand(h), :left => rand(w)
  11. end
  12. end
  13.  
  14. def self.drawit_in_image(w, h)
  15. $app.image :width => w, :height => h, :left => 0, :top => 0 do
  16. $app.stroke gray(0.2)
  17. 2000.times do |i|
  18. $app.fill gray(rand*0.6)
  19. $app.rect rand(w), rand(h), 10, 10
  20.  
  21. ### WHAT I'd like to do (as in NodeBox):
  22. # $app.textsize 14
  23. # $app.text "foo", :top => rand(h), :left => rand(w)
  24. ###
  25. end
  26. end
  27. end
  28. end
  29.  
  30. Shoes.app :width => 1000, :height => 500, :resizable => false do
  31. $app = self
  32. @view_w = width
  33. @view_h = height
  34. @canvas_w = 10000
  35. @canvas_h = 700
  36. @thumb_h = 30
  37. @thumb_w = (@thumb_h * @canvas_w)/@canvas_h
  38. @thumbview_w = (@view_w*@thumb_w)/@canvas_w
  39. @thumbview_h = (@view_h*@thumb_h)/@canvas_h
  40. @last_click = nil
  41. @scrollcanvas, @scrollview = [], []
  42.  
  43. # compute the transparency
  44. def fader(tstart, steps, i)
  45. return tstart*(1.0-1.0/(steps-1)*i)
  46. end
  47.  
  48. @view = stack :width => @view_w, :height => @view_h do
  49. background gray(0.8)
  50. @main = stack :width => @canvas_w, :height => @canvas_h do
  51. eval "Drawer::drawit_in_#{$meth}(@canvas_w, @canvas_h)"
  52. end
  53.  
  54. # create thumbnail view: we use 30 different containers w/ varying
  55. # transparency, cycling through them to fade out
  56. 30.times do |i|
  57. @scrollcanvas[i] = stack :width => @thumb_w+1, :height => @thumb_h+1,
  58. :top => height-@thumb_h-20, :left => width-@thumb_w-20 do
  59. fill gray(0.9, fader(0.7, 30, i))
  60. stroke gray(0.5, fader(1.0, 30, i))
  61. rect 0, 0, @thumb_w, @thumb_h
  62.  
  63. @scrollview[i] = image :width => @thumbview_w+1, :height => @thumbview_h+1 do
  64. fill gray(0.7, fader(0.9, 30, i))
  65. stroke gray(0.4, fader(1.0, 30, i))
  66. rect 0, 0, @thumbview_w, @thumbview_h, 2
  67. end
  68. end
  69. @scrollcanvas[i].hide
  70. end
  71. end
  72.  
  73. def constrain(prop, min, max)
  74. return (prop > max ? max : (prop < min ? min : prop))
  75. end
  76.  
  77. def stop_scrollanim
  78. @scrollcanvas.each do |sc| sc.hide; end
  79. @scrollanim.stop
  80. @scrollanim = nil
  81. end
  82.  
  83. def scroll_by(delx, dely)
  84. # move main canvas image
  85. left = @main.left()+delx
  86. top = @main.top()+dely
  87. left = constrain(left, @view.width() - @main.width(), 0)
  88. top = constrain(top, @view.height() - @main.height(), 0)
  89. @main.move(left, top)
  90.  
  91. # scale for thumbnail view, do fade animation
  92. svl = (-left*@thumb_w)/@canvas_w
  93. svr = (-top*@thumb_h)/@canvas_h
  94. stop_scrollanim if @scrollanim
  95. @scrollcanvas[0].show
  96. @scrollview[0].displace(svl, svr)
  97. @scrollanim = animate 30 do |i|
  98. if i >= 15 then
  99. i = i-15
  100. @scrollcanvas[i-1].hide if i > 0
  101. @scrollcanvas[i].show
  102. @scrollview[i].displace(svl, svr)
  103. stop_scrollanim if i == 29
  104. end
  105. end
  106. end
  107.  
  108. click do |b,l,t|
  109. @last_click = [l,t]
  110. end
  111.  
  112. release do |b,l,t|
  113. @last_click = nil
  114. end
  115.  
  116. motion do |l,t|
  117. if @last_click then
  118. del = [l-@last_click[0], t-@last_click[1]]
  119. scroll_by(del[0], del[1])
  120. @last_click = [l,t]
  121. end
  122. end
  123.  
  124. keypress do |k|
  125. del = 5
  126. ctrl_del = 50
  127. case k
  128. when :left: scroll_by(del, 0);
  129. when :right: scroll_by(-del, 0);
  130. when :up: scroll_by(0, del);
  131. when :down: scroll_by(0, -del);
  132. when :control_left: scroll_by(ctrl_del, 0);
  133. when :control_right: scroll_by(-ctrl_del, 0);
  134. when :control_up: scroll_by(0, ctrl_del);
  135. when :control_down: scroll_by(0, -ctrl_del);
  136. end
  137. end
  138.  
  139. end
Add Comment
Please, Sign In to add comment