Custom dragging with defold-input's cursor script (SOLVED)

Unless I’m missing something, the only way to trigger drag events is from the Drag checkbox on the cursor. This means that you can drag game objects around, and you also trigger DRAG_START and DRAG_END events. But if the checkbox is disabled, then you don’t get either of these behaviours.

Knowing this, is there a way to add custom dragging behaviour, such adding smooth movement or only dragging on one axis?

I did a change to the defold-input library yesterday evening so that you get drag events also when you press and drag without being over a game object. This can be used to for instance drag/pan a map/level to look around. You will also get continuous DRAG events while dragging.

I’m not sure these changes help in your situation though.

What do you mean by

What I’m looking for is the ability to receive DRAG_START and DRAG_END messages without dragging the object around at the same time. Right now I think my only option is to use PRESSED and RELEASED and add my own drag logic and threshold, which can be done, but it would be handy to use the drag messages that are already there (if possible).

By “smooth movement” I just mean having the object use lerp to smoothly move towards the mouse position while it’s being dragged, or something similar to that.

Ah, got it. One change I did is that you now get DRAG_START, DRAG and DRAG_END events even if you aren’t hovering an object. So what if you don’t detect clicks on the object and use the “objectless” drag instead? Would that help? Or do you also need to detect that the object was pressed?

Using PRESSED and RELEASE and disabling or preventing drag would be another option.

Unfortunately I couldn’t get defold-input working for me in this case, so I rolled my own solution.

I check if the mouse is inside the sprite’s bounds for click detection (using the game object’s position and sprite’s size), and I have my own “drag threshold” to check for when the object should start moving towards my mouse position. Now I’m able to use my own logic in update and on_input to smooth movement using lerp, and also drag along just the X axis.

I’m keeping defold-input in my project though, I’m probably going to need it when it comes time to add UI.

1 Like