1. #1
    High Overlord
    10+ Year Old Account
    Join Date
    Jan 2011
    Location
    The Netherlands
    Posts
    187

    List of FontStrings in a single frame

    I'm working on a WoW addon that displays a variable number (between 3 and 10) of timers under eachother in a single frame. These timers of course update through OnUpdate, but when a timer is negative (e.g. -8:23) I want the text for that timer to be red. Otherwise I want it to be green. Right now the timers are fit into a single FontString seperated by "\n"s, but that doesn't allow me to do a simple FontString:SetTextColor(...) because it would give all the timers the same color.

    How can I - taking into account that there is a variable number of timers - give every timer their own color? Thanks!

  2. #2
    several font strings concatenated together?

  3. #3
    You wrote it in your title: Make a list of them :-)

    Code:
    local MyList = {}
    
    for i=1,10 do
     tinsert( MyList, parent:CreateFontString(...) )
    end
    
    for _, k in ipairs(MyList) do
     k:SetTextColor(...)
    end

  4. #4
    High Overlord
    10+ Year Old Account
    Join Date
    Jan 2011
    Location
    The Netherlands
    Posts
    187
    Quote Originally Posted by Crudor View Post
    You wrote it in your title: Make a list of them :-)

    Code:
    local MyList = {}
    
    for i=1,10 do
     tinsert( MyList, parent:CreateFontString(...) )
    end
    
    for _, k in ipairs(MyList) do
     k:SetTextColor(...)
    end
    Thanks, that really helped me out! I had an idea of how I wanted to do it but I'm new to Lua so I would've never thought of this.

  5. #5
    Deleted
    If you want to try a more advanced (and "prettier", from a coding point of view) implementation, Lua metatables allow for something like this:
    Code:
    -- this is simply an example implementation that adds "s" to the seconds value passed and forwards to :SetText - you'd usually have your timer logic, setting up OnUpdate frames and/or animations, here
    local setTimer = function(timer,seconds)
        timer:SetText(("%ds"):format(seconds))
        
        if seconds > 10 then
            timer:SetTextColor(0,1,0)
        else
            timer:SetTextColor(1,0,0)
        end
    
        timer:Show()
    end
    
    
    -- create our myTimers table that will hold the timer objects
    -- it is an empty table (arg1 to setmetatable) with a metatable containing an __index method (arg2 to setmetatable)
    local myTimers = setmetatable({},{__index=function(t,key)
        -- create fontstring
        local timer = parent:CreateFontString()
    
        -- anchoring
        if key == 1 then
            timer:SetPoint("TOP",parent,"BOTTOM",0,-5) -- anchor point for first timer
        else
            timer:SetPoint("TOP",t[key-1],"BOTTOM",0,-5) -- anchor to previous timer
        end
    
        -- add the method
        timer.SetTimer = setTimer
        
        timer:SetTextColor(1,0,0) -- default text color
        timer:SetFont("Fonts\\FRIZQT__.TTF",13,"OUTLINE")
    
        t[key] = timer
        return timer
    end})
    This gives your myTimers table a metamethod that will automatically generate fontstrings as you access a key that doesn't exist yet. Example for accessing:
    Code:
    -- stuffToShow = {1, 15, 32}
    
    -- show table entries in our fontstrings using the :SetTimer method we added to them
    local nStuff = #stuffToShow
    for i=1,nStuff do
        myTimers[i]:SetTimer(stuffToShow[i]) -- we don't need to worry about whether this timer exists - it's automatically created by the metamethod if it doesn't!
    end
    
    -- hide excess timers
    for i=(nStuff+1),#myTimers do
        myTimers[i]:Hide()
    end
    Drycoded, but you get the idea.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •