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 setFeaturesPosition(frame, position)
	frame:ClearAllPoints()
	if position:find("Left") then
		frame:SetPoint("RIGHT", castingBar.frame, "LEFT", position:find("Inner") and 25 or -10, 3)
	elseif position:find("Right") then
		frame:SetPoint("LEFT", castingBar.frame, "RIGHT", position:find("Inner") and -25 or 10, 3)
	end
end

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

local function onEvent(self, event, ...)
	local width, textWidth = getWidth()

	self.border:SetWidth(textWidth)
	self.barFlash:SetWidth(textWidth)
	self:SetWidth(width)

	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.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
		if texture and not addon.db.profile.hideIcon then
			self.icon:SetTexture(texture)
			setFeaturesPosition(self.icon, addon.db.profile.iconPosition)
			self.icon:Show()
		else
			self.icon:Hide()
		end
		if not addon.db.profile.hideTime then
			setFeaturesPosition(self.time, addon.db.profile.timePosition)
			self.time:Show()
		else
			self.time:Hide()
		end
	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/%.2f", 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()