Thread: Fix over scroll
View Single Post
01-05-24, 11:23 PM   #2
Fizzlemizz
I did that?
 
Fizzlemizz's Avatar
Premium Member
AddOn Author - Click to view addons
Join Date: Dec 2011
Posts: 1,894
Here's your code with some adjustments with a table of 50 entries to scroll through. See how buttonHeight (the height of a single row button) is used by FauxScrollFrame to calulate the offset of the thumb position in the bar and used in OnVerticalScroll to calculate back to an offset row in your data table for the update functions starting position.

I calculated the buttonHeight based on fitting 7 rows into the height of your ScrollFrame.

I moved the frames to UIParent so, move the back where you want.

Lua Code:
  1. local buttonHeight
  2. local data = {}
  3. for i=1, 50 do
  4.     tinsert(data, { value = random(1, 500), display = i })
  5. end
  6.  
  7. local function updateScrollFrame(self)
  8.     local rowHeight = buttonHeight
  9.     local totalEntries = #data
  10.     local filled, index, row, rowData = 0
  11.     local offset = self.Offset or 0 -- the data row to start at (caculated at OnVertivalScroll or zero)
  12.     local rows, rowHeight = #self.buttons, self.buttons[1]:GetHeight()
  13.     for i=1, totalEntries do -- Fill the 7 rows and show the row button
  14.         index = offset + i
  15.         rowData = data[index]
  16.         if rowData then
  17.             filled = filled + 1
  18.             row = self.buttons[i]
  19.             row:SetText(format("Data Row %d (%d)", rowData.display, rowData.value))
  20.             row.index = index
  21.             row:Show()
  22.             if i == rows then
  23.                 break
  24.             end
  25.         end
  26.     end
  27.     if filled < rows then -- hide any non-filled button (probably not neede for a simple FauxScrollFrame but...
  28.         for i = filled + 1, rows do
  29.             self.buttons[i]:Hide()
  30.         end
  31.     end
  32.     FauxScrollFrame_Update(self, totalEntries, rows, rowHeight) -- Update the scrollbar
  33. end    
  34.    
  35. local TestScrollFrame = CreateFrame("Frame", "TestScrollFrame", UIParent, "ChatConfigBoxTemplate")
  36. TestScrollFrame:SetSize(500, 150)
  37. --    TestScrollFrame:SetPoint("TOPLEFT", TestInterfaceFrame, "BOTTOMLEFT")
  38. TestScrollFrame:SetPoint("CENTER")
  39.      
  40. local TestScrollFrame =
  41.         CreateFrame("ScrollFrame", "TestScrollFrame", TestScrollFrame, "FauxScrollFrameTemplate")
  42. TestScrollFrame:SetPoint("TOPLEFT", 0, 0)
  43. TestScrollFrame:SetPoint("BOTTOMRIGHT", -25, 0)
  44.  
  45. buttonHeight = TestScrollFrame:GetHeight() /  7 -- so you know the height of a button    
  46.  
  47. TestScrollFrame:SetScript(
  48.         "OnVerticalScroll",
  49.         function(self, offset) -- offset is based on the height of the scrollbar / the height of a row button see: FauxScrollFrame_Update
  50.             self.Offset = math.floor(offset / buttonHeight) -- calulate offset back to the data row to start at
  51.             updateScrollFrame(self)
  52.         end
  53. )
  54.      
  55. TestScrollFrame.buttons = {}
  56.  
  57. for index = 1, 7 do
  58.         TestScrollFrame.buttons[index] =
  59.             CreateFrame("Button", "$parentbtn" .. index, TestScrollFrame, "OptionsListButtonTemplate")
  60.         local button = TestScrollFrame.buttons[index]
  61.         button:SetID(index)
  62.         button:SetSize(TestScrollFrame:GetWidth() - 50, buttonHeight)
  63.         if index == 1 then
  64.             button:SetPoint("TOPLEFT", 8, 0)
  65.         else
  66.             button:SetPoint("TOPLEFT", TestScrollFrame.buttons[index - 1], "BOTTOMLEFT")
  67.         end
  68.         button:SetScript(
  69.             "OnClick",
  70.             function(self, button)
  71.                 if button == "RightButton" then
  72.                     print("Button:", self:GetID(), "Value:", data[self.index].value)
  73. --                    updateScrollFrame()
  74.                 end
  75.             end
  76.         )
  77. end
  78. updateScrollFrame(TestScrollFrame) -- Get Started (no offset so updateScrollFrame will start a zero

As an aside, FauxScrollFrame has been deprecated but I don't expect it will go away any time soon but you may want to consider updating or replacing the Faux... methods with your own at some point.
__________________
Fizzlemizz
Maintainer of Discord Unit Frames and Discord Art.
Author of FauxMazzle, FauxMazzleHUD and Move Pad Plus.

Last edited by Fizzlemizz : 01-05-24 at 11:34 PM.
  Reply With Quote