View Single Post
10-14-21, 12:00 PM   #2
Fizzlemizz
I did that?
 
Fizzlemizz's Avatar
Premium Member
AddOn Author - Click to view addons
Join Date: Dec 2011
Posts: 1,879
You don't create new rows in the list, you create the number of rows you want to display and then re-use them with information from your source table as you scroll through it. If you want to change the table of information you want to display, you re-initialise the list using the other table.

A really quick and dirty example using 3 source tables.
The list only display 3 rows and each table has only 4 entries (so not much scrolling).
You can switch from one source table to another using /kl taunt, /kl hit, /kl stuff

Lua Code:
  1. local initList = "taunt"
  2.  
  3. local sections = {
  4.     taunt = 1,
  5.     hit = 2,
  6.     stuff = 3,
  7. }
  8. local listEntries = {
  9.     [1]={
  10.         {text="123", value=1 },
  11.         {text="456", value=4 },
  12.         {text="789", value=7 },
  13.         {text="000", value=0 },
  14.     },
  15.     [2]={
  16.         {text="ABC", value=1 },
  17.         {text="DEF", value=2 },
  18.         {text="GHI", value=3 },
  19.         {text="JKL", value=4 },
  20.     },
  21.     [3]={
  22.         {text="MNO", value=5 },
  23.         {text="PWR", value=6 },
  24.         {text="STU", value=8 },
  25.         {text="VWX", value=9 },
  26.     },
  27. }
  28.  
  29. local function UpdateList(self, id, init) -- the work of re-using the rows (buttons and texts) happens here
  30.     local table = listEntries[sections[id]]
  31.     if not table then return end
  32.     local numOptions = #table
  33.     if init then
  34.         self.offset = 0
  35.     end
  36.     local offset = self.offset or 0
  37.     local index, button
  38.     local entries = #self.rows
  39.     local current = 0
  40.     for i=1, #table do
  41.         index = offset + i
  42.         if table[index] then
  43.             current = current + 1
  44.             button = self.rows[current]
  45.             button:Show()
  46.             button.index = index
  47.             button.value = table[index].value
  48.             button.Text:SetText(table[index].text.." [value="..table[index].value.."]")
  49.             if current == entries then break end -- only loop enough to full #buttons
  50.         end
  51.     end
  52.     if current < entries then
  53.         for i = current + 1, entries do
  54.             self.rows[i]:Hide()
  55.         end
  56.     end
  57.     FauxScrollFrame_Update(self, numOptions, entries, self.rows[1]:GetHeight())
  58. end
  59.  
  60. local function OnClick(self) -- Use the index set during UpdateList along with the curent source table (initList)
  61.     print("Clicked", listEntries[sections[initList]][self.index].value, listEntries[sections[initList]][self.index].text)
  62. end
  63.  
  64. local f = CreateFrame("ScrollFrame", "KhazakList", UIParent, "FauxScrollFrameTemplate")
  65. f:SetSize(100, 60)
  66. f:SetPoint("TOPLEFT", 5, -5)
  67. f.rows = {}
  68. for i=1, 3 do -- create a fixed no. of rows to display (3)
  69.     local r = CreateFrame("Button", "$parentRow"..i, f)
  70.     tinsert(f.rows, r)
  71.     r:SetSize(f:GetWidth(), f:GetHeight()/3) -- The height of a row determines the relative offset the scroll bar uses (1/3 the list height here)
  72.     if i == 1 then
  73.         r:SetPoint("TOPLEFT")
  74.     else
  75.         r:SetPoint("TOP", f.rows[i-1], "BOTTOM")
  76.     end
  77.     f.Texture = f:CreateTexture()
  78.     f.Texture:SetAllPoints()
  79.     f.Texture:SetTexture("Interface/BUTTONS/WHITE8X8")
  80.     f.Texture:SetVertexColor(0.2, 0.2, 0.2, 0.5)
  81.     r.Text = r:CreateFontString()
  82.     r.Text:SetFontObject(GameFontNormal)
  83.     r.Text:SetPoint("LEFT", 4, 0)
  84.     r:SetScript("OnClick", OnClick)
  85. end
  86.  
  87. f:SetScript("OnVerticalScroll", function(self, offset) -- When the scroll thumb moves, save the new offset and update from the current table
  88.     self.ScrollBar:SetValue(offset)
  89.     self.offset = math.floor(offset / self.rows[1]:GetHeight())
  90.     UpdateList(self, initList)
  91.  
  92. end)
  93.  
  94. UpdateList(f, initList, true) -- first time initalisation of the list.
  95.  
  96. _G["SLASH_KhazakList1"] = "/kl"
  97. SlashCmdList.KhazakList = function(msg)
  98.     msg = strlower(msg)
  99.     if not sections[msg] then
  100.         print(msg, "not found in sections!")
  101.         return
  102.     end
  103.     initList = msg
  104.     UpdateList(f, initList, true) -- Initialise the list using the newly selected table
  105. end
__________________
Fizzlemizz
Maintainer of Discord Unit Frames and Discord Art.
Author of FauxMazzle, FauxMazzleHUD and Move Pad Plus.

Last edited by Fizzlemizz : 10-14-21 at 03:08 PM.
  Reply With Quote