Quantcast
--[[-----------------------------------------------------------------------
	oUF: Stardust - a layout for the oUF framework
	Copyright (c) 2016 Andrew Mordecai <armordecai@protonmail.ch>
	This code is released under the zlib license; see LICENSE for details.
-------------------------------------------------------------------------]]

local _, Stardust = ...

---------------------------
-- Health
---------------------------

function Stardust.PostUpdateHealth(self, unit, cur, max)
	self:SetValue(max - cur)
	self.text:SetText(cur < max and -max + cur or "")
	self.bg:SetVertexColor(0.15, 0.15, 0.15)
end

---------------------------
-- Power
-- DruidMana
-- DemonicFury
---------------------------

function Stardust.PostUpdatePower(self, unit, cur, max)
	self:SetShown(max > 0)
	self.text:SetText(cur)
end

---------------------------
-- ClassIcons
---------------------------

function Stardust.ResizeClassIcons(element)
	local COUNT = element.__max
	local SPACING = 3
	local widthTotal = ((Stardust.config.width - SPACING - SPACING) * 0.6) - (COUNT * SPACING)
	local widthEach = floor(widthTotal / COUNT + 0.5)
	for i = 1, COUNT do
		element[i]:SetWidth(widthEach)
	end
end

function Stardust.PostUpdateClassIcons(element, cur, max, maxChanged, event)
	if maxChanged then
		Stardust.ResizeClassIcons(element)
	end
end

---------------------------
-- Combo points
---------------------------

function Stardust.PostUpdateCPoints(element, cur)
	-- The number of combo points isn't dynamic, so they only need to be resized once per frame.
	element.PreUpdate = nil
	Stardust.ResizeClassIcons(element)
end

---------------------------
-- Runes
---------------------------

function Stardust.PostUpdateRuneType(element, rune, index, alt)
	local r, g, b = rune:GetStatusBarColor()
	rune.bg:SetVertexColor(r * 0.35, g * 0.35, b * 0.35)
end

function Stardust.PostUpdateRune(element, rune, index, start, duration, ready)
	-- The number of runes isn't dynamic, so they only need to be resized once per frame.
	element.PostUpdateRune = nil
	Stardust.ResizeClassIcons(element)
end

---------------------------
-- Totems
-- Partly adapted from oUF_Phanx
---------------------------

local function OnUpdateTotem(bar, elapsed)
	local t = bar.duration - elapsed
	if t > 0 then
		bar.duration = t
		bar:SetValue(t)
	else
		bar:SetValue(0)
	end
end

function Stardust.PostUpdateTotem(element, index, _, name, start, duration)
	local bar = element[index]
	bar.duration, bar.max = duration, duration
	if duration > 0 then
		bar:SetMinMaxValues(0, duration)
		bar:SetScript("OnUpdate", OnUpdateTotem)
	else
		bar:SetScript("OnUpdate", nil)
		bar:SetValue(0)
	end
end

function Stardust.PreUpdateTotems(element, index)
	-- The number of totems isn't dynamic, so they only need to be resized once per frame.
	element.PreUpdate = nil
	Stardust.ResizeClassIcons(element)
end

---------------------------
-- Status icons
---------------------------

function Stardust.PostUpdateStatusIcon(self)
	if not self:IsShown() then return end
	local icons, last = self.__owner.StatusIcons
	for i = 1, #icons do
		local icon = icons[i]
		if icon == self then
			break
		elseif icon:IsShown() then
			last = icon
		end
	end
	if last then
		self:SetPoint("LEFT", last, "RIGHT", 0, 0)
	else
		self:SetPoint("LEFT", self.__owner.Health, "TOPLEFT", 3, 0)
	end
end

function Stardust.PostUpdateCombat(self)
	self.__owner.Resting:ForceUpdate()
end

function Stardust.PostUpdateResting(self)
	self.__owner.Combat:ForceUpdate()
end

---------------------------
-- Glow
---------------------------

local GLOW_SEGMENTS = {
	{ "TOPLEFT", 0, 1/3, 0, 1/3, -1, 1 },
	{ "TOPRIGHT", 2/3, 1, 0, 1/3, 1, 1 },
	{ "BOTTOMRIGHT", 2/3, 1, 2/3, 1, 1, -1 },
	{ "BOTTOMLEFT", 0, 1/3, 2/3, 1, -1, -1 },
	{ "TOP", 1/3, 2/3, 0, 1/3, 0, 1 },
	{ "RIGHT", 2/3, 1, 1/3, 2/3, 1, 0 },
	{ "BOTTOM", 1/3, 2/3, 2/3, 1, 0, -1 },
	{ "LEFT", 0, 1/3, 1/3, 2/3, -1, 0 },
}

function Stardust.SetGlowColor(self, r, g, b, a)
	for i = 1, #self.Glow do
		self.Glow[i]:SetVertexColor(r, g, b, a)
	end
end

function Stardust.SetGlowSize(self, size, offset)
	local Glow = self.Glow
	for i = 1, #Glow do
		Glow[i]:SetSize(size, size)
	end

	local d = d or floor(size * 2 / 3 + 0.5)
	Glow[1]:SetPoint("TOPLEFT", -d, d)
	Glow[2]:SetPoint("TOPRIGHT", d, d)
	Glow[3]:SetPoint("BOTTOMRIGHT", d, -d)
	Glow[4]:SetPoint("BOTTOMLEFT", -d, -d)

	Glow[5]:SetShown(Glow[2]:GetLeft() > Glow[1]:GetRight())
	Glow[6]:SetShown(Glow[2]:GetBottom() > Glow[3]:GetTop())
	Glow[7]:SetShown(Glow[3]:GetLeft() > Glow[4]:GetLeft())
	Glow[8]:SetShown(Glow[1]:GetBottom() > Glow[4]:GetTop())
end

function Stardust.CreateGlow(self)
	local Glow = {}
	for i = 1, #GLOW_SEGMENTS do
		local seg = GLOW_SEGMENTS[i]
		local tex = self:CreateTexture(nil, "BACKGROUND")
		tex:SetTexture("Interface\\AddOns\\oUF_Stardust\\textures\\glow")
		tex:SetTexCoord(seg[2], seg[3], seg[4], seg[5])
		tex:SetVertexColor(0, 0, 0)
		Glow[i] = tex
	end

	-- TOP
	Glow[5]:SetPoint("LEFT", Glow[1], "RIGHT")
	Glow[5]:SetPoint("RIGHT", Glow[2], "LEFT")

	-- RIGHT
	Glow[6]:SetPoint("TOP", Glow[2], "BOTTOM")
	Glow[6]:SetPoint("BOTTOM", Glow[3], "TOP")

	-- BOTTOM
	Glow[7]:SetPoint("LEFT", Glow[4], "RIGHT")
	Glow[7]:SetPoint("RIGHT", Glow[3], "LEFT")

	-- LEFT
	Glow[8]:SetPoint("TOP", Glow[1], "BOTTOM")
	Glow[8]:SetPoint("BOTTOM", Glow[4], "TOP")

	SetGlowSize(self, 12)

	self.Glow = Glow
	self.SetBackdropBorderColor = SetGlowColor
	self.SetGlowColor = SetGlowColor
	self.SetGlowSize = SetGlowSize
	return Glow
end