WoW:Making Draggable Frames

Revision as of 21:57, 15 March 2005 by WoWWiki>KarlKFI (β†’β€ŽTo Make a Frame Draggable)
(diff) ← Older revision | Latest revision (diff) | Newer revision β†’ (diff)

XML Declarations

First, the XML tags movable="true" and enableMouse="true" must be in the frames declaration. Note: Some frame templates like 'button' already include enableMouse="true".

Example:

<Frame name="TellTrackFrame" enableMouse="true" movable="true" resizable="true" parent="UIParent" hidden="true">

Simple Dragging

One simple way to detect drag is to add OnDragStart and OnDragStop script elements to the frame:

<Scripts>
<OnDragStart>
 this:StartMoving();
 this.isMoving = true;
</OnDragStart>
<OnDragStop>
 this:StopMovingOrSizing();
 this.isMoving = false;
</OnDragStop>
</Scripts>

Advanced Dragging

Another way, which is more responsive but requires an onhide element so that the frame wont get stuck to the mouse:

<Scripts>
<OnMouseUp>
 if ( this.isMoving ) then
  this:StopMovingOrSizing();
  this.isMoving = false;
 end
</OnMouseUp>
<OnMouseDown>
 if ( ( ( not this.isLocked ) or ( this.isLocked == 0 ) ) and ( arg1 == "LeftButton" ) ) then
  this:StartMoving();
  this.isMoving = true;
 end
</OnMouseDown>
<OnHide>
 if ( this.isMoving ) then
  this:StopMovingOrSizing();
  this.isMoving = false;
 end
</OnHide>
</Scripts>

Note: this method also demonstrates an optional isLocked parameter to determine whether you can drag the frame or not.

Parent Dragging

Some advanced dragging addons use overlays that make default blizzard frames draggable. This is possible by using GetParent when starting and stopping drag. Unfortunetly this means that the parent frame must already be movable="true". (In the upcomming patch you will be able to use SetMovable(1) to modify a frames' movable tag.) Another drawback with overlay frames that are mouse enabled is that they will prevent the parent frame's click script tags from being called so you often have to simulate their click events.