Skip to content

Commit

Permalink
ThreatBar: Switch back to the threat API
Browse files Browse the repository at this point in the history
  • Loading branch information
nebularg committed Jul 7, 2020
1 parent 1e4d9a8 commit 0f15bf7
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 82 deletions.
1 change: 1 addition & 0 deletions .luacheckrc
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ globals = {
"UnitClassification",
"UnitCreatureFamily",
"UnitCreatureType",
"UnitDetailedThreatSituation",
"UnitExists",
"UnitFactionGroup",
"UnitGUID",
Expand Down
57 changes: 22 additions & 35 deletions Modules/LuaTexts/LuaTexts.lua
Original file line number Diff line number Diff line change
Expand Up @@ -348,35 +348,35 @@ end]],
},
[L["Threat"]] = {
[L["Percent"]] = {
events = {['ThreatLib_ThreatUpdated']=true},
events = {['UNIT_THREAT_LIST_UPDATE']=true,['UNIT_THREAT_SITUATION_UPDATE']=true},
code = [[
local unit_a,unit_b = ThreatPair(unit)
if unit_a and unit_b then
local _,_,percent = ThreatSituation(unit_a, unit_b)
local _,_,percent = UnitDetailedThreatSituation(unit_a, unit_b)
if percent and percent ~= 0 then
return "%s%%",Round(percent,1)
end
end
return ConfigMode()]],
},
[L["Raw percent"]] = {
events = {['ThreatLib_ThreatUpdated']=true},
events = {['UNIT_THREAT_LIST_UPDATE']=true,['UNIT_THREAT_SITUATION_UPDATE']=true},
code = [[
local unit_a,unit_b = ThreatPair(unit)
if unit_a and unit_b then
local _,_,_,raw_percent = ThreatSituation(unit_a, unit_b)
local _,_,_,raw_percent = UnitDetailedThreatSituation(unit_a, unit_b)
if raw_percent and raw_percent ~= 0 then
return "%s%%",Round(raw_percent,1)
end
end
return ConfigMode()]],
},
[L["Colored percent"]] = {
events = {['ThreatLib_ThreatUpdated']=true},
events = {['UNIT_THREAT_LIST_UPDATE']=true,['UNIT_THREAT_SITUATION_UPDATE']=true},
code = [[
local unit_a,unit_b = ThreatPair(unit)
if unit_a and unit_b then
local _,status,percent = ThreatSituation(unit_a, unit_b)
local _,status,percent = UnitDetailedThreatSituation(unit_a, unit_b)
if percent and percent ~= 0 then
local r,g,b = ThreatStatusColor(status)
return "|cff%02x%02x%02x%s%%|r",r,g,b,Round(percent,1)
Expand All @@ -385,11 +385,11 @@ end
return ConfigMode()]],
},
[L["Colored raw percent"]] = {
events = {['ThreatLib_ThreatUpdated']=true},
events = {['UNIT_THREAT_LIST_UPDATE']=true,['UNIT_THREAT_SITUATION_UPDATE']=true},
code = [[
local unit_a,unit_b = ThreatPair(unit)
if unit_a and unit_b then
local _,status,_,raw_percent = ThreatSituation(unit_a, unit_b)
local _,status,_,raw_percent = UnitDetailedThreatSituation(unit_a, unit_b)
if raw_percent and raw_percent ~= 0 then
local r,g,b = ThreatStatusColor(status)
return "|cff%02x%02x%02x%s%%|r",r,g,b,Round(raw_percent,1)
Expand Down Expand Up @@ -522,7 +522,6 @@ do
['PLAYER_XP_UPDATE'] = {player=true},
['UPDATE_FACTION'] = {all=true},
['UNIT_LEVEL'] = {all=true},
['ThreatLib_ThreatUpdated'] = {all=true},
}

-- Iterate the provided codes to fill in all the rest
Expand Down Expand Up @@ -566,10 +565,6 @@ local protected_events = {
['GROUP_ROSTER_UPDATE'] = true,
}

local custom_events = {
['ThreatLib_ThreatUpdated'] = true,
}

-- Provide a way to update changed events so existing LuaTexts configs
-- continue to work transparently to end users.
local compat_event_map = {}
Expand Down Expand Up @@ -601,10 +596,8 @@ local function update_events(events)
end
-- prune "false" and old invalid entries
for event, value in next, events do
if not custom_events[event] then
if not value or not pcall(test_frame.RegisterEvent, test_frame, event) then
events[event] = nil
end
if not value or not pcall(test_frame.RegisterEvent, test_frame, event) then
events[event] = nil
end
end
end
Expand Down Expand Up @@ -638,10 +631,17 @@ local function fix_texts()
text.events["UNIT_POWER_FREQUENT"] = true
end
end
elseif text.code:find("UnitDetailedThreatSituation", nil, true) then
-- switch threat API to ThreatLib
text.code = text.code:gsub("UnitDetailedThreatSituation", "ThreatSituation")
text.events = { ["ThreatLib_ThreatUpdated"] = true }
end
-- and back we go
if text.code:find("ThreatSituation", nil, true) then
if not text.events then text.events = {} end
if text.events["ThreatLib_ThreatUpdated"] then
text.events["ThreatLib_ThreatUpdated"] = nil
end
if not text.events["UNIT_THREAT_LIST_UPDATE"] then
text.events["UNIT_THREAT_LIST_UPDATE"] = true
text.events["UNIT_THREAT_SITUATION_UPDATE"] = true
end
end
end
end
Expand Down Expand Up @@ -682,19 +682,6 @@ function PitBull4_LuaTexts:OnEnable()
player_guid = UnitGUID("player")
PitBull4.LuaTexts.ScriptEnv.player_guid = player_guid

-- Handle threat
local ThreatLib = LibStub("LibThreatClassic2", true) or LibStub("ThreatClassic-1.0", true)
if ThreatLib then
self.ThreatLib = ThreatLib
local function ThreatUpdate()
self:OnEvent("ThreatLib_ThreatUpdated")
end
ThreatLib.RegisterCallback(self, "Activate", ThreatUpdate)
ThreatLib.RegisterCallback(self, "ThreatUpdated", ThreatUpdate) -- source_unit, target_guid, threat
ThreatLib.RegisterCallback(self, "ThreatCleared", ThreatUpdate)
ThreatLib.RegisterCallback(self, "Deactivate", ThreatUpdate)
end

self:SecureHook("SetCVar")
self:SetCVar()

Expand Down Expand Up @@ -1282,7 +1269,7 @@ local function event_cache_insert(event, font_string)
if not event_entry then
event_entry = {}
event_cache[event] = event_entry
if not protected_events[event] and not custom_events[event] then
if not protected_events[event] then
if casterino_events[event] then
LibClassicCasterino.RegisterCallback(PitBull4_LuaTexts, event, "OnSpellEvent")
else
Expand Down
10 changes: 2 additions & 8 deletions Modules/LuaTexts/ScriptEnv.lua
Original file line number Diff line number Diff line change
Expand Up @@ -914,18 +914,12 @@ end
ScriptEnv.ThreatPair = ThreatPair

local function ThreatSituation(unit, target)
if not PitBull4_LuaTexts.ThreatLib then
return nil, 0, nil, nil, 0
end
return PitBull4_LuaTexts.ThreatLib:UnitDetailedThreatSituation(unit, target)
return UnitDetailedThreatSituation(unit, target)
end
ScriptEnv.ThreatSituation = ThreatSituation

local function ThreatStatusColor(status)
if not PitBull4_LuaTexts.ThreatLib then
return 255, 255, 255
end
local r, g, b = PitBull4_LuaTexts.ThreatLib:GetThreatStatusColor(status)
local r, g, b = GetThreatStatusColor(status)
return r * 255, g * 255, b * 255
end
ScriptEnv.ThreatStatusColor = ThreatStatusColor
Expand Down
54 changes: 16 additions & 38 deletions Modules/ThreatBar/ThreatBar.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
local PitBull4 = _G.PitBull4
local L = PitBull4.L

local ThreatLib

local EXAMPLE_VALUE = 0.6

local PitBull4_ThreatBar = PitBull4:NewModule("ThreatBar")
Expand All @@ -25,19 +23,9 @@ PitBull4_ThreatBar:SetDefaults({
})

function PitBull4_ThreatBar:OnEnable()
ThreatLib = LibStub("LibThreatClassic2", true) or LibStub("ThreatClassic-1.0", true)
if not ThreatLib then
-- print("PitBull4_ThreatBar requires the library ThreatClassic-1.0 to be available.")
self:Disable()
return
end

ThreatLib.RegisterCallback(self, "Activate", "UpdateAll")
ThreatLib.RegisterCallback(self, "ThreatUpdated", "UpdateAll")
ThreatLib.RegisterCallback(self, "ThreatCleared", "UpdateAll")
ThreatLib.RegisterCallback(self, "Deactivate", "UpdateAll")

self:RegisterEvent("PLAYER_TARGET_CHANGED", "UpdateAll")
self:RegisterEvent("UNIT_THREAT_LIST_UPDATE", "UpdateAll")
self:RegisterEvent("UNIT_THREAT_SITUATION_UPDATE", "UpdateAll")
self:RegisterEvent("GROUP_ROSTER_UPDATE")
self:RegisterEvent("UNIT_PET")

Expand All @@ -51,8 +39,8 @@ local ACCEPTABLE_CLASSIFICATIONS = {
pet = true,
party = true,
raid = true,
-- partypet = true,
-- raidpet = true,
partypet = true,
raidpet = true,
}

local function check_classification(frame)
Expand All @@ -73,30 +61,15 @@ function PitBull4_ThreatBar:UNIT_PET(_, unit)
end

function PitBull4_ThreatBar:GetValue(frame)
if not ThreatLib or not check_classification(frame) or (not self:GetLayoutDB(frame).show_solo and not player_in_group) then
if not check_classification(frame) or (not self:GetLayoutDB(frame).show_solo and not player_in_group) then
return nil
end

if not ThreatLib:IsActive() then
return 0
end

local target_guid = UnitGUID(("%starget"):format(frame.unit))
if not target_guid then
return 0
end

local current = ThreatLib:GetThreat(frame.guid, target_guid)
if current == 0 then
return 0
end

local max = ThreatLib:GetMaxThreatOnTarget(target_guid)
if max == 0 then
return 0
local _, _, scaled_percent = UnitDetailedThreatSituation(frame.unit, "target")
if not scaled_percent then
return nil
end

return current / max
return scaled_percent / 100
end
function PitBull4_ThreatBar:GetExampleValue(frame)
if frame and not check_classification(frame) then
Expand All @@ -106,8 +79,13 @@ function PitBull4_ThreatBar:GetExampleValue(frame)
end

function PitBull4_ThreatBar:GetColor(frame, value)
local status = frame.guid and ThreatLib:UnitThreatSituation(frame.unit, "target") or 0
return unpack(self.db.profile.global.threat_colors[status])
if frame.guid then
local _, status = UnitDetailedThreatSituation(frame.unit, "target")
if status then
return unpack(self.db.profile.global.threat_colors[status])
end
end
return unpack(self.db.profile.global.threat_colors[0])
end
function PitBull4_ThreatBar:GetExampleColor(frame, value)
return unpack(self.db.profile.global.threat_colors[0])
Expand Down
2 changes: 1 addition & 1 deletion PitBull4.toc
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
## Notes-zhTW: Dog-themed unit frames. Woof. Arf. Yip.
## Author: ckknight, Shekfi, Nebula
## SavedVariables: PitBull4DB
## OptionalDeps: Ace3, LibSharedMedia-3.0, AceGUI-3.0-SharedMediaWidgets, LibClassicDurations, LibClassicCasterino, LibDBIcon-1.0, LibHealComm-4.0, !LibTotemInfo-1.0, LibThreatClassic2, ThreatClassic-1.0, Clique, RealMobHealth
## OptionalDeps: Ace3, LibSharedMedia-3.0, AceGUI-3.0-SharedMediaWidgets, LibClassicDurations, LibClassicCasterino, LibDBIcon-1.0, LibHealComm-4.0, !LibTotemInfo-1.0, Clique, RealMobHealth
## Version: @project-version@

## X-Curse-Project-ID: 7522
Expand Down

0 comments on commit 0f15bf7

Please sign in to comment.