View Single Post
03-03-13, 11:40 AM   #29
Kharthus
A Fallenroot Satyr
AddOn Author - Click to view addons
Join Date: Nov 2006
Posts: 26
Well I found a solution, but I still don't understand why it's doing this. Ended up doubling up the function calls in the OnVerticalScroll handler.

Lua Code:
  1. <ScrollFrame name="LivestockCritterMenuScrollFrameTemplate" inherits="FauxScrollFrameTemplate" virtual="true">
  2.         <Anchors>
  3.             <Anchor point="TOPLEFT">
  4.                 <Offset>
  5.                     <AbsDimension x="0" y="-8"/>
  6.                 </Offset>
  7.             </Anchor>
  8.             <Anchor point="BOTTOMRIGHT">
  9.                 <Offset>
  10.                     <AbsDimension x="-30" y="8"/>
  11.                 </Offset>
  12.             </Anchor>
  13.         </Anchors>
  14.         <Scripts>
  15.             <OnVerticalScroll>
  16.                 FauxScrollFrame_OnVerticalScroll(self, offset, 15, Livestock.BuildMenu("CRITTER"));
  17.                 FauxScrollFrame_OnVerticalScroll(self, offset, 15, Livestock.BuildMenu("CRITTER"));
  18.             </OnVerticalScroll>
  19.         </Scripts>
  20.     </ScrollFrame>

Lua Code:
  1. function Livestock.BuildMenu(kind) -- type is a string, either "CRITTER", "LAND", "FLYING", or "WATER"
  2.     kind = string.upper(kind)
  3.     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
  4.     local settings, mounts, critters = LivestockSettings.Mounts
  5.    
  6.     Livestock.RecycleTable(mountstable)
  7.     Livestock.RecycleTable(critterstable)
  8.    
  9.     if kind == "CRITTER" then
  10.         critters = 0 -- reset critters to 0 so we can start counting critters
  11.         settings = LivestockSettings.Critters
  12.         for k in orderedPairs(settings) do -- Check the list of critters, increment critters, and add the index of the critters to critterstable
  13.             critters = critters + 1
  14.             tinsert(critterstable, settings[k].index)
  15.         end
  16.        
  17.     elseif kind == "LAND" then
  18.         mounts = 0 -- reset mounts to 0 so we can start counting land mounts
  19.         for k in orderedPairs(settings) do -- Check the list of mounts, increment mounts, and add the index of the mounts to mountstable
  20.             if (settings[k].type == "land") then
  21.                 mounts = mounts + 1
  22.                 tinsert(mountstable, settings[k].index)
  23.             end
  24.         end
  25.  
  26.     elseif kind == "FLYING" then -- code is the same for flying mounts with the appropriate changes; perhaps this could be a function call?
  27.         mounts = 0
  28.         for k in orderedPairs(settings) do
  29.             if (settings[k].type == "flying") then
  30.                 mounts = mounts + 1
  31.                 tinsert(mountstable, settings[k].index)
  32.             end
  33.         end
  34.        
  35.     elseif kind == "WATER" then -- code is the same for water mounts with the appropriate changes; perhaps this could be a function call?
  36.         mounts = 0
  37.         for k in orderedPairs(settings) do
  38.             if (settings[k].type == "water") then
  39.                 mounts = mounts + 1
  40.                 tinsert(mountstable, settings[k].index)
  41.             end
  42.         end
  43.     end
  44.    
  45.     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
  46.     local scrollframe = _G["Livestock"..token.."MenuScrollFrame"] or CreateFrame("ScrollFrame", "Livestock"..token.."MenuScrollFrame", menuframe, "Livestock"..token.."MenuScrollFrameTemplate")
  47.     scrollframe:Show()
  48.  
  49.     local header
  50.     if kind == "CRITTER" then
  51.         header = "LivestockCritterMenuButton"
  52.     elseif kind == "LAND" then
  53.         header = "LivestockLandMountMenuButton"
  54.     elseif kind == "FLYING" then
  55.         header = "LivestockFlyingMountMenuButton"
  56.     elseif kind == "WATER" then
  57.         header = "LivestockWaterMountMenuButton"
  58.     end
  59.     menuframe:SetPoint("TOPLEFT", header ,"BOTTOMLEFT",0,-10) -- set the first menu to appear below the associated button
  60.  
  61.     if (mounts or critters) <= menuLength then
  62.         menuframe:SetHeight((((mounts or critters) + 2) * buttonHeight) + 21)
  63.     else
  64.         menuframe:SetHeight((menuLength + 2) * buttonHeight + 21)
  65.     end
  66.  
  67.     local endButton -- this is the number of buttons in the menu...
  68.     if (mounts or critters) <= menuLength then
  69.         endButton = (mounts or critters)
  70.     else
  71.         endButton = menuLength
  72.     end
  73.  
  74.     local buttonplusoffset; -- an index into our data calculated from the scroll offset
  75.     FauxScrollFrame_Update(scrollframe,(mounts or critters),endButton,15);
  76.     DEFAULT_CHAT_FRAME:AddMessage("We're at "..FauxScrollFrame_GetOffset(scrollframe));
  77.  
  78.     for button = 1, endButton do -- iterate over the buttons we need
  79.         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
  80.         menubutton:Show()
  81.         local text = _G["Livestock"..token.."Menu".."Button"..button.."Text"] -- reference the text of the button
  82.  
  83.         buttonplusoffset = button + FauxScrollFrame_GetOffset(scrollframe);
  84.  
  85.         if buttonplusoffset <= (mounts or critters) then
  86.             local name, creatureID, _
  87.             if kind == "CRITTER" then
  88.                 _, _, _, _, _, _, _, name, _, _, creatureID = C_PetJournal.GetPetInfoByIndex(critterstable[buttonplusoffset])
  89.             elseif kind == "LAND" or kind == "FLYING" or kind == "WATER" then
  90.                 creatureID, name = GetCompanionInfo("MOUNT", mountstable[buttonplusoffset])
  91.             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
  92.  
  93.             if name and creatureID then
  94.                 menubutton.critterID = creatureID
  95.                 menubutton.which = "toggle"..kind
  96.                 menubutton.name = name  -- associate the info for the companion with the frame itself to be used for mousing over / clicking
  97.  
  98.                 if text then
  99.                     if text.SetText ~= nil then
  100.                         text:SetText(name)
  101.                     end
  102.                 end
  103.             end
  104.  
  105.             if name and kind == "LAND" then
  106.                 local name2 = strconcat(name, "-")
  107.                 if settings[name2] then
  108.                     name = name2
  109.                 end
  110.             end
  111.  
  112.             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
  113.                 if text.SetTextColor ~= nil then
  114.                     text:SetTextColor(1, 1, 1) -- set the color of the text to be white
  115.                 end
  116.             elseif name and text then
  117.                 if text.SetTextColor ~= nil then
  118.                     text:SetTextColor(0.4, 0.4, 0.4) -- otherwise set the color to be grey
  119.                 end
  120.             elseif not name and debug then -- debug message if the name is nil
  121.                 local which, index
  122.                 local creatureID, creatureName, spellID
  123.                 local numPets, numOwned = C_PetJournal.GetNumPets(true)
  124.                 if mounts then
  125.                     which = "MOUNT"
  126.                     index = mountstable[buttonplusoffset]
  127.                     creatureID, creatureName, spellID = GetCompanionInfo(which, index)
  128.                     DEFAULT_CHAT_FRAME:AddMessage("Max "..kind.." "..which.." is " .. (mounts or "nil"))
  129.                 else
  130.                     which = "CRITTER"
  131.                     index = critterstable[buttonplusoffset]
  132.                     spellID, _, _, _, _, _, _, creatureName, _, _, creatureID = C_PetJournal.GetPetInfoByIndex(critterstable[buttonplusoffset])
  133.                     DEFAULT_CHAT_FRAME:AddMessage("Max "..which.." is " .. (critters or "nil"))
  134.                 end
  135.                 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")))
  136.             end
  137.  
  138.             menubutton:SetHeight(buttonHeight) -- give the button a height
  139.             if button == 1 then -- parent it to the menu's TOPLEFT corner if it's the first button, else underneath the previous button
  140.                 menubutton:SetPoint("TOPLEFT", menuframe, "TOPLEFT", 20, -10)
  141.             else
  142.                 menubutton:SetPoint("TOPLEFT", _G[menuframe:GetName().."Button"..(button - 1)], "BOTTOMLEFT")
  143.             end
  144.  
  145.             if button == endButton and token then -- set all the buttons to have the same width
  146.                 for i = 1, endButton do
  147.                     if _G["Livestock"..token.."Menu".."Button"..i].SetWidth ~= nil then
  148.                         _G["Livestock"..token.."Menu".."Button"..i]:SetWidth(maxWidth)
  149.                     end
  150.                 end
  151.             end
  152.         end
  153.         menuframe:SetWidth(maxWidth + 30) -- bump up the size of the menu frame background to compensate and make sure the border and background look nice
  154.        
  155.         local check = _G["Livestock"..token.."MenuCheckButton"] or CreateFrame("Button", "Livestock"..token.."MenuCheckButton", _G["Livestock"..token.."Menu"], "LivestockMenuButtonTemplate")
  156.         check.which = "check"..kind
  157.         local uncheck = _G["Livestock"..token.."MenuUncheckButton"] or CreateFrame("Button", "Livestock"..token.."MenuUncheckButton", _G["Livestock"..token.."Menu"], "LivestockMenuButtonTemplate")
  158.         uncheck.which = "uncheck"..kind
  159.         check:SetHeight(buttonHeight)
  160.         check:SetWidth(maxWidth)
  161.         uncheck:SetHeight(buttonHeight)
  162.         uncheck:SetWidth(maxWidth) -- reference them or make them, show them, set their heights and widths, associate info with the frame, etc.
  163.         _G[check:GetName().."Text"]:SetText(L.LIVESTOCK_MENU_SELECTALL)
  164.         _G[check:GetName().."Text"]:SetTextColor(1, 1, 0)
  165.         _G[uncheck:GetName().."Text"]:SetText(L.LIVESTOCK_MENU_SELECTNONE)
  166.         _G[uncheck:GetName().."Text"]:SetTextColor(1, 1, 0) -- set their text and color (yellow to stand out)
  167.         check:SetPoint("TOPLEFT", _G["Livestock"..token.."MenuButton"..endButton], "BOTTOMLEFT")
  168.         uncheck:SetPoint("TOPLEFT", check, "BOTTOMLEFT") -- stick the Uncheck All button under the Check All button... and we're done!
  169.     end
  170. end
  Reply With Quote