View Single Post
03-15-11, 11:12 AM   #13
Grimsin
A Molten Giant
 
Grimsin's Avatar
AddOn Author - Click to view addons
Join Date: Sep 2006
Posts: 990
Here is my event handling from the core file of my UI and then a few examples of how it is used in other parts of the UI.

Core File Function -
Code:
--[[-----------------------------------------------------------------------------
Event handling - Use one frame to process multiple individual OnEvent needs
-------------------------------------------------------------------------------]]
do
	local frame, select, next, events = CreateFrame('Frame'), select, pairs({ })
	local register, unregister = frame.RegisterEvent, frame.UnregisterEvent
	frame:Hide()

	frame:SetScript('OnEvent', function(self, event, ...)
		for reference, func in next, events[event], nil do
			func(reference, event, ...)
		end
	end)

	function addon.RegisterEvent(reference, event, func)
		if not events[event] then
			events[event] = { }
			register(frame, event)
		end
		events[event][reference] = func
	end

	function addon.RegisterEvents(reference, func, ...)
		local event
		for index = 1, select('#', ...) do
			event = select(index, ...)
			if not events[event] then
				events[event] = { }
				register(frame, event)
			end
			events[event][reference] = func
		end
	end

	function addon.UnregisterEvent(reference, event)
		if events[event] then
			events[event][reference] = nil
			if not next(events[event]) then
				events[event] = nil
				unregister(frame, event)
			end
		end
	end

	function addon.UnregisterAllEvents(reference)
		for event, registry in next, events, nil do
			registry[reference] = nil
			if not next(registry) then
				events[event] = nil
				unregister(frame, event)
			end
		end
	end
end
These are some examples of it being used.

On Show function that registers events to the party frames -
Code:
--[[-----------------------------------------------------------------------------
Register/Unregister events on Show/Hide
-------------------------------------------------------------------------------]]
local function OnShow(self)
	local register = addon.RegisterEvents
	register(self, UpdateDeadOffline, 'PARTY_MEMBER_DISABLE', 'PARTY_MEMBER_ENABLE')
	register(self, UpdateLeader, 'PARTY_LEADER_CHANGED', 'ZONE_CHANGED_NEW_AREA')
	register(self, UpdateName, 'UNIT_COMBAT', 'UNIT_FLAGS', 'UNIT_NAME_UPDATE', 'UNIT_THREAT_LIST_UPDATE', 'UNIT_THREAT_SITUATION_UPDATE')
	register(self, UpdatePower, 'UNIT_DISPLAYPOWER', 'UNIT_ENERGY', 'UNIT_MANA', 'UNIT_MAXENERGY', 'UNIT_MAXMANA', 'UNIT_MAXRUNICPOWER', 'UNIT_RAGE', 'UNIT_RUNIC_POWER')
	register(self, UpdatePvp, 'UNIT_DYNAMIC_FLAGS', 'UNIT_FACTION')
	register(self, UpdateQType, 'PARTY_MEMBERS_CHANGED', 'LFG_ROLE_CHECK_UPDATE', 'LFG_ROLE_UPDATE', 'PLAYER_ROLES_ASSIGNED', 'LFG_ROLE_CHECK_ROLE_CHOSEN', 'LFG_UPDATE_RANDOM_INFO', 'PLAYER_ENTERING_WORLD')
	register(self, UpdateUnit, 'PARTY_MEMBERS_CHANGED', 'UNIT_AURA', 'UNIT_HEALTH', 'UNIT_LEVEL')
	addon.RegisterEvent(self, 'UNIT_MAXHEALTH', UpdateHealth)
	if UnitFactionGroup(self.unit) == "Alliance" then    
		self.pvpIconFrame.texture:SetTexture("Interface\\GroupFrame\\UI-Group-PVP-Alliance.blp")
	elseif UnitFactionGroup(self.unit) == "Horde" then
		self.pvpIconFrame.texture:SetTexture("Interface\\GroupFrame\\UI-Group-PVP-Horde.blp")
	end
	UpdateUnit(self, "")
	if not addon.settings.movedFrames.GrimUIPartyFrame1 then
		addon:ResetPartyFrames()
	else
		local position = addon.settings.movedFrames[self:GetName()]
	if position then
		addon:UnlockFrame(self)
		self:ClearAllPoints()
		self:SetPoint(unpack(position))
		addon:LockFrame(self)
	end
	end
	
end
A more generic PLAYER_ENTERING_WORLD event registration to fire a slew of functions -
Code:
addon.RegisterEvent("PlayerFrame-Initialize", 'PLAYER_ENTERING_WORLD', function(self, event)
	addon.UnregisterEvent(self, event)
	addon:ConfigureBlizPlayerFrame()
	addon:ConfigurePlayerFrame()
    addon:PlayerFrameSetScale()
end)
__________________
"Are we there yet?"

GrimUI
[SIGPIC][/SIGPIC]
  Reply With Quote