From cdaf14bf3dcb7a43ee250143fff1f401a067bf29 Mon Sep 17 00:00:00 2001 From: Xinhuan Date: Thu, 2 Oct 2008 18:36:23 +0800 Subject: [PATCH] Add animate bar option to config. Move animate function out to an upvalue instead of creating one per bar. Simplify some code. --- Omen.lua | 115 ++++++++++++++++++++++++++++++++----------------------- 1 file changed, 68 insertions(+), 47 deletions(-) diff --git a/Omen.lua b/Omen.lua index 4c80cd5..cea893e 100644 --- a/Omen.lua +++ b/Omen.lua @@ -15,6 +15,7 @@ _G["Omen"] = Omen -- Localize some global functions local floor, format = floor, format local sort = sort +local tinsert, tremove = tinsert, tremove local UnitDetailedThreatSituation = UnitDetailedThreatSituation local UnitExists, UnitGUID, UnitName, UnitClass = UnitExists, UnitGUID, UnitName, UnitClass local UnitIsPlayer, UnitPlayerControlled, UnitCanAttack = UnitIsPlayer, UnitPlayerControlled, UnitCanAttack @@ -34,6 +35,7 @@ local defaults = { CollapseHide = false, Locked = false, BarHeight = 16, + AnimateBars = true, ShowWith = { Pet = true, Alone = false, @@ -349,16 +351,46 @@ end -- Omen bar stuff do - local barCount = 0 + local function animate(self, v) + self.animationCursor = self.animationCursor + v + if self.animationCursor > self.animationTime then + self.texture:SetWidth(self.animations[1]) + tremove(self.animations, 1) + tremove(self.animations, 1) + tremove(self.animations, 1) + self.animationCursor = self.animationCursor - self.animationTime + if #self.animations == 0 then + self:SetScript("OnUpdate", nil) + end + else + self.texture:SetWidth(self.animations[2] + (self.animations[3] * (self.animationCursor / self.animationTime))) + end + end + + local function AnimateTo(self, val) + if val == 1/0 or val == -1/0 then return end + if val == 0 then val = 1 end + if #self.animations > 0 and self.animations[#self.animations-2] == val then + return + end + local currentWidth = self.texture:GetWidth() + local diff = (val - currentWidth) + tinsert(self.animations, val) + tinsert(self.animations, currentWidth) + tinsert(self.animations, diff) + + if #self.animations > 0 then + self:SetScript("OnUpdate", self.animate) + end + end + setmetatable(bars, {__index = function(self, barID) local bar = CreateFrame("Frame", nil, Omen.BarList) self[barID] = bar - barCount = barCount + 1 - bar:SetWidth(Omen.BarList:GetWidth()) bar:SetHeight(db.BarHeight) - bar:SetPoint("TOPLEFT", Omen.BarList, "TOPLEFT", 0, (barCount-1) * -db.BarHeight) + bar:SetPoint("TOPLEFT", Omen.BarList, "TOPLEFT", 0, (barID-1) * -db.BarHeight) bar.Text1 = bar:CreateFontString(nil, nil, "GameFontNormalSmall") bar.Text1:SetPoint("LEFT", bar, "LEFT", 10, 0) @@ -375,42 +407,13 @@ do bar.texture:SetTexture(1, 1, 1, 0.6) bar.texture:SetPoint("TOPLEFT", bar, "TOPLEFT") bar.texture:SetPoint("BOTTOMLEFT", bar, "BOTTOMLEFT") - + bar.animations = {} bar.animationCursor = 0 bar.animationTime = 0.25 - bar.animate = function(self, v) - self.animationCursor = self.animationCursor + v - if self.animationCursor > self.animationTime then - self.texture:SetWidth(self.animations[1]) - tremove(self.animations, 1) - tremove(self.animations, 1) - tremove(self.animations, 1) - self.animationCursor = self.animationCursor - self.animationTime - if #self.animations == 0 then - self:SetScript("OnUpdate", nil) - end - else - self.texture:SetWidth(self.animations[2] + (self.animations[3] * (self.animationCursor / self.animationTime))) - end - end - - bar.AnimateTo = function(self, val) - if val == 1/0 or val == -1/0 then return end - if #self.animations > 0 and self.animations[#self.animations-2] == val then - return - end - local currentWidth = self.texture:GetWidth() - local diff = (val - currentWidth) - tinsert(self.animations, val) - tinsert(self.animations, currentWidth) - tinsert(self.animations, diff) - - if #self.animations > 0 then - self:SetScript("OnUpdate", self.animate) - end - end - + bar.animate = animate + bar.AnimateTo = AnimateTo + return bar end}) end @@ -668,6 +671,7 @@ function Omen:UpdateBars() local w = self.BarList:GetWidth() local h = self.BarList:GetHeight() local topthreat = threatTable[sortTable[1]] + if topthreat == 0 then topthreat = 1 end local tankThreat = threatTable[tankGUID or mobTargetGUID or sortTable[1]] i = 1 for j = 1, #sortTable do @@ -683,16 +687,11 @@ function Omen:UpdateBars() end local c = (class == "PET" and pet_color) or RAID_CLASS_COLORS[class] or default_color bar.texture:SetVertexColor(c.r, c.g, c.b) - - if topthreat == 0 then - topthreat = threatTable[guid] - if topthreat == 0 then - topthreat = 1 - end - end - local width = w * threatTable[guid] / topthreat - bar:AnimateTo(width) - + if db.AnimateBars then + bar:AnimateTo(w * threatTable[guid] / topthreat) + else + bar.texture:SetWidth(w * threatTable[guid] / topthreat) + end bar:Show() i = i + 1 if db.Autocollapse then @@ -878,6 +877,27 @@ local options = { }, }, }, + Bars = { + type = "group", + name = "Bar Settings", + desc = "Bar Settings", + set = function(info, value) + Omen.db.profile[ info[#info] ] = value + Omen:UpdateBars() + end, + args = { + intro = { + order = 1, + type = "description", + name = "Configure bar settings.", + }, + AnimateBars = { + type = "toggle", + name = "Animate Bars", + desc = "Smoothly animate bar changes", + }, + }, + }, Help = { type = "group", name = L["Help File"], @@ -974,6 +994,7 @@ function Omen:SetupOptions() self:RegisterModuleOptions("OmenSlashCommand", optionsSlash, L["Slash Command"]) self.optionsFrames["Help"] = LibStub("AceConfigDialog-3.0"):AddToBlizOptions("Omen", L["Help File"], "Omen", "Help") self.optionsFrames["ShowWhen"] = LibStub("AceConfigDialog-3.0"):AddToBlizOptions("Omen", L["Show When..."], "Omen", "ShowWhen") + self.optionsFrames["Bars"] = LibStub("AceConfigDialog-3.0"):AddToBlizOptions("Omen", "Bar Settings", "Omen", "Bars") end function Omen:RegisterModuleOptions(name, optionTbl, displayName)