Quantcast

-- localise the addon wide data table
local addonName,addonData = ...

-- Make a selected frame movable
addonData.SetMovable = function(self,frame)

	-- If the mouse is ready to move start moving the frame
	frame.OnMouseDown = function(self)
		-- If the frame isn't locked then start moving the frame
		if ( ( not self.isLocked ) or ( self.isLocked == 0 ) ) then
			self:StartMoving()
			self.isMoving = true
		end
	end

	-- If the mouse has stopped moving stop moving frame
	frame.OnMouseUp = function(self)
		-- If frame is moving then stop
		if ( self.isMoving ) then
			self:StopMovingOrSizing()
			self.isMoving = false
		end
	end

	-- If the scroll frame is hidden stop moving
	frame.OnHide = function(self)
		-- If frame is moving then stop
		if ( self.isMoving ) then
			self:StopMovingOrSizing()
			self.isMoving = false
		end
	end

	frame:RegisterForDrag("LeftButton")
	frame:EnableMouse(true)
	frame:SetMovable(true)
	frame:SetScript("OnMouseDown",function(self) self:OnMouseDown() end)
	frame:SetScript("OnMouseUp",function(self) self:OnMouseUp() end)
	frame:SetScript("OnHide",function(self) self:OnHide() end)

end

addonData.BuildFrame = function(self,name,parent,options)
	parent = parent or UIParent
	local frame = CreateFrame("Frame",name,parent)
	frame:SetWidth(options.FrameWidth)
	frame:SetHeight(options.FrameHeight)
   	frame:SetAlpha(1.0)
   	frame:SetClampedToScreen(true)
	frame:SetPoint("CENTER",UIParent,"CENTER",0,0)
	if options.HasBorder then
		self:AddBorder(frame)
	end
	if options.IsMovable then
		self:SetMovable(frame)
	end
	return frame
end

addonData.AddBorder = function(self,frame)
	local border = CreateFrame("Frame",_G[frame:GetName().."Border"],frame)
	border:SetFrameLevel(frame:GetFrameLevel()-1)
	border:SetFrameStrata(frame:GetFrameStrata())
	border:ClearAllPoints()
	border:SetPoint("TOPLEFT",-10,10)
	border:SetPoint("BOTTOMRIGHT",35,-10)

	local backDrop = {
		bgFile = "Interface/Tooltips/UI-Tooltip-Background",
		edgeFile = "Interface/Tooltips/UI-Tooltip-Border",
		tile = true,
		tileSize = 16,
		edgeSize = 16,
		insets =
		{
			left = 4,
			right = 4,
			top = 4,
			bottom = 4
		},
	}
	border:SetBackdrop(backDrop)
	border:SetBackdropBorderColor( 1, 1, 0, 1 )
	border:SetBackdropColor( 0, 0, 0, 1 )

	frame.border = border
end


addonData.BuildScrollFrame = function(self,name,parent,options)
	parent = parent or UIParent
	local frame = CreateFrame("ScrollFrame",name,parent,"UIPanelScrollFrameTemplate")
	frame.ScrollBar = _G[frame:GetName() .. "ScrollBar"];
	frame.ScrollBar:SetPoint("LEFT",frame,"RIGHT",5,0)
	frame:SetWidth(options.FrameWidth - 55)
	frame:SetHeight(options.FrameHeight - 20)
	frame:SetPoint("CENTER",parent,"CENTER",10,-10)
	if options.HasBorder then
		self:AddBorder(frame)
	end
	if options.IsMovable then
		self:SetMovable(frame)
	end
	return frame
end