Honor.lua 5.01 KB
Newer Older
Azilroka's avatar
Azilroka committed
1
local E, L, V, P, G = unpack(select(2, ...)); --Import: Engine, Locales, PrivateDB, ProfileDB, GlobalDB
2
local mod = E:GetModule('DataBars');
3
local LSM = E.Libs.LSM
4 5 6 7

--Lua functions
local format = format
--WoW API / Variables
8 9 10 11
local UnitHonor = UnitHonor
local UnitHonorLevel = UnitHonorLevel
local UnitHonorMax = UnitHonorMax
local UnitIsPVP = UnitIsPVP
12
local UnitLevel = UnitLevel
13 14
local InCombatLockdown = InCombatLockdown
local TogglePVPUI = TogglePVPUI
15
local MAX_PLAYER_LEVEL = MAX_PLAYER_LEVEL
16
local HONOR = HONOR
17

18
--Global variables that we don't cache, list them here for mikk's FindGlobals script
19
-- GLOBALS: GameTooltip, RightChatPanel, CreateFrame
20 21 22

function mod:UpdateHonor(event, unit)
	if not mod.db.honor.enable then return end
23 24
	if event == "PLAYER_FLAGS_CHANGED" and unit ~= "player" then return end

25
	local bar = self.honorBar
Darth Predator's avatar
Darth Predator committed
26
	local showHonor = true
27 28 29 30 31

	if (self.db.honor.hideInCombat and (event == "PLAYER_REGEN_DISABLED" or InCombatLockdown())) then
		showHonor = false
	elseif (self.db.honor.hideOutsidePvP and not UnitIsPVP("player")) then
		showHonor = false
32 33
	elseif (self.db.honor.hideBelowMaxLevel and UnitLevel("player") < MAX_PLAYER_LEVEL) then
		showHonor = false
34 35
	end

Darth Predator's avatar
Darth Predator committed
36 37
	if not showHonor then
		bar:Hide()
38
	else
Darth Predator's avatar
Darth Predator committed
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
		bar:Show()

		local current = UnitHonor("player");
		local max = UnitHonorMax("player");

		--Guard against division by zero, which appears to be an issue when zoning in/out of dungeons
		if max == 0 then max = 1 end

		bar.statusBar:SetMinMaxValues(0, max)
		bar.statusBar:SetValue(current)

		if self.db.honor.hideInVehicle then
			E:RegisterObjectForVehicleLock(bar, E.UIParent)
		else
			E:UnregisterObjectForVehicleLock(bar)
		end

		local text = ''
		local textFormat = self.db.honor.textFormat

		if textFormat == 'PERCENT' then
			text = format('%d%%', current / max * 100)
		elseif textFormat == 'CURMAX' then
			text = format('%s - %s', E:ShortValue(current), E:ShortValue(max))
		elseif textFormat == 'CURPERC' then
			text = format('%s - %d%%', E:ShortValue(current), current / max * 100)
		elseif textFormat == 'CUR' then
			text = format('%s', E:ShortValue(current))
		elseif textFormat == 'REM' then
			text = format('%s', E:ShortValue(max-current))
		elseif textFormat == 'CURREM' then
			text = format('%s - %s', E:ShortValue(current), E:ShortValue(max-current))
		elseif textFormat == 'CURPERCREM' then
			text = format('%s - %d%% (%s)', E:ShortValue(current), current / max * 100, E:ShortValue(max - current))
		end

		bar.text:SetText(text)
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
	end
end

function mod:HonorBar_OnEnter()
	if mod.db.honor.mouseover then
		E:UIFrameFadeIn(self, 0.4, self:GetAlpha(), 1)
	end
	GameTooltip:ClearLines()
	GameTooltip:SetOwner(self, 'ANCHOR_CURSOR', 0, -4)

	local current = UnitHonor("player");
	local max = UnitHonorMax("player");
	local level = UnitHonorLevel("player");

	GameTooltip:AddLine(HONOR)

	GameTooltip:AddDoubleLine(L["Current Level:"], level, 1, 1, 1)
	GameTooltip:AddLine(' ')

Merathilis's avatar
Merathilis committed
95 96 97
	GameTooltip:AddDoubleLine(L["Honor XP:"], format(' %d / %d (%d%%)', current, max, current/max * 100), 1, 1, 1)
	GameTooltip:AddDoubleLine(L["Honor Remaining:"], format(' %d (%d%% - %d '..L["Bars"]..')', max - current, (max - current) / max * 100, 20 * (max - current) / max), 1, 1, 1)

98 99 100
	GameTooltip:Show()
end

101 102
function mod:HonorBar_OnClick()
	TogglePVPUI()
103 104
end

105 106 107 108 109
function mod:UpdateHonorDimensions()
	self.honorBar:Width(self.db.honor.width)
	self.honorBar:Height(self.db.honor.height)
	self.honorBar.statusBar:SetOrientation(self.db.honor.orientation)
	self.honorBar.statusBar:SetReverseFill(self.db.honor.reverseFill)
110
	self.honorBar.text:FontTemplate(LSM:Fetch("font", self.db.honor.font), self.db.honor.textSize, self.db.honor.fontOutline)
111 112 113 114 115 116 117

	if self.db.honor.orientation == "HORIZONTAL" then
		self.honorBar.statusBar:SetRotatesTexture(false)
	else
		self.honorBar.statusBar:SetRotatesTexture(true)
	end

118 119 120 121
	if self.db.honor.mouseover then
		self.honorBar:SetAlpha(0)
	else
		self.honorBar:SetAlpha(1)
122
	end
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
end

function mod:EnableDisable_HonorBar()
	if self.db.honor.enable then
		self:RegisterEvent("HONOR_XP_UPDATE", "UpdateHonor")
		self:UpdateHonor()
		E:EnableMover(self.honorBar.mover:GetName())
	else
		self:UnregisterEvent("HONOR_XP_UPDATE")
		self.honorBar:Hide()
		E:DisableMover(self.honorBar.mover:GetName())
	end
end

function mod:LoadHonorBar()
138
	self.honorBar = self:CreateBar('ElvUI_HonorBar', self.HonorBar_OnEnter, self.HonorBar_OnClick, 'RIGHT', RightChatPanel, 'LEFT', E.Border - E.Spacing*3, 0)
139 140 141
	self.honorBar.statusBar:SetStatusBarColor(240/255, 114/255, 65/255)
	self.honorBar.statusBar:SetMinMaxValues(0, 325)

142 143 144 145
	self.honorBar.eventFrame = CreateFrame("Frame")
	self.honorBar.eventFrame:Hide()
	self.honorBar.eventFrame:RegisterEvent("PLAYER_REGEN_DISABLED")
	self.honorBar.eventFrame:RegisterEvent("PLAYER_REGEN_ENABLED")
146 147
	self.honorBar.eventFrame:RegisterEvent("PLAYER_FLAGS_CHANGED")
	self.honorBar.eventFrame:SetScript("OnEvent", function(self, event, unit) mod:UpdateHonor(event, unit) end)
148

149
	self:UpdateHonorDimensions()
150
	E:CreateMover(self.honorBar, "HonorBarMover", L["Honor Bar"], nil, nil, nil, nil, nil, 'databars,honor')
151 152

	self:EnableDisable_HonorBar()
Simpy's avatar
Simpy committed
153
end