View Single Post
Old 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,301
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.
| Roth UI | Roth UI FAQ | GoogleCode | Zork | TDMOG

"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.
zork is offline   Reply With Quote