Mercurial > wow > icu
comparison Libs/AceEvent-3.0/AceEvent-3.0.lua @ 0:98c6f55e6619
First commit
| author | Xiiph |
|---|---|
| date | Sat, 05 Feb 2011 16:45:02 +0100 |
| parents | |
| children |
comparison
equal
deleted
inserted
replaced
| -1:000000000000 | 0:98c6f55e6619 |
|---|---|
| 1 --- AceEvent-3.0 provides event registration and secure dispatching. | |
| 2 -- All dispatching is done using **CallbackHandler-1.0**. AceEvent is a simple wrapper around | |
| 3 -- CallbackHandler, and dispatches all game events or addon message to the registrees. | |
| 4 -- | |
| 5 -- **AceEvent-3.0** can be embeded into your addon, either explicitly by calling AceEvent:Embed(MyAddon) or by | |
| 6 -- specifying it as an embeded library in your AceAddon. All functions will be available on your addon object | |
| 7 -- and can be accessed directly, without having to explicitly call AceEvent itself.\\ | |
| 8 -- It is recommended to embed AceEvent, otherwise you'll have to specify a custom `self` on all calls you | |
| 9 -- make into AceEvent. | |
| 10 -- @class file | |
| 11 -- @name AceEvent-3.0 | |
| 12 -- @release $Id: AceEvent-3.0.lua 975 2010-10-23 11:26:18Z nevcairiel $ | |
| 13 local MAJOR, MINOR = "AceEvent-3.0", 3 | |
| 14 local AceEvent = LibStub:NewLibrary(MAJOR, MINOR) | |
| 15 | |
| 16 if not AceEvent then return end | |
| 17 | |
| 18 -- Lua APIs | |
| 19 local pairs = pairs | |
| 20 | |
| 21 local CallbackHandler = LibStub:GetLibrary("CallbackHandler-1.0") | |
| 22 | |
| 23 AceEvent.frame = AceEvent.frame or CreateFrame("Frame", "AceEvent30Frame") -- our event frame | |
| 24 AceEvent.embeds = AceEvent.embeds or {} -- what objects embed this lib | |
| 25 | |
| 26 -- APIs and registry for blizzard events, using CallbackHandler lib | |
| 27 if not AceEvent.events then | |
| 28 AceEvent.events = CallbackHandler:New(AceEvent, | |
| 29 "RegisterEvent", "UnregisterEvent", "UnregisterAllEvents") | |
| 30 end | |
| 31 | |
| 32 function AceEvent.events:OnUsed(target, eventname) | |
| 33 AceEvent.frame:RegisterEvent(eventname) | |
| 34 end | |
| 35 | |
| 36 function AceEvent.events:OnUnused(target, eventname) | |
| 37 AceEvent.frame:UnregisterEvent(eventname) | |
| 38 end | |
| 39 | |
| 40 | |
| 41 -- APIs and registry for IPC messages, using CallbackHandler lib | |
| 42 if not AceEvent.messages then | |
| 43 AceEvent.messages = CallbackHandler:New(AceEvent, | |
| 44 "RegisterMessage", "UnregisterMessage", "UnregisterAllMessages" | |
| 45 ) | |
| 46 AceEvent.SendMessage = AceEvent.messages.Fire | |
| 47 end | |
| 48 | |
| 49 --- embedding and embed handling | |
| 50 local mixins = { | |
| 51 "RegisterEvent", "UnregisterEvent", | |
| 52 "RegisterMessage", "UnregisterMessage", | |
| 53 "SendMessage", | |
| 54 "UnregisterAllEvents", "UnregisterAllMessages", | |
| 55 } | |
| 56 | |
| 57 --- Register for a Blizzard Event. | |
| 58 -- 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) | |
| 59 -- Any arguments to the event will be passed on after that. | |
| 60 -- @name AceEvent:RegisterEvent | |
| 61 -- @class function | |
| 62 -- @paramsig event[, callback [, arg]] | |
| 63 -- @param event The event to register for | |
| 64 -- @param callback The callback function to call when the event is triggered (funcref or method, defaults to a method with the event name) | |
| 65 -- @param arg An optional argument to pass to the callback function | |
| 66 | |
| 67 --- Unregister an event. | |
| 68 -- @name AceEvent:UnregisterEvent | |
| 69 -- @class function | |
| 70 -- @paramsig event | |
| 71 -- @param event The event to unregister | |
| 72 | |
| 73 --- Register for a custom AceEvent-internal message. | |
| 74 -- 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) | |
| 75 -- Any arguments to the event will be passed on after that. | |
| 76 -- @name AceEvent:RegisterMessage | |
| 77 -- @class function | |
| 78 -- @paramsig message[, callback [, arg]] | |
| 79 -- @param message The message to register for | |
| 80 -- @param callback The callback function to call when the message is triggered (funcref or method, defaults to a method with the event name) | |
| 81 -- @param arg An optional argument to pass to the callback function | |
| 82 | |
| 83 --- Unregister a message | |
| 84 -- @name AceEvent:UnregisterMessage | |
| 85 -- @class function | |
| 86 -- @paramsig message | |
| 87 -- @param message The message to unregister | |
| 88 | |
| 89 --- Send a message over the AceEvent-3.0 internal message system to other addons registered for this message. | |
| 90 -- @name AceEvent:SendMessage | |
| 91 -- @class function | |
| 92 -- @paramsig message, ... | |
| 93 -- @param message The message to send | |
| 94 -- @param ... Any arguments to the message | |
| 95 | |
| 96 | |
| 97 -- Embeds AceEvent into the target object making the functions from the mixins list available on target:.. | |
| 98 -- @param target target object to embed AceEvent in | |
| 99 function AceEvent:Embed(target) | |
| 100 for k, v in pairs(mixins) do | |
| 101 target[v] = self[v] | |
| 102 end | |
| 103 self.embeds[target] = true | |
| 104 return target | |
| 105 end | |
| 106 | |
| 107 -- AceEvent:OnEmbedDisable( target ) | |
| 108 -- target (object) - target object that is being disabled | |
| 109 -- | |
| 110 -- Unregister all events messages etc when the target disables. | |
| 111 -- this method should be called by the target manually or by an addon framework | |
| 112 function AceEvent:OnEmbedDisable(target) | |
| 113 target:UnregisterAllEvents() | |
| 114 target:UnregisterAllMessages() | |
| 115 end | |
| 116 | |
| 117 -- Script to fire blizzard events into the event listeners | |
| 118 local events = AceEvent.events | |
| 119 AceEvent.frame:SetScript("OnEvent", function(this, event, ...) | |
| 120 events:Fire(event, ...) | |
| 121 end) | |
| 122 | |
| 123 --- Finally: upgrade our old embeds | |
| 124 for target, v in pairs(AceEvent.embeds) do | |
| 125 AceEvent:Embed(target) | |
| 126 end |
