jruby - Ruby Shoes4 : how does Sample13 work? -


i rediscovered ruby shoes framework, tiny graphical framework, using ruby internal dsl capabilities, , more here shoes4.

one of examples (located in ./samples) impressed me in particular: sample13.rb, cannot understand.

running example, canvas button named "new". each time push button, new figure (named box in program) added canvas (with random shape , color). more, can click these figures afterwards, , move them away.

the code surprisingly short :

shoes.app :width => 300, :height => 300   colors = shoes::colors   = 45    button 'new'     += 5     box = rand(2) == 0 ? rect(i, i, 20) : oval(i, i, 20)     box.style :fill => send(colors.keys[rand(colors.keys.size)])     @flag = false     box.click{@flag = true; @box = box}     box.release{@flag = false}   end    motion{|left, top| @box.move(left-10, top-10) if @flag} end 

i explanations how code works.

it seems each figure created stored somewhere, ? there means have access collection of newly created figures ?

moreover, appears, figure further added methods such click , release. case each object in shoes in general (i cannot find on shoes website) ?

so figures not stored anywhere, there quite scoping magic going on. click , release elements available on quite elements, should elements mentioned here under :click list not supported in shoes4 (yet).

so let's @ 1 one:

box.click{@flag = true; @box = box} box.release{@flag = false} 

so when element clicked @flag set true (that flag seems mark element being dragged), why flag set false when mouse click released. furthermore happens on click instance variable @box set box clicked. possible blocks retain scope created in. means scope remembers box refers created box block called when clicking on box. possible since reference saved in local variable box during creation of block, , block captures scope.

so motion:

motion{|left, top| @box.move(left-10, top-10) if @flag} 

motion captures movement of mouse. moves clicked box (if there any, hence @flag) position of current mouse cursor (specified top , left). dunno why -10 needed seem remember had problems elsewhere. i'll try investigate , open issue, that's not important here :-)

oh , thank trying out shoes! :-) plus telling me can drag around, didn't know that!


Comments

Popular posts from this blog

javascript - DIV "hiding" when changing dropdown value -

Does Firefox offer AppleScript support to get URL of windows? -

android - How to install packaged app on Firefox for mobile? -