X-Git-Url: https://www.aleksib.fi/git/wowui.git/blobdiff_plain/b8c70450d10559c08516416bf92fb95434bd3034..875c87e09066367d69d39b51bde0148b4a3ec01b:/OmaRF/Indicators.lua diff --git a/OmaRF/Indicators.lua b/OmaRF/Indicators.lua index ba89735..6fd7b23 100644 --- a/OmaRF/Indicators.lua +++ b/OmaRF/Indicators.lua @@ -1,149 +1,174 @@ -local media = LibStub:GetLibrary("LibSharedMedia-3.0"); -local f = OmaRF.frames; -local positions = OmaRF.positions; -local pad = 2; -local paddings = { - TOPLEFT = {pad, -pad}, - TOPRIGHT = {-pad, -pad}, - CENTER = {0, 0}, - BOTTOMLEFT = {pad, pad}, - BOTTOMRIGHT = {-pad, pad} -}; -local watchedAuras; -- all watched auras -local auraFilters = {"HELPFUL", "HARMFUL"}; -local DEFAULT_ICON = "Interface\\AddOns\\OmaRF\\images\\rhomb"; -local _; - --- global functions used every update -local C_TimerAfter = C_Timer.After +-- Indicators.lua +local pairs, ipairs = pairs, ipairs; +local floor = math.floor; local GetTime = GetTime; local UnitAura = UnitAura; -local UnitIsPlayer = UnitIsPlayer; -local UnitIsConnected = UnitIsConnected; -local UnitIsDeadOrGhost = UnitIsDeadOrGhost; -local CompactRaidFrameContainer_ApplyToFrames = CompactRaidFrameContainer_ApplyToFrames; +local CreateFrame = CreateFrame; +local UnitIsDeadOrGhost, UnitIsConnected = UnitIsDeadOrGhost, UnitIsConnected; +local CTimerAfter = C_Timer.After; --- list of important auras TODO try to use spellIDs -local centerAuras = { - "Power Word: Shield" -}; +local Settings = OmaRFSettings; +local majorAuras = Settings.MajorAuras; +local watchedAuras = {}; -local function configureIndicators(frame, name) - local frameName = name or frame:GetName(); - if not f[frameName] then return end +local updaters = {}; +local updating = {}; +local auraFilters = {"HELPFUL", "HARMFUL"}; - local config = OmaRF.db.profile; - local font = media and media:Fetch('font', config.indicatorFont) or STANDARD_TEXT_FONT; - for pos, ind in pairs(f[frameName]) do - ind.text:SetFont(font, config[pos]["textSize"]); - ind.text:SetTextColor(unpack(config[pos]["textColor"])); - ind.icon:SetWidth(config[pos]["iconSize"]); - ind.icon:SetHeight(config[pos]["iconSize"]); - ind.icon:SetTexture(DEFAULT_ICON); - ind.icon:SetVertexColor(unpack(config[pos]["iconColor"])); - if config[pos]["showIcon"] then - ind.icon:Show(); - else - ind.icon:Hide(); - end +local M = {}; +OmaRFIndicators = M; +M.Class = {}; + +function M.SetupIndicators(frame, class) + frame.indBase = CreateFrame("Frame", nil, frame); + frame.indBase:SetAllPoints(); + frame.indBase:Hide(); + if M.Class[class] then + watchedAuras = M.Class[class].Auras; + frame.inds = M.Class[class].Setup(frame.indBase); + else + frame.inds = {}; + end + + frame.majorBase = CreateFrame("Frame", nil, frame); + frame.majorBase:SetPoint("TOPLEFT", frame, "TOPLEFT", 4, -10); + frame.majorBase:SetPoint("BOTTOMRIGHT"); + frame.majors = {}; + for i = 1,3 do + local tex = frame.majorBase:CreateTexture(nil, "OVERLAY"); + tex = frame.majorBase:CreateTexture(nil, "OVERLAY"); + if i == 1 then tex:SetPoint("TOPLEFT", frame.majorBase, "TOPLEFT"); + else tex:SetPoint("TOPLEFT", frame.majors[i-1], "TOPRIGHT"); end + tex:SetWidth(20); + tex:SetHeight(20); + tex:Hide(); + tex.text = frame.majorBase:CreateFontString(nil, "OVERLAY", "GameFontHighlight"); + tex.text:SetPoint("CENTER", tex, "BOTTOMRIGHT", -2, 2); + tex.text:Hide(); + tex.stack = frame.majorBase:CreateFontString(nil, "OVERLAY", "GameFontHighlight"); + tex.stack:SetPoint("CENTER", tex, "TOPLEFT", 1, 0); + tex.stack:Hide(); + tex.icon = true; + frame.majors[i] = tex; end end --- Create the FontStrings used for indicators -local function setupCompactUnitFrame(frame, name) - f[name] = {}; - for _, pos in ipairs(positions) do - f[name][pos] = {}; - f[name][pos].text = frame:CreateFontString(nil, "OVERLAY", "GameFontHighlightSmall"); - f[name][pos].text:SetPoint(pos, frame, pos, paddings[pos][1], paddings[pos][2]); - f[name][pos].icon = frame:CreateTexture(nil, "OVERLAY"); - f[name][pos].icon:SetPoint(pos, frame, pos, paddings[pos][1], paddings[pos][2]); - end - configureIndicators(frame, name); +local function remaining(text, expires, current) + if expires == 0 then + text:SetText(""); + return false; + end + local remain = expires - current; + if remain > 8 then + text:SetText(""); + else + text:SetText(floor(remain+0.5)); + end + return true; end --- Check the indicators on a frame and update the times on them local function updateIndicators(frame) - local frameName = frame:GetName(); - local unit = frame.unit; - - -- Create indicators if needed - if not f[frameName] then setupCompactUnitFrame(frame, frameName) end - -- Reset current - for _, ind in pairs(f[frameName]) do - ind.text:SetText(""); - ind.icon:SetTexture(""); - end - -- Hide if unit is dead/disconnected - if (not UnitIsConnected(unit)) or UnitIsDeadOrGhost(frame.displayedUnit) then + local unit = frame.displayed; + if not frame:IsShown() or not UnitIsConnected(unit) or UnitIsDeadOrGhost(unit) then + updating[frame] = nil; return; end - local name, icon, count, debuff, expires, caster, id; + local needUpdate = false; local current = GetTime(); - for _, filter in ipairs(auraFilters) do - local i = 1; - while true do - name, _, icon, count, debuff, _, expires, caster, _, _, id = UnitAura(unit, i, filter); - if not id then break end - local pos = watchedAuras[name] or watchedAuras[id] or watchedAuras[debuff]; - if pos then - local config = OmaRF.db.profile[pos]; - if not config.mine or UnitIsPlayer(caster) then - if config.showIcon and not config.useDefaultIcon then - -- show icon - ind.icon:SetTexture(icon); - end - if config.showText then - -- show text - local text; - local remaining = expires - current; - if remaining > 60 then - text = string.format("%dm", ceil(remaining/60)); - else - text = string.format("%d", floor(remaining+0.5)); - end - if count > 1 and config.stack then - if text then - text = count.."-"..text; - else - text = count; - end - end - - ind.text:SetText(text); - end - end - end - i = i + 1; + for _, ind in pairs(frame.inds) do + if ind.text and ind.text.expires ~= nil then + needUpdate = remaining(ind.text, ind.text.expires, current) or needUpdate; + end + end + for _, ind in pairs(frame.majors) do + if ind.text and ind.text.expires ~= nil then + needUpdate = remaining(ind.text, ind.text.expires, current) or needUpdate; end end + if needUpdate then + CTimerAfter(0.16, updaters[frame]); + else + updating[frame] = nil; + end +end + +local function showInd(ind, expires, current, count, icon) + local needUpdate = false; + if ind.icon then + ind:SetTexture(icon); + end + if ind.text then + needUpdate = remaining(ind.text, expires, current); + ind.text.expires = expires; + ind.text:Show(); + end + if ind.stack and count > 1 then + ind.stack:SetText(count); + ind.stack:Show(); + end + ind:Show(); + return needUpdate; end --- Update all indicators -function OmaRF:UpdateAllIndicators() - CompactRaidFrameContainer_ApplyToFrames(CompactRaidFrameContainer, "normal", updateIndicators); - if self.running then - C_TimerAfter(0.15, self:UpdateAllIndicators); +local function hideInd(ind) + if ind.text then + ind.text.expires = nil; + ind.text:Hide(); end + if ind.stack then ind.stack:Hide() end + ind:Hide(); end --- Used to update everything that is affected by the configuration -function OmaRF:RefreshConfig() - self:OnDisable(); -- clear everything - if self.db.profile.enabled then - CompactRaidFrameContainer_ApplyToFrames(CompactRaidFrameContainer, "normal", configureIndicators); - -- Format aura strings - watchedAuras = {}; - for _, pos in ipairs(positions) do - for _, aura in ipairs(self.db.profile[pos]["auras"]) do - watchedAuras[aura] = pos; -- TODO single aura only in one position +function M.CheckIndicators(frame, unit) + for _, ind in pairs(frame.inds) do + hideInd(ind); + end + for _, ind in pairs(frame.majors) do + hideInd(ind); + end + local name, icon, count, expires, caster, id; + local showInds, showMajors, needUpdate = false, false, false; + local majorPos = 1; + local alert = false; -- color the whole bar + local current = GetTime(); + for _, filter in ipairs(auraFilters) do + local i = 1; + while true do + name, _, icon, count, _, _, expires, caster, _, _, id = UnitAura(unit, i, filter); + if not id then break end + local pos = watchedAuras[id] or watchedAuras[name]; + if pos and caster == "player" then + needUpdate = showInd(frame.inds[pos], expires, current, count, icon) or needUpdate; + showInds = true; end + local major = majorAuras[id] or majorAuras[name]; + if major and majorPos <= 3 then + needUpdate = showInd(frame.majors[majorPos], expires, current, count, icon) or needUpdate; + if major.bar then alert = true end + showMajors = true; + majorPos = majorPos + 1; + end + i = i + 1; end - - if next(watchedAuras) ~= nil then - self.running = true; - C_TimerAfter(0.15, self:UpdateAllIndicators); + end + if showInds or showMajors then + frame.indBase:Show(); + frame.majorBase:Show(); + if needUpdate and not updating[frame] then + updating[frame] = true; -- race? + -- create a function for updating the indicator + local func = updaters[frame]; + if not func then + func = function() updateIndicators(frame) end; + updaters[frame] = func; + end + CTimerAfter(0.16, func); end + else + frame.indBase:Hide(); + frame.majorBase:Hide(); end + + return alert; end