Thread Tools Display Modes
01-20-11, 02:03 AM   #1
zork
A Pyroguard Emberseer
 
zork's Avatar
AddOn Author - Click to view addons
Join Date: Jul 2008
Posts: 1,740
Moving frames and saving position without using SavedVariables

See below...
__________________
| Simple is beautiful.
| WoWI AddOns | GitHub | Zork (WoW)

"I wonder what the non-pathetic people are doing tonight?" - Rajesh Koothrappali (The Big Bang Theory)

Last edited by zork : 03-13-12 at 04:43 PM.
  Reply With Quote
03-13-12, 04:41 PM   #2
zork
A Pyroguard Emberseer
 
zork's Avatar
AddOn Author - Click to view addons
Join Date: Jul 2008
Posts: 1,740
I reviewed my code.

Now we have a dedicated dragframe that sits on top of the anchored frame.
The dragframe receives the drag event and delegates the mouse move event to the anchored frame.

Additionally lock/unlock and reset function can be anchored directly on the frame.

lua Code:
  1. --apply drag functionality to any frame
  2.   local applyDragFunctionality = function(self)
  3.     --save the default position
  4.     local getPoint = function(self)
  5.       local pos = {}
  6.       pos.a1, pos.af, pos.a2, pos.x, pos.y = self:GetPoint()
  7.       if pos.af and pos.af:GetName() then pos.af = pos.af:GetName() end
  8.       return pos
  9.     end
  10.     self.defaultPosition = getPoint(self)
  11.     --the drag frame
  12.     local df = CreateFrame("Frame",nil,self)
  13.     df:SetAllPoints(self)
  14.     df:SetFrameStrata("HIGH")
  15.     df:SetHitRectInsets(0,0,0,0)
  16.     df:SetScript("OnDragStart", function(self) if IsAltKeyDown() and IsShiftKeyDown() then self:GetParent():StartMoving() end end)
  17.     df:SetScript("OnDragStop", function(self) self:GetParent():StopMovingOrSizing() end)
  18.     --dragframe texture
  19.     local t = df:CreateTexture(nil,"OVERLAY",nil,6)
  20.     t:SetAllPoints(df)
  21.     t:SetTexture(0,1,0)
  22.     t:SetAlpha(0.2)
  23.     --stuff
  24.     df.texture = t
  25.     df:Hide()
  26.     self.dragframe = df
  27.     self:SetClampedToScreen(true)
  28.     self:SetMovable(true)
  29.     self:SetUserPlaced(true)
  30.     --helper functions
  31.     --unlock
  32.     local unlock = function(self)
  33.       if not self:IsUserPlaced() then return end
  34.       self.dragframe:Show()
  35.       self.dragframe:EnableMouse(true)
  36.       self.dragframe:RegisterForDrag("LeftButton")
  37.       self.dragframe:SetScript("OnEnter", function(self)
  38.         GameTooltip:SetOwner(self, "ANCHOR_TOP")
  39.         GameTooltip:AddLine(self:GetParent():GetName(), 0, 1, 0.5, 1, 1, 1)
  40.         GameTooltip:AddLine("Hold down ALT+SHIFT to drag!", 1, 1, 1, 1, 1, 1)
  41.         GameTooltip:Show()
  42.       end)
  43.       self.dragframe:SetScript("OnLeave", function() GameTooltip:Hide() end)
  44.     end
  45.     --lock
  46.     local lock = function(self)
  47.       if not self:IsUserPlaced() then return end
  48.       self.dragframe:Hide()
  49.       self.dragframe:EnableMouse(false)
  50.       self.dragframe:RegisterForDrag(nil)
  51.       self.dragframe:SetScript("OnEnter", nil)
  52.       self.dragframe:SetScript("OnLeave", nil)
  53.     end
  54.     --reset position
  55.     local reset = function(self)
  56.       if self.defaultPosition then
  57.         self:ClearAllPoints()
  58.         local pos = self.defaultPosition
  59.         if pos.af and pos.a2 then
  60.           self:SetPoint(pos.a1 or "CENTER", pos.af, pos.a2, pos.x or 0, pos.y or 0)
  61.         elseif pos.af then
  62.           self:SetPoint(pos.a1 or "CENTER", pos.af, pos.x or 0, pos.y or 0)
  63.         else
  64.           self:SetPoint(pos.a1 or "CENTER", pos.x or 0, pos.y or 0)
  65.         end
  66.       else
  67.         self:SetPoint("CENTER",0,0)
  68.       end
  69.     end
  70.     self.unlock = unlock
  71.     self.lock = lock
  72.     self.reset = reset
  73.   end

If you want to call the function you can make use of the global frame name and slash commands.

lua Code:
  1. --CALL
  2.  
  3.     local function SlashCmd(cmd)
  4.     if (cmd:match"unlock") then
  5.       myFrame.unlock(myFrame)
  6.     elseif (cmd:match"lock") then
  7.       myFrame.lock(myFrame)
  8.     elseif (cmd:match"reset") then
  9.       myFrame.reset(myFrame)
  10.     end
  11.   end
  12.  
  13.   SlashCmdList["mymod"] = SlashCmd;
  14.   SLASH_mymod1 = "/mymod";

The most important part is that the drag function gets called on code init. It must not be called on events like PLAYER_LOGIN. That will reset the values that Blizzard read from the layout-local.txt file.

Example implementations:
http://code.google.com/p/rothui/source/detail?r=841
__________________
| Simple is beautiful.
| WoWI AddOns | GitHub | Zork (WoW)

"I wonder what the non-pathetic people are doing tonight?" - Rajesh Koothrappali (The Big Bang Theory)

Last edited by zork : 03-14-12 at 09:51 AM.
  Reply With Quote
08-09-12, 07:58 AM   #3
zork
A Pyroguard Emberseer
 
zork's Avatar
AddOn Author - Click to view addons
Join Date: Jul 2008
Posts: 1,740
Moving Blizzard frames with the same method is no problem aswell.

Lua Code:
  1. --making the colorpicker frame movable
  2. local cpf = ColorPickerFrame
  3. local mover = CreateFrame("Frame", nil, cpf)
  4. mover:SetPoint("TOP",0,0)
  5. mover:SetSize(120,15)
  6. mover:EnableMouse(true)
  7. mover:SetScript("OnMouseDown", function(self,button,...)
  8.   if button == "LeftButton" then
  9.     cpf:StartMoving()
  10.   end
  11. end)
  12. mover:SetScript("OnMouseUp", function()
  13.   cpf:StopMovingOrSizing()
  14. end)
  15. cpf:SetUserPlaced(true)
__________________
| Simple is beautiful.
| WoWI AddOns | GitHub | Zork (WoW)

"I wonder what the non-pathetic people are doing tonight?" - Rajesh Koothrappali (The Big Bang Theory)
  Reply With Quote
08-09-12, 09:15 AM   #4
p3lim
A Pyroguard Emberseer
 
p3lim's Avatar
AddOn Author - Click to view addons
Join Date: Feb 2007
Posts: 1,710
I belive I used something like this before:
Lua Code:
  1. frameToMove:CreateTitleRegion():SetAllPoints()
  2. frameToMove:SetUserPlaced(true)
  Reply With Quote
08-09-12, 10:54 AM   #5
zork
A Pyroguard Emberseer
 
zork's Avatar
AddOn Author - Click to view addons
Join Date: Jul 2008
Posts: 1,740
Oh. Never knew of: http://wowprogramming.com/docs/widge...ateTitleRegion
Awesome.
__________________
| Simple is beautiful.
| WoWI AddOns | GitHub | Zork (WoW)

"I wonder what the non-pathetic people are doing tonight?" - Rajesh Koothrappali (The Big Bang Theory)
  Reply With Quote

WoWInterface » Developer Discussions » Tutorials & Other Helpful Info. » Moving frames and saving position without using SavedVariables

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off