yellowfive@57: --- AceEvent-3.0 provides event registration and secure dispatching. yellowfive@57: -- All dispatching is done using **CallbackHandler-1.0**. AceEvent is a simple wrapper around yellowfive@57: -- CallbackHandler, and dispatches all game events or addon message to the registrees. yellowfive@57: -- yellowfive@57: -- **AceEvent-3.0** can be embeded into your addon, either explicitly by calling AceEvent:Embed(MyAddon) or by yellowfive@57: -- specifying it as an embeded library in your AceAddon. All functions will be available on your addon object yellowfive@57: -- and can be accessed directly, without having to explicitly call AceEvent itself.\\ yellowfive@57: -- It is recommended to embed AceEvent, otherwise you'll have to specify a custom `self` on all calls you yellowfive@57: -- make into AceEvent. yellowfive@57: -- @class file yellowfive@57: -- @name AceEvent-3.0 yellowfive@57: -- @release $Id: AceEvent-3.0.lua 975 2010-10-23 11:26:18Z nevcairiel $ yellowfive@57: local MAJOR, MINOR = "AceEvent-3.0", 3 yellowfive@57: local AceEvent = LibStub:NewLibrary(MAJOR, MINOR) yellowfive@57: yellowfive@57: if not AceEvent then return end yellowfive@57: yellowfive@57: -- Lua APIs yellowfive@57: local pairs = pairs yellowfive@57: yellowfive@57: local CallbackHandler = LibStub:GetLibrary("CallbackHandler-1.0") yellowfive@57: yellowfive@57: AceEvent.frame = AceEvent.frame or CreateFrame("Frame", "AceEvent30Frame") -- our event frame yellowfive@57: AceEvent.embeds = AceEvent.embeds or {} -- what objects embed this lib yellowfive@57: yellowfive@57: -- APIs and registry for blizzard events, using CallbackHandler lib yellowfive@57: if not AceEvent.events then yellowfive@57: AceEvent.events = CallbackHandler:New(AceEvent, yellowfive@57: "RegisterEvent", "UnregisterEvent", "UnregisterAllEvents") yellowfive@57: end yellowfive@57: yellowfive@57: function AceEvent.events:OnUsed(target, eventname) yellowfive@57: AceEvent.frame:RegisterEvent(eventname) yellowfive@57: end yellowfive@57: yellowfive@57: function AceEvent.events:OnUnused(target, eventname) yellowfive@57: AceEvent.frame:UnregisterEvent(eventname) yellowfive@57: end yellowfive@57: yellowfive@57: yellowfive@57: -- APIs and registry for IPC messages, using CallbackHandler lib yellowfive@57: if not AceEvent.messages then yellowfive@57: AceEvent.messages = CallbackHandler:New(AceEvent, yellowfive@57: "RegisterMessage", "UnregisterMessage", "UnregisterAllMessages" yellowfive@57: ) yellowfive@57: AceEvent.SendMessage = AceEvent.messages.Fire yellowfive@57: end yellowfive@57: yellowfive@57: --- embedding and embed handling yellowfive@57: local mixins = { yellowfive@57: "RegisterEvent", "UnregisterEvent", yellowfive@57: "RegisterMessage", "UnregisterMessage", yellowfive@57: "SendMessage", yellowfive@57: "UnregisterAllEvents", "UnregisterAllMessages", yellowfive@57: } yellowfive@57: yellowfive@57: --- Register for a Blizzard Event. yellowfive@57: -- The callback will be called with the optional `arg` as the first argument (if supplied), and the event name as the second (or first, if no arg was supplied) yellowfive@57: -- Any arguments to the event will be passed on after that. yellowfive@57: -- @name AceEvent:RegisterEvent yellowfive@57: -- @class function yellowfive@57: -- @paramsig event[, callback [, arg]] yellowfive@57: -- @param event The event to register for yellowfive@57: -- @param callback The callback function to call when the event is triggered (funcref or method, defaults to a method with the event name) yellowfive@57: -- @param arg An optional argument to pass to the callback function yellowfive@57: yellowfive@57: --- Unregister an event. yellowfive@57: -- @name AceEvent:UnregisterEvent yellowfive@57: -- @class function yellowfive@57: -- @paramsig event yellowfive@57: -- @param event The event to unregister yellowfive@57: yellowfive@57: --- Register for a custom AceEvent-internal message. yellowfive@57: -- The callback will be called with the optional `arg` as the first argument (if supplied), and the event name as the second (or first, if no arg was supplied) yellowfive@57: -- Any arguments to the event will be passed on after that. yellowfive@57: -- @name AceEvent:RegisterMessage yellowfive@57: -- @class function yellowfive@57: -- @paramsig message[, callback [, arg]] yellowfive@57: -- @param message The message to register for yellowfive@57: -- @param callback The callback function to call when the message is triggered (funcref or method, defaults to a method with the event name) yellowfive@57: -- @param arg An optional argument to pass to the callback function yellowfive@57: yellowfive@57: --- Unregister a message yellowfive@57: -- @name AceEvent:UnregisterMessage yellowfive@57: -- @class function yellowfive@57: -- @paramsig message yellowfive@57: -- @param message The message to unregister yellowfive@57: yellowfive@57: --- Send a message over the AceEvent-3.0 internal message system to other addons registered for this message. yellowfive@57: -- @name AceEvent:SendMessage yellowfive@57: -- @class function yellowfive@57: -- @paramsig message, ... yellowfive@57: -- @param message The message to send yellowfive@57: -- @param ... Any arguments to the message yellowfive@57: yellowfive@57: yellowfive@57: -- Embeds AceEvent into the target object making the functions from the mixins list available on target:.. yellowfive@57: -- @param target target object to embed AceEvent in yellowfive@57: function AceEvent:Embed(target) yellowfive@57: for k, v in pairs(mixins) do yellowfive@57: target[v] = self[v] yellowfive@57: end yellowfive@57: self.embeds[target] = true yellowfive@57: return target yellowfive@57: end yellowfive@57: yellowfive@57: -- AceEvent:OnEmbedDisable( target ) yellowfive@57: -- target (object) - target object that is being disabled yellowfive@57: -- yellowfive@57: -- Unregister all events messages etc when the target disables. yellowfive@57: -- this method should be called by the target manually or by an addon framework yellowfive@57: function AceEvent:OnEmbedDisable(target) yellowfive@57: target:UnregisterAllEvents() yellowfive@57: target:UnregisterAllMessages() yellowfive@57: end yellowfive@57: yellowfive@57: -- Script to fire blizzard events into the event listeners yellowfive@57: local events = AceEvent.events yellowfive@57: AceEvent.frame:SetScript("OnEvent", function(this, event, ...) yellowfive@57: events:Fire(event, ...) yellowfive@57: end) yellowfive@57: yellowfive@57: --- Finally: upgrade our old embeds yellowfive@57: for target, v in pairs(AceEvent.embeds) do yellowfive@57: AceEvent:Embed(target) yellowfive@57: end