Quantcast
--[[
	Copyright (c) 2014 Eyal Shilony <Lynxium>

	Permission is hereby granted, free of charge, to any person obtaining
	a copy of this software and associated documentation files (the
	"Software"), to deal in the Software without restriction, including
	without limitation the rights to use, copy, modify, merge, publish,
	distribute, sublicense, and/or sell copies of the Software, and to
	permit persons to whom the Software is furnished to do so, subject to
	the following conditions:

	The above copyright notice and this permission notice shall be
	included in all copies or substantial portions of the Software.

	THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
	EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
	MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
	NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
	LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
	OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
	WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
]]

local addonName, addon = ...
local castingBar = addon:NewModule("CastingBar")

local CASTINGBAR_WIDTH = 256
local CASTINGBAR_HEIGHT = 64

local CUT_WIDTH = 40
local FRAGMENT_WIDTH = CUT_WIDTH / CASTINGBAR_WIDTH
local RIGHT_EDGE_START = (CASTINGBAR_WIDTH - CUT_WIDTH) / CASTINGBAR_WIDTH

castingBar.frame = CreateFrame("StatusBar", "DefaultCastingBar", UIParent, "CastingBarFrameTemplate")

local function createTexture(fileName, left, right, width)
	local texture = castingBar.frame:CreateTexture()
	texture:SetTexture(fileName);
	texture:SetTexCoord(left, right, 0, 1)
	texture:SetSize(width, CASTINGBAR_HEIGHT)
	return texture
end

local function buildTexture(fileName, width)
	local middle = createTexture(fileName, FRAGMENT_WIDTH, FRAGMENT_WIDTH, width)
	middle:SetPoint("TOP", 0, 28)

	local left = createTexture(fileName, 0, FRAGMENT_WIDTH, CUT_WIDTH)
	left:SetPoint("RIGHT", middle, "LEFT")
	middle.left = left

	local right = createTexture(fileName, RIGHT_EDGE_START, 1, CUT_WIDTH)
	right:SetPoint("LEFT", middle, "RIGHT")
	middle.right = right

	return middle
end

local function updateVisibility(source, ...)
	for i = 1, select('#', ...) do
		local dest = select(i, ...)
		if source:IsVisible() then
			dest:Show()
		else
			dest:Hide()
		end
		dest:SetAlpha(source:GetAlpha())
	end
end

local function setStatusBarColor()
	local useClassColors = addon.db.profile.useClassColors
	if not useClassColors:find("None") then
		local _, class
		if useClassColors:find("or") then
			_, class = UnitClass("target")
			if not class then
				_, class = UnitClass("player")
			end
		elseif useClassColors:find("Target") then
			_, class = UnitClass("target")
		elseif useClassColors:find("Player") then
			_, class = UnitClass("player")
		end
		if class then
			local color = RAID_CLASS_COLORS[class]
			castingBar.frame:SetStatusBarColor(color.r, color.g, color.b)
		end
	end
end

local function setIconAndTimePositions(frame, position)
	frame:ClearAllPoints()

	local parent, x, y = castingBar.frame, -10, 3
	local isTime = frame == parent.time

	if isTime then
		if not position:find("Outer") then
			x = addon.db.profile.hideTotalTime and 30 or 60
			y = position:find("Bottom") and -18 or position:find("Top") and 20 or y
		elseif not addon.db.profile.hideIcon and not addon.db.profile.hideTime then
			local isPosEqual = addon.db.profile.timePosition:find(addon.db.profile.iconPosition)
			if isPosEqual then
				parent, x, y = parent.icon, -5, 0
			end
		end
	end

	if position:find("Left") then
		frame:SetPoint("RIGHT", parent, "LEFT", x, y)
	elseif position:find("Right") then
		frame:SetPoint("LEFT", parent, "RIGHT", x * -1, y)
	end
end

local function setIcon(castingBar, texture)
	if texture and not addon.db.profile.hideIcon then
		castingBar.icon:SetTexture(texture)
		setIconAndTimePositions(castingBar.icon, addon.db.profile.iconPosition)
		castingBar.icon:Show()
	else
		castingBar.icon:Hide()
	end
end

local function setTime(castingBar)
	if not addon.db.profile.hideTime then
		setIconAndTimePositions(castingBar.time, addon.db.profile.timePosition)
		castingBar.time:Show()
	else
		castingBar.time:Hide()
	end
end

local function getWidth()
	return addon.db.profile.width, addon.db.profile.width - CUT_WIDTH/2
end

local function setWidth(castingBar)
	local width, textWidth = getWidth()

	castingBar.border:SetWidth(textWidth)
	castingBar.barFlash:SetWidth(textWidth)
	castingBar:SetWidth(width)
end

local function onEvent(self, event, ...)
	setWidth(self)

	self.borderShield:Hide()

	CastingBarFrame_OnEvent(self, event, ...)

	updateVisibility(self.border, self.border.left, self.border.right)
	updateVisibility(self.barFlash, self.barFlash.left, self.barFlash.right)

	if self.casting or self.channeling then
		setStatusBarColor()
	end

	if self.unit then
		local texture
		if self.casting then
			texture = select(4, UnitCastingInfo(self.unit))
		elseif self.channeling then
			texture = select(4, UnitChannelInfo(self.unit))
		end
		setIcon(self, texture)
		setTime(self)
	end

	DefaultCastingBar_DragFrame:Hide()
end

local function onUpdate(self, elapsed)
	CastingBarFrame_OnUpdate(self, elapsed)

	updateVisibility(self.border, self.border.left, self.border.right)
	updateVisibility(self.barFlash, self.barFlash.left, self.barFlash.right)

	if self.casting then
		local value = self.maxValue - self.value
		if not addon.db.profile.hideTotalTime then
			self.time:SetFormattedText("%.1f/%.1f", value, self.maxValue)
		else
			self.time:SetFormattedText("%.1f", value)
		end
	elseif self.channeling then
		self.time:SetFormattedText("%.1f", self.value)
	end
end

function castingBar:OnInitialize()
	local name = self.frame:GetName()

	local width, textWidth = getWidth()

	self.frame.border:Hide()
	self.frame.border:ClearAllPoints()

	self.frame.borderShield:Hide()
	self.frame.borderShield:ClearAllPoints()

	self.frame.barFlash:Hide()
	self.frame.barFlash:ClearAllPoints()

	local border = buildTexture([[Interface\CastingBar\UI-CastingBar-Border]], textWidth)
	self.frame.border = border
	_G[name.."Border"] = border

	local barFlash = buildTexture([[Interface\CastingBar\UI-CastingBar-Flash]], textWidth)
	barFlash:SetBlendMode("ADD")
	self.frame.barFlash = barFlash
	_G[name.."Flash"] = barFlash

	self.frame.time = self.frame:CreateFontString(nil, "ARTWORK", "GameFontHighlight")

	self.frame.icon = _G[name .. "Icon"]
	self.frame.icon:SetSize(22, 22)
	self.frame.icon:SetTexCoord(0.07, 0.93, 0.07, 0.93)

	self.frame:SetPoint("CENTER", DefaultCastingBar_DragFrame)
	self.frame:SetSize(width, addon.barMaxHeight)
	self.frame:Hide()

	self.frame:SetScript('OnUpdate', onUpdate)
	self.frame:SetScript('OnEvent', onEvent)
end

CastingBarFrame:UnregisterAllEvents()
CastingBarFrame:Hide()