WoWInterface SVN Broker_BubbleToggle

[/] [Broker_BubbleToggle.lua] - Rev 3

Compare with Previous | Blame | View Log

local addonName = ...
local L = LibStub("AceLocale-3.0"):GetLocale(addonName, true)
local db
local dataobj
local ShowKeybindFrame -- local function, forward declaration
local frame -- first used for ADDON_LOADED, then can be recycled for keybinding


-- Print with colored prefix.
local printPrefix = "|cff33ff99"..addonName.."|r:"
local function print(...)
        _G.print(printPrefix, ...)
end


-- Globals for bindings
BINDING_HEADER_BROKER_BUBBLETOGGLE = addonName
BINDING_NAME_BROKER_BUBBLETOGGLE_TOGGLE = L["Toggle Chat Bubbles"]


-- Global. Used in Bindings.
function Broker_BubbleToggle_Toggle()
        SetCVar("ChatBubbles", 1 - GetCVar("ChatBubbles"))
        UpdateDataObject()      
        if GameTooltip:IsShown() then
                GameTooltip:Hide()
        end
end


-- Updates aspects of the dataobj that change when we change settings.
function UpdateDataObject()
        if db.showText then
                dataobj.type = "data source"
        else
                dataobj.type = "launcher"
        end
        if GetCVarBool("ChatBubbles") then
                dataobj.text = L["Chat Bubbles"]
                dataobj.icon = "Interface\\Addons\\Broker_BubbleToggle\\images\\green.tga"
                dataobj.modeDescription = "Chat Bubbles"
                dataobj.otherModeDescription = L["No Bubbles"]
        else
                dataobj.text = L["No Bubbles"]
                dataobj.icon = "Interface\\Addons\\Broker_BubbleToggle\\images\\red.tga"
                dataobj.modeDescription = L["No Bubbles"]
                dataobj.otherModeDescription = "Chat Bubbles"
        end
end


-- Init. Use frame to wait for ADDON_LOADED.
frame = CreateFrame("Frame")
frame:SetScript("OnEvent",  function(self, event, name)
        if name ~= addonName then return end
        frame:UnregisterEvent("ADDON_LOADED")
        frame:SetScript("OnEvent", nil)

        Broker_BubbleToggleDB = Broker_BubbleToggleDB or {}
        db = Broker_BubbleToggleDB
        if db.showText == nil then
                db.showText = true
        end
        
        dataobj = {}
        UpdateDataObject()
        dataobj = LibStub:GetLibrary("LibDataBroker-1.1"):NewDataObject("BubbleToggle", dataobj)
        hooksecurefunc("RestartGx", UpdateDataObject)
        hooksecurefunc("ConsoleExec", function(arg)
                if "gxrestart" == strlower(strtrim(arg)) then
                        UpdateDataObject()
                end
        end)

        function dataobj:OnTooltipShow()
                self:AddLine(dataobj.modeDescription)
                self:AddDoubleLine(L["Left-Click"], dataobj.otherModeDescription)
                self:AddDoubleLine(L["Middle-Click"], (db.showText and L["Hide Text"] or L["Show Text"]))
                self:AddDoubleLine(L["Right-Click"], KEY_BINDING)
        end

        function dataobj:OnClick(button)
                if button == "LeftButton" then                  
                        Broker_BubbleToggle_Toggle()                    
                elseif button == "MiddleButton" then
                        db.showText = not db.showText
                        UpdateDataObject()
                elseif button == "RightButton" then
                        if not InCombatLockdown() then
                                ShowKeybindFrame()
                        else
                                print(L["Can't keybind in combat."])
                        end
                end
        end
end)
frame:RegisterEvent("ADDON_LOADED")
frame:RegisterEvent("PLAYER_LOGIN")


-----------------------------------
-- Keybind Frame
-----------------------------------

-- This can be called many times; it only does extensive init the first time.
function ShowKeybindFrame() -- declared local
        frame:SetFrameStrata("DIALOG")
        frame:EnableKeyboard(true)
        frame:SetClampedToScreen(true)
        frame:Hide()
        frame:SetPoint("CENTER", 0, UIParent:GetHeight() / 7)
        frame:SetBackdrop({
                bgFile=[[Interface\Tooltips\UI-Tooltip-Background]],
                edgeFile=[[Interface\Tooltips\UI-Tooltip-Border]], tile = true, tileSize = 16, edgeSize = 16,
                insets = { left = 4, right = 4, top = 4, bottom = 4 }
        })
        frame:SetBackdropColor(0, 0, 0)

        frame.text = frame:CreateFontString(nil, "ARTWORK", "GameFontNormal")
        frame.text:SetPoint("TOP", 0, -40)
        frame.text:SetText(L[ [=[
Press a key to bind.

Or press ESC to cancel.]=] ])

        frame:SetScript("OnShow", function(self)
                local width = self.text:GetWidth() + 80
                local height = self.text:GetHeight() + 80
                self:SetWidth(width)
                self:SetHeight(height)
                
                self:RegisterEvent("PLAYER_REGEN_DISABLED") -- so we can hide in combat
        end)
        
        frame:SetScript("OnHide", function(self)
                self:UnregisterEvent("PLAYER_REGEN_DISABLED")
        end)
        
        frame:SetScript("OnEvent", function(self, event, ...)
                if event == "PLAYER_REGEN_DISABLED" then
                        print(L["Can't keybind in combat."])
                        self:Hide() -- hide in combat
                end
        end)

        -- These generate OnKeyDown events, but aren't bindable and should be discarded.
        local unbindableKeys = {
                LALT = 1,
                LCTRL = 1,
                LSHIFT = 1,
                RALT = 1,
                RCTRL = 1,
                RSHIFT = 1,
                UNKNOWN = 1,
        }

        frame:SetScript("OnKeyDown", function(self, key)
                if unbindableKeys[key] then
                        return
                end
                if key == "ESCAPE" then
                        self:Hide()
                        return
                end
                if GetBindingFromClick(key) == "SCREENSHOT" then
                        TakeScreenshot()
                        return
                end
                if IsShiftKeyDown() then
                        key = "SHIFT-"..key
                end
                if IsControlKeyDown() then
                        key = "CTRL-"..key
                end
                if IsAltKeyDown() then
                        key = "ALT-"..key
                end

                if SetBinding(key, "BROKER_BUBBLETOGGLE_TOGGLE") then
                        print(KEY_BOUND..": "..key)
                        SaveBindings(GetCurrentBindingSet())
                else
                        print(KEY_BINDING..": "..FAILED)
                end
                self:Hide()
        end)
        
        ShowKeybindFrame = function() frame:Show() end
        frame:Show()
end

Compare with Previous | Blame