Making Draggable Frames 【為wow插件編寫可拖動的Frame】
FW: http://www.wowwiki.com/Making_Draggable_Frames
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>
<OnLoad>
this:RegisterForDrag("LeftButton");
</OnLoad>
<OnDragStart>
this:StartMoving();
this.isMoving = true;
</OnDragStart>
<OnDragStop>
this:StopMovingOrSizing();
this.isMoving = false;
</OnDragStop>
</Scripts>
<OnLoad>
this:RegisterForDrag("LeftButton");
</OnLoad>
<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>
<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. To do this, one must make the parent frame movable through the use of the SetMovable widget function, i.e. frame:SetMovable(true). One 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.Quick Dragging Code
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><Frame name="myname" frameStrata="HIGH" toplevel="true" enableMouse="true" movable="true" parent="UIParent">
<TitleRegion setAllPoints="true"/>

</Frame>
<TitleRegion setAllPoints="true"/>

</Frame>
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.
Using this method can result in the frame not responding to other mouse events, also both mouse buttons will drag the frame.
You can also specify <Size> and <Anchors> within <TitleRegion>, e.g.
<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>
<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>.
Lua Only Approach
If your frame is called MyFrame -MyFrame:SetMovable(true)
MyFrame:EnableMouse(true)
MyFrame:SetScript("OnMouseDown",function()
MyFrame:StartMoving()
end)
MyFrame:SetScript("OnMouseUp",function()
MyFrame:StopMovingOrSizing()
end)
MyFrame:EnableMouse(true)
MyFrame:SetScript("OnMouseDown",function()
MyFrame:StartMoving()
end)
MyFrame:SetScript("OnMouseUp",function()
MyFrame:StopMovingOrSizing()
end)
posted on 2008-10-21 16:11 DoNeY 閱讀(743) 評論(0) 編輯 收藏 所屬分類: GameDev