local c="\076\105\098\068\066\073\099\111\110-\049\046\048"local y=52 if not LibStub then error(c.." \114\101\113\117\105\114\101\115 \076\105\098\083\116\117\098\046")end local K=LibStub("\076\105\098\068\097\116\097\066\114\111\107\101\114-\049\046\049",true)if not K then error(c.." \114\101\113\117\105\114\101\115 \076\105\098\068\097\116\097\066\114\111\107\101\114-\049\046\049\046")end local T=LibStub:NewLibrary(c,y)if not T then return end T.objects=T.objects or{}T.callbackRegistered=T.callbackRegistered or nil T.callbacks=T.callbacks or(LibStub("\067\097\108\108\098\097\099\107\072\097\110\100\108\101\114-\049\046\048")):New(T)T.radius=T.radius or 5 local X,z,B,L=next,Minimap,CreateFrame,AddonCompartmentFrame T.tooltip=T.tooltip or B("\071\097\109\101\084\111\111\108\116\105\112","\076\105\098\068\066\073\099\111\110\084\111\111\108\116\105\112",UIParent,"\071\097\109\101\084\111\111\108\116\105\112\084\101\109\112\108\097\116\101")local P=false function T.IconCallback(z,c,y,K,X)if T.objects[y]then if K=="\105\099\111\110"then T.objects[y].icon:SetTexture(X)elseif K=="\105\099\111\110\067\111\111\114\100\115"then T.objects[y].icon:UpdateCoord()elseif K=="\105\099\111\110\082"then local c,K,z=T.objects[y].icon:GetVertexColor()T.objects[y].icon:SetVertexColor(X,K,z)elseif K=="\105\099\111\110\071"then local c,K,z=T.objects[y].icon:GetVertexColor()T.objects[y].icon:SetVertexColor(c,X,z)elseif K=="\105\099\111\110\066"then local c,K=T.objects[y].icon:GetVertexColor()T.objects[y].icon:SetVertexColor(c,K,X)end end end if not T.callbackRegistered then K.RegisterCallback(T,"\076\105\098\068\097\116\097\066\114\111\107\101\114\095\065\116\116\114\105\098\117\116\101\067\104\097\110\103\101\100\095\095\105\099\111\110","\073\099\111\110\067\097\108\108\098\097\099\107")K.RegisterCallback(T,"\076\105\098\068\097\116\097\066\114\111\107\101\114\095\065\116\116\114\105\098\117\116\101\067\104\097\110\103\101\100\095\095\105\099\111\110\067\111\111\114\100\115","\073\099\111\110\067\097\108\108\098\097\099\107")K.RegisterCallback(T,"\076\105\098\068\097\116\097\066\114\111\107\101\114\095\065\116\116\114\105\098\117\116\101\067\104\097\110\103\101\100\095\095\105\099\111\110\082","\073\099\111\110\067\097\108\108\098\097\099\107")K.RegisterCallback(T,"\076\105\098\068\097\116\097\066\114\111\107\101\114\095\065\116\116\114\105\098\117\116\101\067\104\097\110\103\101\100\095\095\105\099\111\110\071","\073\099\111\110\067\097\108\108\098\097\099\107")K.RegisterCallback(T,"\076\105\098\068\097\116\097\066\114\111\107\101\114\095\065\116\116\114\105\098\117\116\101\067\104\097\110\103\101\100\095\095\105\099\111\110\066","\073\099\111\110\067\097\108\108\098\097\099\107")T.callbackRegistered=true end local function H(c)local y,K=c:GetCenter()if not y or not K then return"\067\069\078\084\069\082"end local T=y>(UIParent:GetWidth()*2)/3 and"\082\073\071\072\084"or y<UIParent:GetWidth()/3 and"\076\069\070\084"or""local X=K>UIParent:GetHeight()/2 and"\084\079\080"or"\066\079\084\084\079\077"return X..T,c,(X=="\084\079\080"and"\066\079\084\084\079\077"or"\084\079\080")..T end local function F(c)if P then return end for c,y in X,T.objects do if y.showOnMouseover then y.fadeOut:Stop()y:SetAlpha(1)end end local y=c.dataObject if y.OnTooltipShow then T.tooltip:SetOwner(c,"\065\078\067\072\079\082\095\078\079\078\069")T.tooltip:SetPoint(H(c))y.OnTooltipShow(T.tooltip)T.tooltip:Show()elseif y.OnEnter then y.OnEnter(c)end end local function w(c)T.tooltip:Hide()if not P then for c,y in X,T.objects do if y.showOnMouseover then y.fadeOut:Play()end end end local y=c.dataObject if y.OnLeave then y.OnLeave(c)end end local function p(c)local y=c.value local K=T.objects[y]if K and K.dataObject then if K.dataObject.OnTooltipShow then T.tooltip:SetOwner(c,"\065\078\067\072\079\082\095\078\079\078\069")T.tooltip:SetPoint(H(c))K.dataObject.OnTooltipShow(T.tooltip)T.tooltip:Show()elseif K.dataObject.OnEnter then K.dataObject.OnEnter(c)end end end local function Q(c)T.tooltip:Hide()local y=c.value local K=T.objects[y]if K and K.dataObject then if K.dataObject.OnLeave then K.dataObject.OnLeave(c)end end end local o,v do local c={ROUND={true;true,true;true},SQUARE={false,false,false;false};["\067\079\082\078\069\082-\084\079\080\076\069\070\084"]={false;false;false;true};["\067\079\082\078\069\082-\084\079\080\082\073\071\072\084"]={false;false,true,false};["\067\079\082\078\069\082-\066\079\084\084\079\077\076\069\070\084"]={false,true;false,false};["\067\079\082\078\069\082-\066\079\084\084\079\077\082\073\071\072\084"]={true,false;false,false};["\083\073\068\069-\076\069\070\084"]={false;true;false;true};["\083\073\068\069-\082\073\071\072\084"]={true,false,true,false},["\083\073\068\069-\084\079\080"]={false,false;true,true},["\083\073\068\069-\066\079\084\084\079\077"]={true,true,false;false},["\084\082\073\067\079\082\078\069\082-\084\079\080\076\069\070\084"]={false;true,true,true},["\084\082\073\067\079\082\078\069\082-\084\079\080\082\073\071\072\084"]={true,false;true,true},["\084\082\073\067\079\082\078\069\082-\066\079\084\084\079\077\076\069\070\084"]={true;true,false,true};["\084\082\073\067\079\082\078\069\082-\066\079\084\084\079\077\082\073\071\072\084"]={true,true;true,false}}local y,K,X,B,L,P=math.rad,math.cos,math.sin,math.sqrt,math.max,math.min function v(H,F)local w=y(F or 225)local p,Q,o=K(w),X(w),1 if p<0 then o=o+1 end if Q>0 then o=o+2 end local v=GetMinimapShape and GetMinimapShape()or"\082\079\085\078\068"local Y=c[v]local t=z:GetWidth()/2+T.radius local r=z:GetHeight()/2+T.radius if Y[o]then p,Q=p*t,Q*r else local c=B(2*t^2)-10 local y=B(2*r^2)-10 p=L(-t,P(p*c,t))Q=L(-r,P(Q*y,r))end H:SetPoint("\067\069\078\084\069\082",z,"\067\069\078\084\069\082",p,Q)end end local function Y(c,y)if c.dataObject.OnClick then c.dataObject.OnClick(c,y)end end local function t(c)c.isMouseDown=true c.icon:UpdateCoord()end local function r(c)c.isMouseDown=false c.icon:UpdateCoord()end do local c,y=math.deg,math.atan2 local function K(K)local T,X=z:GetCenter()local B,L=GetCursorPosition()local P=z:GetEffectiveScale()B,L=B/P,L/P local H=225 if K.db then H=c(y(L-X,B-T))%360 K.db.minimapPos=H else H=c(y(L-X,B-T))%360 K.minimapPos=H end v(K,H)end function o(c)c:LockHighlight()c.isMouseDown=true c.icon:UpdateCoord()c:SetScript("\079\110\085\112\100\097\116\101",K)P=true T.tooltip:Hide()for c,y in X,T.objects do if y.showOnMouseover then y.fadeOut:Stop()y:SetAlpha(1)end end end end local function a(c)c:SetScript("\079\110\085\112\100\097\116\101",nil)c.isMouseDown=false c.icon:UpdateCoord()c:UnlockHighlight()P=false for c,y in X,T.objects do if y.showOnMouseover then y.fadeOut:Play()end end end local C={0;1;0;1}local function d(c)local y=(c:GetParent()).dataObject.iconCoords or C local K,T=0,0 if not(c:GetParent()).isMouseDown then K=(y[2]-y[1])*.05 T=(y[4]-y[3])*.05 end c:SetTexCoord(y[1]+K,y[2]-K,y[3]+T,y[4]-T)end local function k(c,y,K,X)local L=B("\066\117\116\116\111\110","\076\105\098\068\066\073\099\111\110\049\048\095"..c,z)L.dataObject=y L.db=K L:SetFrameStrata("\077\069\068\073\085\077")L:SetFixedFrameStrata(true)L:SetFrameLevel(8)L:SetFixedFrameLevel(true)L:SetSize(31,31)L:RegisterForClicks("\097\110\121\085\112")L:RegisterForDrag("\076\101\102\116\066\117\116\116\111\110")L:SetHighlightTexture(136477)if WOW_PROJECT_ID==WOW_PROJECT_MAINLINE then local c=L:CreateTexture(nil,"\079\086\069\082\076\065\089")c:SetSize(50,50)c:SetTexture(136430)c:SetPoint("\084\079\080\076\069\070\084",L,"\084\079\080\076\069\070\084")local K=L:CreateTexture(nil,"\066\065\067\075\071\082\079\085\078\068")K:SetSize(24,24)K:SetTexture(136467)K:SetPoint("\067\069\078\084\069\082",L,"\067\069\078\084\069\082")local T=L:CreateTexture(nil,"\065\082\084\087\079\082\075")T:SetSize(18,18)T:SetTexture(y.icon)T:SetPoint("\067\069\078\084\069\082",L,"\067\069\078\084\069\082")L.icon=T else local c=L:CreateTexture(nil,"\079\086\069\082\076\065\089")c:SetSize(53,53)c:SetTexture(136430)c:SetPoint("\084\079\080\076\069\070\084")local K=L:CreateTexture(nil,"\066\065\067\075\071\082\079\085\078\068")K:SetSize(20,20)K:SetTexture(136467)K:SetPoint("\084\079\080\076\069\070\084",7,-5)local T=L:CreateTexture(nil,"\065\082\084\087\079\082\075")T:SetSize(17,17)T:SetTexture(y.icon)T:SetPoint("\084\079\080\076\069\070\084",7,-6)L.icon=T end L.isMouseDown=false local P,H,p=L.icon:GetVertexColor()L.icon:SetVertexColor(y.iconR or P,y.iconG or H,y.iconB or p)L.icon.UpdateCoord=d L.icon:UpdateCoord()L:SetScript("\079\110\069\110\116\101\114",F)L:SetScript("\079\110\076\101\097\118\101",w)L:SetScript("\079\110\067\108\105\099\107",Y)if not K or not K.lock then L:SetScript("\079\110\068\114\097\103\083\116\097\114\116",o)L:SetScript("\079\110\068\114\097\103\083\116\111\112",a)end L:SetScript("\079\110\077\111\117\115\101\068\111\119\110",t)L:SetScript("\079\110\077\111\117\115\101\085\112",r)L.fadeOut=L:CreateAnimationGroup()local Q=L.fadeOut:CreateAnimation("\065\108\112\104\097")Q:SetOrder(1)Q:SetDuration(.2)Q:SetFromAlpha(1)Q:SetToAlpha(0)Q:SetStartDelay(1)L.fadeOut:SetToFinalAlpha(true)T.objects[c]=L if T.loggedIn then v(L,K and K.minimapPos)if not K or not K.hide then L:Show()else L:Hide()end end if K and K.showInCompartment then T:AddButtonToCompartment(c,X)end T.callbacks:Fire("\076\105\098\068\066\073\099\111\110\095\073\099\111\110\067\114\101\097\116\101\100",L,c)end if not T.loggedIn then local c=B("\070\114\097\109\101")c:SetScript("\079\110\069\118\101\110\116",function(c)for c,y in X,T.objects do v(y,y.db and y.db.minimapPos)if not y.db or not y.db.hide then y:Show()else y:Hide()end end T.loggedIn=true c:SetScript("\079\110\069\118\101\110\116",nil)end)c:RegisterEvent("\080\076\065\089\069\082\095\076\079\071\073\078")end do local function c()if P then return end for c,y in X,T.objects do if y.showOnMouseover then y.fadeOut:Stop()y:SetAlpha(1)end end end local function y()if P then return end for c,y in X,T.objects do if y.showOnMouseover then y.fadeOut:Play()end end end z:HookScript("\079\110\069\110\116\101\114",c)z:HookScript("\079\110\076\101\097\118\101",y)end function T.Register(B,y,K,X,z)if not K.icon then error("\067\097\110\'\116 \114\101\103\105\115\116\101\114 \076\068\066 \111\098\106\101\099\116\115 \119\105\116\104\111\117\116 \105\099\111\110\115 \115\101\116\033")end if T:GetMinimapButton(y)then error(c..("\058 \079\098\106\101\099\116 \'"..(y.."\' \105\115 \097\108\114\101\097\100\121 \114\101\103\105\115\116\101\114\101\100\046")))end k(y,K,X,z)end function T.Lock(y,c)local K=T:GetMinimapButton(c)if K then K:SetScript("\079\110\068\114\097\103\083\116\097\114\116",nil)K:SetScript("\079\110\068\114\097\103\083\116\111\112",nil)if K.db then K.db.lock=true end end end function T.Unlock(y,c)local K=T:GetMinimapButton(c)if K then K:SetScript("\079\110\068\114\097\103\083\116\097\114\116",o)K:SetScript("\079\110\068\114\097\103\083\116\111\112",a)if K.db then K.db.lock=nil end end end function T.Hide(y,c)local K=T:GetMinimapButton(c)if K then K:Hide()end end function T.Show(y,c)local K=T:GetMinimapButton(c)if K then K:Show()v(K,K.db and K.db.minimapPos or K.minimapPos)end end function T.IsRegistered(y,c)return T.objects[c]and true or false end function T.Refresh(K,c,y)local X=T:GetMinimapButton(c)if X then if y then X.db=y end v(X,X.db and X.db.minimapPos or X.minimapPos)if not X.db or not X.db.hide then X:Show()else X:Hide()end if not X.db or not X.db.lock then X:SetScript("\079\110\068\114\097\103\083\116\097\114\116",o)X:SetScript("\079\110\068\114\097\103\083\116\111\112",a)else X:SetScript("\079\110\068\114\097\103\083\116\097\114\116",nil)X:SetScript("\079\110\068\114\097\103\083\116\111\112",nil)end end end function T.ShowOnEnter(K,c,y)local X=T:GetMinimapButton(c)if X then if y then X.showOnMouseover=true X.fadeOut:Stop()X:SetAlpha(0)else X.showOnMouseover=false X.fadeOut:Stop()X:SetAlpha(1)end end end function T.GetMinimapButton(y,c)return T.objects[c]end function T.GetButtonList(c)local y={}for c in X,T.objects do y[#y+1]=c end return y end function T.SetButtonRadius(y,c)if type(c)=="\110\117\109\098\101\114"then T.radius=c for c,y in X,T.objects do v(y,y.db and y.db.minimapPos or y.minimapPos)end end end function T.SetButtonToPosition(K,c,y)v(T.objects[c]or c,y)end function T.IsButtonCompartmentAvailable(c)if L then return true end end function T.IsButtonInCompartment(y,c)local K=T.objects[c]if K and(K.db and K.db.showInCompartment)then return true end return false end function T.AddButtonToCompartment(K,c,y)local X=T.objects[c]if X and(not X.compartmentData and L)then if X.db then X.db.showInCompartment=true end X.compartmentData={text=c,icon=y or X.dataObject.icon;notCheckable=true,registerForAnyClick=true,func=function(c,y,K,T,z)X.dataObject.OnClick(c,z)end;funcOnEnter=p,funcOnLeave=Q}L:RegisterAddon(X.compartmentData)end end function T.RemoveButtonFromCompartment(y,c)local K=T.objects[c]if K and K.compartmentData then for c=1,#L.registeredAddons,1 do local y=L.registeredAddons[c]if y==K.compartmentData then K.compartmentData=nil if K.db then K.db.showInCompartment=nil end table.remove(L.registeredAddons,c)L:UpdateDisplay()return end end end end for c,y in X,T.objects do local K=y.db if not K or not K.lock then y:SetScript("\079\110\068\114\097\103\083\116\097\114\116",o)y:SetScript("\079\110\068\114\097\103\083\116\111\112",a)end y:SetScript("\079\110\069\110\116\101\114",F)y:SetScript("\079\110\076\101\097\118\101",w)y:SetScript("\079\110\067\108\105\099\107",Y)y:SetScript("\079\110\077\111\117\115\101\068\111\119\110",t)y:SetScript("\079\110\077\111\117\115\101\085\112",r)if not y.fadeOut then y.fadeOut=y:CreateAnimationGroup()local c=y.fadeOut:CreateAnimation("\065\108\112\104\097")c:SetOrder(1)c:SetDuration(.2)c:SetFromAlpha(1)c:SetToAlpha(0)c:SetStartDelay(1)y.fadeOut:SetToFinalAlpha(true)end end T:SetButtonRadius(T.radius)if T.notCreated then for c in X,T.notCreated do k(c,T.notCreated[c][1],T.notCreated[c][2])end T.notCreated=nil end
--@curseforge-project-slug: libdbicon-1-0@
-----------------------------------------------------------------------
-- LibDBIcon-1.0
--
-- Allows addons to easily create a lightweight minimap icon as an alternative to heavier LDB displays.
--

local DBICON10 = "LibDBIcon-1.0"
local DBICON10_MINOR = 52 -- Bump on changes
if not LibStub then error(DBICON10 .. " requires LibStub.") end
local ldb = LibStub("LibDataBroker-1.1", true)
if not ldb then error(DBICON10 .. " requires LibDataBroker-1.1.") end
local lib = LibStub:NewLibrary(DBICON10, DBICON10_MINOR)
if not lib then return end

lib.objects = lib.objects or {}
lib.callbackRegistered = lib.callbackRegistered or nil
lib.callbacks = lib.callbacks or LibStub("CallbackHandler-1.0"):New(lib)
lib.radius = lib.radius or 5
local next, Minimap, CreateFrame, AddonCompartmentFrame = next, Minimap, CreateFrame, AddonCompartmentFrame
lib.tooltip = lib.tooltip or CreateFrame("GameTooltip", "LibDBIconTooltip", UIParent, "GameTooltipTemplate")
local isDraggingButton = false

function lib:IconCallback(event, name, key, value)
	if lib.objects[name] then
		if key == "icon" then
			lib.objects[name].icon:SetTexture(value)
		elseif key == "iconCoords" then
			lib.objects[name].icon:UpdateCoord()
		elseif key == "iconR" then
			local _, g, b = lib.objects[name].icon:GetVertexColor()
			lib.objects[name].icon:SetVertexColor(value, g, b)
		elseif key == "iconG" then
			local r, _, b = lib.objects[name].icon:GetVertexColor()
			lib.objects[name].icon:SetVertexColor(r, value, b)
		elseif key == "iconB" then
			local r, g = lib.objects[name].icon:GetVertexColor()
			lib.objects[name].icon:SetVertexColor(r, g, value)
		end
	end
end
if not lib.callbackRegistered then
	ldb.RegisterCallback(lib, "LibDataBroker_AttributeChanged__icon", "IconCallback")
	ldb.RegisterCallback(lib, "LibDataBroker_AttributeChanged__iconCoords", "IconCallback")
	ldb.RegisterCallback(lib, "LibDataBroker_AttributeChanged__iconR", "IconCallback")
	ldb.RegisterCallback(lib, "LibDataBroker_AttributeChanged__iconG", "IconCallback")
	ldb.RegisterCallback(lib, "LibDataBroker_AttributeChanged__iconB", "IconCallback")
	lib.callbackRegistered = true
end

local function getAnchors(frame)
	local x, y = frame:GetCenter()
	if not x or not y then return "CENTER" end
	local hhalf = (x > UIParent:GetWidth()*2/3) and "RIGHT" or (x < UIParent:GetWidth()/3) and "LEFT" or ""
	local vhalf = (y > UIParent:GetHeight()/2) and "TOP" or "BOTTOM"
	return vhalf..hhalf, frame, (vhalf == "TOP" and "BOTTOM" or "TOP")..hhalf
end

local function onEnter(self)
	if isDraggingButton then return end

	for _, button in next, lib.objects do
		if button.showOnMouseover then
			button.fadeOut:Stop()
			button:SetAlpha(1)
		end
	end

	local obj = self.dataObject
	if obj.OnTooltipShow then
		lib.tooltip:SetOwner(self, "ANCHOR_NONE")
		lib.tooltip:SetPoint(getAnchors(self))
		obj.OnTooltipShow(lib.tooltip)
		lib.tooltip:Show()
	elseif obj.OnEnter then
		obj.OnEnter(self)
	end
end

local function onLeave(self)
	lib.tooltip:Hide()

	if not isDraggingButton then
		for _, button in next, lib.objects do
			if button.showOnMouseover then
				button.fadeOut:Play()
			end
		end
	end

	local obj = self.dataObject
	if obj.OnLeave then
		obj.OnLeave(self)
	end
end

local function onEnterCompartment(self)
	local buttonName = self.value
	local object = lib.objects[buttonName]
	if object and object.dataObject then
		if object.dataObject.OnTooltipShow then
			lib.tooltip:SetOwner(self, "ANCHOR_NONE")
			lib.tooltip:SetPoint(getAnchors(self))
			object.dataObject.OnTooltipShow(lib.tooltip)
			lib.tooltip:Show()
		elseif object.dataObject.OnEnter then
			object.dataObject.OnEnter(self)
		end
	end
end

local function onLeaveCompartment(self)
	lib.tooltip:Hide()

	local buttonName = self.value
	local object = lib.objects[buttonName]
	if object and object.dataObject then
		if object.dataObject.OnLeave then
			object.dataObject.OnLeave(self)
		end
	end
end

--------------------------------------------------------------------------------

local onDragStart, updatePosition

do
	local minimapShapes = {
		["ROUND"] = {true, true, true, true},
		["SQUARE"] = {false, false, false, false},
		["CORNER-TOPLEFT"] = {false, false, false, true},
		["CORNER-TOPRIGHT"] = {false, false, true, false},
		["CORNER-BOTTOMLEFT"] = {false, true, false, false},
		["CORNER-BOTTOMRIGHT"] = {true, false, false, false},
		["SIDE-LEFT"] = {false, true, false, true},
		["SIDE-RIGHT"] = {true, false, true, false},
		["SIDE-TOP"] = {false, false, true, true},
		["SIDE-BOTTOM"] = {true, true, false, false},
		["TRICORNER-TOPLEFT"] = {false, true, true, true},
		["TRICORNER-TOPRIGHT"] = {true, false, true, true},
		["TRICORNER-BOTTOMLEFT"] = {true, true, false, true},
		["TRICORNER-BOTTOMRIGHT"] = {true, true, true, false},
	}

	local rad, cos, sin, sqrt, max, min = math.rad, math.cos, math.sin, math.sqrt, math.max, math.min
	function updatePosition(button, position)
		local angle = rad(position or 225)
		local x, y, q = cos(angle), sin(angle), 1
		if x < 0 then q = q + 1 end
		if y > 0 then q = q + 2 end
		local minimapShape = GetMinimapShape and GetMinimapShape() or "ROUND"
		local quadTable = minimapShapes[minimapShape]
		local w = (Minimap:GetWidth() / 2) + lib.radius
		local h = (Minimap:GetHeight() / 2) + lib.radius
		if quadTable[q] then
			x, y = x*w, y*h
		else
			local diagRadiusW = sqrt(2*(w)^2)-10
			local diagRadiusH = sqrt(2*(h)^2)-10
			x = max(-w, min(x*diagRadiusW, w))
			y = max(-h, min(y*diagRadiusH, h))
		end
		button:SetPoint("CENTER", Minimap, "CENTER", x, y)
	end
end

local function onClick(self, b)
	if self.dataObject.OnClick then
		self.dataObject.OnClick(self, b)
	end
end

local function onMouseDown(self)
	self.isMouseDown = true
	self.icon:UpdateCoord()
end

local function onMouseUp(self)
	self.isMouseDown = false
	self.icon:UpdateCoord()
end

do
	local deg, atan2 = math.deg, math.atan2
	local function onUpdate(self)
		local mx, my = Minimap:GetCenter()
		local px, py = GetCursorPosition()
		local scale = Minimap:GetEffectiveScale()
		px, py = px / scale, py / scale
		local pos = 225
		if self.db then
			pos = deg(atan2(py - my, px - mx)) % 360
			self.db.minimapPos = pos
		else
			pos = deg(atan2(py - my, px - mx)) % 360
			self.minimapPos = pos
		end
		updatePosition(self, pos)
	end

	function onDragStart(self)
		self:LockHighlight()
		self.isMouseDown = true
		self.icon:UpdateCoord()
		self:SetScript("OnUpdate", onUpdate)
		isDraggingButton = true
		lib.tooltip:Hide()
		for _, button in next, lib.objects do
			if button.showOnMouseover then
				button.fadeOut:Stop()
				button:SetAlpha(1)
			end
		end
	end
end

local function onDragStop(self)
	self:SetScript("OnUpdate", nil)
	self.isMouseDown = false
	self.icon:UpdateCoord()
	self:UnlockHighlight()
	isDraggingButton = false
	for _, button in next, lib.objects do
		if button.showOnMouseover then
			button.fadeOut:Play()
		end
	end
end

local defaultCoords = {0, 1, 0, 1}
local function updateCoord(self)
	local coords = self:GetParent().dataObject.iconCoords or defaultCoords
	local deltaX, deltaY = 0, 0
	if not self:GetParent().isMouseDown then
		deltaX = (coords[2] - coords[1]) * 0.05
		deltaY = (coords[4] - coords[3]) * 0.05
	end
	self:SetTexCoord(coords[1] + deltaX, coords[2] - deltaX, coords[3] + deltaY, coords[4] - deltaY)
end

local function createButton(name, object, db, customCompartmentIcon)
	local button = CreateFrame("Button", "LibDBIcon10_"..name, Minimap)
	button.dataObject = object
	button.db = db
	button:SetFrameStrata("MEDIUM")
	button:SetFixedFrameStrata(true)
	button:SetFrameLevel(8)
	button:SetFixedFrameLevel(true)
	button:SetSize(31, 31)
	button:RegisterForClicks("anyUp")
	button:RegisterForDrag("LeftButton")
	button:SetHighlightTexture(136477) --"Interface\\Minimap\\UI-Minimap-ZoomButton-Highlight"
	if WOW_PROJECT_ID == WOW_PROJECT_MAINLINE then
		local overlay = button:CreateTexture(nil, "OVERLAY")
		overlay:SetSize(50, 50)
		overlay:SetTexture(136430) --"Interface\\Minimap\\MiniMap-TrackingBorder"
		overlay:SetPoint("TOPLEFT", button, "TOPLEFT")
		local background = button:CreateTexture(nil, "BACKGROUND")
		background:SetSize(24, 24)
		background:SetTexture(136467) --"Interface\\Minimap\\UI-Minimap-Background"
		background:SetPoint("CENTER", button, "CENTER")
		local icon = button:CreateTexture(nil, "ARTWORK")
		icon:SetSize(18, 18)
		icon:SetTexture(object.icon)
		icon:SetPoint("CENTER", button, "CENTER")
		button.icon = icon
	else
		local overlay = button:CreateTexture(nil, "OVERLAY")
		overlay:SetSize(53, 53)
		overlay:SetTexture(136430) --"Interface\\Minimap\\MiniMap-TrackingBorder"
		overlay:SetPoint("TOPLEFT")
		local background = button:CreateTexture(nil, "BACKGROUND")
		background:SetSize(20, 20)
		background:SetTexture(136467) --"Interface\\Minimap\\UI-Minimap-Background"
		background:SetPoint("TOPLEFT", 7, -5)
		local icon = button:CreateTexture(nil, "ARTWORK")
		icon:SetSize(17, 17)
		icon:SetTexture(object.icon)
		icon:SetPoint("TOPLEFT", 7, -6)
		button.icon = icon
	end

	button.isMouseDown = false
	local r, g, b = button.icon:GetVertexColor()
	button.icon:SetVertexColor(object.iconR or r, object.iconG or g, object.iconB or b)

	button.icon.UpdateCoord = updateCoord
	button.icon:UpdateCoord()

	button:SetScript("OnEnter", onEnter)
	button:SetScript("OnLeave", onLeave)
	button:SetScript("OnClick", onClick)
	if not db or not db.lock then
		button:SetScript("OnDragStart", onDragStart)
		button:SetScript("OnDragStop", onDragStop)
	end
	button:SetScript("OnMouseDown", onMouseDown)
	button:SetScript("OnMouseUp", onMouseUp)

	button.fadeOut = button:CreateAnimationGroup()
	local animOut = button.fadeOut:CreateAnimation("Alpha")
	animOut:SetOrder(1)
	animOut:SetDuration(0.2)
	animOut:SetFromAlpha(1)
	animOut:SetToAlpha(0)
	animOut:SetStartDelay(1)
	button.fadeOut:SetToFinalAlpha(true)

	lib.objects[name] = button

	if lib.loggedIn then
		updatePosition(button, db and db.minimapPos)
		if not db or not db.hide then
			button:Show()
		else
			button:Hide()
		end
	end

	if db and db.showInCompartment then
		lib:AddButtonToCompartment(name, customCompartmentIcon)
	end
	lib.callbacks:Fire("LibDBIcon_IconCreated", button, name) -- Fire 'Icon Created' callback
end

-- Wait a bit with the initial positioning to let any GetMinimapShape addons
-- load up.
if not lib.loggedIn then
	local frame = CreateFrame("Frame")
	frame:SetScript("OnEvent", function(self)
		for _, button in next, lib.objects do
			updatePosition(button, button.db and button.db.minimapPos)
			if not button.db or not button.db.hide then
				button:Show()
			else
				button:Hide()
			end
		end
		lib.loggedIn = true
		self:SetScript("OnEvent", nil)
	end)
	frame:RegisterEvent("PLAYER_LOGIN")
end

do
	local function OnMinimapEnter()
		if isDraggingButton then return end
		for _, button in next, lib.objects do
			if button.showOnMouseover then
				button.fadeOut:Stop()
				button:SetAlpha(1)
			end
		end
	end
	local function OnMinimapLeave()
		if isDraggingButton then return end
		for _, button in next, lib.objects do
			if button.showOnMouseover then
				button.fadeOut:Play()
			end
		end
	end
	Minimap:HookScript("OnEnter", OnMinimapEnter)
	Minimap:HookScript("OnLeave", OnMinimapLeave)
end

--------------------------------------------------------------------------------
-- Button API
--

function lib:Register(name, object, db, customCompartmentIcon)
	if not object.icon then error("Can't register LDB objects without icons set!") end
	if lib:GetMinimapButton(name) then error(DBICON10.. ": Object '".. name .."' is already registered.") end
	createButton(name, object, db, customCompartmentIcon)
end

function lib:Lock(name)
	local button = lib:GetMinimapButton(name)
	if button then
		button:SetScript("OnDragStart", nil)
		button:SetScript("OnDragStop", nil)
		if button.db then
			button.db.lock = true
		end
	end
end

function lib:Unlock(name)
	local button = lib:GetMinimapButton(name)
	if button then
		button:SetScript("OnDragStart", onDragStart)
		button:SetScript("OnDragStop", onDragStop)
		if button.db then
			button.db.lock = nil
		end
	end
end

function lib:Hide(name)
	local button = lib:GetMinimapButton(name)
	if button then
		button:Hide()
	end
end

function lib:Show(name)
	local button = lib:GetMinimapButton(name)
	if button then
		button:Show()
		updatePosition(button, button.db and button.db.minimapPos or button.minimapPos)
	end
end

function lib:IsRegistered(name)
	return lib.objects[name] and true or false
end

function lib:Refresh(name, db)
	local button = lib:GetMinimapButton(name)
	if button then
		if db then
			button.db = db
		end
		updatePosition(button, button.db and button.db.minimapPos or button.minimapPos)
		if not button.db or not button.db.hide then
			button:Show()
		else
			button:Hide()
		end
		if not button.db or not button.db.lock then
			button:SetScript("OnDragStart", onDragStart)
			button:SetScript("OnDragStop", onDragStop)
		else
			button:SetScript("OnDragStart", nil)
			button:SetScript("OnDragStop", nil)
		end
	end
end

function lib:ShowOnEnter(name, value)
	local button = lib:GetMinimapButton(name)
	if button then
		if value then
			button.showOnMouseover = true
			button.fadeOut:Stop()
			button:SetAlpha(0)
		else
			button.showOnMouseover = false
			button.fadeOut:Stop()
			button:SetAlpha(1)
		end
	end
end

function lib:GetMinimapButton(name)
	return lib.objects[name]
end

function lib:GetButtonList()
	local t = {}
	for name in next, lib.objects do
		t[#t+1] = name
	end
	return t
end

function lib:SetButtonRadius(radius)
	if type(radius) == "number" then
		lib.radius = radius
		for _, button in next, lib.objects do
			updatePosition(button, button.db and button.db.minimapPos or button.minimapPos)
		end
	end
end

function lib:SetButtonToPosition(button, position)
	updatePosition(lib.objects[button] or button, position)
end

--------------------------------------------------------------------------------
-- Addon Compartment API
--

function lib:IsButtonCompartmentAvailable()
	if AddonCompartmentFrame then
		return true
	end
end

function lib:IsButtonInCompartment(buttonName)
	local object = lib.objects[buttonName]
	if object and object.db and object.db.showInCompartment then
		return true
	end
	return false
end

function lib:AddButtonToCompartment(buttonName, customIcon)
	local object = lib.objects[buttonName]
	if object and not object.compartmentData and AddonCompartmentFrame then
		if object.db then
			object.db.showInCompartment = true
		end
		object.compartmentData = {
			text = buttonName,
			icon = customIcon or object.dataObject.icon,
			notCheckable = true,
			registerForAnyClick = true,
			func = function(frame, _, _, _, clickType)
				object.dataObject.OnClick(frame, clickType)
			end,
			funcOnEnter = onEnterCompartment,
			funcOnLeave = onLeaveCompartment,
		}
		AddonCompartmentFrame:RegisterAddon(object.compartmentData)
	end
end

function lib:RemoveButtonFromCompartment(buttonName)
	local object = lib.objects[buttonName]
	if object and object.compartmentData then
		for i = 1, #AddonCompartmentFrame.registeredAddons do
			local entry = AddonCompartmentFrame.registeredAddons[i]
			if entry == object.compartmentData then
				object.compartmentData = nil
				if object.db then
					object.db.showInCompartment = nil
				end
				table.remove(AddonCompartmentFrame.registeredAddons, i)
				AddonCompartmentFrame:UpdateDisplay()
				return
			end
		end
	end
end

--------------------------------------------------------------------------------
-- Upgrades
--

for name, button in next, lib.objects do
	local db = button.db
	if not db or not db.lock then
		button:SetScript("OnDragStart", onDragStart)
		button:SetScript("OnDragStop", onDragStop)
	end
	button:SetScript("OnEnter", onEnter)
	button:SetScript("OnLeave", onLeave)
	button:SetScript("OnClick", onClick)
	button:SetScript("OnMouseDown", onMouseDown)
	button:SetScript("OnMouseUp", onMouseUp)

	if not button.fadeOut then -- Upgrade to 39
		button.fadeOut = button:CreateAnimationGroup()
		local animOut = button.fadeOut:CreateAnimation("Alpha")
		animOut:SetOrder(1)
		animOut:SetDuration(0.2)
		animOut:SetFromAlpha(1)
		animOut:SetToAlpha(0)
		animOut:SetStartDelay(1)
		button.fadeOut:SetToFinalAlpha(true)
	end
end
lib:SetButtonRadius(lib.radius) -- Upgrade to 40
if lib.notCreated then -- Upgrade to 50
	for name in next, lib.notCreated do
		createButton(name, lib.notCreated[name][1], lib.notCreated[name][2])
	end
	lib.notCreated = nil
end
