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