PDA

View Full Version : Anyone managed to get drag and click to work seperate from one another?


jorishilhorst
06-22-2006, 02:42 PM
Hi,

I am trying to use the dragmanager with an element, but at the same time support onClick to allow the element to be selected. I'm not sure if this is flex2 specific (but am using the latest version)

in the attributes of the object I have put:
click="clickHandler(event)"
mouseDown="dragThumbnail(event)"


and inside the script block I have defined the following functions:
public function clickHandler(event:MouseEvent):void {
parentApplication.setSelectedPhone(this);
}

public function dragThumbnail(event:MouseEvent):void {
var ds:DragSource = new DragSource();
ds.addData(this, dragType);

DragManager.doDrag(IUIComponent(this), ds, event);
}

This works after a fashion, as the mouseclick gets caught, and on mousedown the canvas gets dragged around. However does anyone know how I can make sure the dragThumbnail method does not start in the case of a mouseclick?
The problem is that the drag action always starts (even on a regular mouseclick).

I was thinking of making a timer which polls or something but that seems like hacking to me. Anyone have any thoughts?

cheers,
joris

jorishilhorst
06-23-2006, 10:35 AM
Heya, I managed to figure part of this out:

I'm not 100% sure I won't run into synchronisation issues with the mouseUpHandler and the timerEventHandler interfering with one another, perhaps someone can give me some advice on it.


private var timer:Timer;
private var mouseDownEvent:MouseEvent;
public function clickHandler(event:MouseEvent):void
{
parentApplication.setSelectedPhone(this);
// this is a click (so shouldn't be a drag)
}

public function mouseDownHandler(event:MouseEvent):void {
if (timer != null && timer.running) {
// timer was already running, we reset it
timer.stop();
timer.removeEventListener("timerComplete", dragThumbnail);
timer.reset();
}
timer = new Timer(100, 1);
timer.addEventListener("timerComplete", dragThumbnail);
timer.start()
mouseDownEvent = event;
}
public function mouseUpHandler(event:MouseEvent):void {
if (timer.running) {
timer.stop();
timer.removeEventListener("timerComplete", dragThumbnail);
}
}
public function dragThumbnail(timer:TimerEvent):void {
var ds:DragSource = new DragSource();
ds.addData(this, dragType);

DragManager.doDrag(IUIComponent(this), ds, mouseDownEvent);
DragManager.showFeedback(DragManager.MOVE);
}


There is an additional issue, which I think is listed in the flash 9 known bug list: if the mouse does not move, the DragManager will be in a weird state.
(so click, hold, release will actually result in a dragComplete but not a dragDrop.) As I had used an online example that implemented 'to remove the element from a list, drag it outside of the area' which actually uses dragComplete, this now fires if the mouse has not moved. (so click, hold, release causes the element under the mouse to go away).

Does anyone know a workaround for that bug? (if i'm not being clear I can try to make a code example for it)

cheers,
joris