vanilla-wow-addons – Blame information for rev 1
?pathlinks?
Rev | Author | Line No. | Line |
---|---|---|---|
1 | office | 1 | -- |
2 | -- $Id: BlindFlashCount.lua 26 2005-04-17 14:46:10Z savage $ |
||
3 | -- $Date: 2005-04-17 16:46:10 +0200 (Sun, 17 Apr 2005) $ |
||
4 | -- $Rev: 26 $ |
||
5 | -- |
||
6 | |||
7 | -- Default display type of BlindFlashCount |
||
8 | BFC_Type = 1; |
||
9 | |||
10 | -- The number of powders for a "green" color indication |
||
11 | BFC_Max = 10; |
||
12 | |||
13 | -- Use colors? |
||
14 | BFC_UseColor = 1; |
||
15 | |||
16 | -- Show debug info |
||
17 | BFC_DebugLevel = 0; |
||
18 | -- ------------------------------------------- |
||
19 | -- You shouldnt be editing anything below here |
||
20 | -- unless you know what you are doing. |
||
21 | -- ------------------------------------------- |
||
22 | BFC_VERSION = "1.2"; |
||
23 | BFC_ICON_VANISH = "Interface\\Icons\\Ability_Vanish"; |
||
24 | BFC_ICON_BLIND = "Interface\\Icons\\Spell_Shadow_MindSteal"; |
||
25 | |||
26 | -- Text Display Types, i removed HotKey because its ugly and doesnt work on all bars |
||
27 | BFC_DisplayTypes = { |
||
28 | "Count", "Name", |
||
29 | }; |
||
30 | |||
31 | BFC_BlindCount = 0; |
||
32 | BFC_FlashCount = 0; |
||
33 | BFCIcons = {}; |
||
34 | |||
35 | -- Onload function, setups hook |
||
36 | function BlindFlashCount_OnLoad() |
||
37 | -- Dont load this addon for non ROGUE classes |
||
38 | local _,class = UnitClass("player"); |
||
39 | if (class ~= nil and class ~= "ROGUE") then |
||
40 | return; |
||
41 | end |
||
42 | |||
43 | -- setup hook |
||
44 | BlindFlashCount_SetupHook(1); |
||
45 | |||
46 | -- register events |
||
47 | BlindFlashCount_Register(1); |
||
48 | |||
49 | SlashCmdList["BLINDFLASHCOUNT"] = BFC_SlashCmdHandler; |
||
50 | SLASH_BLINDFLASHCOUNT1 = "/bfc"; |
||
51 | SLASH_BLINDFLASHCOUNT2 = "/bf"; |
||
52 | |||
53 | DEFAULT_CHAT_FRAME:AddMessage("BlindFlashCount "..BFC_VERSION.." AddOn loaded. Type ".. |
||
54 | GREEN_FONT_COLOR_CODE.."/bfc"..FONT_COLOR_CODE_CLOSE.." for options"); |
||
55 | end |
||
56 | |||
57 | function BlindFlashCount_Register(register) |
||
58 | if (register == 1) then |
||
59 | this:RegisterEvent("BAG_UPDATE"); |
||
60 | this:RegisterEvent("UNIT_INVENTORY_CHANGED"); |
||
61 | else |
||
62 | this:UnregisterEvent("BAG_UPDATE"); |
||
63 | this:UnregisterEvent("UNIT_INVENTORY_CHANGED"); |
||
64 | end |
||
65 | end |
||
66 | |||
67 | function BlindFlashCount_SetupHook(enable) |
||
68 | |||
69 | if (enable == 1) then |
||
70 | -- Save original GetActionText function and hook it with ours |
||
71 | if ((GetActionText ~= BFC_GetActionText) and (BFC_OriginalGetActionText == nil)) then |
||
72 | BFC_OriginalGetActionText = GetActionText; |
||
73 | GetActionText = BFC_GetActionText; |
||
74 | end |
||
75 | else |
||
76 | -- Unhook the function |
||
77 | if (GetActionText == BFC_GetActionText) then |
||
78 | GetActionText = BFC_OriginalGetActionText; |
||
79 | BFC_OriginalGetActionText = nil; |
||
80 | end |
||
81 | end |
||
82 | end |
||
83 | |||
84 | -- Something changed in our bags, update the counts |
||
85 | function BlindFlashCount_OnEvent() |
||
86 | BFC_DebugPrint("BlindFlashCount_OnEvent() -> ".. event); |
||
87 | |||
88 | if ( (event == "UNIT_INVENTORY_CHANGED") and (arg1 ~= "player") ) then |
||
89 | -- inventory change did not occur on player |
||
90 | return; |
||
91 | end |
||
92 | |||
93 | BFC_BlindCount, BFC_FlashCount = BFC_GetBlindFlashCount(); |
||
94 | BFC_UpdateIcons(); |
||
95 | end |
||
96 | |||
97 | |||
98 | function BFC_SlashCmdHandler(msg) |
||
99 | local GREEN = GREEN_FONT_COLOR_CODE; |
||
100 | local CLOSE = FONT_COLOR_CODE_CLOSE; |
||
101 | local NORMAL = NORMAL_FONT_COLOR_CODE; |
||
102 | |||
103 | if ( (not msg) or (strlen(msg) <= 0 ) or (msg == "help") ) then |
||
104 | DEFAULT_CHAT_FRAME:AddMessage(NORMAL.."BlindFlashCount "..BFC_VERSION.." usage:"..CLOSE); |
||
105 | DEFAULT_CHAT_FRAME:AddMessage(GREEN.." /bfc help "..CLOSE.." - This screen"); |
||
106 | DEFAULT_CHAT_FRAME:AddMessage(GREEN.." /bfc on | off"..CLOSE.." - Enable/Disable BlindFlashCount"); |
||
107 | DEFAULT_CHAT_FRAME:AddMessage(GREEN.." /bfc color "..CLOSE.." - Toggle use of color indication"); |
||
108 | DEFAULT_CHAT_FRAME:AddMessage(GREEN.." /bfc type "..CLOSE.." - Toggle count indication type (count/name)"); |
||
109 | elseif (msg == "on") then |
||
110 | BlindFlashCount_SetupHook(1); |
||
111 | BlindFlashCount_Register(1); |
||
112 | |||
113 | BFC_UpdateIcons(1); |
||
114 | BFC_UpdateIcons(); |
||
115 | |||
116 | DEFAULT_CHAT_FRAME:AddMessage("BlindFlashCount is now "..GREEN.."Enabled"..CLOSE); |
||
117 | |||
118 | elseif (msg == "off") then |
||
119 | BlindFlashCount_SetupHook(0); |
||
120 | BlindFlashCount_Register(0); |
||
121 | |||
122 | BFC_UpdateIcons(1); |
||
123 | |||
124 | DEFAULT_CHAT_FRAME:AddMessage("BlindFlashCount is now "..GREEN.."Disabled"..CLOSE); |
||
125 | |||
126 | elseif (msg == "color") then |
||
127 | if (BFC_UseColor == 1) then |
||
128 | BFC_UseColor = 0; |
||
129 | DEFAULT_CHAT_FRAME:AddMessage("BlindFlashCount's Color Indicator is now turned "..GREEN.."Off"..CLOSE); |
||
130 | else |
||
131 | BFC_UseColor = 1; |
||
132 | DEFAULT_CHAT_FRAME:AddMessage("BlindFlashCount's Color Indicator is now turned "..GREEN.."On"..CLOSE); |
||
133 | end |
||
134 | |||
135 | BFC_UpdateIcons(); |
||
136 | elseif (msg == "type") then |
||
137 | |||
138 | if (BFC_Type >= getn(BFC_DisplayTypes)) then |
||
139 | BFC_Type = 1; |
||
140 | else |
||
141 | BFC_Type = BFC_Type + 1; |
||
142 | end |
||
143 | |||
144 | DEFAULT_CHAT_FRAME:AddMessage("BlindFlashCount's DisplayType is set to ".. |
||
145 | GREEN..BFC_DisplayTypes[BFC_Type]..CLOSE); |
||
146 | BFC_UpdateIcons(1); |
||
147 | BFC_UpdateIcons(); |
||
148 | elseif (msg == "debug") then |
||
149 | if (BFC_DebugLevel == nil or BFC_DebugLevel == 0) then |
||
150 | BFC_DebugLevel = 1; |
||
151 | DEFAULT_CHAT_FRAME:AddMessage("BlindFlashCount Debug Information ".. |
||
152 | GREEN.."ON"..CLOSE); |
||
153 | else |
||
154 | BFC_DebugLevel = 0; |
||
155 | DEFAULT_CHAT_FRAME:AddMessage("BlindFlashCount Debug Information ".. |
||
156 | GREEN.."OFF"..CLOSE); |
||
157 | end |
||
158 | else |
||
159 | DEFAULT_CHAT_FRAME:AddMessage("Unknown BlindFlashCount command: "..GREEN..msg..CLOSE.. |
||
160 | ", try "..GREEN.."/bfc help"..CLOSE); |
||
161 | end |
||
162 | end |
||
163 | |||
164 | -- Hook function for GetActionText |
||
165 | function BFC_GetActionText(slot) |
||
166 | |||
167 | --local slotCount = getglobal(this:GetName().."Count"); |
||
168 | --local slotName = getglobal(this:GetName().."Name"); |
||
169 | --local slotHotKey = getglobal(this:GetName().."HotKey"); |
||
170 | |||
171 | if (this == nil) then |
||
172 | -- the "this" reference is nil, that means we cant get the button's name, so bail out. |
||
173 | return BFC_OriginalGetActionText(slot); |
||
174 | end |
||
175 | |||
176 | local texture = GetActionTexture(slot); |
||
177 | local slotText = getglobal(this:GetName()..BFC_DisplayTypes[BFC_Type]); |
||
178 | local buttonName = this:GetName(); |
||
179 | |||
180 | if (slotText == nil) then |
||
181 | -- sometimes the button doesnt have a full name yet, so we are trying to guess it |
||
182 | slotText = getglobal(this:GetName().."Button"..slot..BFC_DisplayTypes[BFC_Type]); |
||
183 | buttonName = this:GetName().."Button"..slot; |
||
184 | if (slotText == nil) then |
||
185 | -- failed, lets just bail out before something goes wrong |
||
186 | return BFC_OriginalGetActionText(slot); |
||
187 | end |
||
188 | end |
||
189 | |||
190 | if (texture == BFC_ICON_VANISH or texture == BFC_ICON_BLIND) then |
||
191 | -- Add this button/slot to icon table so it can be updated later |
||
192 | BFC_AddButtonSlotToIconTable(slot, buttonName); |
||
193 | |||
194 | local count = BFC_FlashCount; |
||
195 | if (texture == BFC_ICON_BLIND) then |
||
196 | count = BFC_BlindCount; |
||
197 | end |
||
198 | |||
199 | BFC_SetColor(slotText, count); |
||
200 | |||
201 | if (BFC_DisplayTypes[BFC_Type] == "Name") then |
||
202 | -- GetActionText is called from a "Name" button. |
||
203 | -- return the count, so it gets updated with SetText. |
||
204 | return count; |
||
205 | else |
||
206 | slotText:SetText(count); |
||
207 | end |
||
208 | end |
||
209 | |||
210 | return BFC_OriginalGetActionText(slot); |
||
211 | end |
||
212 | |||
213 | -- Sets the color of the powder count |
||
214 | function BFC_SetColor(button, count) |
||
215 | local r, g, b; |
||
216 | |||
217 | if ((count == "") or (BFC_UseColor == 0)) then |
||
218 | button:SetTextColor(1.0, 1.0, 1.0); |
||
219 | return; |
||
220 | end |
||
221 | |||
222 | if ( (count < 0) or (count > BFC_Max) ) then |
||
223 | count = BFC_Max |
||
224 | end |
||
225 | count = count / BFC_Max; |
||
226 | |||
227 | if(count > 0.5) then |
||
228 | r = (1.0 - count) * 2; |
||
229 | g = 1.0; |
||
230 | else |
||
231 | r = 1.0; |
||
232 | g = count * 2; |
||
233 | end |
||
234 | |||
235 | b = 0.0; |
||
236 | button:SetTextColor(r, g, b); |
||
237 | end; |
||
238 | |||
239 | -- Returns the itemname for bag, slot (code from CapnBry) |
||
240 | function BFC_GetItemName(bag, slot) |
||
241 | local linktext = nil; |
||
242 | |||
243 | if (bag == -1) then |
||
244 | linktext = GetInventoryItemLink("player", slot); |
||
245 | else |
||
246 | linktext = GetContainerItemLink(bag, slot); |
||
247 | end |
||
248 | |||
249 | if linktext then |
||
250 | local _,_,name = string.find(linktext, "^.*%[(.*)%].*$"); |
||
251 | return name; |
||
252 | else |
||
253 | return ""; |
||
254 | end; |
||
255 | end; |
||
256 | |||
257 | -- Returns blind, flash count |
||
258 | function BFC_GetBlindFlashCount() |
||
259 | local blind = 0; |
||
260 | local flash = 0; |
||
261 | |||
262 | for bag = 0, 4, 1 do |
||
263 | for slot = 1, GetContainerNumSlots(bag), 1 do |
||
264 | local name = BFC_GetItemName(bag, slot); |
||
265 | -- check if item is a blinding powder |
||
266 | if (name == BFC_ITEM_BLIND) then |
||
267 | local _, itemCount = GetContainerItemInfo(bag, slot); |
||
268 | blind = blind + itemCount; |
||
269 | -- check if item is a flash powder |
||
270 | elseif (name == BFC_ITEM_FLASH) then |
||
271 | local _, itemCount = GetContainerItemInfo(bag, slot); |
||
272 | flash = flash + itemCount; |
||
273 | end |
||
274 | end |
||
275 | end |
||
276 | |||
277 | return blind, flash; |
||
278 | end |
||
279 | |||
280 | |||
281 | -- Updates the buttons in the icon table with the new blind/flash count |
||
282 | -- mode 1 = refresh all text labels, used when changing display type |
||
283 | -- mode 2 = clear all text, used for turning off BlindFlashCount |
||
284 | function BFC_UpdateIcons(clear) |
||
285 | local newIcons = BFCIcons; |
||
286 | |||
287 | for k, v in BFCIcons do |
||
288 | --local slot = getglobal(button):GetID(); |
||
289 | local slot = v[1]; |
||
290 | local slotText = getglobal(v[2]..BFC_DisplayTypes[BFC_Type]); |
||
291 | |||
292 | local texture = GetActionTexture(slot); |
||
293 | local count = ""; |
||
294 | if (texture == BFC_ICON_VANISH) then |
||
295 | BFC_DebugPrint("!!updating button: ".. v[2] .. " id: "..slot); |
||
296 | count = BFC_FlashCount; |
||
297 | elseif (texture == BFC_ICON_BLIND) then |
||
298 | BFC_DebugPrint("!!updating button: ".. v[2] .. " id: "..slot); |
||
299 | count = BFC_BlindCount; |
||
300 | else |
||
301 | -- icon became something else, remove from table |
||
302 | BFC_DebugPrint("--removing stale icon ".. v[2] .. " id: "..slot); |
||
303 | tremove(newIcons, key); |
||
304 | end |
||
305 | |||
306 | if (clear == 1) then |
||
307 | for k, displaytype in BFC_DisplayTypes do |
||
308 | local oldText = getglobal(v[2]..displaytype); |
||
309 | BFC_DebugPrint("****Name: " .. oldText:GetName()); |
||
310 | oldText:SetText(""); |
||
311 | oldText:SetTextColor(1.0, 1.0, 1.0); |
||
312 | end |
||
313 | else |
||
314 | slotText:SetText(count); |
||
315 | BFC_SetColor(slotText, count); |
||
316 | end |
||
317 | end |
||
318 | |||
319 | BFCIcons = newIcons; |
||
320 | end |
||
321 | |||
322 | -- Adds a button + slot to the icon table, so it can be updated later |
||
323 | function BFC_AddButtonSlotToIconTable(slot, button) |
||
324 | if (slot == nil or button == nil) then |
||
325 | return; |
||
326 | end |
||
327 | |||
328 | BFC_DebugPrint("++Adding button: " .. button .. " id: " .. slot); |
||
329 | |||
330 | for k, v in BFCIcons do |
||
331 | if (v[1] == slot and v[2] == button) then |
||
332 | BFCIcons[k] = {slot, button}; |
||
333 | BFC_DebugPrint("**Duplicate Button: " .. button .. " id: " .. slot); |
||
334 | return; |
||
335 | end |
||
336 | end |
||
337 | |||
338 | if (not BFCIcons) then |
||
339 | BFCIcons = {}; |
||
340 | end |
||
341 | |||
342 | tinsert(BFCIcons, {slot, button}); |
||
343 | end |
||
344 | |||
345 | -- some debug functions |
||
346 | -- |
||
347 | function BFC_DebugPrint(msg, level) |
||
348 | if (not BFC_DebugLevel) then |
||
349 | BFC_DebugLevel = 0; |
||
350 | end |
||
351 | |||
352 | if (not level) then |
||
353 | level = 1; |
||
354 | end |
||
355 | |||
356 | if (BFC_DebugLevel >= level) then |
||
357 | DEFAULT_CHAT_FRAME:AddMessage("|cff00caca[BFC]|r " .. msg); |
||
358 | end |
||
359 | end |
||
360 | |||
361 | function BFC_SetBlindCount(count) |
||
362 | BFC_BlindCount = count; |
||
363 | BFC_UpdateIcons(); |
||
364 | end |
||
365 | |||
366 | function BFC_SetFlashCount(count) |
||
367 | BFC_FlashCount = count; |
||
368 | BFC_UpdateIcons(); |
||
369 | end |
||
370 | |||
371 | function BFC_SetMax(max) |
||
372 | BFC_Max = max; |
||
373 | BFC_UpdateIcons(); |
||
374 | end |
||
375 | |||
376 | function BFC_SetDebug(level) |
||
377 | BFC_DebugLevel = level; |
||
378 | end |