Quantcast
WoWInterface - View Single Post - How do you loop k,v pairs to create fontstrings?
View Single Post
05-14-21, 12:04 PM   #6
Fizzlemizz
I did that?
 
Fizzlemizz's Avatar
Premium Member
AddOn Author - Click to view addons
Join Date: Dec 2011
Posts: 1,429
This has both problems I described.
Every time you press the G button you are creating a new set of fonstrings on top of the previous set of fontstring.

Because you've been lucky with the display order doesn't mean you will continue to be lucky if you change or extend the table entries:

The offsetting is to display what's happening if you press the G button multiple times
Lua Code:
  1. local table = {
  2.     ["entry"] = "one",
  3.     ["entry2"] = "two",
  4.     ["entry3"] = "three",
  5.     ["entry4"] = "Four",
  6.     ["entry5"] = "Five",
  7.     ["entry6"] = "Six",
  8.     ["entry7"] = "Seven",
  9.     ["entry8"] = "Eight",
  10.     ["entry9"] = "Nine",
  11.     ["entry10"] = "Ten",
  12. }
  13.  
  14. local f = CreateFrame("Frame", nil, UIParent, "BasicFrameTemplateWithInset")
  15. f:SetSize(250, 400)
  16. f:SetPoint("CENTER")
  17.  
  18. f.Title = f:CreateFontString(nil, "OVERLAY", "GameTooltipText")
  19. f.Title:SetPoint("TOP", 0, -2)
  20. f.Title:SetText("Original")
  21.  
  22. f.grabdata = CreateFrame("Button", nil, f, "SharedButtonTemplate")
  23. f.grabdata:SetText("G")
  24. f.grabdata:SetPoint("TOPLEFT", f, "TOPLEFT", 0, 0)
  25. f.grabdata:SetSize(40, 20)
  26. local offset = 0
  27. f.grabdata:SetScript("OnClick",
  28.     function()
  29.         local i = 0
  30.         offset = offset + 1
  31.         for k,v in pairs(table) do
  32.             local fsname = f:CreateFontString(nil, "OVERLAY", "GameTooltipText")
  33.             fsname:SetPoint("TOPLEFT", f, "TOPLEFT", offset, -17*(i-(-2)))
  34.             fsname:SetText(k)
  35.            
  36.             local fsname2 = f:CreateFontString(nil, "OVERLAY", "GameTooltipText")
  37.             fsname2:SetPoint("TOPRIGHT", f, "TOPRIGHT", -offset, -17*(i-(-2)))
  38.             fsname2:SetText(v)              
  39.             i = i + 1
  40.         end
  41.     end
  42. )
  43.  
  44. -- Constant ordered display
  45. local table2 = {
  46.     { display="entry", value="one" },
  47.     { display="entry2", value="two" },
  48.     { display="entry3", value="three" },
  49.     { display="entry4", value="Four" },
  50.     { display="entry5", value="Five" },
  51.     { display="entry6", value="Six" },
  52.     { display="entry7", value="Seven" },
  53.     { display="entry8", value="Eight" },
  54.     { display="entry9", value="Nine" },
  55.     { display="entry10", value="Ten" },
  56. }
  57.  
  58. local f2 = CreateFrame("Frame", nil, UIParent, "BasicFrameTemplateWithInset")
  59. f2:SetSize(250, 400)
  60. f2:SetPoint("LEFT", f, "RIGHT")
  61.  
  62. f2.Title = f2:CreateFontString(nil, "OVERLAY", "GameTooltipText")
  63. f2.Title:SetPoint("TOP", 0, -2)
  64. f2.Title:SetText("Constant Ordered")
  65.  
  66. f2.grabdata = CreateFrame("Button", nil, f2, "SharedButtonTemplate")
  67. f2.grabdata:SetText("G")
  68. f2.grabdata:SetPoint("TOPLEFT", f2, "TOPLEFT", 0, 0)
  69. f2.grabdata:SetSize(40, 20)
  70. local offset = 0
  71. f2.Fonstrings = {}
  72. f2.grabdata:SetScript("OnClick", function(self)
  73.     local parent = self:GetParent()
  74.     local left, right
  75.     offset = offset + 1
  76.     for k,v in ipairs(table2) do
  77.         if not parent.Fonstrings[i] then
  78.             left = self:CreateFontString(nil, "OVERLAY", "GameTooltipText")
  79.             right = self:CreateFontString(nil, "OVERLAY", "GameTooltipText")
  80.             if k == 1 then
  81.                 left:SetPoint("TOPLEFT", self:GetParent(), 9, -22)
  82.                 right:SetPoint("TOPRIGHT", self:GetParent(), -9, -22)
  83.             else
  84.                 left:SetPoint("TOPLEFT", parent.Fonstrings[k-1].left, "BOTTOMLEFT", offset, -2)
  85.                 right:SetPoint("TOPRIGHT", parent.Fonstrings[k-1].right, "BOTTOMRIGHT", -offset, -2)
  86.             end
  87.             tinsert(parent.Fonstrings, {left=left, right=right})
  88.         end
  89.         parent.Fonstrings[k].left:SetText(v.display)
  90.         parent.Fonstrings[k].right:SetText(v.value)
  91.     end
  92. end)
__________________
Fizzlemizz
Maintainer of Discord Unit Frames and Discord Art.
Author of FauxMazzle, FauxMazzleHUD and Move Pad Plus.

Last edited by Fizzlemizz : 05-14-21 at 01:20 PM.
  Reply With Quote