function Livestock.BuildMenu(kind) -- type is a string, either "CRITTER", "LAND", "FLYING", or "WATER"
kind = string.upper(kind)
local token = buildtable[kind].token -- buildtable contains references and variables that are specific to each type of menu, indexed by the same string as the variable type
local settings, mounts, critters = LivestockSettings.Mounts
Livestock.RecycleTable(mountstable)
Livestock.RecycleTable(critterstable)
if kind == "CRITTER" then
critters = 0 -- reset critters to 0 so we can start counting critters
settings = LivestockSettings.Critters
for k in orderedPairs(settings) do -- Check the list of critters, increment critters, and add the index of the critters to critterstable
critters = critters + 1
tinsert(critterstable, settings[k].index)
end
elseif kind == "LAND" then
mounts = 0 -- reset mounts to 0 so we can start counting land mounts
for k in orderedPairs(settings) do -- Check the list of mounts, increment mounts, and add the index of the mounts to mountstable
if (settings[k].type == "land") then
mounts = mounts + 1
tinsert(mountstable, settings[k].index)
end
end
elseif kind == "FLYING" then -- code is the same for flying mounts with the appropriate changes; perhaps this could be a function call?
mounts = 0
for k in orderedPairs(settings) do
if (settings[k].type == "flying") then
mounts = mounts + 1
tinsert(mountstable, settings[k].index)
end
end
elseif kind == "WATER" then -- code is the same for water mounts with the appropriate changes; perhaps this could be a function call?
mounts = 0
for k in orderedPairs(settings) do
if (settings[k].type == "water") then
mounts = mounts + 1
tinsert(mountstable, settings[k].index)
end
end
end
local menuframe = _G["Livestock"..token.."Menu"] or CreateFrame("Frame","Livestock"..token.."Menu", LivestockMenuFrame, "LivestockBlueFrameTemplate") -- make the background of the menu (or reference it if it already exists), naming it with the proper token and number
local scrollframe = _G["Livestock"..token.."MenuScrollFrame"] or CreateFrame("ScrollFrame", "Livestock"..token.."MenuScrollFrame", menuframe, "Livestock"..token.."MenuScrollFrameTemplate")
scrollframe:Show()
local header
if kind == "CRITTER" then
header = "LivestockCritterMenuButton"
elseif kind == "LAND" then
header = "LivestockLandMountMenuButton"
elseif kind == "FLYING" then
header = "LivestockFlyingMountMenuButton"
elseif kind == "WATER" then
header = "LivestockWaterMountMenuButton"
end
menuframe:SetPoint("TOPLEFT", header ,"BOTTOMLEFT",0,-10) -- set the first menu to appear below the associated button
if (mounts or critters) <= menuLength then
menuframe:SetHeight((((mounts or critters) + 2) * buttonHeight) + 21)
else
menuframe:SetHeight((menuLength + 2) * buttonHeight + 21)
end
local endButton -- this is the number of buttons in the menu...
if (mounts or critters) <= menuLength then
endButton = (mounts or critters)
else
endButton = menuLength
end
local buttonplusoffset; -- an index into our data calculated from the scroll offset
FauxScrollFrame_Update(scrollframe,(mounts or critters),endButton,15);
DEFAULT_CHAT_FRAME:AddMessage("We're at "..FauxScrollFrame_GetOffset(scrollframe));
for button = 1, endButton do -- iterate over the buttons we need
local menubutton = _G["Livestock"..token.."Menu".."Button"..button] or CreateFrame("Button", "Livestock"..token.."Menu".."Button"..button, menuframe, "LivestockMenuButtonTemplate") -- create or reference the button, with the appropriate name and number
menubutton:Show()
local text = _G["Livestock"..token.."Menu".."Button"..button.."Text"] -- reference the text of the button
buttonplusoffset = button + FauxScrollFrame_GetOffset(scrollframe);
if buttonplusoffset <= (mounts or critters) then
local name, creatureID, _
if kind == "CRITTER" then
_, _, _, _, _, _, _, name, _, _, creatureID = C_PetJournal.GetPetInfoByIndex(critterstable[buttonplusoffset])
elseif kind == "LAND" or kind == "FLYING" or kind == "WATER" then
creatureID, name = GetCompanionInfo("MOUNT", mountstable[buttonplusoffset])
end -- get the ID and name of the companion for the button by mapping the position of the button in the menus(s) to the index of the companion
if name and creatureID then
menubutton.critterID = creatureID
menubutton.which = "toggle"..kind
menubutton.name = name -- associate the info for the companion with the frame itself to be used for mousing over / clicking
if text then
if text.SetText ~= nil then
text:SetText(name)
end
end
end
if name and kind == "LAND" then
local name2 = strconcat(name, "-")
if settings[name2] then
name = name2
end
end
if name and text and settings[name].show == 1 then -- if the name isn't nil (Blizzard bug sometimes returns nil for companion names >:o) and the settings say this companion is selected then
if text.SetTextColor ~= nil then
text:SetTextColor(1, 1, 1) -- set the color of the text to be white
end
elseif name and text then
if text.SetTextColor ~= nil then
text:SetTextColor(0.4, 0.4, 0.4) -- otherwise set the color to be grey
end
elseif not name and debug then -- debug message if the name is nil
local which, index
local creatureID, creatureName, spellID
local numPets, numOwned = C_PetJournal.GetNumPets(true)
if mounts then
which = "MOUNT"
index = mountstable[buttonplusoffset]
creatureID, creatureName, spellID = GetCompanionInfo(which, index)
DEFAULT_CHAT_FRAME:AddMessage("Max "..kind.." "..which.." is " .. (mounts or "nil"))
else
which = "CRITTER"
index = critterstable[buttonplusoffset]
spellID, _, _, _, _, _, _, creatureName, _, _, creatureID = C_PetJournal.GetPetInfoByIndex(critterstable[buttonplusoffset])
DEFAULT_CHAT_FRAME:AddMessage("Max "..which.." is " .. (critters or "nil"))
end
DEFAULT_CHAT_FRAME:AddMessage(string.format("Error for "..which.." %u at menu build, ID: %s, name: %s, spellID: %s", index, (creatureID or "nil"), (creatureName or "nil"), (spellID or "nil")))
end
menubutton:SetHeight(buttonHeight) -- give the button a height
if button == 1 then -- parent it to the menu's TOPLEFT corner if it's the first button, else underneath the previous button
menubutton:SetPoint("TOPLEFT", menuframe, "TOPLEFT", 20, -10)
else
menubutton:SetPoint("TOPLEFT", _G[menuframe:GetName().."Button"..(button - 1)], "BOTTOMLEFT")
end
if button == endButton and token then -- set all the buttons to have the same width
for i = 1, endButton do
if _G["Livestock"..token.."Menu".."Button"..i].SetWidth ~= nil then
_G["Livestock"..token.."Menu".."Button"..i]:SetWidth(maxWidth)
end
end
end
end
menuframe:SetWidth(maxWidth + 30) -- bump up the size of the menu frame background to compensate and make sure the border and background look nice
local check = _G["Livestock"..token.."MenuCheckButton"] or CreateFrame("Button", "Livestock"..token.."MenuCheckButton", _G["Livestock"..token.."Menu"], "LivestockMenuButtonTemplate")
check.which = "check"..kind
local uncheck = _G["Livestock"..token.."MenuUncheckButton"] or CreateFrame("Button", "Livestock"..token.."MenuUncheckButton", _G["Livestock"..token.."Menu"], "LivestockMenuButtonTemplate")
uncheck.which = "uncheck"..kind
check:SetHeight(buttonHeight)
check:SetWidth(maxWidth)
uncheck:SetHeight(buttonHeight)
uncheck:SetWidth(maxWidth) -- reference them or make them, show them, set their heights and widths, associate info with the frame, etc.
_G[check:GetName().."Text"]:SetText(L.LIVESTOCK_MENU_SELECTALL)
_G[check:GetName().."Text"]:SetTextColor(1, 1, 0)
_G[uncheck:GetName().."Text"]:SetText(L.LIVESTOCK_MENU_SELECTNONE)
_G[uncheck:GetName().."Text"]:SetTextColor(1, 1, 0) -- set their text and color (yellow to stand out)
check:SetPoint("TOPLEFT", _G["Livestock"..token.."MenuButton"..endButton], "BOTTOMLEFT")
uncheck:SetPoint("TOPLEFT", check, "BOTTOMLEFT") -- stick the Uncheck All button under the Check All button... and we're done!
end
end