Skip to content

Commit

Permalink
Updates and caching
Browse files Browse the repository at this point in the history
  • Loading branch information
Kkthnx committed Aug 20, 2024
1 parent c6fb70c commit 56edfc3
Show file tree
Hide file tree
Showing 5 changed files with 196 additions and 180 deletions.
195 changes: 144 additions & 51 deletions KkthnxUI/Modules/Auras/Core.lua
Original file line number Diff line number Diff line change
@@ -1,16 +1,22 @@
local K, C, L = KkthnxUI[1], KkthnxUI[2], KkthnxUI[3]
local Module = K:NewModule("Auras")

-- Cache WoW API and Lua functions
local math_floor, select, string_format = math.floor, select, string.format
local CreateFrame, GetTime, GetWeaponEnchantInfo = CreateFrame, GetTime, GetWeaponEnchantInfo
local DebuffTypeColor, RegisterAttributeDriver, RegisterStateDriver = DebuffTypeColor, RegisterAttributeDriver, RegisterStateDriver
local GameTooltip, GetInventoryItemQuality, GetInventoryItemTexture = GameTooltip, GetInventoryItemQuality, GetInventoryItemTexture
-- Cache Lua and WoW API functions
local math_floor, string_format, select, tonumber, unpack = math.floor, string.format, select, tonumber, unpack
local CreateFrame, DebuffTypeColor, GameTooltip, GetInventoryItemQuality, GetInventoryItemTexture = CreateFrame, DebuffTypeColor, GameTooltip, GetInventoryItemQuality, GetInventoryItemTexture
local GetTime, GetWeaponEnchantInfo, RegisterAttributeDriver, RegisterStateDriver, SecureHandlerSetFrameRef = GetTime, GetWeaponEnchantInfo, RegisterAttributeDriver, RegisterStateDriver, SecureHandlerSetFrameRef
local UIParent, IsAltKeyDown, IsControlKeyDown = UIParent, IsAltKeyDown, IsControlKeyDown

local day, hour, minute = 86400, 3600, 60

function Module:OnEnable()
local loadAuraModules = { "HideBlizBuff", "BuildBuffFrame", "CreateTotems", "CreateReminder" }
local loadAuraModules = {
"HideBlizBuff",
"BuildBuffFrame",
"CreateTotems",
"CreateReminder",
}

for _, funcName in ipairs(loadAuraModules) do
local func = self[funcName]
if type(func) == "function" then
Expand All @@ -26,11 +32,12 @@ function Module:HideBlizBuff()
if not C["Auras"].Enable and not C["Auras"].HideBlizBuff then
return
end

K:RegisterEvent("PLAYER_ENTERING_WORLD", function(_, isLogin, isReload)
if isLogin or isReload then
K.HideInterfaceOption(_G.BuffFrame)
K.HideInterfaceOption(_G.DebuffFrame)
BuffFrame.numHideableBuffs = 0 -- Prevent error in edit mode
BuffFrame.numHideableBuffs = 0 -- fix error when on editmode
end
end)
end
Expand All @@ -40,29 +47,42 @@ function Module:BuildBuffFrame()
return
end

-- Buff and Debuff settings
-- Config
Module.settings = {
Buffs = { offset = 12, size = C["Auras"].BuffSize, wrapAfter = C["Auras"].BuffsPerRow, maxWraps = 3, reverseGrow = C["Auras"].ReverseBuffs },
Debuffs = { offset = 12, size = C["Auras"].DebuffSize, wrapAfter = C["Auras"].DebuffsPerRow, maxWraps = 1, reverseGrow = C["Auras"].ReverseDebuffs },
Buffs = {
offset = 12,
size = C["Auras"].BuffSize,
wrapAfter = C["Auras"].BuffsPerRow,
maxWraps = 3,
reverseGrow = C["Auras"].ReverseBuffs,
},
Debuffs = {
offset = 12,
size = C["Auras"].DebuffSize,
wrapAfter = C["Auras"].DebuffsPerRow,
maxWraps = 1,
reverseGrow = C["Auras"].ReverseDebuffs,
},
}

-- Create Buff Header
-- Movers
Module.BuffFrame = Module:CreateAuraHeader("HELPFUL")
Module.BuffFrame.mover = K.Mover(Module.BuffFrame, "Buffs", "BuffAnchor", { "TOPRIGHT", _G.Minimap, "TOPLEFT", -6, 0 })
Module.BuffFrame:ClearAllPoints()
Module.BuffFrame:SetPoint("TOPRIGHT", Module.BuffFrame.mover)

-- Create Debuff Header
Module.DebuffFrame = Module:CreateAuraHeader("HARMFUL")
Module.DebuffFrame.mover = K.Mover(Module.DebuffFrame, "Debuffs", "DebuffAnchor", { "TOPRIGHT", Module.BuffFrame.mover, "BOTTOMRIGHT", 0, -12 })
Module.DebuffFrame:ClearAllPoints()
Module.DebuffFrame:SetPoint("TOPRIGHT", Module.DebuffFrame.mover)
end

function Module:FormatAuraTime(s)
if s >= day then
return string_format("%d" .. K.MyClassColor .. "d", s / day), s % day
elseif s >= hour * 2 then
elseif s >= 2 * hour then
return string_format("%d" .. K.MyClassColor .. "h", s / hour), s % hour
elseif s >= minute * 10 then
elseif s >= 10 * minute then
return string_format("%d" .. K.MyClassColor .. "m", s / minute), s % minute
elseif s >= minute then
return string_format("%d:%.2d", s / minute, s % minute), s - math_floor(s)
Expand All @@ -77,54 +97,69 @@ end

function Module:UpdateTimer(elapsed)
local onTooltip = GameTooltip:IsOwned(self)

if not (self.timeLeft or self.expiration or onTooltip) then
self:SetScript("OnUpdate", nil)
return
end

if self.timeLeft then
self.timeLeft = self.timeLeft - elapsed
elseif self.expiration then
self.timeLeft = (self.expiration / 1e3) - (GetTime() - self.oldTime)
end

if self.timeLeft and self.timeLeft >= 0 then
if self.nextUpdate > 0 then
self.nextUpdate = self.nextUpdate - elapsed
return
end
if self.nextUpdate > 0 then
self.nextUpdate = self.nextUpdate - elapsed
return
end

if self.expiration then
self.timeLeft = self.expiration / 1e3 - (GetTime() - self.oldTime)
end

if self.timeLeft and self.timeLeft >= 0 then
local timer, nextUpdate = Module:FormatAuraTime(self.timeLeft)
self.nextUpdate = nextUpdate
self.timer:SetText(timer)
else
self.timer:SetText("") -- Clear the timer if timeLeft is invalid
end

-- If the tooltip is showing, update it
if onTooltip then
Module:Button_SetTooltip(self)
end
end

function Module:GetSpellStat(arg16, arg17, arg18)
return (arg16 > 0 and L["Versa"]) or (arg17 > 0 and L["Mastery"]) or (arg18 > 0 and L["Haste"]) or L["Crit"]
end

function Module:UpdateAuras(button, index)
local unit, filter = button.header:GetAttribute("unit"), button.filter
local auraData = C_UnitAuras.GetAuraDataByIndex(unit, index, filter)
if not auraData then
return
end

button.timeLeft = auraData.duration > 0 and (auraData.expirationTime - GetTime()) or nil
if button.timeLeft then
if auraData.duration > 0 and auraData.expirationTime then
local timeLeft = auraData.expirationTime - GetTime()
if not button.timeLeft then
button.nextUpdate = -1
button.timeLeft = timeLeft
button:SetScript("OnUpdate", Module.UpdateTimer)
else
button.timeLeft = timeLeft
end
button.nextUpdate = -1
button:SetScript("OnUpdate", Module.UpdateTimer)
Module.UpdateTimer(button, 0)
else
button.timeLeft = nil
button.timer:SetText("")
end

button.count:SetText(auraData.applications and auraData.applications > 1 and auraData.applications or "")
button.icon:SetTexture(auraData.icon)
local count = auraData.applications
if count and count > 1 then
button.count:SetText(count)
else
button.count:SetText("")
end

if filter == "HARMFUL" then
local color = DebuffTypeColor[auraData.dispelName or "none"]
Expand All @@ -133,23 +168,33 @@ function Module:UpdateAuras(button, index)
K.SetBorderColor(button.KKUI_Border)
end

-- Show spell stat for 'Soleahs Secret Technique'
if auraData.spellId == 368512 then
button.count:SetText(Module:GetSpellStat(unpack(auraData.points)))
end

button.spellID = auraData.spellId
button.icon:SetTexture(auraData.icon)
button.expiration = nil
end

function Module:UpdateTempEnchant(button, index)
local expirationTime = select(button.enchantOffset, GetWeaponEnchantInfo())
if expirationTime then
local quality = GetInventoryItemQuality("player", index)
button.KKUI_Border:SetVertexColor(K.QualityColors[quality or 1].r, K.QualityColors[quality or 1].g, K.QualityColors[quality or 1].b)
local color = K.QualityColors[quality or 1]
button.KKUI_Border:SetVertexColor(color.r, color.g, color.b)
button.icon:SetTexture(GetInventoryItemTexture("player", index))

button.expiration = expirationTime
button.oldTime = GetTime()
button:SetScript("OnUpdate", Module.UpdateTimer)
button.nextUpdate = -1
Module.UpdateTimer(button, 0)
else
button.expiration = nil
button.timeLeft = nil
button.timer:SetText("")
button.expiration, button.timeLeft = nil, nil
end
end

Expand All @@ -162,46 +207,85 @@ function Module:OnAttributeChanged(attribute, value)
end

function Module:UpdateHeader(header)
local cfg = Module.settings[header.filter == "HELPFUL" and "Buffs" or "Debuffs"]
local cfg = Module.settings.Debuffs
if header.filter == "HELPFUL" then
cfg = Module.settings.Buffs
header:SetAttribute("consolidateTo", 0)
header:SetAttribute("weaponTemplate", string_format("KKUI_AuraTemplate%d", cfg.size))
end

local margin = 6

header:SetAttribute("separateOwn", 1)
header:SetAttribute("sortMethod", "INDEX")
header:SetAttribute("sortDirection", "+")
header:SetAttribute("wrapAfter", cfg.wrapAfter)
header:SetAttribute("maxWraps", cfg.maxWraps)
header:SetAttribute("point", cfg.reverseGrow and "TOPLEFT" or "TOPRIGHT")
header:SetAttribute("xOffset", (cfg.reverseGrow and 1 or -1) * (cfg.size + 6))
header:SetAttribute("minWidth", (cfg.size + margin) * cfg.wrapAfter)
header:SetAttribute("minHeight", (cfg.size + cfg.offset) * cfg.maxWraps)
header:SetAttribute("xOffset", (cfg.reverseGrow and 1 or -1) * (cfg.size + margin))
header:SetAttribute("yOffset", 0)
header:SetAttribute("wrapXOffset", 0)
header:SetAttribute("wrapYOffset", -(cfg.size + cfg.offset))
header:SetAttribute("template", string_format("KKUI_AuraTemplate%d", cfg.size))
header:SetAttribute("minWidth", (cfg.size + 6) * cfg.wrapAfter)
header:SetAttribute("minHeight", (cfg.size + cfg.offset) * cfg.maxWraps)
header:SetAttribute("sortMethod", "INDEX")
header:SetAttribute("sortDirection", "+")

-- Update child aura frames
local fontSize = math_floor(cfg.size / 30 * 12 + 0.5)
for i, child in ipairs({ header:GetChildren() }) do
if i <= (cfg.maxWraps * cfg.wrapAfter) then
local index = 1
local child = select(index, header:GetChildren())
while child do
if (math_floor(child:GetWidth() * 100 + 0.5) / 100) ~= cfg.size then
child:SetSize(cfg.size, cfg.size)
child.count:SetFontObject(K.UIFontOutline)
child.count:SetFont(select(1, child.count:GetFont()), fontSize, select(3, child.count:GetFont()))
child.timer:SetFontObject(K.UIFontOutline)
child.timer:SetFont(select(1, child.timer:GetFont()), fontSize, select(3, child.timer:GetFont()))
else
child:Hide() -- Hide extra frames beyond wrap limit
end

child.count:SetFontObject(K.UIFontOutline)
child.count:SetFont(select(1, child.count:GetFont()), fontSize, select(3, child.count:GetFont()))

child.timer:SetFontObject(K.UIFontOutline)
child.timer:SetFont(select(1, child.timer:GetFont()), fontSize, select(3, child.timer:GetFont()))

-- Blizzard bug fix, icons arent being hidden when you reduce the amount of maximum buttons
if index > (cfg.maxWraps * cfg.wrapAfter) and child:IsShown() then
child:Hide()
end

index = index + 1
child = select(index, header:GetChildren())
end
end

function Module:CreateAuraHeader(filter)
local header = CreateFrame("Frame", "KKUI_Player" .. (filter == "HELPFUL" and "Buffs" or "Debuffs"), UIParent, "SecureAuraHeaderTemplate")
local name = "KKUI_PlayerDebuffs"
if filter == "HELPFUL" then
name = "KKUI_PlayerBuffs"
end

local header = CreateFrame("Frame", name, UIParent, "SecureAuraHeaderTemplate")
header:SetClampedToScreen(true)
header:UnregisterEvent("UNIT_AURA") -- we only need to watch player and vehicle
header:RegisterUnitEvent("UNIT_AURA", "player", "vehicle")
header:SetAttribute("unit", "player")
header:SetAttribute("filter", filter)
header.filter = filter
RegisterAttributeDriver(header, "unit", "[vehicleui] vehicle; player")

header.visibility = CreateFrame("Frame", nil, UIParent, "SecureHandlerStateTemplate")
header.visibility:RegisterEvent("WEAPON_ENCHANT_CHANGED")
RegisterStateDriver(header.visibility, "customVisibility", "[petbattle] 0;1")
header.visibility:SetAttribute("_onstate-customVisibility", [[local header = self:GetFrameRef("AuraHeader") if newstate == 0 then header:Hide() else header:Show() end]])
SecureHandlerSetFrameRef(header.visibility, "AuraHeader", header)
RegisterStateDriver(header.visibility, "customVisibility", "[petbattle] 0;1")
header.visibility:SetAttribute(
"_onstate-customVisibility",
[[
local header = self:GetFrameRef("AuraHeader")
local hide, shown = newstate == 0, header:IsShown()
if hide and shown then header:Hide() elseif not hide and not shown then header:Show() end
]]
)

if filter == "HELPFUL" then
header:SetAttribute("consolidateDuration", -1)
header:SetAttribute("includeWeapons", 1)
end

Module:UpdateHeader(header)
header:Show()
Expand All @@ -210,7 +294,7 @@ function Module:CreateAuraHeader(filter)
end

function Module:RemoveSpellFromIgnoreList()
if IsAltKeyDown() and IsControlKeyDown() and self.spellID then
if IsAltKeyDown() and IsControlKeyDown() and self.spellID and KkthnxUIDB.Variables[K.Realm][K.Name].AuraWatchList.IgnoreSpells[self.spellID] then
KkthnxUIDB.Variables[K.Realm][K.Name].AuraWatchList.IgnoreSpells[self.spellID] = nil
K.Print(string_format(L["RemoveFromIgnoreList"], "", self.spellID))
end
Expand All @@ -230,10 +314,19 @@ function Module:Button_OnEnter()
self:SetScript("OnUpdate", Module.UpdateTimer)
end

local indexToOffset = { 2, 6, 10 }
function Module:CreateAuraIcon(button)
button.header = button:GetParent()
button.filter = button.header.filter
local fontSize = math_floor(Module.settings[button.filter == "HELPFUL" and "Buffs" or "Debuffs"].size / 30 * 12 + 0.5)
button.name = button:GetName()
local enchantIndex = tonumber(strmatch(button.name, "TempEnchant(%d)$"))
button.enchantOffset = indexToOffset[enchantIndex]

local cfg = Module.settings.Debuffs
if button.filter == "HELPFUL" then
cfg = Module.settings.Buffs
end
local fontSize = math_floor(cfg.size / 30 * 12 + 0.5)

button.icon = button:CreateTexture(nil, "BORDER")
button.icon:SetAllPoints()
Expand Down
Loading

0 comments on commit 56edfc3

Please sign in to comment.