Squeak
  links to this page:    
View this PageEdit this PageUploads to this PageHistory of this PageTop of the SwikiRecent ChangesSearch the SwikiHelp Guide
Programming morphs - page 6 - mouse events
Last updated at 12:43 pm UTC on 12 June 2018
Eventually, you will want to let your morphs respond to user input, and the most common way to do this is with mouse events.

Whenever the mouse is pressed down, the Morphic system will query each morph underneath the mouse using the handlesMouseDown: message. If any morph responds true to this request, then Morphic will send a mouseDown: message to it immediately, and a mouseUp: message whenever the mouse button is released. (If all morphs respond false, then the Morphic system will initiate a grab.)

Both mouseDown: and mouseUp: will be passed a MorphicEvent instance. Browse class MorphicEvent to see all the possible messages it responds to.

joe, here, is an instance of ClickCountingMorph, and his mouse handling methods are shown to the right. Whenever you click the red or yellow buttons on top of joe, the relevant counter is incremented–try it! (Note, it is now impossible to pick up joe by just clicking on him. Instead, you must blue-click and use the black halo handle.)


To try it yourself, see if you can modify ToggleMorph above and on the right so that whenever the yellow button is pressed, the color of the morph switches between black and red. That is, if it's black and you click on it, it turns red, and vice versa. If the red button is pressed, then don't handle the mouse event so that morphic can grab the morph.


"————————–"
"joe := ClickCountingMorph new.
joe extent: 80@60.
joe openInWorld.
jane := ToggleMorph new openInWorld.
"

"
selectors := #(handlesMouseDown: mouseDown:).
methodRefs := selectors collect: [ :sel |
MethodReference new setStandardClass: ClickCountingMorph methodSymbol: sel ].
MessageSet openMessageList: methodRefs name: 'ClickCountingMorph mouse handling'.
"