WoWInterface SVN RaidWatch2

[/] [trunk/] [RaidWatch_Core/] [Prototypes/] [Messages.lua] - Rev 22

Compare with Previous | Blame | View Log

local RW = LibStub("AceAddon-3.0"):GetAddon("Raid Watch")
local L = LibStub("AceLocale-3.0"):GetLocale("RW2-Plugin-Base")

local LSM = LibStub("LibSharedMedia-3.0")

local Boss = RW.BossPrototype
local messageProt = {}
local mt = {__index = messageProt}

local Party = RW.Party
local Utils = RW.Utils

-- If text is a number this warning is considered not a custom one
-- If text is not a number then it is custom and it can be possibly sent as a table,
-- then the first value in the table will be the actual text, and the second oen will be the option text as its show in the GUI
-- 3rd value in the table will be the tooltip text
local spell
function Boss:_Message(mType, text, icon, optionName, default, color, sound)
        if type(text) == "number" then 
                spell = text
                return self:_Message(mType, GetSpellInfo(text), icon or text, optionName or text, default, color, sound) 
        end
        if type(icon) == "number" then
                spell = icon
                return self:_Message(mType, text, select(3, GetSpellInfo(icon)), optionName or icon, default, color, sound)
        end
        
        optionName = type(optionName) == "number" and Utils.SpellName[optionName] or optionName
        
        -- If no option name is found by now we use the text as default
        if not optionName then optionName = type(text) == "table" and text[1] or text end
        
        self:AddToCategory(nil, optionName)
        
        default = Utils:BuildRoleDefaults(default)
        
        local defaults = self:AddDefaults(optionName, "MESSAGE", mType ~= "CUSTOM" and mType or text[2], default)
        defaults.color = color
        defaults.sound = sound
        
        local base = self:GetBaseAlertObject(optionName)
        if not base then
                base = {
                        icon = icon,                    
                        id = optionName,
                        spell = spell,
                        mod = self,
                        scheduled = {},
                }
                tinsert(self.BaseAlertObjects, base)
        end
        
        local obj = setmetatable({
                mainType = "MESSAGE",
                type = mType,   
                text = type(text) == "table" and text[1] or text,
                optText = (mType == "CUSTOM" and type(text) == "table") and text[2] or nil,
                base = base,
        }, mt)
        
        self.AlertObjects.MESSAGE = self.AlertObjects.MESSAGE or {}
        tinsert(self.AlertObjects.MESSAGE, obj)
        
        spell = nil
        
        return obj
end

function Boss:Message(...)
        return self:_Message("NORMAL", ...)
end

function Boss:CastMessage(time, ...)
        return self:_Message("CAST", time, ...)
end

function Boss:TargetMessage(...)
        return self:_Message("TARGET", ...)
end

function Boss:StackMessage(...)
        return self:_Message("STACK", ...)
end

function Boss:TargetStackMessage(...)
        return self:_Message("TARGET_STACK", ...)
end

function Boss:SoonMessage(...)
        return self:_Message("SOON", ...)
end

function Boss:PhaseMessage(text, icon, option, ...)
        return self:_Message("PHASE", nil, icon or [[Interface\Icons\Achievement_PVP_A_01]], option or "Phase Switch", ...)
end

function Boss:CustomMessage(text, optText, ...)
        --@debug
        assert(type(text) == "string", self:GetName()..": CustomTimer, variable \"text\" is not of type text")
        assert(type(optText) == "string", self:GetName()..": CustomTimer, variable \"optText\" is not of type text")
        --@end-debug
        return self:_Message("CUSTOM", {text, optText}, ...)
end

function messageProt:SetIcon(icon)
        self.icon = icon
end

function messageProt:Show(...)  
        if not self.base.mod.DB[self.base.id].enabled then return end
        if not Utils:EvaluateRoleOption(self.base.mod.DB[self.base.id].MESSAGE[self.optText or self.type].enabled) then return end
        
        local text
        if self.optText then    
                text = Utils:Format(self.text, ...)
        else
                text = Utils:Format(L["FORMAT_MESSAGE_"..self.type], self.text or ..., ...)
        end

        if self.type == "PHASE" and self.base.icon:find("Interface\\Icons\\Achievement_PVP_A_0") then
                local phase = ...
                self.icon = "Interface\\Icons\\Achievement_PVP_A_0"..phase..".blp"
        end
        
        text = Utils:ColorNames(text)
        
        local tc = self.base.mod.DB[self.base.id].MESSAGE[self.optText or self.type].color
        local r, g, b, a
        if tc then
                if type(tc) == "table" then
                        r, g, b, a = unpack(tc)
                else    
                        -- Add color plugin stuff here
                end
        end
        RW.Callbacks:Fire("MessageShow", text, r, g, b, a, self.base.icon, self.base.mod.DB[self.base.id].MESSAGE[self.optText or self.type].sound, self.base.mod)
end
messageProt.Start = messageProt.Show

function messageProt:AddDelayTarget(target)
        self.delayMessage = self.delayMessage or {}
        self.delayMessage[#self.delayMessage + 1] = target
end

local function delayShow(message, ...)
        if type(message.delayMessage) == "table" then
                message:Show(table.concat(message.delayMessage, "<, >"))
        else
                message:Show(message.delayMessage, ...)
        end
        message.delayMessage = nil
        Utils:TRemove(message.base.scheduled, "MESSAGE"..message.base.id.."delay")
end

function messageProt:DelayedShow(delay)
        self.base.mod:Schedule("MESSAGE"..self.base.id.."delay", delay, delayShow, self)
        tinsert(self.base.scheduled, "MESSAGE"..self.base.id.."delay")
end

local function showMessage(message, ...)
        message:Show(...)
end

function messageProt:Schedule(stime, info, ...)
        self.base.mod:Schedule("MESSAGE"..self.base.id..(info or ""), stime, showMessage, self, info, ...)
        tinsert(self.base.scheduled, "MESSAGE"..self.base.id..(info or ""))
end

function messageProt:ScheduleRepeating(stime, now, info, ...)
        self.base.mod:ScheduleRepeating("MESSAGE"..self.base.id..(info or ""), stime, showMessage, now, self, info, ...)
        tinsert(self.base.scheduled, "MESSAGE"..self.base.id..(info or ""))
end

function messageProt:Unschedule(extraID)
        if not extraID then
                for i, messageID in pairs(self.base.scheduled) do
                        self.base.mod:Unschedule(messageID)
                end
                wipe(self.base.scheduled)
        else
                self.mod:Unschedule("MESSAGE"..self.base.id..(extraID or ""))
                Utils:TRemove(self.scheduled, "MESSAGE"..self.base.id..(extraID or ""))
        end
end
messageProt.Cancel = messageProt.Unschedule

-- Options
function messageProt:GetOption()
        local Options = RW:GetPlugin("Options")
        local boss = self.base.mod
        local AceGUI = LibStub("AceGUI-3.0")
        local UIF = LibStub("LibGUIFactory-1.0"):GetFactory("RW")

        local subGroup = UIF:InlineGroup("")
        
        -- Workaround for old non-default values
        if type(boss.DB[self.base.id].MESSAGE[self.optText or self.type].enabled) ~= "table" then
                boss.DB[self.base.id].MESSAGE[self.optText or self.type].enabled = boss.defaults[self.base.id].MESSAGE[self.optText or self.type].enabled
        end
        -- End workaroudn
        local enabledDrop = UIF:MultiSelectDropdown(L.ENABLED, boss.DB[self.base.id].MESSAGE[self.optText or self.type].enabled, nil, Utils.Constants.EnableList, 0.5)
        
        if self.type == "CUSTOM" then
                subGroup:SetTitle(self.optText)                         
        else
                subGroup:SetTitle(L["MESSAGE_"..self.type])
        end                     
        
        local color = UIF:ColorSelect(L.COLOR, boss.DB[self.base.id].MESSAGE[self.optText or self.type], "color", nil, nil, 0.5)        
        
        local drop = UIF:LSMDropdown("sound", L.SOUND, boss.DB[self.base.id].MESSAGE[self.optText or self.type], "sound", nil, 0.5)
        
        local reset = UIF:Button("Reset", function()
                boss.DB[self.base.id].MESSAGE[self.optText or self.type].enabled = boss.defaults[self.base.id].MESSAGE[self.optText or self.type].enabled
                boss.DB[self.base.id].MESSAGE[self.optText or self.type].color = boss.defaults[self.base.id].MESSAGE[self.optText or self.type].color
                boss.DB[self.base.id].MESSAGE[self.optText or self.type].sound = boss.defaults[self.base.id].MESSAGE[self.optText or self.type].sound
                
                if type(boss.defaults[self.base.id].MESSAGE[self.optText or self.type].enabled) == "table" then
                        for i, v in pairs(boss.defaults[self.base.id].MESSAGE[self.optText or self.type].enabled) do
                                boss.DB[self.base.id].MESSAGE[self.optText or self.type].enabled[i] = v
                                enabledDrop:SetItemValue(i, v)
                        end
                else
                        boss.DB[self.base.id].MESSAGE[self.optText or self.type].enabled = boss.defaults[self.base.id].MESSAGE[self.optText or self.type].enabled
                end
                
                drop:SetValue(boss.DB[self.base.id].MESSAGE[self.optText or self.type].sound or "")
                local colorvalue = boss.DB[self.base.id].MESSAGE[self.optText or self.type].color or nil
                if colorvalue then 
                        if type(colorvalue) == "table" then
                                color:SetColor(unpack(colorvalue))
                        else
                                -- Add color plugin stuff here
                                color:SetColor("")
                        end
                else
                        color:SetColor("")
                end
        end)
        
        subGroup:AddChild(enabledDrop)
        subGroup:AddChild(drop)
        subGroup:AddChild(color)        
        subGroup:AddChild(reset)
        
        return subGroup
end 

Compare with Previous | Blame