Advertisement
Guest User

NoTroll v2

a guest
Jul 20th, 2014
220
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 14.73 KB | None | 0 0
  1. local ADDON, private = ...
  2. local f = CreateFrame("Frame", ADDON, InterfaceOptionsFramePanelContainer)
  3.  
  4. local db = { -- default values
  5.     [2825]   = "RAID", -- Bloodlust
  6.     [32182]  = "RAID", -- Heroism
  7.     [80353]  = "RAID", -- Time Warp
  8.     [13159]  = "RAID", -- Aspect of Fail
  9.     [41450]  = "RAID", -- Blessing of Piss me off
  10.     [73325]  = "RAID", -- Leap of Fail
  11.     [34477]  = "RAID", -- MD
  12.     [20736]  = "RAID", -- Distracting Shot
  13.     [62124]  = "RAID", -- Reckoning * paladin
  14.     [56222]  = "RAID", -- Dark Command
  15.     [49576]  = "RAID", -- Death Grip
  16.     [2649]   = "RAID", -- Generic hunter growl
  17.     [39270]  = "RAID", -- main target growl
  18.     [103128] = "RAID", -- suffering
  19. }
  20.  
  21. local L = {
  22.     ANNOUNCE_MESSAGE = "%1$s used %2$s!",
  23.  
  24.     ADD_SPELL = "Add Spell",
  25.     ADD_SPELL_DESC = "Use this dialog to add a new spell to be announced.",
  26.     ADD_SPELL_HELP = "Enter the numeric ID of the spell you want to add. If you don't know the ID, look up the spell on Wowhead.com and copy the number out of the spell page URL.",
  27.     ADD_SPELL_INVALID = "That is not a valid spell ID!",
  28.     DELETE_SPELL_DESC = "Permanently remove this spell from this list. If you only want to disable it temporarily, change the channel to NONE instead.",
  29. }
  30.  
  31. local ChannelNames = {
  32.     NONE = NONE,
  33.     SELF = PLAYER,
  34.     SAY = CHAT_MSG_SAY,
  35.     PARTY = CHAT_MSG_PARTY,
  36.     RAID = CHAT_MSG_RAID,
  37. }
  38.  
  39. local orderedChannels = { "NONE", "SELF", "SAY", "PARTY", "RAID" }
  40.  
  41. -------------------------------------------------------------------------
  42. --  Event handling
  43.  
  44. f:RegisterEvent("PLAYER_LOGIN")
  45. f:SetScript("OnEvent", function(self, event)
  46.     NoTrollDB = NoTrollDB or db
  47.     db = NoTrollDB
  48.  
  49.     for id in pairs(db) do
  50.         if not GetSpellInfo(id) then
  51.             print("Invalid spell in db:", id)
  52.         end
  53.     end
  54.  
  55.     self:UnregisterEvent("PLAYER_LOGIN")
  56.     self:RegisterEvent("UNIT_SPELLCAST_SUCCEEDED")
  57.  
  58.     self:SetScript("OnEvent", function(self, event, unit, _, _, _, spellID)
  59.         local channel = db[spellID]
  60.         if channel then
  61.             self:Announce(spellID, unit, channel)
  62.         end
  63.     end)
  64. end)
  65.  
  66. -------------------------------------------------------------------------
  67. --  Announce logic
  68.  
  69. function f:Announce(spellID, unit, channel)
  70.     if channel == "NONE" then return end
  71.  
  72.     local spellLink = GetSpellLink(spellID)
  73.     local playerName = GetUnitName(unit, true)
  74.  
  75.     if channel == "SELF" then
  76.         -- Clickable player links in self messages
  77.         local playerLink = "|Hplayer:"..playerName.."|h["..playerName.."]|h"
  78.         print("|cffffff9a[NoTroll]|r", format(L.ANNOUNCE_MESSAGE, playerLink, spellLink))
  79.     else
  80.         -- Can't send player links to channels
  81.         SendChatMessage(format(L.ANNOUNCE_MESSAGE, playerName, spellLink), channel)
  82.     end
  83. end
  84.  
  85. -------------------------------------------------------------------------
  86. -- Make player names in channel messages from the addon clickable
  87.  
  88. local ANNOUNCE_PATTERN = L.ANNOUNCE_MESSAGE:gsub("%%1$s", "(.+)"):gsub("%%2$s", ".+")
  89.  
  90. function f:AddLocalMessageLinks(_, message, ...)
  91.     local name = strmatch(message, ANNOUNCE_PATTERN)
  92.     if name then
  93.         local char = strmatch(playerName, "^[^%s%-]+")
  94.         local class = UnitClass(char)
  95.         local link = "|Hplayer:"..char.."|h["..name.."]|h"
  96.         message = gsub(message, name, link)
  97.         return false, message, ...
  98.     end
  99. end
  100.  
  101. ChatFrame_AddMessageEventFilter("CHAT_MSG_PARTY", f.AddLocalMessageLinks)
  102. ChatFrame_AddMessageEventFilter("CHAT_MSG_RAID", f.AddLocalMessageLinks)
  103. ChatFrame_AddMessageEventFilter("CHAT_MSG_SAY", f.AddLocalMessageLinks)
  104.  
  105. -------------------------------------------------------------------------
  106. -- Options panel
  107.  
  108. f:Hide()
  109. f:SetScript("OnShow", function(self)
  110.     local title = self:CreateFontString(nil, "ARTWORK", "GameFontNormalLarge")
  111.     title:SetPoint("TOPLEFT", 16, -16)
  112.     title:SetPoint("TOPRIGHT", -32, -16)
  113.     title:SetJustifyH("LEFT")
  114.     title:SetText(self.name)
  115.     self.Title = Title
  116.  
  117.     local notes = self:CreateFontString(nil, "ARTWORK", "GameFontHighlightSmall")
  118.     notes:SetPoint("TOPLEFT", title, "BOTTOMLEFT", 0, -8)
  119.     notes:SetPoint("TOPRIGHT", title, "BOTTOMRIGHT", 0, -8)
  120.     notes:SetHeight(32)
  121.     notes:SetJustifyH("LEFT")
  122.     notes:SetJustifyV("TOP")
  123.     notes:SetText(GetAddOnMetadata(ADDON, "Notes"))
  124.     self.Notes = Notes
  125.  
  126.     local scrollFrame = CreateFrame("ScrollFrame", "$parentScrollFrame", self, "UIPanelScrollFrameTemplate")
  127.     scrollFrame:SetPoint("TOPLEFT", notes, "BOTTOMLEFT", 0, -16)
  128.     scrollFrame:SetPoint("BOTTOMRIGHT", -38, 16)
  129.     self.ScrollFrame = scrollFrame
  130.  
  131.     scrollFrame.ScrollBar:EnableMouseWheel(true)
  132.     scrollFrame.ScrollBar:SetScript("OnMouseWheel", function(self, delta)
  133.         ScrollFrameTemplate_OnMouseWheel(scrollFrame, delta)
  134.     end)
  135.  
  136.     local barBG = scrollFrame:CreateTexture(nil, "BACKGROUND", nil, -6)
  137.     barBG:SetPoint("TOP")
  138.     barBG:SetPoint("RIGHT", 25, 0)
  139.     barBG:SetPoint("BOTTOM")
  140.     barBG:SetWidth(26)
  141.     barBG:SetTexture("Interface\\PaperDollInfoFrame\\UI-Character-ScrollBar")
  142.     barBG:SetTexCoord(0, 0.45, 0.1640625, 1)
  143.     barBG:SetAlpha(0.5)
  144.     scrollFrame.ScrollBar.bg = barBG
  145.  
  146.     local scrollChild = CreateFrame("Frame", nil, scrollFrame)
  147.     scrollChild:SetSize(scrollFrame:GetWidth(), 100)
  148.     scrollFrame.ScrollChild = scrollChild
  149.  
  150.     scrollFrame:SetScrollChild(scrollChild)
  151.     scrollFrame:SetScript("OnSizeChanged", function(self)
  152.         scrollChild:SetWidth(self:GetWidth())
  153.     end)
  154.  
  155.     --------------------------------------------------------------------
  156.  
  157.     local dialog = CreateFrame("Frame", nil, self)
  158.     dialog:SetPoint("TOPLEFT", notes, "BOTTOMLEFT", 0, -16)
  159.     dialog:SetPoint("BOTTOMRIGHT", -16, 16)
  160.     dialog:SetBackdrop({ edgeFile = "Interface\\Tooltips\\UI-Tooltip-Border", edgeSize = 16 })
  161.     dialog:SetBackdropBorderColor(1, 0.82, 0, 0.8)
  162.     self.Dialog = dialog
  163.  
  164.     dialog.bg = dialog:CreateTexture(nil, "BACKGROUND")
  165.     dialog.bg:SetPoint("BOTTOMLEFT", 3, 3)
  166.     dialog.bg:SetPoint("TOPRIGHT", -3, -3)
  167.     dialog.bg:SetTexture("Interface\\FrameGeneral\\UI-Background-Rock", true, true)
  168.     dialog.bg:SetHorizTile(true)
  169.     dialog.bg:SetVertTile(true)
  170.     dialog.bg:SetVertexColor(0.4, 0.4, 0.4, 0.8)
  171.  
  172.     dialog:Hide()
  173.     dialog:SetScript("OnShow", function(self)
  174.         self:GetParent().ScrollFrame:Hide()
  175.         self.EditBox:SetText("")
  176.         self.Text:SetText("")
  177.         self.EditBox:SetFocus()
  178.     end)
  179.     dialog:SetScript("OnHide", function(self)
  180.         self:GetParent().ScrollFrame:Show()
  181.         self.EditBox:SetText("")
  182.         self.Text:SetText("")
  183.         self:GetParent().refresh()
  184.     end)
  185.  
  186.     local dialogTitle = dialog:CreateFontString(nil, "ARTWORK", "GameFontNormalLarge")
  187.     dialogTitle:SetPoint("TOPLEFT", 16, -16)
  188.     dialogTitle:SetText(L.ADD_SPELL)
  189.     dialog.Title = dialogTitle
  190.  
  191.     local dialogNotes = dialog:CreateFontString(nil, "ARTWORK", "GameFontHighlightSmall")
  192.     dialogNotes:SetPoint("TOPLEFT", dialogTitle, "BOTTOMLEFT", 0, -8)
  193.     dialogNotes:SetPoint("RIGHT", -32, 0)
  194.     dialogNotes:SetHeight(16)
  195.     dialogNotes:SetJustifyH("LEFT")
  196.     dialogNotes:SetJustifyV("TOP")
  197.     dialogNotes:SetText(L.ADD_SPELL_DESC)
  198.  
  199.     local dialogBox = CreateFrame("EditBox", "$parentEditBox", dialog, "InputBoxTemplate")
  200.     dialogBox:SetPoint("TOPLEFT", dialogNotes, "BOTTOMLEFT", 6, -12)
  201.     dialogBox:SetSize(160, 24)
  202.     dialogBox:SetAltArrowKeyMode(false)
  203.     dialogBox:SetAutoFocus(false)
  204.     dialogBox:SetMaxLetters(6)
  205.     dialogBox:SetNumeric(true)
  206.     dialog.EditBox = dialogBox
  207.  
  208.     dialogBox:SetScript("OnTextChanged", function(self, userInput)
  209.         if not userInput then return end
  210.  
  211.         local spell = self:GetNumber()
  212.         if spell == 0 then
  213.             dialog.Text:SetText("")
  214.             dialog.Button:SetEnabled(false)
  215.             return
  216.         end
  217.  
  218.         local name, _, icon = GetSpellInfo(spell)
  219.         if name and icon then
  220.             dialog.Text:SetFormattedText("|T%s:0|t %s", icon, name)
  221.             dialog.Button:SetEnabled(true)
  222.         else
  223.             dialog.Text:SetText(RED_FONT_COLOR_CODE .. L.ADD_SPELL_INVALID .. "|r")
  224.             dialog.Button:Disable()
  225.         end
  226.     end)
  227.  
  228.     dialogBox:SetScript("OnEnterPressed", function(self)
  229.         if not dialog.Button:IsEnabled() then return end
  230.         local id = self:GetNumber()
  231.         if id and id > 0 and GetSpellInfo(id) then
  232.             db[id] = "RAID"
  233.             dialog:Hide()
  234.         end
  235.     end)
  236.  
  237.     local dialogButton = CreateFrame("Button", "$parentAddButton", dialog, "UIPanelButtonTemplate")
  238.     dialogButton:SetPoint("LEFT", dialogBox, "RIGHT", 12, 0)
  239.     dialogButton:SetWidth(80)
  240.     dialogButton:SetText(OKAY)
  241.     dialogButton:SetScript("OnClick", function()
  242.         dialogBox:GetScript("OnEnterPressed")(dialogBox)
  243.     end)
  244.     dialog.Button = dialogButton
  245.  
  246.     local dialogText = dialog:CreateFontString(nil, "OVERLAY", "GameFontHighlight")
  247.     dialogText:SetPoint("LEFT", dialogButton, "RIGHT", 12, 0)
  248.     dialogText:SetPoint("RIGHT", dialog, -16, 0)
  249.     dialogText:SetJustifyH("LEFT")
  250.     dialogText:SetJustifyV("TOP")
  251.     dialogText:SetText("")
  252.     dialog.Text = dialogText
  253.  
  254.     local dialogHelp = dialog:CreateFontString(nil, "ARTWORK", "GameFontHighlightSmall")
  255.     dialogHelp:SetPoint("TOPLEFT", dialogBox, "BOTTOMLEFT", -6, -16)
  256.     dialogHelp:SetPoint("RIGHT", dialog, -16, 0)
  257.     dialogHelp:SetHeight(32)
  258.     dialogHelp:SetJustifyH("LEFT")
  259.     dialogHelp:SetJustifyV("TOP")
  260.     dialogHelp:SetNonSpaceWrap(true)
  261.     dialogHelp:SetText(L.ADD_SPELL_HELP)
  262.     dialog.HelpText = dialogHelp
  263.  
  264.     --------------------------------------------------------------------
  265.  
  266.     local add = CreateFrame("Button", "$parentPanelButton", self, "UIPanelButtonTemplate")
  267.     add:SetPoint("TOPRIGHT", title, "BOTTOMRIGHT", 0, -8)
  268.     add:SetSize(160, 32)
  269.     add:SetText("|TInterface\\LFGFRAME\\LFGROLE_BW:0:0:0:0:64:16:48:64:0:16:255:255:153|t " .. L.ADD_SPELL)
  270.     self.AddButton = add
  271.  
  272.     notes:SetPoint("TOPRIGHT", add, "TOPLEFT", -24, 0)
  273.  
  274.     add:SetScript("OnClick", function(self)
  275.         if dialog:IsShown() then
  276.             dialog:Hide()
  277.             self:SetText("|TInterface\\LFGFRAME\\LFGROLE_BW:0:0:0:0:64:16:48:64:0:16:255:255:153|t " .. L.ADD_SPELL)
  278.         else
  279.             dialog:Show()
  280.             self:SetText(CANCEL)
  281.         end
  282.     end)
  283.  
  284.     --------------------------------------------------------------------
  285.  
  286.     local function Row_OnEnter(self)
  287.         self.name:SetTextColor(1, 1, 1)
  288.         self.highlight:Show()
  289.         GameTooltip:SetOwner(self, "ANCHOR_NONE")
  290.         GameTooltip:SetPoint("TOPRIGHT", self, "TOPLEFT")
  291.         GameTooltip:SetSpellByID(self.id)
  292.         GameTooltip:Show()
  293.     end
  294.     local function Row_OnLeave(self)
  295.         self.name:SetTextColor(1, 0.82, 0)
  296.         self.highlight:Hide()
  297.         GameTooltip:Hide()
  298.     end
  299.     local function Row_OnHide(self)
  300.         self.id = 0
  301.         self.icon:SetTexture(nil)
  302.         self.name:SetText("")
  303.         self.channel.valueText:SetText("")
  304.     end
  305.  
  306.     -- Delete button functions:
  307.     local function Delete_OnClick(self)
  308.         if GameTooltip:IsOwned(self) then
  309.             GameTooltip:Hide()
  310.         end
  311.         local id = self:GetParent().id
  312.         db[id] = nil
  313.         self.refresh()
  314.     end
  315.     local function Delete_OnEnter(self)
  316.         GameTooltip:SetOwner(self, "ANCHOR_LEFT")
  317.         GameTooltip:SetText(L.DELETE_SPELL_DESC, nil, nil, nil, true)
  318.         GameTooltip:Show()
  319.     end
  320.  
  321.     -- Filter dropdown functions:
  322.     local CURRENT_SPELL, CURRENT_DROPDOWN
  323.     local function Dropdown_ClickFunc(info)
  324.         db[CURRENT_SPELL] = info.value
  325.         CURRENT_DROPDOWN:SetValue(info.value, ChannelNames[info.value])
  326.     end
  327.     local function Dropdown_Initialize(self, level) -- self is the dropdown menu
  328.         if not level then return end
  329.         CURRENT_DROPDOWN = self:GetParent()
  330.         CURRENT_SPELL = CURRENT_DROPDOWN:GetParent().id
  331.         local selected = db[CURRENT_SPELL]
  332.         local info = UIDropDownMenu_CreateInfo()
  333.         for i = 1, #orderedChannels do
  334.             local k = orderedChannels[i]
  335.             info.text = ChannelNames[k]
  336.             info.value = k
  337.             info.checked = k == selected
  338.             info.func = Dropdown_ClickFunc
  339.             UIDropDownMenu_AddButton(info, level)
  340.         end
  341.     end
  342.     local function Dropdown_OnEnter(self)
  343.         Row_OnEnter(self:GetParent())
  344.     end
  345.     local function Dropdown_OnLeave(self)
  346.         Row_OnLeave(self:GetParent())
  347.     end
  348.  
  349.     -- Rows:
  350.     local rows = setmetatable({}, { __index = function(t, i)
  351.         local row = CreateFrame("Frame", nil, scrollChild)
  352.         row:SetHeight(40)
  353.         row:SetPoint("LEFT")
  354.         row:SetPoint("RIGHT")
  355.         if i > 1 then
  356.             row:SetPoint("TOP", t[i-1], "BOTTOM", 0, -4)
  357.         else
  358.             row:SetPoint("TOP")
  359.         end
  360.  
  361.         row:EnableMouse(true)
  362.         row:SetScript("OnEnter", Row_OnEnter)
  363.         row:SetScript("OnLeave", Row_OnLeave)
  364.  
  365.         row:EnableMouseWheel(true)
  366.         row:SetScript("OnMouseWheel", function(self, delta)
  367.             ScrollFrameTemplate_OnMouseWheel(scrollFrame, delta)
  368.         end)
  369.  
  370.         local highlight = row:CreateTexture(nil, "BACKGROUND")
  371.         highlight:SetAllPoints(true)
  372.         highlight:SetBlendMode("ADD")
  373.         highlight:SetTexture([[Interface\QuestFrame\UI-QuestLogTitleHighlight]])
  374.         highlight:SetVertexColor(0.2, 0.4, 0.8)
  375.         highlight:Hide()
  376.         row.highlight = highlight
  377.  
  378.         local delete = CreateFrame("Button", "$parentDelete"..i, row, "UIPanelCloseButton")
  379.         delete:SetPoint("LEFT", -2, -1)
  380.         delete:SetSize(32, 32)
  381.         delete:SetScript("OnClick", Delete_OnClick)
  382.         delete:SetScript("OnEnter", Delete_OnEnter)
  383.         delete:SetScript("OnLeave", GameTooltip_Hide)
  384.         row.delete = delete
  385.  
  386.         local icon = row:CreateTexture(nil, "ARTWORK")
  387.         icon:SetPoint("LEFT", delete, "RIGHT", 2, 0)
  388.         icon:SetSize(32, 32)
  389.         icon:SetTexCoord(0.1, 0.9, 0.1, 0.9)
  390.         row.icon = icon
  391.  
  392.         local name = row:CreateFontString(nil, "ARTWORK", "GameFontNormalLarge")
  393.         name:SetPoint("LEFT", icon, "RIGHT", 8, 0)
  394.         row.name = name
  395.  
  396.         local channel = LibStub("PhanxConfig-Dropdown"):New(row, nil, nil, Dropdown_Initialize)
  397.         channel:SetPoint("RIGHT", 0, 5)
  398.         channel:SetWidth(200)
  399.         channel.OnEnter = Dropdown_OnEnter
  400.         channel.OnLeave = Dropdown_OnLeave
  401.         row.channel = channel
  402.  
  403.         t[i] = row
  404.         return row
  405.     end })
  406.     self.rows = rows
  407.  
  408.     --------------------------------------------------------------------
  409.  
  410.     -- OnShow for self:
  411.     local pool = {}
  412.     local sortedSpells = {}
  413.     local sortFunc = function(a, b)
  414.         return a.name < b.name or (a.name == b.name and a.id < b.id)
  415.     end
  416.     self.refresh = function()
  417.         for i = #sortedSpells, 1, -1 do
  418.             pool[tremove(sortedSpells, i)] = true
  419.         end
  420.  
  421.         for id, channel in pairs(db) do
  422.             local name, _, icon = GetSpellInfo(id)
  423.             if name and icon then
  424.                 local spell = next(pool) or {}
  425.                 pool[spell] = nil
  426.                 spell.name = name
  427.                 spell.icon = icon
  428.                 spell.id = id
  429.                 spell.channel = channel
  430.                 tinsert(sortedSpells, spell)
  431.             end
  432.         end
  433.         sort(sortedSpells, sortFunc)
  434.  
  435.         if #sortedSpells > 0 then
  436.             local height = 0
  437.             for i = 1, #sortedSpells do
  438.                 local spell = sortedSpells[i]
  439.                 local row = rows[i]
  440.                 row.id = spell.id
  441.                 row.name:SetText(spell.name)
  442.                 row.icon:SetTexture(spell.icon)
  443.                 row.channel:SetValue(spell.channel, ChannelNames[spell.channel])
  444.                 row:Show()
  445.                 height = height + 4 + row:GetHeight()
  446.             end
  447.             for i = #sortedSpells + 1, #rows do
  448.                 rows[i]:Hide()
  449.             end
  450.             scrollChild:SetHeight(height)
  451.             add:Show()
  452.         else
  453.             for i = 1, #rows do
  454.                 rows[i]:Hide()
  455.             end
  456.             scrollChild:SetHeight(100)
  457.             dialog:Show()
  458.             add:Hide()
  459.         end
  460.     end
  461.  
  462.     self:SetScript("OnShow", nil)
  463.     self.refresh()
  464. end)
  465.  
  466. f.name = GetAddOnMetadata(ADDON, "Title")
  467. InterfaceOptions_AddCategory(f)
  468.  
  469. _G["SLASH_"..ADDON.."1"] = "/notroll"
  470. SlashCmdList[ADDON] = function()
  471.     InterfaceOptionsFrame_OpenToCategory(f)
  472.     InterfaceOptionsFrame_OpenToCategory(f)
  473. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement