How to Mouse Drag

Post Reply
UrbanCyborg
Posts: 625
Joined: Mon Nov 15, 2021 9:23 pm

How to Mouse Drag

Post by UrbanCyborg »

I've been all over the SDK for this, and gotten really ambiguous results. I need to be able to tell which direction a control was dragged over. Click on the control, drag, click off while still on the control. I've been looking for something that would return location values. I note that the Control_Something notifications seem to return mouse values in x and y, but it's not clear exactly which notification to use. From the SDK, it sounds like the Drag notifications are rather specialized, but it's not clear if the mouse down and up notifications would be appropriate. Anyone know this one? Thanks.

Reid
Cyberwerks Heavy Industries -- viewforum.php?f=76
UrbanCyborg
Posts: 625
Joined: Mon Nov 15, 2021 9:23 pm

Re: How to Mouse Drag

Post by UrbanCyborg »

Never mind, I figured out a way. Just had to go back to when I first had to code to the metal on the Atari-ST, and then the Mac.

Reid
Cyberwerks Heavy Industries -- viewforum.php?f=76
User avatar
honki-bobo
Posts: 310
Joined: Sat Nov 09, 2019 1:18 pm

Re: How to Mouse Drag

Post by honki-bobo »

Hey Reid,

do you mind sharing your solution?

Best regards,
Martin
Image
Monkey Business Audio
Modules - Music - Twitter - YouTube
UrbanCyborg
Posts: 625
Joined: Mon Nov 15, 2021 9:23 pm

Re: How to Mouse Drag

Post by UrbanCyborg »

Sure. Here's the relevant excerpt:

Code: Select all

case Object_LeftButtonDown:
{
    if(component == page[currentPage]) {
        mouseStart = x;
        break;
    }
}
break;

case Object_LeftButtonUp:
{
    if(component == page[currentPage]) {
        mouseEnd = x;
        if(Math.abs(mouseEnd - mouseStart) > 4) {  // guarantee this is actually a drag
            if(mouseStart < mouseEnd) {            // swiping left or right
                // right-drag action
            }
            else {
                // left-drag action
            }
        }
        break;
    }
}
break;

private int mouseStart;                            // start of a mouse drag
private int mouseEnd;                              // end of a mouse drag
In this instance I only needed left and right drags, but the extension to up and down or rotary is obvious. For page[currentPage] just substitute the control you need to drag over.

Reid
Cyberwerks Heavy Industries -- viewforum.php?f=76
User avatar
honki-bobo
Posts: 310
Joined: Sat Nov 09, 2019 1:18 pm

Re: How to Mouse Drag

Post by honki-bobo »

Hey Reid,

thanks for sharing the code. I was actually thinking about something similar, but also including the y coordinate and then calculating angle and distance. Since I'm on vacation at the moment I cannot test anything, but does this also work when the Object_LeftButtonUp event is fired by a different component than the one the Object_LeftButtonDown event was fired by? E.g. when you press the mouse button on component A, drag in some direction and release the button over component B or a component that does not receive mouse notifications or even drag the mouse pointer outside your module?

Best regards,
Martin
Image
Monkey Business Audio
Modules - Music - Twitter - YouTube
UrbanCyborg
Posts: 625
Joined: Mon Nov 15, 2021 9:23 pm

Re: How to Mouse Drag

Post by UrbanCyborg »

Apparently, yes. I believe the mouse down and up are coupled by VM. BTW, to do rotary tracking, it would be enough to track the direction and relative amounts of x and y in whatever quadrant (relative to the implied coordinate system based on the control center) the drag starts in. If you imagine a diamond around the origin with vertices (1, 0), (0, 1), (-1, 0), (0, -1), the sides give you the rough directions for valid motion, two per quadrant. If you further imagine four lines from the origin bisecting those sides, you have the directions that are invalid for each quadrant. To allow for inaccurate hand control, you could add a heuristic that compares the relative sizes of x and y and chooses the larger to get to one of the correct choices for the quadrant.

Okay, clear as English skies in August? :D For example, if the signs of x and y tell you the drag is in the third quadrant, valid drags would be (up, left) and (down, right), corresponding to the diamond side in that quadrant. However, in the event the actual user drag corresponds more to one of the invalid drags (i.e., the line from the origin at 225 degrees, you could either just abort the drag, or apply the heuristic, choose whichever of x or y is the larger motion, and let that determine which direction on the diamond side you're moving. Gives you some slop factor. In the odd case that the user dragged exactly vertical or horizontal, the heuristic still works. Okay, that ought to be as pellucid as winter skies in Utah. :D

Reid
Cyberwerks Heavy Industries -- viewforum.php?f=76
Post Reply

Return to “Module Designer”