local Range = {}
LunaUF:RegisterModule(Range, "range", LunaUF.L["Range"])
local proximity = ProximityLib:GetInstance("1")
local within40yd = {}
local lastUpdate = 0
local REFRESH_INTERVAL = 1
do
local f = CreateFrame'Frame'
f:SetScript('OnEvent', function()this[event]() end)
f:RegisterEvent'PLAYER_LOGIN'
CreateFrame('GameTooltip', 'RangeCheckTooltip', nil, 'GameTooltipTemplate')
local spell, slot
do
function f.UPDATE()
local Time = GetTime()
if this.lastUpdate==nil then this.lastUpdate=0 end
if (this.lastUpdate < Time) then
local openFrame = (InspectFrame and InspectFrame:IsVisible()) or (LootFrame and LootFrame:IsVisible()) or (XLootFrame and XLootFrame:IsVisible()) or (TradeFrame and TradeFrame:IsVisible())
local enemyTargeted = UnitExists('target') and not UnitIsFriend('target','player')
local str, unitID
if GetNumRaidMembers() > 0 then
str = "raid"
elseif GetNumPartyMembers() > 0 and not UnitInRaid('player') then
str = "party"
else
this.lastUpdate = Time + 5;
return
end
if not (openFrame or enemyTargeted) then
local oldTargetName,_ = UnitName('target')
for i = 1, (GetNumRaidMembers() or GetNumPartyMembers()) do
unitID = str..i
if UnitIsConnected(unitID) and (not UnitIsDeadOrGhost(unitID)) and UnitIsVisible(unitID) and UnitIsFriend('player',unitID) then --because Mind Control?
if CheckInteractDistance(unitID, 4) then
within40yd[unitID] = GetTime()
else
TargetUnit(unitID)
within40yd[unitID] = slot and IsActionInRange(slot) and GetTime()
end
end
end
if oldTargetName then TargetByName(oldTargetName,true) else ClearTarget() end
end
this.lastUpdate = Time + REFRESH_INTERVAL;
end
end
end
function f.ACTIONBAR_SLOT_CHANGED()
local newSlot
for i = 1, 120 do
RangeCheckTooltip:SetOwner(UIParent, 'ANCHOR_NONE')
RangeCheckTooltip:SetAction(i)
if RangeCheckTooltipTextLeft1:GetText() == spell then
newSlot = i
end
end
slot = newSlot
if not slot then f:SetScript('OnUpdate', nil) else f:SetScript('OnUpdate', f.UPDATE) end
end
function f.PLAYER_LOGIN()
spell = ({ Paladin='Holy Light', Priest='Flash Heal', Druid='Healing Touch', Shaman='Healing Wave' })[UnitClass'player']
if spell then
f:RegisterEvent'ACTIONBAR_SLOT_CHANGED'
f.ACTIONBAR_SLOT_CHANGED()
f:SetScript('OnUpdate', f.UPDATE)
end
end
end
local function OnUpdate()
Range:FullUpdate(this:GetParent())
end
function Range:OnEnable(frame)
if not frame.range then
frame.range = CreateFrame("Frame", nil, frame)
end
frame.range:SetScript("OnUpdate", OnUpdate)
end
function Range:OnDisable(frame)
if frame.range then
frame.range:SetScript("OnUpdate", nil)
end
end
function Range:FullUpdate(frame)
if frame.DisableRangeAlpha then return end
local range, lastseen = proximity:GetUnitRange(frame.unit)
if (not range or range > 30) and within40yd[frame.unit] then
range, lastseen = 40, within40yd[frame.unit]
end
if range and ((GetTime()-lastseen) < REFRESH_INTERVAL + 1 ) then
if range <= 30 then
frame:SetAlpha(LunaUF.db.profile.units[frame.unitGroup].fader.enabled and LunaUF.db.profile.units[frame.unitGroup].fader.combatAlpha or 1)
elseif range <= 40 and slot then
frame:SetAlpha(LunaUF.db.profile.units[frame.unitGroup].fader.enabled and LunaUF.db.profile.units[frame.unitGroup].fader.combatAlpha or .8)
end
else
frame:SetAlpha(LunaUF.db.profile.units[frame.unitGroup].range.alpha)
end
end