5

I have a big morph with dozens of submorphs. The submorphs are of two kinds: pieces and squares.

I have googled a lot and read quite a bit of Morphic documentation, but somehow I can’t get it to work. I have not yet found a document that clearly explains what to do.

Currently, the pieces won’t be dragged (except when I Alt-click on them). The squares have implemented the mouseEnterDragging and mouseLeaveDragging methods and they are being called (they change the color of the squares, so it’s easy to spot). But it’s like something invisible is being dragged.

What I want: the pieces should be draggable, just by holding down the left mouse button and moving the mouse, not by using Alt-click to bring up the halo. The squares should not be draggable. The pieces should be droppable on squares, but not on pieces.

Which methods should I implement/override?

5
  • Have you tried sending enableDrag: true to the pieces? Commented Mar 25, 2023 at 14:24
  • Yes I have. The behavior is that when I try to drag one piece, everything is dragged, the base morph, so to speak, which is the owner of all other morphs, and everything with it. Commented Mar 25, 2023 at 14:57
  • I would try this: when your piece receives startDrag: send grabMorph:. Depending on the result, you may need to do more. Not sure if you also need to removeMorph: the piece from its owner. Commented Mar 25, 2023 at 19:41
  • I have overridden both handleMouseDown: and startDrag: Both methods are not called. I have disabled drag for all morphs, except the piece morphs, yet when I try to drag a piece, the complete board, with everything on it, is dragged. If I look at “documentation”, all I read is that I should override certain methods. But it doesn’t say what the methods should contain. What I am looking for are precise instructions on what methods to override with what content. Surely I don’t have to program dragging by myself? I should be able to use standard behavior? Commented Mar 26, 2023 at 11:40
  • 1
    I'm not a Squeak user so I can't tell. Post the question to the Squeak-dev list. If you don't get an answer, come back to me. I'll find the right person to ask. Commented Mar 26, 2023 at 12:52

1 Answer 1

4

Starting with a mouseDown: event, the hand can then observe the following mouseMove: or mouseUp: events to give the interested morph a callback about click, double-click, or start-drag.

So, your to-be-dragged morph should implement handlesMouseDown: and mouseDown: and in the latter send waitForClicksOrDrag:event: to the hand, which is in the event you get as argument in mouseDown:.

You may refine the callback via waitForClicksOrDrag:event:selectors:threshold: but the default callbacks are click:, doubleClick:, doubleClickTimeout:, and startDrag:.

A simple way to implement a startDrag: (or similar) is event hand grabMorph: self.

Then, for the dropping, there is a simple handshake via wantsDroppedMorph:event: and wantsToBeDroppedInto: and finally acceptDroppingMorph:event:. Those are simply configured via dropEnabled. You can but you don't have to use (i.e., drag-and-drop) the surrogate TransferMorph.

You can read more about the mechanism by reading the following source code (and other senders/implementors) in the image:

  • HandMorph >> #waitForClicksOrDrag:event:
  • Morph >> #startDrag:
  • Morph >> #handleDropMorph:

Besides implementing stuff in a subclass of Morph, you can also script it from the outside via eventHandler:

m := Morph new.
m eventHandler: EventHandler new.
m eventHandler handlesClickOrDrag: true.
m on: #startDrag send: #value:value: to: [:event :source |
    Transcript showln: 'drag start'.
    event hand grabMorph: source].
m openInHand.

This will free you from having to bother with waitForClicksOrDrag:event:.

Hope this helps! Happy Squeaking. :-)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.