phaseindicator.lua 2.26 KB
Newer Older
1 2
--[[
# Element: Phasing Indicator
3

4
Toggles the visibility of an indicator based on the unit's phasing relative to the player.
5

6
## Widget
7

8
PhaseIndicator - Any UI widget.
9

10
## Notes
11

Adrian L Lange's avatar
Adrian L Lange committed
12
A default texture will be applied if the widget is a Texture and doesn't have a texture or a color set.
13

14
## Examples
15

16 17 18 19
    -- Position and size
    local PhaseIndicator = self:CreateTexture(nil, 'OVERLAY')
    PhaseIndicator:SetSize(16, 16)
    PhaseIndicator:SetPoint('TOPLEFT', self)
Adrian L Lange's avatar
Linting  
Adrian L Lange committed
20

21 22 23
    -- Register it with oUF
    self.PhaseIndicator = PhaseIndicator
--]]
24

Rainrider's avatar
Rainrider committed
25
local _, ns = ...
Evilpaul's avatar
Evilpaul committed
26 27
local oUF = ns.oUF

Adrian L Lange's avatar
Linting  
Adrian L Lange committed
28
local function Update(self, event)
29
	local element = self.PhaseIndicator
30 31 32 33 34 35

	--[[ Callback: PhaseIndicator:PreUpdate()
	Called before the element has been updated.

	* self - the PhaseIndicator element
	--]]
Adrian L Lange's avatar
Linting  
Adrian L Lange committed
36 37
	if(element.PreUpdate) then
		element:PreUpdate()
Trond A Ekseth's avatar
Trond A Ekseth committed
38 39
	end

Val Voronov's avatar
Val Voronov committed
40
	local isInSamePhase = UnitInPhase(self.unit) and not UnitIsWarModePhased(self.unit)
41
	if(isInSamePhase) then
Adrian L Lange's avatar
Linting  
Adrian L Lange committed
42
		element:Hide()
Evilpaul's avatar
Evilpaul committed
43
	else
Adrian L Lange's avatar
Linting  
Adrian L Lange committed
44
		element:Show()
Evilpaul's avatar
Evilpaul committed
45
	end
Trond A Ekseth's avatar
Trond A Ekseth committed
46

47
	--[[ Callback: PhaseIndicator:PostUpdate(isInSamePhase)
48 49
	Called after the element has been updated.

50 51
	* self          - the PhaseIndicator element
	* isInSamePhase - indicates whether the element is hidden (boolean)
52
	--]]
Adrian L Lange's avatar
Linting  
Adrian L Lange committed
53
	if(element.PostUpdate) then
54
		return element:PostUpdate(isInSamePhase)
Trond A Ekseth's avatar
Trond A Ekseth committed
55
	end
Evilpaul's avatar
Evilpaul committed
56 57
end

Adrian L Lange's avatar
Linting  
Adrian L Lange committed
58
local function Path(self, ...)
59
	--[[ Override: PhaseIndicator.Override(self, event, ...)
60 61
	Used to completely override the internal update function.

62
	* self  - the parent object
63
	* event - the event triggering the update (string)
64
	* ...   - the arguments accompanying the event
65
	--]]
66
	return (self.PhaseIndicator.Override or Update) (self, ...)
Evilpaul's avatar
Evilpaul committed
67 68
end

Adrian L Lange's avatar
Linting  
Adrian L Lange committed
69
local function ForceUpdate(element)
Evilpaul's avatar
Evilpaul committed
70 71 72
	return Path(element.__owner, 'ForceUpdate')
end

Adrian L Lange's avatar
Linting  
Adrian L Lange committed
73
local function Enable(self)
74
	local element = self.PhaseIndicator
Adrian L Lange's avatar
Linting  
Adrian L Lange committed
75 76 77
	if(element) then
		element.__owner = self
		element.ForceUpdate = ForceUpdate
Evilpaul's avatar
Evilpaul committed
78

79
		self:RegisterEvent('UNIT_PHASE', Path, true)
Evilpaul's avatar
Evilpaul committed
80

Adrian L Lange's avatar
Linting  
Adrian L Lange committed
81 82
		if(element:IsObjectType('Texture') and not element:GetTexture()) then
			element:SetTexture([[Interface\TargetingFrame\UI-PhasingIcon]])
Evilpaul's avatar
Evilpaul committed
83 84 85 86 87 88
		end

		return true
	end
end

Adrian L Lange's avatar
Linting  
Adrian L Lange committed
89
local function Disable(self)
90
	local element = self.PhaseIndicator
Adrian L Lange's avatar
Linting  
Adrian L Lange committed
91 92 93
	if(element) then
		element:Hide()

Evilpaul's avatar
Evilpaul committed
94 95 96 97
		self:UnregisterEvent('UNIT_PHASE', Path)
	end
end

98
oUF:AddElement('PhaseIndicator', Path, Enable, Disable)