简体   繁体   中英

How to draw interactively with Pen on a Morph in Squeak?

It can't get the Pen to work properly in a Morph.

I arrived at the snippet below but I don't understand how to update the view of the Morph after I moved the pen. I tried to send "changed" in various (sm, pen, (sm owner)) object but it does not solve it.

In the snippet below the line gets drawn if I resize the window. Or, also, if i put "openInWindow" as last line.

sm := SketchMorph new.
sm clearExtent: 600@600 fillColor: (Color yellow) darker darker.
sm position: 100@100.
sm openInWindow. 
pen := sm penOnMyForm.
pen defaultNib: 4; color: (Color red).
pen up.
pen goto: 10@10. 
pen down.
pen goto: 100@100.

The message you should send to the SketchMorph , instead of #layoutChanged , is

sm revealPenStrokes

which is -pun intended- quite intention revealing; isn't it?

How did I find it

Using the hint provided by Nicola, I debugged sm layoutChanged trying to understand why it showed the line. I had tried sm changed before with no luck, so I knew that the clue had to be in generateRotatedForm . And since this method redefines the ivar rotatedForm , I looked for all methods that changed it, as I suspected that the problem was in some cache not being invalidated by changed . Quickly enough, the selector revealPenStrokes emerged victorious from the rather short list of methods modifying rotatedForm .

I found the method, but I don't understand the logic.

Just add this at the end.

sm layoutChanged.
pen goto: 150@100.
sm layoutChanged.
" ... and so on "

If I look into the "layoutChanged" definition and I try to send those methods myself I see the command that rules is "generateRotatedForm" ... Umm ... why ?

If some Morphic expert would like to shed some light into this I would be grateful.

bye Nicola

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM