local addon, Soundwave = ... |
local BUTTONS, POOL = {}, {} |
|
local function findKey(id) |
if not SoundwaveDB then return false end |
for k,v in pairs(SoundwaveDB) do |
if v == id then |
return true |
end |
end |
return false |
end |
|
local function removeKey(id) |
for k,v in pairs(SoundwaveDB) do |
if v == id then |
table.remove(SoundwaveDB, k) |
end |
end |
end |
|
local function clearButtons() |
if #BUTTONS == 0 then return end |
for i=1, #BUTTONS do |
local t = table.remove(BUTTONS) |
table.insert(POOL, t) |
t:ClearAllPoints() |
t:Hide() |
end |
end |
|
local row = 7 |
local size = 36 |
local function CreateButton(parent) |
if #POOL > 0 then return table.remove(POOL, 1) end |
|
local cb = CreateFrame("Button", nil, parent) |
cb:SetSize(size, size) |
cb:RegisterForClicks("anyUp") |
cb:EnableMouse(true) |
cb:Hide() |
|
local pushed = cb:CreateTexture() |
pushed:SetTexture("Interface\\Buttons\\UI-Quickslot-Depress") |
pushed:SetAllPoints(cb) |
cb:SetPushedTexture(pushed) |
|
local highlight = cb:CreateTexture() |
highlight:SetTexture("Interface\\Buttons\\ButtonHilight-Square") |
highlight:SetAllPoints(cb) |
cb:SetHighlightTexture(highlight) |
|
local icon = cb:CreateTexture() |
icon:SetAllPoints(cb) |
icon:SetTexCoord(0.03, 0.97, 0.03, 0.97) |
cb.icon = icon |
|
cb:SetScript("OnEnter", function(self) |
GameTooltip:SetOwner(self, "ANCHOR_RIGHT", self:GetWidth()/-2, 5) |
GameTooltip:SetSpellByID(self.id) |
GameTooltip:AddLine("\nClick to add or remove.", 0, 1, 0) |
GameTooltip:AddLine("Shift-click to summon (won't add pet to set).", 0, 1, 0, true) |
GameTooltip:Show() |
end) |
|
cb:SetScript("OnLeave", function(self) GameTooltip:Hide() end) |
|
cb:SetScript("OnClick", function(self) |
if IsShiftKeyDown() then CallCompanion("CRITTER", self.index) return end |
|
if findKey(self.index) then |
removeKey(self.index) |
self.icon:SetDesaturated(true) |
else |
table.insert(SoundwaveDB, self.index) |
self.icon:SetDesaturated(false) |
end |
end) |
|
cb:SetScript("OnShow", function(self) |
self.icon:SetDesaturated(not findKey(self.index)) |
end) |
|
return cb |
end |
|
local f = CreateFrame("Frame", "SoundwaveGUI", UIParent, "ButtonFrameTemplate") |
f:SetPoint("CENTER") |
f:Hide() |
|
f:SetScript("OnShow", function() |
SetPortraitToTexture(f.portrait, "Interface\\AddOns\\Soundwave\\icon") |
f.TitleText:SetText("Soundwave Configuration") |
f:SetMovable(true) |
f:SetClampedToScreen(true) |
|
-- ScrollFrame |
local scroll = CreateFrame("ScrollFrame", "SoundwaveGUIScroll", f, "UIPanelScrollFrameTemplate") |
scroll:SetPoint("TOPLEFT", f.Inset, "TOPLEFT", 0, -10) |
scroll:SetPoint("BOTTOMRIGHT", f.Inset, "BOTTOMRIGHT", -32, 10) |
scroll.scrollBarHideable = true |
scroll.ScrollBar:Hide() |
|
local container = CreateFrame("Frame", nil, scroll) |
container:SetSize(1,1) |
scroll:SetScrollChild(container) |
container:SetPoint("TOPLEFT", scroll, "TOPLEFT") |
|
local function updatePets() |
for i=1, GetNumCompanions("CRITTER") do |
local spellID, icon = select(3, GetCompanionInfo("CRITTER", i)) |
local button = CreateButton(container) |
button.index = i |
button.id = spellID |
button.icon:SetTexture(icon) |
table.insert(BUTTONS, button) |
end |
|
for i=1, #BUTTONS do |
local b = BUTTONS[i] |
if i == 1 then |
b:SetPoint("TOPLEFT", container, "TOPLEFT", 10, 0) |
else |
if mod(i-1, row) == 0 then |
b:SetPoint("TOPLEFT", BUTTONS[i-row], "BOTTOMLEFT", 0, -5) |
else |
b:SetPoint("LEFT", BUTTONS[i-1], "RIGHT", 5, 0) |
end |
end |
b:Show() |
end |
end |
|
local clear = CreateFrame("Button", nil, f, "UIPanelButtonTemplate") |
clear:SetSize(80, 22) |
clear:SetText("Reset") |
clear:SetPoint("TOP", f.Inset, "BOTTOM", 0, 0) |
|
clear:SetScript("OnClick", function() |
if #SoundwaveDB == 0 then return end |
wipe(SoundwaveDB) |
|
clearButtons() |
updatePets() |
end) |
|
local mover = CreateFrame("Frame", nil, f) |
mover:SetAllPoints(f.topBorderBar) |
mover:EnableMouse(true) |
mover:SetScript("OnMouseDown", function() |
f:ClearAllPoints() |
f:StartMoving() |
end) |
|
mover:SetScript("OnMouseUp", function() |
f:StopMovingOrSizing() |
end) |
|
table.insert(UISpecialFrames, "SoundwaveGUI") |
|
updatePets() |
f:SetScript("OnShow", updatePets) |
end) |
|
f:SetScript("OnHide", function() clearButtons() Soundwave:COMPANION_UPDATE("CRITTER") end) |
Soundwave.GUI = f |