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")

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

local function handlePosition(frame, position)
	frame:ClearAllPoints()
	if position:find("Left") then
		frame:SetPoint("RIGHT", castingBar.frame, "LEFT", position:find("Inside") and 25 or -10, 3)
	elseif position:find("Right") then
		frame:SetPoint("LEFT", castingBar.frame, "RIGHT", position:find("Inside") and -25 or 10, 3)
	end
end

local function onEvent(self, event, ...)
	CastingBarFrame_OnEvent(self, event, ...)
	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)
			handlePosition(self.icon, addon.db.profile.iconPosition)
			self.icon:Show()
		else
			self.icon:Hide()
		end
		if not addon.db.profile.hideTime then
			handlePosition(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)
	if self.casting then
		self.time:SetFormattedText('%.1f', self.maxValue - self.value)
	elseif self.channeling then
		self.time:SetFormattedText('%.1f', self.value)
	end
end

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

	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(CastingBarFrame:GetWidth(), CastingBarFrame:GetHeight())
	self.frame:Hide()

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

CastingBarFrame:UnregisterAllEvents()
CastingBarFrame:Hide()