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