WoWInterface

WoWInterface (https://www.wowinterface.com/forums/index.php)
-   Tutorials & Other Helpful Info. (https://www.wowinterface.com/forums/forumdisplay.php?f=12)
-   -   Moving frames and saving position without using SavedVariables (https://www.wowinterface.com/forums/showthread.php?t=38484)

zork 01-20-11 02:03 AM

Moving frames and saving position without using SavedVariables
 
See below...

zork 03-13-12 04:41 PM

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

zork 08-09-12 07:58 AM

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)

p3lim 08-09-12 09:15 AM

I belive I used something like this before:
Lua Code:
  1. frameToMove:CreateTitleRegion():SetAllPoints()
  2. frameToMove:SetUserPlaced(true)

zork 08-09-12 10:54 AM

Oh. Never knew of: http://wowprogramming.com/docs/widge...ateTitleRegion
Awesome.


All times are GMT -6. The time now is 05:16 PM.

vBulletin © 2024, Jelsoft Enterprises Ltd
© 2004 - 2022 MMOUI