Skip to content

Instantly share code, notes, and snippets.

@nefftd
Created August 5, 2014 23:51
Show Gist options
  • Save nefftd/a5ad1f29197b65eef3d4 to your computer and use it in GitHub Desktop.
Save nefftd/a5ad1f29197b65eef3d4 to your computer and use it in GitHub Desktop.
-- Depends on: nfAddon
-- Grab host namespace
local _,mod = ...
-- Frame signals
-- Gives frame objects :Listen and :Update, a system for sending and receiving
-- signals to a parent frame and all its children. Useful for scenarios like
-- unit frames that may have many children/regions that want to update for
-- specific scenarios. I use this in nfUnitFrames so the event which listens
-- to health changes can then do something like:
-- units['player']:Update('health',current,max)
--
-- Then, children/regions of the base player frame can update their health
-- display like:
-- playerframe.statusbar:Listen('health',function(self,current,max)
-- self:SetValue(current/max)
-- end)
local function Listen(self,event,func)
mod:argcheck(event,1,'string')
mod:argcheck(func,2,'function')
-- :Update is called on the parent, so insert it into their registry.
local parent = self._nfparent
local signal_registry = parent._nfsigreg
local sigtbl = signal_registry[event]
if not sigtbl then
sigtbl = {}
signal_registry[event] = sigtbl
end
sigtbl[#sigtbl+1] = function(...) func(self,...) end
end
local function Update(self,event,...)
mod:argcheck(event,1,'string')
local sigtbl = self._nfsigreg[event]
if sigtbl then
local succ,err
for i = 1,#sigtbl do
succ,err = pcall(sigtbl[i],...)
if not succ then
mod:softerror(err)
end
end
end
end
local function HasUpdate(self,event)
return (not not self._nfsigreg[event])
end
-- Z-levels
-- Transparently creates container holders to allow for infinite fine-grained
-- z-level positioning of regions. Simply: Texture:SetZLevel(n). Regions in a
-- higher `n` always appear layered above those in a lower `n`. An `n` of <=
-- 0 places the region directly on the parent (lowest Z level).
local function SetZLevel(self,lvl)
mod:argcheck(lvl,1,'number')
local parent = self._nfparent
if lvl <= 0 then
self:SetParent(parent)
else
local zlevels = parent._nfzlevels
if lvl > #zlevels then
for i = #zlevels+1,lvl do
zlevels[i] = CreateFrame('Frame',nil,(zlevels[i-1] or parent))
zlevels[i]:SetAllPoints(true)
end
end
self:SetParent(zlevels[lvl])
end
end
-- Creation wrapper
local function CreateTexture(self,...)
local r = self:_nfCreateTexture(...)
r._nfparent = self
r.Listen = Listen
r.SetZLevel = SetZLevel
return r
end
local function CreateFontString(self,...)
local r = self:_nfCreateFontString(...)
r._nfparent = self
r.Listen = Listen
r.SetZLevel = SetZLevel
return r
end
function mod:CreateFrame(...)
local f = CreateFrame(...)
f._nfsigreg = {}
f.Listen = Listen
f.Update = Update
f.HasUpdate = HasUpdate
f._nfzlevels = {}
f._nfCreateTexture = f.CreateTexture
f.CreateTexture = CreateTexture
f._nfCreateFontString = f.CreateFontString
f.CreateFontString = CreateFontString
return f
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment