WoWInterface SVN RaidWatch2

[/] [trunk/] [RaidWatch_Plugins/] [WarningFrame.lua] - Rev 22

Compare with Previous | Blame | View Log

--[[
Plugin for showing a warning text in the middle of the screen (or desired place)
]]

----------------------------------
-- Create plugin

local RW = LibStub("AceAddon-3.0"):GetAddon("Raid Watch")
local plugin = RW:Plugin("WarningFrame")
if not plugin then return end

----------------------------------
-- Locals
local LSM = LibStub("LibSharedMedia-3.0")
local L = LibStub("AceLocale-3.0"):GetLocale("RW2-Plugin-Warnings")
local LB = LibStub("AceLocale-3.0"):GetLocale("RW2-Plugin-Base")
local Utils = RW.Utils
local Party
local db

----------------------------------
-- Colors
plugin.Colors = {
        {
                name = "Posetive",
                description = "Posetive messages",
                color = {0, 1, 0},
        },
        {
                name = "Negative",
                description = "Negative messages",
                color = {1, 0.15, 0.15},
        },
        {
                name = "Information",
                description = "Informative messages",
                color = {1, 0.95, 0.64},
        },
        {
                name = "Personal",
                description = "Personal messages only shown to you",
                color = {0.33, 0.7, 1},
        }
}

----------------------------------
-- Defaults

local defaults = {
        profile = {
                Width = 180,
                DefaultColor = {0, 0.5, 1},
                DefaultSize = 32,
                DefaultSound = "RW: Tata",
                PlaySound = true,
                UseFlash = true,
                PluginEnabled = true,
                x = 0,
                y = 0,
                Font = "accid",
                FontSize = 16,
                FontOutline = "OUTLINE",
                FontColor = {0.47, 0.75, 1, 1},
                FontAlign = "LEFT",
                BGTexture = "Solid",
                BGColor = {0, 0, 0, 0.5},
                Border = "IshBorder",
                BorderColor = {0.15, 0.24, 0.31, 1},
                BorderSize = 12,
                BarTexture = "HalD",
                BarColor = {0.22, 0.34, 0.46, 1},
                Insets = 1,
                BorderMargin = 5
        }
}

----------------------------------
-- Init

function plugin:OnRegister()
        self.db = RW.db:RegisterNamespace(self:GetName(), defaults)
        db = self.db.profile
        
        self.db.RegisterCallback(self, "OnProfileChanged", "Reset")
        self.db.RegisterCallback(self, "OnProfileCopied", "Reset")
        self.db.RegisterCallback(self, "OnProfileReset", "Reset")
end

function plugin:PluginEnable()
        if not db.PluginEnabled then
                self:Disable()
                return
        end

        self:RegisterCallback("ConfigMode")
        
        self:RegisterCallback("WarningShow")
        
        Party = RW.Party
end

function plugin:PluginDisable()
        self:UnregisterAllCallbacks()
end

-----------------------------------
-- Anchor

local function dragStart(self)
        self:StartMoving()
end
local function dragStop(self)

        self:StopMovingOrSizing()
        local scale = self:GetEffectiveScale() / UIParent:GetEffectiveScale()
        
        local x, y = self:GetCenter()
        x, y = x * scale, y * scale
        
        x = x - GetScreenWidth()/2
        y = y - GetScreenHeight()/2
        
        db.x = x
        db.y = y
        
        RW.Callbacks:Fire("RefreshOptions")
end

local function getAnchor()
        if plugin.anchor then return plugin.anchor end
        local frame = Utils:CreateAnchor(plugin:GetName(), UIParent, "Warning frame")
        frame:SetScript("OnDragStart", dragStart)
        frame:SetScript("OnDragStop", dragStop)
        
        plugin.anchor = frame           
        
        plugin:Reset()
        
        return frame
end

local function getWarningFrame()
        if plugin.warningFrame then return plugin.warningFrame end
        local db = plugin.db.profile
        local anchor = getAnchor()
        
        local frame = CreateFrame("Frame", "RW_WarningFrame")
        frame:SetPoint("BOTTOM", anchor, "TOP")
        frame:SetWidth(db.Width)
        frame:SetHeight(1)
        
        local text = frame:CreateFontString(frame:GetName().."String", "ARTWORK")
        text:SetPoint("BOTTOM", frame, "TOP")
        
        local icon = frame:CreateTexture(frame:GetName().."Icon", "OVERLAY")
        icon:SetPoint("RIGHT", text, "LEFT", -10, 0)
        icon:SetTexCoord(0.07, 0.93, 0.07, 0.93)
        
        local iconback = frame:CreateTexture(nil, "ARTOWRK")
        iconback:SetTexture(0, 0, 0, 1)
        iconback:SetPoint("TOPLEFT", icon, -3, 3)
        iconback:SetPoint("BOTTOMRIGHT", icon, 3, -3)
        
        frame:SetScript("OnUpdate", function(self, elapsed)
                self.timer = self.timer - elapsed
                if self.timer >= 3 and self.timer <= 4 then
                        LowHealthFrame:SetAlpha(self.timer - 3)
                elseif self.timer <= 0.1 then
                        frame:Hide()
                elseif self.timer <= 2 then
                        frame:SetAlpha(self.timer/2)
                end
        end)
        
        frame.text = text
        frame.icon = icon
        plugin.warningFrame = frame
        plugin:ApplySettings()
        return frame
end

-----------------------------------
-- Warning functions

-- Reset position
function plugin:Reset()
        db = self.db.profile
        if self.anchor then
                local frame = self.anchor
                local scale = frame:GetEffectiveScale() / UIParent:GetEffectiveScale()
                frame:ClearAllPoints()
                frame:SetPoint("CENTER", UIParent, "CENTER", db.x / scale, db.y / scale)                
                frame:SetWidth(db.Width)
                
                plugin:ApplySettings()
        end
end

-- Apply the visual settings
function plugin:ApplySettings()
        if not plugin.warningFrame then return end
        local frame = getWarningFrame()
        
        frame.text:SetTextHeight(1)
        frame.text:SetFont(LSM:Fetch("font", db.Font), db.DefaultSize, "OUTLINE")
        frame.text:SetText(frame.text:GetText() or "T")
        frame.text:SetTextColor(unpack(db.DefaultColor))
        frame.icon:SetWidth(frame.text:GetStringHeight())
        frame.icon:SetHeight(frame.text:GetStringHeight())
end

--- Show a warning message
-- @param text The text to shows 
-- @param icon Optional icon to the left of the text
-- @param color Optional color for the warning, else it will be the default color
function plugin:WarningShow(event, text, icon, color, sound, flash)
        local frame = getWarningFrame()
        local c = color or self.db.profile.DefaultColor
        frame.text:SetText(text)
        frame.text:SetTextColor(unpack(c))
        frame.icon:SetTexture(icon)
        frame:Show()
        frame:SetAlpha(1)
        frame.timer = 5
        
        if db.PlaySound then
                local soundfile = LSM:Fetch("sound", sound)
                if soundfile == "Interface\\Quiet.mp3" then soundfile = sound or LSM:Fetch("sound", db.DefaultSound) or soundfile end
                PlaySoundFile(soundfile)
        end
        if db.UseFlash and flash then
                RW.Callbacks:Fire("FlashShow", color)
        end
end

function plugin:ConfigMode(event, start)
        if start then
                getAnchor():Show()
                getWarningFrame():Show()
                getWarningFrame():SetAlpha(1)
                getWarningFrame().timer = 11111111
                getWarningFrame().text:SetText("Warning Frame")
        else
                getAnchor():Hide()
                getWarningFrame():Hide()
                getWarningFrame().timer = 0
        end
end

function plugin:GetOptions()
        return LB.PLUGINS, LB.ALERT_PLUGINS, L.NAME, self.GetGUI, [[Interface\Icons\INV_Misc_ClothScrap_02]]
end

local function changeCallback()
        plugin:Reset()
end

function plugin:GetGUI()
        local self = self or plugin
        local UIF = LibStub("LibGUIFactory-1.0"):GetFactory("RW")
        local AceGUI = LibStub("AceGUI-3.0")
        
        local group, label, slider, check, igroup, button, color, select
        
        group = AceGUI:Create("ScrollFrame")
        group:SetLayout("Flow")
        
        label = UIF:MainTitleLabel(L.NAME)
        group:AddChild(label)
        
        igroup = UIF:InlineGroup("")
        group:AddChild(igroup)  
        
        label = UIF:NormalText(L.DESC)
        label:SetImage([[Interface\Icons\INV_Misc_ClothScrap_02]])
        igroup:AddChild(label)
        
        igroup = UIF:InlineGroup("")
        group:AddChild(igroup)  
        
        check = UIF:CheckBox(LB.ENABLED, db, "PluginEnabled", function() 
                if not db.PluginEnabled then
                        self:Disable()
                else
                        self:Enable()
                end
        end)
        igroup:AddChild(check)
        
        check = UIF:CheckBox(L.FLASH, db, "UseFlash", changeCallback)
        igroup:AddChild(check)
        
        check = UIF:CheckBox(LB.PLAY_SOUND, db, "PlaySound", changeCallback)
        check:SetFullWidth(false)
        igroup:AddChild(check)
        
        select = AceGUI:Create("LSM30_Sound")
        select:SetLabel(LB.DEFAULT_SOUND)
        select:SetList()
        select:SetValue(db["DefaultSound"])
        select:SetUserData("db", db)
        select:SetUserData("key", "DefaultSound")
        select:SetCallback("OnValueChanged", function(widget, event, value)
                db["DefaultSound"] = value
        end)
        igroup:AddChild(select)
        
        color = UIF:ColorSelect(LB.DEFAULT_COLOR, db, "DefaultColor", changeCallback)
        igroup:AddChild(color)
        
        slider = UIF:Slider(LB.FONT_SIZE, db, "DefaultSize", 5, 100, 1, changeCallback)
        igroup:AddChild(slider)
        
        local sizeGroup = UIF:InlineGroup2(LB.POSITION)
        igroup:AddChild(sizeGroup)
        
        slider = UIF:Slider("X", db, "x", 
                -floor(GetScreenWidth() * UIParent:GetEffectiveScale()),
                floor(GetScreenWidth() * UIParent:GetEffectiveScale()),
                1,
                changeCallback)
        sizeGroup:AddChild(slider)
        
        slider = UIF:Slider("Y", db, "y", 
                -floor(GetScreenHeight() * UIParent:GetEffectiveScale()),
                floor(GetScreenHeight() * UIParent:GetEffectiveScale()),
                1,
                changeCallback)
        sizeGroup:AddChild(slider)
        
        button = UIF:Button(LB.TEST, function() 
                local spellName, rank, icon = Utils:GetRandomSpell()
                RW.Callbacks:Fire("WarningShow", spellName ..(rank ~= "" and (" ("..rank..")" ) or "" ), icon, nil, nil, true) 
        end)
        group:AddChild(button)
                
        button = UIF:Button(LB.RESET, function() 
                plugin.db:ResetProfile() 
        end)
        button:SetUserData("confirm", true)
        button:SetUserData("TriggerGlobalUpdate", true)
        group:AddChild(button)
        
        group:FixScroll()
        
        return group
end

Compare with Previous | Blame