vanilla-wow-addons – Blame information for rev 1

Subversion Repositories:
Rev:
Rev Author Line No. Line
1 office 1  
2 -- Title: TitanRepair v0.2
3 -- Notes: Adds Durability Info to Titan Panel, also reminds you to repair
4 -- Author: lua@lumpn.de
5  
6 TITAN_REPAIR_ID = "Repair";
7  
8 REPAIR_INDEX = 0;
9 REPAIR_MONEY = 0;
10 REPAIR_ITEM_STATUS = {};
11 REPAIR_ITEM_STATUS[1] = { val = 0, max = 0, cost = 0, name = INVTYPE_HEAD, slot = "Head" };
12 REPAIR_ITEM_STATUS[2] = { val = 0, max = 0, cost = 0, name = INVTYPE_SHOULDER, slot = "Shoulder" };
13 REPAIR_ITEM_STATUS[3] = { val = 0, max = 0, cost = 0, name = INVTYPE_CHEST, slot = "Chest" };
14 REPAIR_ITEM_STATUS[4] = { val = 0, max = 0, cost = 0, name = INVTYPE_WAIST, slot = "Waist" };
15 REPAIR_ITEM_STATUS[5] = { val = 0, max = 0, cost = 0, name = INVTYPE_LEGS, slot = "Legs" };
16 REPAIR_ITEM_STATUS[6] = { val = 0, max = 0, cost = 0, name = INVTYPE_FEET, slot = "Feet" };
17 REPAIR_ITEM_STATUS[7] = { val = 0, max = 0, cost = 0, name = INVTYPE_WRIST, slot = "Wrist" };
18 REPAIR_ITEM_STATUS[8] = { val = 0, max = 0, cost = 0, name = INVTYPE_HAND, slot = "Hands" };
19 REPAIR_ITEM_STATUS[9] = { val = 0, max = 0, cost = 0, name = INVTYPE_WEAPONMAINHAND, slot = "MainHand" };
20 REPAIR_ITEM_STATUS[10] = { val = 0, max = 0, cost = 0, name = INVTYPE_WEAPONOFFHAND, slot = "SecondaryHand" };
21 REPAIR_ITEM_STATUS[11] = { val = 0, max = 0, cost = 0, name = INVTYPE_RANGED, slot = "Ranged" };
22 REPAIR_ITEM_STATUS[12] = { val = 0, max = 0, cost = 0, name = INVENTORY_TOOLTIP };
23  
24  
25 StaticPopupDialogs["REPAIR_CONFIRMATION"] = {
26 text = TEXT(REPAIR_LOCALE["confirmation"]),
27 button1 = TEXT(YES),
28 button2 = TEXT(NO),
29 OnAccept = function()
30 TitanRepair_RepairItems();
31 end,
32 OnShow = function()
33 MoneyFrame_Update(this:GetName().."MoneyFrame", REPAIR_MONEY);
34 end,
35 hasMoneyFrame = 1,
36 timeout = 0,
37 };
38  
39  
40 function TitanPanelRepairButton_OnLoad()
41 -- register plugin
42 this.registry = {
43 id = TITAN_REPAIR_ID,
44 builtIn = 1,
45 version = "0.3.1800",
46 menuText = REPAIR_LOCALE["menu"],
47 buttonTextFunction = "TitanPanelRepairButton_GetButtonText",
48 tooltipTitle = REPAIR_LOCALE["tooltip"],
49 tooltipTextFunction = "TitanPanelRepairButton_GetTooltipText",
50 icon = "Interface\\AddOns\\Titan\\Artwork\\TitanRepair",
51 iconWidth = 16,
52 savedVariables = {
53 ShowIcon = 1,
54 ShowLabelText = 1,
55 ShowItemName = TITAN_NIL,
56 ShowUndamaged = TITAN_NIL,
57 ShowPopup = TITAN_NIL,
58 ShowPercentage = TITAN_NIL,
59 ShowColoredText = TITAN_NIL,
60 RepairInventory = TITAN_NIL
61 }
62 };
63  
64 this:RegisterEvent("BAG_UPDATE");
65 this:RegisterEvent("UPDATE_INVENTORY_ALERTS");
66 this:RegisterEvent("MERCHANT_SHOW");
67 this:RegisterEvent("MERCHANT_CLOSED");
68 end
69  
70  
71 function TitanPanelRepairButton_OnEvent()
72 if (event == "MERCHANT_SHOW") then
73 if TitanGetVar(TITAN_REPAIR_ID,"ShowPopup") == 1 then
74 local repairCost, canRepair = GetRepairAllCost();
75 if (canRepair) then
76 if (TitanGetVar(TITAN_REPAIR_ID, "RepairInventory")) then
77 repairCost = repairCost + TitanRepair_GetRepairInvCost();
78 end
79 if (repairCost > 0) then
80 REPAIR_MONEY = repairCost;
81 StaticPopup_Show("REPAIR_CONFIRMATION");
82 end
83 end
84 end
85 end
86  
87 if ( event == "MERCHANT_CLOSED" ) then
88 StaticPopup_Hide("REPAIR_CONFIRMATION");
89 end
90  
91 if ( event == "UPDATE_INVENTORY_ALERTS" ) then
92 local min_status = 1.0;
93 local min_val = 0;
94 local min_max = 0;
95 local min_index = 0;
96  
97 for index, value in INVENTORY_ALERT_STATUS_SLOTS do
98  
99 local act_status, act_val, act_max, act_cost = TitanRepair_GetStatus(index);
100 if ( act_status < min_status ) then
101 min_status = act_status;
102 min_val = act_val;
103 min_max = act_max;
104 min_index = index;
105 end
106  
107 REPAIR_ITEM_STATUS[index].val = act_val;
108 REPAIR_ITEM_STATUS[index].max = act_max;
109 REPAIR_ITEM_STATUS[index].cost = act_cost;
110  
111 end
112  
113 REPAIR_INDEX = min_index;
114  
115 TitanPanelButton_UpdateButton(TITAN_REPAIR_ID);
116 TitanPanelButton_UpdateTooltip();
117 end
118  
119 if (event == "BAG_UPDATE") then
120  
121 local min_status = 1.0;
122 local min_val = 0;
123 local min_max = 0;
124  
125 REPAIR_ITEM_STATUS[12].cost = 0;
126  
127 for bag = 0, 4 do
128 for slot = 1, GetContainerNumSlots(bag) do
129  
130 local act_status, act_val, act_max, act_cost = TitanRepair_GetStatus(slot, bag);
131 if (act_status < min_status) then
132 min_status = act_status;
133 min_val = act_val;
134 min_max = act_max;
135  
136 if((REPAIR_INDEX > 0) and (act_status < TitanRepair_GetStatusPercent(REPAIR_ITEM_STATUS[REPAIR_INDEX].val, REPAIR_ITEM_STATUS[REPAIR_INDEX].max))) then
137 REPAIR_INDEX = 12;
138 end
139  
140 REPAIR_ITEM_STATUS[12].val = act_val;
141 REPAIR_ITEM_STATUS[12].max = act_max;
142 end
143 REPAIR_ITEM_STATUS[12].cost = REPAIR_ITEM_STATUS[12].cost + act_cost;
144 end
145 end
146  
147 TitanPanelButton_UpdateButton(TITAN_REPAIR_ID);
148 TitanPanelButton_UpdateTooltip();
149 end
150 end
151  
152 function TitanRepair_GetStatusPercent(val, max)
153  
154 if (max > 0) then
155 return (val / max);
156 end
157  
158 return 1.0;
159  
160 end;
161  
162  
163 function TitanRepair_GetStatus(index, bag)
164  
165 local val = 0;
166 local max = 0;
167 local cost = 0;
168  
169 local hasItem, repairCost
170 if (bag) then
171 local _, lRepairCost = TRTooltip:SetBagItem(bag, index);
172 repairCost = lRepairCost;
173 hasItem = 1;
174 else
175 local slotName = REPAIR_ITEM_STATUS[index].slot .. "Slot";
176  
177 local id = GetInventorySlotInfo(slotName);
178 local lHasItem, _, lRepairCost = TRTooltip:SetInventoryItem("player", id);
179 hasItem = lHasItem;
180 repairCost = lRepairCost;
181 end
182  
183 TRTooltip:Hide();
184  
185 if (hasItem) then
186  
187 if (repairCost) then
188 cost = repairCost;
189 end
190  
191 for i = 1, 30 do
192 local text = getglobal("TRTooltipTextLeft" .. i):GetText();
193 if (text) then
194  
195 -- find durability
196 local _, _, f_val, f_max = string.find(text, REPAIR_LOCALE["pattern"]);
197 if (f_val) then
198 val = tonumber(f_val);
199 max = tonumber(f_max);
200 end
201  
202 end
203  
204 end
205  
206 end
207  
208 return TitanRepair_GetStatusPercent(val, max), val, max, cost;
209  
210 end
211  
212  
213 function TitanRepair_GetStatusStr(index, short)
214  
215 -- skip if fully repaired
216 if (index == 0) then
217 return TitanUtils_GetHighlightText("100%");
218 end
219  
220 local valueText = "";
221 local item_status = REPAIR_ITEM_STATUS[index];
222 local item_frac = TitanRepair_GetStatusPercent(item_status.val, item_status.max);
223  
224 -- skip if empty slot
225 if (item_status.max == 0) then
226 return nil;
227 end
228  
229 -- percent or value
230 if (TitanGetVar(TITAN_REPAIR_ID,"ShowPercentage") or short) then
231 valueText = string.format("%d%%", item_frac * 100);
232 else
233 valueText = string.format("%d / %d", item_status.val, item_status.max);
234 end
235  
236 -- color
237 if (TitanGetVar(TITAN_REPAIR_ID, "ShowColoredText")) then
238 if (item_frac == 0.0) then
239 valueText = TitanUtils_GetRedText(valueText);
240 elseif (item_frac < 0.2) then
241 valueText = TitanUtils_GetNormalText(valueText);
242 elseif (item_frac < 0.9) then
243 valueText = TitanUtils_GetGreenText(valueText);
244 else
245 valueText = TitanUtils_GetHighlightText(valueText);
246 end
247 else
248 valueText = TitanUtils_GetHighlightText(valueText);
249 end
250  
251 -- name
252 if (not short or TitanGetVar(TITAN_REPAIR_ID, "ShowItemName")) then
253 valueText = valueText .. " " .. item_status.name;
254 end
255  
256 -- add repair cost
257 local item_cost = TitanRepair_GetCostStr(item_status.cost);
258 if (not short and item_cost) then
259 valueText = valueText .. "\t" .. item_cost;
260 end
261  
262 return valueText;
263  
264 end
265  
266  
267 function TitanRepair_GetCostStr(cost)
268  
269 if (cost > 0) then
270 return TitanUtils_GetHighlightText(format("%.2fg", cost / 10000));
271 end
272  
273 return nil;
274  
275 end
276  
277  
278 function TitanPanelRepairButton_GetButtonText(id)
279  
280 -- supports turning off labels
281 return REPAIR_LOCALE["button"], TitanRepair_GetStatusStr(REPAIR_INDEX, 1);
282 end
283  
284  
285 function TitanPanelRepairButton_GetTooltipText()
286  
287 local out = "";
288 local cost = 0;
289 local sum = 0;
290  
291 for i = 1, table.getn(REPAIR_ITEM_STATUS) do
292 local str = TitanRepair_GetStatusStr(i);
293 cost = REPAIR_ITEM_STATUS[i].cost;
294 sum = sum + cost;
295  
296 if ((str) and (TitanGetVar(TITAN_REPAIR_ID,"ShowUndamaged") or (cost > 0))) then
297 out = out .. str .. "\n";
298 end
299 end
300  
301 if (sum > 0) then
302 local costStr = TitanRepair_GetCostStr(sum);
303 if (costStr) then
304 out = out .. "\n" .. REPAIR_COST .. " " .. costStr;
305 end
306 else
307 out = out .. "\n" .. REPAIR_LOCALE["nothing"];
308 end
309  
310 return out;
311  
312 end
313  
314  
315 function TitanPanelRightClickMenu_PrepareRepairMenu()
316 TitanPanelRightClickMenu_AddTitle(TitanPlugins[TITAN_REPAIR_ID].menuText);
317  
318 local info = {};
319 info.text = REPAIR_LOCALE["percentage"];
320 info.func = TitanRepair_ShowPercentage;
321 info.checked = TitanGetVar(TITAN_REPAIR_ID,"ShowPercentage");
322 UIDropDownMenu_AddButton(info);
323  
324 local info = {};
325 info.text = REPAIR_LOCALE["itemname"];
326 info.func = TitanRepair_ShowItemName;
327 info.checked = TitanGetVar(TITAN_REPAIR_ID,"ShowItemName");
328 UIDropDownMenu_AddButton(info);
329  
330 local info = {};
331 info.text = REPAIR_LOCALE["undamaged"];
332 info.func = TitanRepair_ShowUndamaged;
333 info.checked = TitanGetVar(TITAN_REPAIR_ID,"ShowUndamaged");
334 UIDropDownMenu_AddButton(info);
335  
336 local info = {};
337 info.text = REPAIR_LOCALE["popup"];
338 info.func = TitanRepair_ShowPop;
339 info.checked = TitanGetVar(TITAN_REPAIR_ID,"ShowPopup");
340 UIDropDownMenu_AddButton(info);
341  
342 local info = {};
343 info.text = REPAIR_LOCALE["repinventory"];
344 info.func = TitanRepair_RepairInventory;
345 info.checked = TitanGetVar(TITAN_REPAIR_ID,"RepairInventory");
346 UIDropDownMenu_AddButton(info);
347  
348 TitanPanelRightClickMenu_AddSpacer();
349 TitanPanelRightClickMenu_AddToggleIcon(TITAN_REPAIR_ID);
350 TitanPanelRightClickMenu_AddToggleLabelText(TITAN_REPAIR_ID);
351 TitanPanelRightClickMenu_AddToggleColoredText(TITAN_REPAIR_ID);
352  
353 TitanPanelRightClickMenu_AddSpacer();
354 TitanPanelRightClickMenu_AddCommand(TITAN_PANEL_MENU_HIDE, TITAN_REPAIR_ID, TITAN_PANEL_MENU_FUNC_HIDE);
355 end
356  
357  
358 function TitanRepair_ShowPercentage()
359 TitanToggleVar(TITAN_REPAIR_ID, "ShowPercentage");
360 TitanPanelButton_UpdateButton(TITAN_REPAIR_ID);
361 end
362  
363  
364 function TitanRepair_ShowItemName()
365 TitanToggleVar(TITAN_REPAIR_ID, "ShowItemName");
366 TitanPanelButton_UpdateButton(TITAN_REPAIR_ID);
367 end
368  
369  
370 function TitanRepair_ShowUndamaged()
371 TitanToggleVar(TITAN_REPAIR_ID, "ShowUndamaged");
372 end
373  
374 function TitanRepair_ShowPop()
375 TitanToggleVar(TITAN_REPAIR_ID, "ShowPopup");
376 end
377  
378 function TitanRepair_RepairInventory()
379 TitanToggleVar(TITAN_REPAIR_ID, "RepairInventory");
380 end
381  
382 function TitanRepair_RepairItems()
383 RepairAllItems();
384  
385 if (not TitanGetVar(TITAN_REPAIR_ID, "RepairInventory")) then
386 return;
387 end
388  
389 ShowRepairCursor();
390 local bag, slot
391 for bag = 0, 4 do
392 for slot = 1, GetContainerNumSlots(bag) do
393 local _, repairCost = TRTooltip:SetBagItem(bag, slot);
394 if (repairCost and (repairCost > 0)) then
395 UseContainerItem(bag,slot);
396 end
397 end
398 end
399 HideRepairCursor();
400 end
401  
402  
403 function TitanRepair_GetRepairInvCost()
404  
405 local result = 0;
406 for bag = 0, 4 do
407 for slot = 1, GetContainerNumSlots(bag) do
408 local _, repairCost = TRTooltip:SetBagItem(bag, slot);
409 if (repairCost and (repairCost > 0)) then
410 result = result + repairCost;
411 end
412 end
413 end
414 TRTooltip:Hide();
415  
416 return result;
417 end