vanilla-wow-addons – Blame information for rev 1
?pathlinks?
Rev | Author | Line No. | Line |
---|---|---|---|
1 | office | 1 | --[[ |
2 | BScript |
||
3 | Functions for creating and calling events on the fly for Bongos |
||
4 | --]] |
||
5 | |||
6 | local eventScripts = {}; --do stuff, not attached to a specific bar |
||
7 | local barScripts = {}; --do stuff, attached to a barID, or all bars |
||
8 | local eventFrame = CreateFrame("Frame"); |
||
9 | |||
10 | local function UnregisterUnused(unusedEvents) |
||
11 | for event in unusedEvents do |
||
12 | eventFrame:UnregisterEvent(event); |
||
13 | end |
||
14 | end |
||
15 | |||
16 | BScript = { |
||
17 | --[[ Add ]]-- |
||
18 | AddEventAction = function(event, action, runNow) |
||
19 | assert(event, "No event given"); |
||
20 | |||
21 | if action then |
||
22 | if not eventScripts[event] then |
||
23 | eventScripts[event] = {}; |
||
24 | if not barScripts[event] then |
||
25 | eventFrame:RegisterEvent(event); |
||
26 | end |
||
27 | end |
||
28 | table.insert(eventScripts[event], action); |
||
29 | |||
30 | if runNow then |
||
31 | action(action); |
||
32 | end |
||
33 | end |
||
34 | end, |
||
35 | |||
36 | AddStartupAction = function(action, runNow) |
||
37 | if action then |
||
38 | BScript.AddEventAction("VARIABLES_LOADED", action) |
||
39 | end |
||
40 | end, |
||
41 | |||
42 | AddBarEventAction = function(barID, event, action, runNow) |
||
43 | assert(barID, "No barID given"); |
||
44 | assert(event, "No event given"); |
||
45 | |||
46 | if action then |
||
47 | if not barScripts[event] then |
||
48 | if not eventScripts[event] then |
||
49 | eventFrame:RegisterEvent(event); |
||
50 | end |
||
51 | barScripts[event] = {}; |
||
52 | end |
||
53 | barScripts[event][barID] = action; |
||
54 | if runNow then |
||
55 | BScript.CallBarEventForBar(event, barID); |
||
56 | end |
||
57 | end |
||
58 | end, |
||
59 | |||
60 | --[[ Removal ]]-- |
||
61 | |||
62 | RemoveEventAction = function(event, action) |
||
63 | assert(event, "No event given."); |
||
64 | assert(action, "No action given"); |
||
65 | |||
66 | if eventScripts[event] then |
||
67 | for i, eventAction in pairs(eventScripts[event]) do |
||
68 | if eventAction == action then |
||
69 | table.remove(eventScripts[event], i) |
||
70 | break; |
||
71 | end |
||
72 | end |
||
73 | if table.getn(eventScripts[event]) == 0 then |
||
74 | eventScripts[event] = nil; |
||
75 | if not barScripts[event] then |
||
76 | eventFrame:UnregisterEvent(event); |
||
77 | end |
||
78 | end |
||
79 | end |
||
80 | end, |
||
81 | |||
82 | RemoveEvent = function(event) |
||
83 | assert(event, "No event given"); |
||
84 | assert(event == "VARIABLES_LOADED", "Cannot remove event: " .. event); |
||
85 | |||
86 | if eventScripts[event] then |
||
87 | eventScripts[event] = nil; |
||
88 | if not barScripts[event] then |
||
89 | eventFrame:UnregisterEvent(event); |
||
90 | end |
||
91 | end |
||
92 | end, |
||
93 | |||
94 | RemoveAllEvents = function() |
||
95 | local unusedEvents = {}; |
||
96 | |||
97 | for event in eventScripts[event] do |
||
98 | if event ~= "VARIABLES_LOADED" then |
||
99 | unusedEvents[event] = true |
||
100 | eventScripts[event] = nil; |
||
101 | end |
||
102 | end |
||
103 | for event in barScripts[event] do |
||
104 | if unusedEvents[event] then |
||
105 | unusedEvents[event] = nil; |
||
106 | end |
||
107 | end |
||
108 | |||
109 | UnregisterUnused(unusedEvents); |
||
110 | end, |
||
111 | |||
112 | --bar events |
||
113 | RemoveEventForBar = function(event, barID) |
||
114 | assert(barID, "No barID given"); |
||
115 | assert(event, "No event given"); |
||
116 | |||
117 | if barScripts[event] and barScripts[event][barID] then |
||
118 | barScripts[event][barID] = nil; |
||
119 | for i in barScripts[event] do |
||
120 | return; |
||
121 | end |
||
122 | --there weren't any bar actions for the given event, so check if its used |
||
123 | barScripts[event] = nil; |
||
124 | if not eventScripts[event] then |
||
125 | eventFrame:UnregisterEvent(event); |
||
126 | end |
||
127 | end |
||
128 | end, |
||
129 | |||
130 | RemoveAllEventsForBar = function(barID) |
||
131 | assert(barID, "No barID given"); |
||
132 | |||
133 | for event in barScripts do |
||
134 | if barScripts[event][barID] then |
||
135 | barScripts[event][barID] = nil; |
||
136 | |||
137 | local eventUsed; |
||
138 | for i in barScripts[event] do |
||
139 | eventUsed = true; |
||
140 | break; |
||
141 | end |
||
142 | if not eventUsed then |
||
143 | barScripts[event] = nil; |
||
144 | if not eventScripts[event] then |
||
145 | eventFrame:UnregisterEvent(event); |
||
146 | end |
||
147 | end |
||
148 | end |
||
149 | end |
||
150 | end, |
||
151 | |||
152 | --[[ Execution ]]-- |
||
153 | CallEvent = function(event) |
||
154 | assert(event, "No event given"); |
||
155 | |||
156 | if eventScripts[event] then |
||
157 | for _, action in pairs(eventScripts[event]) do |
||
158 | action(action); |
||
159 | end |
||
160 | end |
||
161 | end, |
||
162 | |||
163 | CallStartup = function() |
||
164 | BScript.CallEvent("VARIABLES_LOADED"); |
||
165 | end, |
||
166 | |||
167 | CallBarEvent = function(event) |
||
168 | assert(event, "No event given"); |
||
169 | |||
170 | if barScripts[event] then |
||
171 | for barID in barScripts[event] do |
||
172 | BScript.CallBarEventForBar(event, barID); |
||
173 | end |
||
174 | end |
||
175 | end, |
||
176 | |||
177 | CallBarEventForBar = function(event, barList) |
||
178 | assert(event, "No event given"); |
||
179 | assert(barList, "No bar list given"); |
||
180 | |||
181 | if barScripts[event] and barScripts[event][barList] then |
||
182 | if barList == "all" then |
||
183 | BBar.ForAll(barScripts[event][barList], event); |
||
184 | else |
||
185 | for barID in string.gfind(barList, "[^%s]+") do |
||
186 | local _, _, startID, endID = string.find(barID, "(%d+)-(%d+)") |
||
187 | if startID then |
||
188 | for id = tonumber(startID), tonumber(endID) do |
||
189 | local bar = BBar.IDToBar(id); |
||
190 | if bar then |
||
191 | barScripts[event][barList](bar, event); |
||
192 | end |
||
193 | end |
||
194 | else |
||
195 | local bar = BBar.IDToBar(barID); |
||
196 | if bar then |
||
197 | barScripts[event][barList](bar, event); |
||
198 | end |
||
199 | end |
||
200 | end |
||
201 | end |
||
202 | end |
||
203 | end, |
||
204 | } |
||
205 | |||
206 | eventFrame:SetScript("OnEvent", function() |
||
207 | BScript.CallEvent(event); |
||
208 | BScript.CallBarEvent(event); |
||
209 | end); |