Quantcast
local A, L = ...

L.HiddenFrame = CreateFrame('Frame')
L.HiddenFrame:Hide()

--CreateBackdrop
local function CreateBackdrop(self, relativeTo, anotherBackdrop)
  local backdrop = anotherBackdrop or L.C.backdrop
  local parent = self.IsObjectType and self:IsObjectType('Texture') and self:GetParent() or self
  local bd = CreateFrame("Frame", nil, parent, BackdropTemplateMixin and "BackdropTemplate")
  if (parent:GetFrameLevel() - 1) >= 0 then
	bd:SetFrameLevel(parent:GetFrameLevel() - 1)
  else
	bd:SetFrameLevel(0)
  end

  bd:SetPoint("TOPLEFT", relativeTo or self, "TOPLEFT", -backdrop.inset, backdrop.inset)
  bd:SetPoint("BOTTOMRIGHT", relativeTo or self, "BOTTOMRIGHT", backdrop.inset, -backdrop.inset)
  bd:SetBackdrop(backdrop)
  bd:SetBackdropColor(unpack(backdrop.bgColor))
  bd:SetBackdropBorderColor(unpack(backdrop.edgeColor))
  return bd
end
L.CreateBackdrop = CreateBackdrop

--From http://wow.gamepedia.com/UI_coordinates
local function FramesOverlap(frameA, frameB)
	if not frameA or not frameB then return	end

	local sA, sB = frameA:GetEffectiveScale(), frameB:GetEffectiveScale()
	if not sA or not sB then return	end

	local frameALeft, frameARight, frameABottom, frameATop = frameA:GetLeft(), frameA:GetRight(), frameA:GetBottom(), frameA:GetTop()
	local frameBLeft, frameBRight, frameBBottom, frameBTop = frameB:GetLeft(), frameB:GetRight(), frameB:GetBottom(), frameB:GetTop()
	if not (frameALeft and frameARight and frameABottom and frameATop) then return end
	if not (frameBLeft and frameBRight and frameBBottom and frameBTop) then return end

	return ((frameALeft*sA) < (frameBRight*sB)) and ((frameBLeft*sB) < (frameARight*sA)) and ((frameABottom*sA) < (frameBTop*sB)) and ((frameBBottom*sB) < (frameATop*sA))
end
L.FramesOverlap = FramesOverlap

local function Kill(object)
	if object.UnregisterAllEvents then
		object:UnregisterAllEvents()
		object:SetParent(L.HiddenFrame)
	else
		object.Show = object.Hide
	end

	object:Hide()
end
L.Kill = Kill

--SetPoint
local function SetPoint(self,relativeTo,point)
  --adjut the setpoint function to make it possible to reference a relativeTo object that is set on runtime and it not available on config init
  local a,b,c,d,e = unpack(point)
  if not b then
    self:SetPoint(a)
  elseif b and type(b) == "string" and not _G[b] then
    self:SetPoint(a,relativeTo,b,c,d)
  else
    self:SetPoint(a,b,c,d,e)
  end
end
L.SetPoint = SetPoint

--CreateIcon
local function CreateIcon(self,layer)
  local icon = self:CreateTexture(nil,layer)
  icon:SetAllPoints()
  return icon
end
L.CreateIcon = CreateIcon

local function StripTextures(self, kill)
	for i = 1, self:GetNumRegions() do
		local Region = select(i, self:GetRegions())
		if (Region and Region:GetObjectType() == "Texture") then
			if (kill and type(kill) == "boolean") then
				L.Kill(Region)
			elseif (Region:GetDrawLayer() == kill) then
				Region:SetTexture(nil)
			elseif (kill and type(kill) == "string" and Region:GetTexture() ~= kill) then
				Region:SetTexture(nil)
			else
				Region:SetTexture(nil)
			end
		end
	end
end
L.StripTextures = StripTextures

--RGB to Hex
local function RGBToHex(r, g, b, header, ending)
	r = r <= 1 and r >= 0 and r or 1
	g = g <= 1 and g >= 0 and g or 1
	b = b <= 1 and b >= 0 and b or 1
	return format('%s%02x%02x%02x%s', header or '|cff', r*255, g*255, b*255, ending or '')
end
L.RGBToHex = RGBToHex

local function HandleButton(button)
	if button.isSkinned then return end

	if button.SetNormalTexture and not overrideTex then button:SetNormalTexture('') end
	if button.SetHighlightTexture then button:SetHighlightTexture('') end
	if button.SetPushedTexture then button:SetPushedTexture('') end
	if button.SetDisabledTexture then button:SetDisabledTexture('') end

	button.isSkinned = true
end
L.HandleButton = HandleButton