Thread Tools Display Modes
08-09-13, 12:54 AM   #1
Niketa
A Wyrmkin Dreamwalker
 
Niketa's Avatar
AddOn Author - Click to view addons
Join Date: Jul 2013
Posts: 54
Dynamic Frame Height

I'm trying to make an options section that automatically populates a scroll frame with values in a table. However, the issue I'm having now is trying to dynamically adjust the height of the content frame (favcontent) to fit the contents that will be displayed. As of now, I can put the height as 175 for example but if there's nothing in the table then you'd be able to scroll down for that whole frame or if the contents don't fill up the entire frame, you have all that extra space to scroll down after the values are listed. Or if there are more values than space to display on the frame, then some will be cut off.


Lua Code:
  1. Mountaholic = {}
  2.  
  3. -- Create main options panel in Interface>Addons.
  4. Mountaholic.Options = CreateFrame("Frame", nil, UIParent)
  5. Mountaholic.Options.name = "Mountaholic"
  6. InterfaceOptions_AddCategory(Mountaholic.Options)
  7.  
  8. -- Create child panel for Help text.
  9. Mountaholic.Help = CreateFrame("Frame", nil, Mountaholic.Options)
  10. Mountaholic.Help.name = "Help"
  11. Mountaholic.Help.parent = Mountaholic.Options.name
  12. InterfaceOptions_AddCategory(Mountaholic.Help)
  13.  
  14. -- Title font string.
  15. local title = Mountaholic.Options:CreateFontString(nil, "OVERLAY", "GameFontNormalLarge")
  16.       title:SetPoint("TOPLEFT", 16, -16)
  17.       title:SetText("Mountaholic")
  18.      
  19. -- Favorites title.
  20. local favtitle = Mountaholic.Options:CreateFontString(nil, "OVERLAY", "GameFontNormal")
  21.       favtitle:SetPoint("TOPLEFT", title, "BOTTOMLEFT", 9, -20)
  22.       favtitle:SetText("Favorite Mounts")
  23.      
  24. -- Favorites description.
  25. local favdesc = Mountaholic.Options:CreateFontString(nil, "OVERLAY", "GameFontHighlightSmall")
  26.       favdesc:SetPoint("TOPLEFT", favtitle, "BOTTOMLEFT", 0, -5)
  27.       favdesc:SetText(favdesctxt)
  28.       favdesc:SetWidth(575)
  29.       favdesc:SetWordWrap(true)
  30.       favdesc:SetJustifyH("LEFT")
  31.  
  32. -- Create frame for favorites.
  33. local favframe = CreateFrame("ScrollFrame", nil, Mountaholic.Options)
  34.       favframe:SetPoint("TOPLEFT", favdesc, "BOTTOMLEFT", 0, -20)
  35.       favframe:SetPoint("BOTTOMRIGHT", favdesc, "BOTTOMLEFT", 575, -125)
  36.  
  37. local favframebg = favframe:CreateTexture(nil, "BACKGROUND")
  38.       favframebg:SetAllPoints(favframe)
  39.       favframebg:SetTexture(0, 0, 0, 0.4)
  40.  
  41. Mountaholic.Options.favscrollframe = favframe
  42.  
  43. local favcontent = CreateFrame("Frame", nil, scrollframea)
  44.       favcontent:SetSize(575, 175)
  45.    
  46. favframe.content = favcontent
  47. favframe:SetScrollChild(favcontent)
  48.  
  49. local favscrollbar = CreateFrame("Slider", nil, favframe, "UIPanelScrollBarTemplate")
  50.       favscrollbar:SetPoint("TOPLEFT", favframe, "TOPRIGHT", -16, -16)
  51.       favscrollbar:SetPoint("BOTTOMLEFT", favframe, "BOTTOMRIGHT", 16, 16)
  52.       favscrollbar:SetMinMaxValues(1, favcontent:GetHeight())
  53.       favscrollbar:SetValueStep(1)
  54.       favscrollbar:SetValue(0)
  55.       favscrollbar:SetWidth(16)
  56.  
  57. favscrollbar:SetScript("OnValueChanged", function(self, value)
  58.     self:GetParent():SetVerticalScroll(value)
  59. end)
  60.  
  61. local favscrollbarbg = favscrollbar:CreateTexture(nil, "BACKGROUND")
  62.       favscrollbarbg:SetAllPoints(favscrollbar)
  63.       favscrollbarbg:SetTexture(0, 0, 0, 0.4)
  64.    
  65. Mountaholic.Options.favscrollbar = favscrollbar
  66.  
  67. favcontent:SetScript("OnMouseWheel", function(self, value)
  68.     if value > 0 then
  69.         favscrollbar:SetValue(favscrollbar:GetValue() - (favscrollbar:GetHeight()/100))
  70.     else
  71.         favscrollbar:SetValue(favscrollbar:GetValue() + (favscrollbar:GetHeight()/100))
  72.     end
  73. end)
  74.  
  75. -- Populate favframe.
  76. favcontent:SetScript("OnShow", function(self, event, ...)
  77.     local vars = {}
  78.     local y = -10
  79.     local numMounts, mountName = GetNumCompanions("mount"), nil
  80.    
  81.     for k, v in pairs(Mountaholic_Favorites) do
  82.         for i = 1, numMounts do
  83.             local _, creatureName, creatureSpellID = GetCompanionInfo("mount",i)
  84.             if creatureSpellID == k then
  85.                 mountName = creatureName
  86.                 break
  87.             end
  88.         end
  89.  
  90.         vars[k.."str"] = self:CreateFontString(nil, "OVERLAY", "GameFontHighlight")
  91.         vars[k.."str"]:SetPoint("TOPLEFT", self, "TOPLEFT", 10, y)
  92.         vars[k.."str"]:SetText(mountName)
  93.         vars[k.."str"]:SetWordWrap(true)
  94.         vars[k.."str"]:SetJustifyH("LEFT")
  95.        
  96.         vars[k.."str2"] = self:CreateFontString(nil, "OVERLAY", "GameFontHighlight")
  97.         vars[k.."str2"]:SetPoint("TOPLEFT", vars[k.."str"], "TOPRIGHT", 5, 0)
  98.         vars[k.."str2"]:SetText(" |cff71C671("..k..")")
  99.         vars[k.."str2"]:SetWordWrap(true)
  100.         vars[k.."str2"]:SetJustifyH("LEFT")
  101.        
  102.         y = y - 15
  103.     end
  104. end)

Here's a screenshot of what I'm talking about:


Last edited by Niketa : 08-09-13 at 12:58 AM.
  Reply With Quote
08-09-13, 02:45 AM   #2
zork
A Pyroguard Emberseer
 
zork's Avatar
AddOn Author - Click to view addons
Join Date: Jul 2008
Posts: 1,740
You can calculate the height of a frame based on the strings inside. You can do that by summing up the values from:
Lua Code:
  1. string:GetStringHeight()

After that you can apply the sum to adjust the height of your scrollframe. Once it surpases a defined max-value the height will not grow any higher.

If you have your setpoints right all your frames anchor onto the frame above. All but the first frame which is anchored to the panel itself. Thus if a frame shrinks/grows all other frames will follow automatically.
__________________
| Simple is beautiful.
| WoWI AddOns | GitHub | Zork (WoW)

"I wonder what the non-pathetic people are doing tonight?" - Rajesh Koothrappali (The Big Bang Theory)

Last edited by zork : 08-09-13 at 02:52 AM.
  Reply With Quote

WoWInterface » Developer Discussions » Lua/XML Help » Dynamic Frame Height


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off