WoW:Making Draggable Frames: Difference between revisions

From AddOn Studio
Jump to navigation Jump to search
m (Move page script moved page Making Draggable Frames to Making Draggable Frames without leaving a redirect)
 
(10 intermediate revisions by 10 users not shown)
Line 1: Line 1:
== XML Declarations ==
{{wow/uihowto}}
Draggable frames can be moved by having the user hold down a mouse button over the frame, then move the mouse to reposition the frame. This HOWTO describes how to make a frame draggable.


First, the XML tags movable="true" and enableMouse="true" must be in the frames declaration.
== Summary ==
Note: Some frame templates like 'button' already include enableMouse="true".
To initiate dragging of a frame, call {{api|Frame StartMoving|Frame:StartMoving()}}; to stop, call {{api|Frame StopMovingOrSizing|Frame:StopMovingOrSizing()}}. The frame must be flagged as movable (either through the movable attribute in its XML declaration or using {{api|Frame SetMovable|Frame:SetMovable}}(isMovable).


Example:
There are generally 2 methods that are used for moving a frame, by Dragging the frame (OnDragStart & OnDragStop Handlers) or Clicking the frame (OnMouseDown & OnMouseUp Handlers). Note: that to receive mouse events (including dragging), the frame must be mouse enabled (either through the enableMouse attribute in its XML declaration or using {{api|Frame EnableMouse|Frame:EnableMouse}}(isEnabled)); buttons are mouse-enabled by default.
<Frame name="TellTrackFrame" enableMouse="true" movable="true" resizable="true" parent="UIParent" hidden="true">


== Simple Dragging ==
== Using XML ==
 
=== Using OnDragStart / OnDragStop ===
One simple way to detect drag is to add OnDragStart and OnDragStop script elements to the frame:
The code below creates a draggable Frame widget and uses the OnDrag widget handlers to initiate dragging. Note the use of movable and enableMouse attributes:
  <Ui>
  <Frame name="DragFrame1" enableMouse="true" movable="true">
  <Scripts>
  <OnLoad>self:RegisterForDrag("LeftButton");</OnLoad>
  <OnDragStart>self:StartMoving();</OnDragStart>
  <OnDragStop>self:StopMovingOrSizing();</OnDragStop>
  </Scripts>
  <nowiki><!-- Tags below add a visual element to the frame. --></nowiki>
  <Layers>
  &lt;Layer level="ARTWORK">
<nowiki> </nowiki>  <Texture setAllPoints="true">
<nowiki> </nowiki>  <Color r="1.0" g="0.5" b="0.0" a="0.5" />
<nowiki>  </Texture>
  </Layer>
  </Layers>
  </nowiki><Size x="64" y="64" />
<nowiki> </nowiki><Anchors><Anchor point="CENTER" relativeTo="UIParent"/></Anchors>
</Frame>
</Ui>


<Scripts>
Note the use of the OnLoad Script, In order to drag a frame it must register for drag via {{api|Frame RegisterForDrag|Frame:RegisterForDrag("button")}}. Above code edited to include the <Ui> </Ui> tags as it
<OnLoad>
is .xml and won't work without this.
  this:RegisterForDrag("LeftButton");
</OnLoad>
<OnDragStart>
  this:StartMoving();
  this.isMoving = true;
</OnDragStart>
<OnDragStop>
  this:StopMovingOrSizing();
  this.isMoving = false;
</OnDragStop>
</Scripts>


== Advanced Dragging ==
=== Using OnMouseUp/OnMouseDown ===
 
While OnDrag* handlers typically require the mouse button to be held down for a small amount of time prior to enabling the mouse behavior, making them well suited for dragging widgets that normally respond to clicks. However, if the frame you wish to make draggable is not normally a button, you can use OnMouseUp/OnMouseDown to provide a more responsive experience.
Another way, which is more responsive but requires an onhide element so that the frame wont get stuck to the mouse:


The following snippet illustrates this concept, and should replace the <Scripts> block above:
  <Scripts>
  <Scripts>
<OnMouseUp>
  <OnMouseDown>
  if ( this.isMoving ) then
  if button == "LeftButton" and not self.isMoving then
  this:StopMovingOrSizing();
    self:StartMoving();
  this.isMoving = false;
    self.isMoving = true;
  end
  end
</OnMouseUp>
  </OnMouseDown>
<OnMouseDown>
  <OnMouseUp>
  if ( ( ( not this.isLocked ) or ( this.isLocked == 0 ) ) and ( arg1 == "LeftButton" ) ) then
  if button == "LeftButton" and self.isMoving then
  this:StartMoving();
    self:StopMovingOrSizing();
  this.isMoving = true;
    self.isMoving = false;
  end
  end
</OnMouseDown>
  </OnMouseUp>
<OnHide>
  <OnHide>
  if ( this.isMoving ) then
  if ( this.isMoving ) then
  this:StopMovingOrSizing();
    this:StopMovingOrSizing();
  this.isMoving = false;
    this.isMoving = false;
  end
  end
</OnHide>
  </OnHide>
  </Scripts>
  </Scripts>


Note: this method also demonstrates an optional isLocked parameter to determine whether you can drag the frame or not.
Note: this also adds a frame.isLocked boolean field to control whether the frame can be dragged or not. There's generally no limit to the amount of conditional logic you can insert prior to :StartMoving() -- for instance, requiring the user to hold down some modifier button is a possible alternative.


== Parent Dragging ==
=== Using TitleRegion ===
Frames can have a TitleRegion object that handles dragging automatically -- as long as the mouse is held down within the TitleRegion, it'll allow the frame to be dragged.


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
The frame created below will be draggable by clicking on its top 20 pixels:
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.
  <Frame enableMouse="true">
  <Size x="100" y="100"/>
  <TitleRegion>
  <Size x="100" y="20"/>
  <Anchors><Anchor point="TOP"/></Anchors>
  </TitleRegion>
  <nowiki><!-- Tags below add a visual element to the frame. --></nowiki>
  <Layers>
  &lt;Layer level="ARTWORK">
<nowiki> </nowiki>  <Texture setAllPoints="true">
<nowiki> </nowiki>  <Color r="1.0" g="0.5" b="0.0" a="0.5" />
<nowiki>  </Texture>
    </Layer>
  </Layers>
  </nowiki><Anchors><Anchor point="CENTER" relativeTo="UIParent"/></Anchors>
  </Frame>


== Quick Dragging Code ==
== Using Lua ==
=== Using OnDragStart OnDragStop ===
The code sample below illustrates the use of the [[Widget API]] to achieve the same results as the XML above:
local frame = CreateFrame("Frame", "DragFrame2", UIParent)
frame:SetMovable(true)
frame:EnableMouse(true)
frame:RegisterForDrag("LeftButton")
frame:SetScript("OnDragStart", frame.StartMoving)
frame:SetScript("OnDragStop", frame.StopMovingOrSizing)
-- The code below makes the frame visible, and is not necessary to enable dragging.
frame:SetPoint("CENTER"); frame:SetWidth(64); frame:SetHeight(64);
local tex = frame:CreateTexture("ARTWORK");
tex:SetAllPoints();
tex:SetTexture(1.0, 0.5, 0); tex:SetAlpha(0.5);


While somewhat untested there is an easier and more automatic way to activate dragging. If you have your <Frame> delcaration attributes "enableMouse" and "movable" set to true, dragging may be accomplished by adding a <TitleRegion> tag inside of your <Frame>
=== Using OnMouseDown / OnMouseUp ===
 
The code sample below illustrates the use of the [[Widget API]] to achieve the same results as the XML above:
  <Frame name="myname" frameStrata="HIGH" toplevel="true" enableMouse="true" movable="true" parent="UIParent">
local frame = CreateFrame("Frame", "DragFrame2", UIParent)
  <TitleRegion setAllPoints="true"/>
frame:SetMovable(true)
  ...
frame:EnableMouse(true)
</Frame>
  frame:SetScript("OnMouseDown", function(self, button)
  if button == "LeftButton" and not self.isMoving then
    self:StartMoving();
    self.isMoving = true;
  end
end)
frame:SetScript("OnMouseUp", function(self, button)
  if button == "LeftButton" and self.isMoving then
    self:StopMovingOrSizing();
    self.isMoving = false;
  end
end)
frame:SetScript("OnHide", function(self)
  if ( self.isMoving ) then
    self:StopMovingOrSizing();
    self.isMoving = false;
  end
end)
-- The code below makes the frame visible, and is not necessary to enable dragging.
frame:SetPoint("CENTER"); frame:SetWidth(64); frame:SetHeight(64);
local tex = frame:CreateTexture("ARTWORK");
tex:SetAllPoints();
tex:SetTexture(1.0, 0.5, 0); tex:SetAlpha(0.5);


I haven't discovered any adverse side effects to doing this yet, I am not even sure if this is the intended use for it.
==General Notes==


Using this method can result in the frame not responding to other mouse events, also both mouse buttons will drag the frame.
The use of the OnHide Handler is not entirely necessary. However it is a good idea if your frames are being Hidden or Shown by running code and not the User.


The use of full functions is also not required in lua:
frame:SetScript("OnDragStart" frame.StartMoving)
frame:SetScript("OnDragStop" frame.StopMovingOrSizing)


You can also specify <Size> and <Anchors> within <TitleRegion>, e.g.
frame:SetScript("OnMouseDown" frame.StartMoving)
frame:SetScript("OnMouseUp" frame.StopMovingOrSizing)


<Frame name="myname" frameStrata="HIGH" toplevel="true" enableMouse="true" movable="true" parent="UIParent">
  <TitleRegion>
  <Size>
    <AbsDimension x="200" y="20"/>
  </Size>
  <Anchors>
    <Anchor point="TOP"/>
  </Anchors>
  </TitleRegion>
  ...
</Frame>


This way, your <Frame> can still receive mouse events, and you can only drag it by clicking within its <TitleRegion>.
In XML all that is required is just the function:
<OnDragStart>self:StartMoving();</OnDragStart>
<OnDragStop>self:StopMovingOrSizing();</OnDragStop>


[[Category: HOWTOs]]
<OnMouseDown>self:StartMoving();</OnMouseDown>
<OnMouseUp>self:StopMovingOrSizing();</OnMouseUp>

Latest revision as of 04:48, 15 August 2023

HOWTOs

Draggable frames can be moved by having the user hold down a mouse button over the frame, then move the mouse to reposition the frame. This HOWTO describes how to make a frame draggable.

Summary

To initiate dragging of a frame, call Frame:StartMoving(); to stop, call Frame:StopMovingOrSizing(). The frame must be flagged as movable (either through the movable attribute in its XML declaration or using Frame:SetMovable(isMovable).

There are generally 2 methods that are used for moving a frame, by Dragging the frame (OnDragStart & OnDragStop Handlers) or Clicking the frame (OnMouseDown & OnMouseUp Handlers). Note: that to receive mouse events (including dragging), the frame must be mouse enabled (either through the enableMouse attribute in its XML declaration or using Frame:EnableMouse(isEnabled)); buttons are mouse-enabled by default.

Using XML

Using OnDragStart / OnDragStop

The code below creates a draggable Frame widget and uses the OnDrag widget handlers to initiate dragging. Note the use of movable and enableMouse attributes:

 <Ui>
 <Frame name="DragFrame1" enableMouse="true" movable="true">
 <Scripts>
  <OnLoad>self:RegisterForDrag("LeftButton");</OnLoad>
  <OnDragStart>self:StartMoving();</OnDragStart>
  <OnDragStop>self:StopMovingOrSizing();</OnDragStop>
 </Scripts>
 <!-- Tags below add a visual element to the frame. -->
 <Layers>
  <Layer level="ARTWORK">
   <Texture setAllPoints="true">
    <Color r="1.0" g="0.5" b="0.0" a="0.5" />
   </Texture>
   </Layer>
  </Layers>
  <Size x="64" y="64" />
 <Anchors><Anchor point="CENTER" relativeTo="UIParent"/></Anchors>
</Frame>
</Ui>

Note the use of the OnLoad Script, In order to drag a frame it must register for drag via Frame:RegisterForDrag("button"). Above code edited to include the <Ui> </Ui> tags as it is .xml and won't work without this.

Using OnMouseUp/OnMouseDown

While OnDrag* handlers typically require the mouse button to be held down for a small amount of time prior to enabling the mouse behavior, making them well suited for dragging widgets that normally respond to clicks. However, if the frame you wish to make draggable is not normally a button, you can use OnMouseUp/OnMouseDown to provide a more responsive experience.

The following snippet illustrates this concept, and should replace the <Scripts> block above:

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

Note: this also adds a frame.isLocked boolean field to control whether the frame can be dragged or not. There's generally no limit to the amount of conditional logic you can insert prior to :StartMoving() -- for instance, requiring the user to hold down some modifier button is a possible alternative.

Using TitleRegion

Frames can have a TitleRegion object that handles dragging automatically -- as long as the mouse is held down within the TitleRegion, it'll allow the frame to be dragged.

The frame created below will be draggable by clicking on its top 20 pixels:

<Frame enableMouse="true">
 <Size x="100" y="100"/>
 <TitleRegion>
  <Size x="100" y="20"/>
  <Anchors><Anchor point="TOP"/></Anchors>
 </TitleRegion>
 <!-- Tags below add a visual element to the frame. -->
 <Layers>
  <Layer level="ARTWORK">
   <Texture setAllPoints="true">
    <Color r="1.0" g="0.5" b="0.0" a="0.5" />
   </Texture>
    </Layer>
   </Layers>
   <Anchors><Anchor point="CENTER" relativeTo="UIParent"/></Anchors>
</Frame>

Using Lua

Using OnDragStart OnDragStop

The code sample below illustrates the use of the Widget API to achieve the same results as the XML above:

local frame = CreateFrame("Frame", "DragFrame2", UIParent)
frame:SetMovable(true)
frame:EnableMouse(true)
frame:RegisterForDrag("LeftButton")
frame:SetScript("OnDragStart", frame.StartMoving)
frame:SetScript("OnDragStop", frame.StopMovingOrSizing)
-- The code below makes the frame visible, and is not necessary to enable dragging.
frame:SetPoint("CENTER"); frame:SetWidth(64); frame:SetHeight(64);
local tex = frame:CreateTexture("ARTWORK");
tex:SetAllPoints();
tex:SetTexture(1.0, 0.5, 0); tex:SetAlpha(0.5);

Using OnMouseDown / OnMouseUp

The code sample below illustrates the use of the Widget API to achieve the same results as the XML above:

local frame = CreateFrame("Frame", "DragFrame2", UIParent)
frame:SetMovable(true)
frame:EnableMouse(true)
frame:SetScript("OnMouseDown", function(self, button)
  if button == "LeftButton" and not self.isMoving then
   self:StartMoving();
   self.isMoving = true;
  end
end)
frame:SetScript("OnMouseUp", function(self, button)
  if button == "LeftButton" and self.isMoving then
   self:StopMovingOrSizing();
   self.isMoving = false;
  end
end)
frame:SetScript("OnHide", function(self)
  if ( self.isMoving ) then
   self:StopMovingOrSizing();
   self.isMoving = false;
  end
end)
-- The code below makes the frame visible, and is not necessary to enable dragging.
frame:SetPoint("CENTER"); frame:SetWidth(64); frame:SetHeight(64);
local tex = frame:CreateTexture("ARTWORK");
tex:SetAllPoints();
tex:SetTexture(1.0, 0.5, 0); tex:SetAlpha(0.5);

General Notes

The use of the OnHide Handler is not entirely necessary. However it is a good idea if your frames are being Hidden or Shown by running code and not the User.

The use of full functions is also not required in lua:

frame:SetScript("OnDragStart" frame.StartMoving)
frame:SetScript("OnDragStop" frame.StopMovingOrSizing)
frame:SetScript("OnMouseDown" frame.StartMoving)
frame:SetScript("OnMouseUp" frame.StopMovingOrSizing)


In XML all that is required is just the function:

<OnDragStart>self:StartMoving();</OnDragStart>
<OnDragStop>self:StopMovingOrSizing();</OnDragStop>
<OnMouseDown>self:StartMoving();</OnMouseDown>
<OnMouseUp>self:StopMovingOrSizing();</OnMouseUp>