Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # change the line below ("image" or "stack")
- $meth = "image"
- class Drawer
- def self.drawit_in_stack(w, h)
- $app.stroke gray(0.2)
- 1000.times do |i|
- $app.fill gray(rand*0.6)
- $app.rect rand(w), rand(h), 10, 10
- $app.inscription "foo", :top => rand(h), :left => rand(w)
- end
- end
- def self.drawit_in_image(w, h)
- $app.image :width => w, :height => h, :left => 0, :top => 0 do
- $app.stroke gray(0.2)
- 2000.times do |i|
- $app.fill gray(rand*0.6)
- $app.rect rand(w), rand(h), 10, 10
- ### WHAT I'd like to do (as in NodeBox):
- # $app.textsize 14
- # $app.text "foo", :top => rand(h), :left => rand(w)
- ###
- end
- end
- end
- end
- Shoes.app :width => 1000, :height => 500, :resizable => false do
- $app = self
- @view_w = width
- @view_h = height
- @canvas_w = 10000
- @canvas_h = 700
- @thumb_h = 30
- @thumb_w = (@thumb_h * @canvas_w)/@canvas_h
- @thumbview_w = (@view_w*@thumb_w)/@canvas_w
- @thumbview_h = (@view_h*@thumb_h)/@canvas_h
- @last_click = nil
- @scrollcanvas, @scrollview = [], []
- # compute the transparency
- def fader(tstart, steps, i)
- return tstart*(1.0-1.0/(steps-1)*i)
- end
- @view = stack :width => @view_w, :height => @view_h do
- background gray(0.8)
- @main = stack :width => @canvas_w, :height => @canvas_h do
- eval "Drawer::drawit_in_#{$meth}(@canvas_w, @canvas_h)"
- end
- # create thumbnail view: we use 30 different containers w/ varying
- # transparency, cycling through them to fade out
- 30.times do |i|
- @scrollcanvas[i] = stack :width => @thumb_w+1, :height => @thumb_h+1,
- :top => height-@thumb_h-20, :left => width-@thumb_w-20 do
- fill gray(0.9, fader(0.7, 30, i))
- stroke gray(0.5, fader(1.0, 30, i))
- rect 0, 0, @thumb_w, @thumb_h
- @scrollview[i] = image :width => @thumbview_w+1, :height => @thumbview_h+1 do
- fill gray(0.7, fader(0.9, 30, i))
- stroke gray(0.4, fader(1.0, 30, i))
- rect 0, 0, @thumbview_w, @thumbview_h, 2
- end
- end
- @scrollcanvas[i].hide
- end
- end
- def constrain(prop, min, max)
- return (prop > max ? max : (prop < min ? min : prop))
- end
- def stop_scrollanim
- @scrollcanvas.each do |sc| sc.hide; end
- @scrollanim.stop
- @scrollanim = nil
- end
- def scroll_by(delx, dely)
- # move main canvas image
- left = @main.left()+delx
- top = @main.top()+dely
- left = constrain(left, @view.width() - @main.width(), 0)
- top = constrain(top, @view.height() - @main.height(), 0)
- @main.move(left, top)
- # scale for thumbnail view, do fade animation
- svl = (-left*@thumb_w)/@canvas_w
- svr = (-top*@thumb_h)/@canvas_h
- stop_scrollanim if @scrollanim
- @scrollcanvas[0].show
- @scrollview[0].displace(svl, svr)
- @scrollanim = animate 30 do |i|
- if i >= 15 then
- i = i-15
- @scrollcanvas[i-1].hide if i > 0
- @scrollcanvas[i].show
- @scrollview[i].displace(svl, svr)
- stop_scrollanim if i == 29
- end
- end
- end
- click do |b,l,t|
- @last_click = [l,t]
- end
- release do |b,l,t|
- @last_click = nil
- end
- motion do |l,t|
- if @last_click then
- del = [l-@last_click[0], t-@last_click[1]]
- scroll_by(del[0], del[1])
- @last_click = [l,t]
- end
- end
- keypress do |k|
- del = 5
- ctrl_del = 50
- case k
- when :left: scroll_by(del, 0);
- when :right: scroll_by(-del, 0);
- when :up: scroll_by(0, del);
- when :down: scroll_by(0, -del);
- when :control_left: scroll_by(ctrl_del, 0);
- when :control_right: scroll_by(-ctrl_del, 0);
- when :control_up: scroll_by(0, ctrl_del);
- when :control_down: scroll_by(0, -ctrl_del);
- end
- end
- end
Add Comment
Please, Sign In to add comment