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