View Single Post
06-09-17, 05:02 AM   #8
Resike
A Pyroguard Emberseer
AddOn Author - Click to view addons
Join Date: Mar 2010
Posts: 1,290
Lua Code:
  1. math.round = function(self)
  2.     return math.floor(self + 0.5)
  3. end
  4.  
  5. local frame = CreateFrame("Frame", "TestFrame", UIParent)
  6. frame:SetSize(400, 200)
  7. frame:SetPoint("TOPLEFT", UIParent, "TOPLEFT", (UIParent:GetWidth() / 2) - (frame:GetWidth() / 2), (-UIParent:GetHeight() / 2) + (frame:GetHeight() / 2))
  8. frame:SetClampedToScreen(true)
  9.  
  10. frame.backdrop = {
  11.     bgFile = "Interface\\Buttons\\WHITE8X8",
  12.     edgeFile = "Interface\\Tooltips\\UI-Tooltip-Border",
  13.     tile = true,
  14.     tileSize = 16,
  15.     edgeSize = 14,
  16.     insets = {left = 3, right = 3, top = 3, bottom = 3},
  17. }
  18.  
  19. frame:SetBackdrop(frame.backdrop)
  20. frame:SetBackdropColor(0.0, 0.0, 0.0, 0.5)
  21. frame:SetBackdropBorderColor(0.3, 0.3, 0.3, 1.0)
  22.  
  23. -- Hooks
  24. hooksecurefunc(frame, "SetPoint", function(self)
  25.     if self.moving then
  26.         return
  27.     end
  28.  
  29.     self.moving = true
  30.     self:SetMovable(true)
  31.  
  32.     local left, bottom = self:GetLeft(), self:GetBottom()
  33.  
  34.     if left and bottom then
  35.         local x = math.round(left)
  36.         local y = math.round(-UIParent:GetHeight() + bottom + self:GetHeight())
  37.  
  38.         self:ClearAllPoints()
  39.         self:SetPoint("TOPLEFT", UIParent, "TOPLEFT", x, y)
  40.     end
  41.  
  42.     self:SetMovable(false)
  43.     self.moving = nil
  44. end)
  45.  
  46. hooksecurefunc(frame, "StopMovingOrSizing", function(self)
  47.     self:SetMovable(true)
  48.  
  49.     local left, bottom = self:GetLeft(), self:GetBottom()
  50.  
  51.     if left and bottom then
  52.         local x = math.round(left)
  53.         local y = math.round(-UIParent:GetHeight() + bottom + self:GetHeight())
  54.  
  55.         self:ClearAllPoints()
  56.         self:SetPoint("TOPLEFT", UIParent, "TOPLEFT", x, y)
  57.     end
  58.  
  59.     self:SetMovable(false)
  60. end)
  61.  
  62. frame:HookScript("OnSizeChanged", function(self)
  63.     self:SetSize(math.round(self:GetWidth()), math.round(self:GetHeight()))
  64. end)
  65.  
  66. -- Moving and Resizing functions
  67. frame:EnableMouse(true)
  68. --frame:SetMaxResize(800, 400)
  69. frame:SetMinResize(40, 40)
  70.  
  71. frame:SetScript("OnMouseDown", function(self, button)
  72.     if button == "LeftButton" then
  73.         self:SetMovable(true)
  74.         self:StartMoving()
  75.     end
  76. end)
  77. frame:SetScript("OnMouseUp", function(self, button)
  78.     if button == "LeftButton" then
  79.         self:StopMovingOrSizing()
  80.         self:SetMovable(false)
  81.  
  82.         local left, bottom = self:GetLeft(), self:GetBottom()
  83.  
  84.         if left and bottom then
  85.             local x = math.round(left)
  86.             local y = math.round(-UIParent:GetHeight() + bottom + self:GetHeight())
  87.  
  88.             self:ClearAllPoints()
  89.             self:SetPoint("TOPLEFT", UIParent, "TOPLEFT", x, y)
  90.         end
  91.     end
  92. end)
  93.  
  94. frame.br = CreateFrame("Frame", nil, frame)
  95. frame.br:SetPoint("TOPLEFT", frame, "BOTTOMRIGHT", -15, 15)
  96. frame.br:SetSize(12, 12)
  97. frame.br:EnableMouse(true)
  98.  
  99. frame.br.texture = frame.br:CreateTexture(nil, "OVERLAY")
  100. frame.br.texture:SetPoint("TOPLEFT", frame.br, "TOPLEFT", 0, 0)
  101. frame.br.texture:SetSize(12, 12)
  102. frame.br.texture:SetTexture("Interface\\ChatFrame\\UI-ChatIM-SizeGrabber-Up")
  103.  
  104. frame.br:SetScript("OnEnter", function(self)
  105.     frame.br.texture:SetTexture("Interface\\ChatFrame\\UI-ChatIM-SizeGrabber-Highlight")
  106. end)
  107. frame.br:SetScript("OnLeave", function(self)
  108.     frame.br.texture:SetTexture("Interface\\ChatFrame\\UI-ChatIM-SizeGrabber-Up")
  109. end)
  110.  
  111. frame.br:SetScript("OnMouseDown", function(self, button)
  112.     if button == "LeftButton" then
  113.         frame.br.texture:SetTexture("Interface\\ChatFrame\\UI-ChatIM-SizeGrabber-Down")
  114.  
  115.         frame:SetResizable(true)
  116.         frame:StartSizing("BottomRight")
  117.     end
  118. end)
  119. frame.br:SetScript("OnMouseUp", function(self, button)
  120.     frame:StopMovingOrSizing()
  121.     frame:SetResizable(false)
  122.  
  123.     frame.br.texture:SetTexture("Interface\\ChatFrame\\UI-ChatIM-SizeGrabber-Up")
  124. end)

This frame can be moved/resized on the fly, and it keeps it's pixel perfection sizes on all of it's elements all the time, even after any (SetSize|SetWidth|SetHeight|SetPoint) calls.
  Reply With Quote