Update:
I'm working on an update for ncDatatext, in the next version it will be made a library with LibStub, the defaults are embedded in the library(no need to give them anymore). I didn't test the code yet, but here is the core code:
Code:
local lib = LibStub:NewLibrary("ncDatatext", 1)
if not lib then return end
function lib:Create(tab)
assert(type(tab) == "table", "ncDatatext: Bad arguments given, for reference look at the config file.")
for name in pairs(tab) do
assert(type(tab[name].content) == "function", "ncDatatext: Content must be a function, the value of the frame is the return value of the function.")
local f, t, e, rate, font, fontsize, anchor, parent, x, y, flags, event, content = CreateFrame("Frame", name), f:CreateFontString(nil, "OVERLAY"), 0, tab[name].rate or 1, tab[name].font or "Fonts\\FRIZQT__.ttf", tab[name].fontsize or 8, tab[name].anchor or "CENTER", tab[name].parent or "UIParent", tab[name].x or 0, tab[name].y or 0, tab[name].flags or "", tab[name].event or nil, tab[name].content
t:SetFont(font, fontsize, flags)
f:SetPoint(anchor, parent, anchor, x, y)
t:SetPoint("CENTER", f)
function f:Update(self, event, ...)
t:SetText(content(self, event, ...))
f:SetHeight(t:GetStringHeight())
f:SetWidth(t:GetStringWidth())
end
if event then
f:SetScript("OnEvent", function()
f:Update()
end)
f:RegisterEvent(event)
else
f:SetScript("OnUpdate", function(self, elapsed)
e = e + elapsed
if (e>rate) then
f:Update()
end
end)
f:Update()
end
end
end
The defaults are still given, but this is merely for documentation and has no effect whatsoever. Also the examples have been modified to call the lib:
Code:
-- The default value will be used if no value is specified in the frame.
LibStub("ncDatatext"):Create({
["Defaults"] = {
rate = 1, -- This value determines how often all frames update in seconds(aka global update). Realtime updating(when the event occurs) must be done by registering the frame with the value "event". If an event is registered with the frame, it will not update with the global update anymore. (Self, event, ...) are passed to the content function if registered with an event.
font = "Fonts\\FRIZQT__.ttf", -- The font that should be used. If you want to use an other font than the default, place your font in the ncDatatext folder, and change this value to "Interface\\Addons\\ncDatatext\\<Yourfontname>.ttf". It must be a TrueType font.
fontsize = 8, -- The font size, simple.
anchor = "CENTER", -- Which the frame should anchor to, can be any frame or BOTTOMLEFT, BOTTOM, BOTTOMRIGHT, LEFT, CENTER, RIGHT, TOPLEFT, TOP, TOPRIGHT.
parent = "UIParent", -- The parent of the frame.
x = 0, -- The horizontal offset from the anchor. A negative value moves x pixels to the left, a positive value x pixels to the right.
y = 0, -- Same as above, but vertical.
flags = "", -- Can be any comma-seperated combination of MONOCHROME, OUTLINE and THICKOUTLINE. For example flags = "OUTLINE" creates a little black border around the font.
content = function()
return ""
end, -- The actual content of the frame. Must always be a function, the return value is the displayed value.
event = nil, -- The registered event of the frame, if one is given the frame will not update with the global update anymore(see top).
},
})
-- The actual frames, 5 examples have been given.
LibStub("ncDatatext"):Create({
["ExperienceRep"] = {
x = 100,
y = 100,
content = function()
if (UnitLevel("player")<MAX_PLAYER_LEVEL) then
return "XP: "..UnitXP('player').."/"..UnitXPMax('player').." | "..string.format("%.2f", (UnitXP('player')/UnitXPMax('player')*100)).."%"
else
local _, _, minimum, maximum, value = GetWatchedFactionInfo()
if ((value-minimum)==999) and ((maximum-minimum)==1000) then
return "REP: "..(value-minimum).."/"..(maximum-minimum).." | 100%"
else
return "REP: "..(value-minimum).."/"..(maximum-minimum).." | "..string.format("%.2f", (value-minimum)/(maximum-minimum)*100).."%"
end
end
end,
event = "PLAYER_XP_UPDATE",
},
["FPS"] = {
x = -100,
y = -100,
content = function()
return "FPS: "..floor(GetFramerate())
end,
},
["Mail"] = {
fontsize = 14,
x = -100,
y = 100,
content = function()
local mail = (HasNewMail() or 0)
if mail>0 then
return "You've got mail"
else
return "No new mail"
end
end,
},
["Latency"] = {
x = 100,
y = -100,
content = function()
return select(3, GetNetStats()).." MS"
end,
},
["Memory"] = {
content = function()
local t = 0
UpdateAddOnMemoryUsage()
for i=1, GetNumAddOns(), 1 do
t = t + GetAddOnMemoryUsage(i)
end
if t > 1024 then
return "Memory: "..(floor(t)/1024).." MB"
else
return "Memory: "..floor(t).." KB"
end
end,
},
})
This update will be put in the addon when I am at home(currently at school) and have tested it.