WoWInterface

WoWInterface (https://www.wowinterface.com/forums/index.php)
-   Lua/XML Help (https://www.wowinterface.com/forums/forumdisplay.php?f=16)
-   -   Setting (and changing) anchors (https://www.wowinterface.com/forums/showthread.php?t=57521)

Nibs 09-19-19 07:48 PM

Setting (and changing) anchors
 
I have a very simple add-on that displays four pieces of information textually instead of with a visual representation - each on their own frame. Each one can be turned on an off at the discretion of the user. I would like these frames to all be anchored to each other horizontally. Easy enough right? Anchor left edge of Frame2 to the right edge of Frame 1, Frame3 to Frame2 in the same manner, etc. But what if Frame2 is disabled? Then Frame3 needs to be anchored to Frame1.

I tried to run Frame:GetChildren() on an anchor frame to count the children and anchor them to the anchor frame itself instead of to each other, but Frame:GetChildren() returns a table of tables, and the "#" operator doesn't count tables.

As a bonus, I would like the user to be able to change the order of the frames - 3, 1, 4, 2; 4, 2, 3, 1; etc.

The question of how to do this has consumed me all day today. Perhaps it's the lack of sleep, or perhaps the lack of Lua experience. Either way, any help would be much appreciated.

Kanegasi 09-19-19 10:12 PM

GetChildren returns a table of tables because frames are tables.

Lua Code:
  1. local parent = CreateFrame("frame","Foo")
  2. local child1 = CreateFrame("frame","Bar1",parent) -- can also use Foo instead of parent, cannot be a string
  3. local child2 = CreateFrame("frame","Bar2",parent)
  4.  
  5. local children = parent:GetChildren()
  6.  
  7. print( children[1] == child1 ) -- true
  8. print( children[2] == child2 ) -- true
  9. print( children[1] == Bar1 ) -- true
  10. print( children[2] == Bar2 ) -- true
  11.  
  12. local count = 0
  13. for k in ipairs(children) do
  14.     count = count + 1
  15. end

Nibs 09-20-19 06:31 AM

Yes, I knew that the frames themselves were tables. I come from a web-dev background and I would tackle this problem a little differently in JS. Your reply helps a lot. Thanks a bunch.

Sharparam 09-20-19 07:08 AM

Quote:

Originally Posted by Nibs (Post 333876)
[...] the "#" operator doesn't count tables.

Yes it does. see for example:

Lua Code:
  1. local tables = {
  2.     {1, 2, 3},
  3.     {4, 5, 6},
  4.     {7, 8, 9}
  5. }
  6.  
  7. print(#tables) -- prints "3"

As long as the indices start at 1 and are contiguous it will count just fine. But if you have an index missing somewhere in the chain, it will stop counting there. For example:

Lua Code:
  1. local tables = {}
  2. tables[1] = {1, 2, 3}
  3. tables[3] = {4, 5, 6} -- Notice we skipped index 2
  4. tables[4] = {7, 8, 9}
  5.  
  6. print(#tables) -- prints "1"

Edit: Actually, the behaviour of the "#" operator on a non-contiguous table is undefined, so you cannot rely on the output if the table doesn't have indices in sequence.


All times are GMT -6. The time now is 10:14 AM.

vBulletin © 2020, Jelsoft Enterprises Ltd
© 2004 - 2019 MMOUI