Game Development Community

mouseDragging over the path

by Michal Janiak · in Torque Game Builder · 05/28/2009 (12:11 pm) · 0 replies

Hi.

How can I restrict dragging the mouse to move the object just after the path?

My behavior:

if (!isObject(MouseDraggableBehavior))
{
%template = new BehaviorTemplate(MouseDraggableBehavior);

%template.friendlyName = "Mouse Draggable";
%template.behaviorType = "Input";
%template.description = "Make an object draggable by the mouse";

%template.addBehaviorField(centerOnMouse, "Center the object on the mouse", bool, false);
%template.addBehaviorField(toggleDragState, "Start dragging on one click, stop dragging on the next click", bool, false);
}

function MouseDraggableBehavior::onBehaviorAdd(%this)
{
%this.dragging = false;
%this.cancelOnMouseUp = true;
%this.offset = "0 0";
%this.owner.setUseMouseEvents(true);
}

function MouseDraggableBehavior::onMouseDown(%this, %modifier, %worldPos)
{
// Toggle the drag status.
%this.dragging = !%this.dragging;

// We always stop dragging on mouse up unless the toggle option is set.
%this.cancelOnMouseUp = !%this.toggleDragState;

// Schedule this or else we'll get two onMouseDowns. One for the locked
// objects, and again for the not locked objects.
%this.owner.schedule(0, setMouseLocked, %this.dragging);

if (!%this.centerOnMouse)
%this.offset = t2dVectorSub(%this.owner.position, %worldPos);
}

function MouseDraggableBehavior::onMouseUp(%this, %modifier, %worldPos)
{
if (%this.cancelOnMouseUp)
{
%this.dragging = false;
%this.owner.setMouseLocked(false);
}
}

function MouseDraggableBehavior::onMouseDragged(%this, %modifier, %worldPos)
{
if (%this.dragging)
{
%this.moveToMouse(%worldPos);

// Once we have dragged, then dragging will always stop on mouse up.
%this.cancelOnMouseUp = true;
}
}

function MouseDraggableBehavior::onMouseMove(%this, %modifier, %worldPos)
{
if (%this.dragging)
%this.moveToMouse(%worldPos);
}

function MouseDraggableBehavior::moveToMouse(%this, %worldPos)
{
%this.owner.position = t2dVectorAdd(%worldPos, %this.offset);
}

About the author

Recent Threads