Constrained Morph dragging

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Constrained Morph dragging

senTalker
Hello Morphic experts,

I am learning / experimenting with Morphic and have the following question:

"How do I constrain the movement of a dragged Morph?"

As illustrated here:

http://jqueryui.com/draggable/#constrain-movement

(e.g. constrain within axis/container)

So far the "hack" I came up with was to aggresively re-position the
dragged Morph in its "drawOn:", if it is attached to a HandMorph:

drawOn: aCanvas
        self owner isHandMorph "is there a better way to ask: am I being dragged?"
                ifTrue: [ self position: (self position x) @ 400 ].
        super drawOn: aCanvas.


I am sure this solution is pretty bad... I also get visual artifacts
when I move the mouse up/down.

Are there better ways/hooks for this?

Generally: is there a method that is called on submorphs whenever the
parent Morph was re-positioned/re-sized?

Thanks in advance!


Sebastian

Reply | Threaded
Open this post in threaded view
|

Re: Constrained Morph dragging

Igor Stasenko
On 18 November 2012 08:53, Sebastian Nozzi <[hidden email]> wrote:

> Hello Morphic experts,
>
> I am learning / experimenting with Morphic and have the following question:
>
> "How do I constrain the movement of a dragged Morph?"
>
> As illustrated here:
>
> http://jqueryui.com/draggable/#constrain-movement
>
> (e.g. constrain within axis/container)
>
> So far the "hack" I came up with was to aggresively re-position the
> dragged Morph in its "drawOn:", if it is attached to a HandMorph:
>
> drawOn: aCanvas
>         self owner isHandMorph "is there a better way to ask: am I being dragged?"
>                 ifTrue: [ self position: (self position x) @ 400 ].
>         super drawOn: aCanvas.
>
>
> I am sure this solution is pretty bad... I also get visual artifacts
> when I move the mouse up/down.
>
> Are there better ways/hooks for this?
>
> Generally: is there a method that is called on submorphs whenever the
> parent Morph was re-positioned/re-sized?
>

yes.
if i remember it called doLayoutIn:

but it would be really nice, if someone would trace all the messages
which going between morphs,
and document the right protocol(s) to use and customize.

> Thanks in advance!
>
>
> Sebastian
>



--
Best regards,
Igor Stasenko.

Reply | Threaded
Open this post in threaded view
|

Re: Constrained Morph dragging

senTalker
In reply to this post by senTalker
I came up with this... does what I need and I have  no visual
artifacts anymore. Still, I'm not happy for having to override a
private method in order for this to work... :-/

Overridden existing method in my Morph subclass:

privateFullMoveBy: delta
        super privateFullMoveBy: delta.
        (self wantsToConstrainDragging & self isBeingDragged)
                ifTrue: [ self adjustPositionWhileDragging ]

New methods in my Morph subclass:

isBeingDragged
        ^ self owner isHandMorph
       
wantsToConstrainDragging
        ^ true

adjustPositionWhileDragging
        self position: (self constrainedDragPositionFrom: (self topLeft))
       
constrainedDragPositionFrom: aNonConstrainedPosition
        "Adjust the position to be constrained however you want. In this case
horizontally at y=450"
        ^ (aNonConstrainedPosition x) @ 450.

You would need to override only "wantsToConstrainDragging" and
"constrainedDragPositionFor:".

Any critique to the code and/or naming welcome! :-)

Cheers,

Sebastian

Reply | Threaded
Open this post in threaded view
|

Re: Constrained Morph dragging

Igor Stasenko
On 18 November 2012 10:29, Sebastian Nozzi <[hidden email]> wrote:

> I came up with this... does what I need and I have  no visual
> artifacts anymore. Still, I'm not happy for having to override a
> private method in order for this to work... :-/
>
> Overridden existing method in my Morph subclass:
>
> privateFullMoveBy: delta
>         super privateFullMoveBy: delta.
>         (self wantsToConstrainDragging & self isBeingDragged)
>                 ifTrue: [ self adjustPositionWhileDragging ]
>
> New methods in my Morph subclass:
>
> isBeingDragged
>         ^ self owner isHandMorph
>
> wantsToConstrainDragging
>         ^ true
>
> adjustPositionWhileDragging
>         self position: (self constrainedDragPositionFrom: (self topLeft))
>
> constrainedDragPositionFrom: aNonConstrainedPosition
>         "Adjust the position to be constrained however you want. In this case
> horizontally at y=450"
>         ^ (aNonConstrainedPosition x) @ 450.
>
> You would need to override only "wantsToConstrainDragging" and
> "constrainedDragPositionFor:".
>
> Any critique to the code and/or naming welcome! :-)

the main problem of morphic code that it is impossible to critique it..
all you can do is just study it, cut the cruft out, and only then you
can see whether design
was sound or not and try to critique :)

what i would propose, is to not narrow it just for constraining dragging.
i think better would be to say 'wants to constrain own
movement/repositioning and/or resizing'
but i cannot find a proper selector which would clearly tell the
purpose for that :)

also, i wouldn't be surprised, that if you do some archeology in
morphic code, you will find
behavior which already does what you did :)

>
> Cheers,
>
> Sebastian
>



--
Best regards,
Igor Stasenko.