Zerotorescue@0
|
1 local addon = LibStub("AceAddon-3.0"):GetAddon("Inventory");
|
Zerotorescue@0
|
2 local mod = addon:NewModule("Summary", "AceEvent-3.0");
|
Zerotorescue@0
|
3
|
Zerotorescue@0
|
4 local AceGUI = LibStub("AceGUI-3.0");
|
Zerotorescue@0
|
5
|
Zerotorescue@0
|
6 function mod:OnEnable()
|
Zerotorescue@0
|
7 self:RegisterWidgets();
|
Zerotorescue@0
|
8
|
Zerotorescue@0
|
9 self:BuildMain();
|
Zerotorescue@0
|
10 self:Build();
|
Zerotorescue@0
|
11 end
|
Zerotorescue@0
|
12
|
Zerotorescue@0
|
13 function mod:RegisterWidgets()
|
Zerotorescue@0
|
14 -- Register InlineGroupWithButton-widget
|
Zerotorescue@0
|
15 -- This widget adds a button next to the header of an inline group
|
Zerotorescue@0
|
16 -- SetPoint doesn't seem usable within AceGUI.
|
Zerotorescue@0
|
17
|
Zerotorescue@0
|
18 local widgetType = "InlineGroupWithButton";
|
Zerotorescue@0
|
19 local widgetVersion = 1;
|
Zerotorescue@0
|
20
|
Zerotorescue@0
|
21 local function Constructor()
|
Zerotorescue@0
|
22 local widget = AceGUI:Create("InlineGroup");
|
Zerotorescue@0
|
23 widget.type = widgetType;
|
Zerotorescue@0
|
24
|
Zerotorescue@0
|
25 widget.MakeButton = function(self, buttonSettings)
|
Zerotorescue@0
|
26 if type(buttonSettings) == "table" then
|
Zerotorescue@0
|
27 local button = CreateFrame("Button", nil, self.frame, "UIPanelButtonTemplate");
|
Zerotorescue@0
|
28 button:SetText(buttonSettings.name);
|
Zerotorescue@0
|
29 button:SetHeight(22);
|
Zerotorescue@0
|
30 button:SetWidth(120);
|
Zerotorescue@0
|
31 button:SetPoint("TOPRIGHT", self.frame, "TOPRIGHT", -10, 5);
|
Zerotorescue@0
|
32 button:SetScript("OnClick", buttonSettings.exec);
|
Zerotorescue@0
|
33 button.tooltipTitle = buttonSettings.name;
|
Zerotorescue@0
|
34 button.tooltip = buttonSettings.desc or "";
|
Zerotorescue@0
|
35 button:SetScript("OnEnter", function(self)
|
Zerotorescue@0
|
36 GameTooltip:SetOwner(self, "ANCHOR_NONE")
|
Zerotorescue@0
|
37 GameTooltip:SetPoint("BOTTOM", self, "TOP")
|
Zerotorescue@0
|
38 GameTooltip:SetText(self.tooltipTitle, 1, .82, 0, 1)
|
Zerotorescue@0
|
39
|
Zerotorescue@0
|
40 if type(self.tooltip) == "string" then
|
Zerotorescue@0
|
41 GameTooltip:AddLine(self.tooltip, 1, 1, 1, 1);
|
Zerotorescue@0
|
42 end
|
Zerotorescue@0
|
43
|
Zerotorescue@0
|
44 GameTooltip:Show();
|
Zerotorescue@0
|
45 end);
|
Zerotorescue@0
|
46 button:SetScript("OnLeave", function(self)
|
Zerotorescue@0
|
47 GameTooltip:Hide();
|
Zerotorescue@0
|
48 end);
|
Zerotorescue@0
|
49 else
|
Zerotorescue@0
|
50 error("settings must be a table - usage: MakeButton(table);");
|
Zerotorescue@0
|
51 end
|
Zerotorescue@0
|
52 end
|
Zerotorescue@0
|
53
|
Zerotorescue@0
|
54 return widget;
|
Zerotorescue@0
|
55 end
|
Zerotorescue@0
|
56
|
Zerotorescue@0
|
57 AceGUI:RegisterWidgetType(widgetType, Constructor, widgetVersion);
|
Zerotorescue@0
|
58 end
|
Zerotorescue@0
|
59
|
Zerotorescue@0
|
60 function mod:BuildMain()
|
Zerotorescue@0
|
61 local frame = AceGUI:Create("Frame");
|
Zerotorescue@0
|
62 frame:SetTitle("Inventory Summary");
|
Zerotorescue@0
|
63 frame:SetLayout("Fill");
|
Zerotorescue@0
|
64
|
Zerotorescue@0
|
65 local scrollFrame = AceGUI:Create("ScrollFrame");
|
Zerotorescue@0
|
66 scrollFrame:SetLayout("Flow");
|
Zerotorescue@0
|
67
|
Zerotorescue@0
|
68 frame:AddChild(scrollFrame);
|
Zerotorescue@0
|
69
|
Zerotorescue@0
|
70 mod.scrollFrame = scrollFrame;
|
Zerotorescue@0
|
71 end
|
Zerotorescue@0
|
72
|
Zerotorescue@0
|
73 local temp = {};
|
Zerotorescue@0
|
74
|
Zerotorescue@0
|
75 local sortMethod = "item";
|
Zerotorescue@0
|
76 local sortDirectory = "ASC";
|
Zerotorescue@0
|
77 local function ReSort(subject)
|
Zerotorescue@0
|
78 if sortMethod == subject then
|
Zerotorescue@0
|
79 sortDirectory = (sortDirectory == "ASC" and "DESC") or "ASC";
|
Zerotorescue@0
|
80 else
|
Zerotorescue@0
|
81 sortDirectory = "ASC";
|
Zerotorescue@0
|
82 end
|
Zerotorescue@0
|
83 sortMethod = subject;
|
Zerotorescue@0
|
84
|
Zerotorescue@0
|
85 mod:Build();
|
Zerotorescue@0
|
86 end
|
Zerotorescue@0
|
87
|
Zerotorescue@0
|
88 function mod:Build()
|
Zerotorescue@0
|
89 mod.scrollFrame:ReleaseChildren();
|
Zerotorescue@0
|
90
|
Zerotorescue@0
|
91 for groupName, values in pairs(addon.db.global.groups) do
|
Zerotorescue@0
|
92 if values.items then
|
Zerotorescue@0
|
93 -- Get group settings
|
Zerotorescue@0
|
94 local stockRequired = values.minimumStock or addon.db.global.defaults.minimumStock;
|
Zerotorescue@0
|
95 local showWhenBelow = (values.summaryThresholdShow or addon.db.global.defaults.summaryThresholdShow);
|
Zerotorescue@0
|
96
|
Zerotorescue@0
|
97 -- Make group container
|
Zerotorescue@0
|
98 local iGroup = AceGUI:Create("InlineGroupWithButton");
|
Zerotorescue@0
|
99 iGroup:SetTitle(groupName);
|
Zerotorescue@0
|
100 iGroup:SetFullWidth(true);
|
Zerotorescue@0
|
101 iGroup:SetLayout("Flow");
|
Zerotorescue@0
|
102 iGroup:MakeButton({
|
Zerotorescue@0
|
103 name = "Queue",
|
Zerotorescue@0
|
104 desc = "Queue all items in this group.",
|
Zerotorescue@0
|
105 exec = function()
|
Zerotorescue@0
|
106 print(groupName);
|
Zerotorescue@0
|
107 end,
|
Zerotorescue@0
|
108 });
|
Zerotorescue@0
|
109
|
Zerotorescue@0
|
110 -- Headers
|
Zerotorescue@0
|
111 -- Itemlink
|
Zerotorescue@0
|
112 local lblItem = AceGUI:Create("InteractiveLabel");
|
Zerotorescue@0
|
113 lblItem:SetText("|cfffed000Item|r");
|
Zerotorescue@0
|
114 lblItem:SetFontObject(GameFontHighlight);
|
Zerotorescue@0
|
115 lblItem:SetRelativeWidth(0.7);
|
Zerotorescue@0
|
116 lblItem:SetCallback("OnClick", function() ReSort("item"); end);
|
Zerotorescue@0
|
117
|
Zerotorescue@0
|
118 iGroup:AddChild(lblItem);
|
Zerotorescue@0
|
119
|
Zerotorescue@0
|
120 -- Current quantity
|
Zerotorescue@0
|
121 local lblQuantity = AceGUI:Create("InteractiveLabel");
|
Zerotorescue@0
|
122 lblQuantity:SetText("|cfffed000Cur.|r");
|
Zerotorescue@0
|
123 lblQuantity:SetFontObject(GameFontHighlight);
|
Zerotorescue@0
|
124 lblQuantity:SetRelativeWidth(0.149);
|
Zerotorescue@0
|
125 lblQuantity:SetCallback("OnClick", function() ReSort("current"); end);
|
Zerotorescue@0
|
126
|
Zerotorescue@0
|
127 iGroup:AddChild(lblQuantity);
|
Zerotorescue@0
|
128
|
Zerotorescue@0
|
129 -- Required stock
|
Zerotorescue@0
|
130 local lblStockRequired = AceGUI:Create("InteractiveLabel");
|
Zerotorescue@0
|
131 lblStockRequired:SetText("|cfffed000Req.|r");
|
Zerotorescue@0
|
132 lblStockRequired:SetFontObject(GameFontHighlight);
|
Zerotorescue@0
|
133 lblStockRequired:SetRelativeWidth(0.149);
|
Zerotorescue@0
|
134 lblStockRequired:SetCallback("OnClick", function() ReSort("percentage"); end);
|
Zerotorescue@0
|
135
|
Zerotorescue@0
|
136 iGroup:AddChild(lblStockRequired);
|
Zerotorescue@0
|
137
|
Zerotorescue@0
|
138 -- Sort item list
|
Zerotorescue@0
|
139 for itemId in pairs(values.items) do
|
Zerotorescue@0
|
140 local itemName, itemLink, itemRarity = GetItemInfo(itemId);
|
Zerotorescue@0
|
141
|
Zerotorescue@0
|
142 table.insert(temp, {
|
Zerotorescue@0
|
143 id = itemId,
|
Zerotorescue@0
|
144 name = itemName,
|
Zerotorescue@0
|
145 link = itemLink,
|
Zerotorescue@0
|
146 rarity = itemRarity,
|
Zerotorescue@0
|
147 count = addon:GetItemCount(itemId),
|
Zerotorescue@0
|
148 });
|
Zerotorescue@0
|
149 end
|
Zerotorescue@0
|
150
|
Zerotorescue@0
|
151 local sortNameFormat = "%d%s"; --rarity .. name
|
Zerotorescue@0
|
152 table.sort(temp, function(a, b)
|
Zerotorescue@0
|
153 if sortMethod == "item" then
|
Zerotorescue@0
|
154 if sortDirectory == "ASC" then
|
Zerotorescue@0
|
155 return sortNameFormat:format((7 - a.rarity), a.name):upper() < sortNameFormat:format((7 - b.rarity), b.name):upper();
|
Zerotorescue@0
|
156 else
|
Zerotorescue@0
|
157 return sortNameFormat:format((7 - a.rarity), a.name):upper() > sortNameFormat:format((7 - b.rarity), b.name):upper();
|
Zerotorescue@0
|
158 end
|
Zerotorescue@0
|
159 elseif sortMethod == "current" then
|
Zerotorescue@0
|
160 if sortDirectory == "ASC" then
|
Zerotorescue@0
|
161 return a.count < b.count;
|
Zerotorescue@0
|
162 else
|
Zerotorescue@0
|
163 return a.count > b.count;
|
Zerotorescue@0
|
164 end
|
Zerotorescue@0
|
165 elseif sortMethod == "percentage" then
|
Zerotorescue@0
|
166 if sortDirectory == "ASC" then
|
Zerotorescue@0
|
167 return ( a.count / stockRequired ) < ( b.count / stockRequired );
|
Zerotorescue@0
|
168 else
|
Zerotorescue@0
|
169 return ( a.count / stockRequired ) > ( b.count / stockRequired );
|
Zerotorescue@0
|
170 end
|
Zerotorescue@0
|
171 end
|
Zerotorescue@0
|
172 end);
|
Zerotorescue@0
|
173
|
Zerotorescue@0
|
174 -- Show stuff
|
Zerotorescue@0
|
175 for _, item in pairs(temp) do
|
Zerotorescue@0
|
176 if ( item.count / stockRequired ) < showWhenBelow then
|
Zerotorescue@0
|
177 local btnItemLink = AceGUI:Create("ItemLinkButton");
|
Zerotorescue@0
|
178 btnItemLink:SetText(item.id);
|
Zerotorescue@0
|
179 btnItemLink:SetRelativeWidth(0.7);
|
Zerotorescue@0
|
180 btnItemLink:SetCallback("OnEnter", function() end);
|
Zerotorescue@0
|
181
|
Zerotorescue@0
|
182 iGroup:AddChild(btnItemLink);
|
Zerotorescue@0
|
183
|
Zerotorescue@0
|
184 -- Current quantity
|
Zerotorescue@0
|
185 local lblQuantity = AceGUI:Create("Label");
|
Zerotorescue@0
|
186 lblQuantity:SetText(self:ColorCode(item.count, stockRequired));
|
Zerotorescue@0
|
187 lblQuantity:SetRelativeWidth(0.149);
|
Zerotorescue@0
|
188
|
Zerotorescue@0
|
189 iGroup:AddChild(lblQuantity);
|
Zerotorescue@0
|
190
|
Zerotorescue@0
|
191 -- Required stock
|
Zerotorescue@0
|
192 local lblStockRequired = AceGUI:Create("Label");
|
Zerotorescue@0
|
193 lblStockRequired:SetText(stockRequired);
|
Zerotorescue@0
|
194 lblStockRequired:SetRelativeWidth(0.149);
|
Zerotorescue@0
|
195
|
Zerotorescue@0
|
196 iGroup:AddChild(lblStockRequired);
|
Zerotorescue@0
|
197 end
|
Zerotorescue@0
|
198 end
|
Zerotorescue@0
|
199
|
Zerotorescue@0
|
200 -- We no longer need this, so forget about it
|
Zerotorescue@0
|
201 table.wipe(temp);
|
Zerotorescue@0
|
202
|
Zerotorescue@0
|
203 mod.scrollFrame:AddChild(iGroup);
|
Zerotorescue@0
|
204 end
|
Zerotorescue@0
|
205 end
|
Zerotorescue@0
|
206 end
|
Zerotorescue@0
|
207
|
Zerotorescue@0
|
208 function mod:ColorCode(num, required)
|
Zerotorescue@0
|
209 local percentage = ( num / required );
|
Zerotorescue@0
|
210
|
Zerotorescue@0
|
211 if percentage >= addon.db.global.defaults.colors.green then
|
Zerotorescue@0
|
212 return ("|cff00ff00%d|r"):format(num);
|
Zerotorescue@0
|
213 elseif percentage >= addon.db.global.defaults.colors.yellow then
|
Zerotorescue@0
|
214 return ("|cffffff00%d|r"):format(num);
|
Zerotorescue@0
|
215 elseif percentage >= addon.db.global.defaults.colors.orange then
|
Zerotorescue@0
|
216 return ("|cffff9933%d|r"):format(num);
|
Zerotorescue@0
|
217 elseif percentage >= addon.db.global.defaults.colors.red then
|
Zerotorescue@0
|
218 return ("|cffff0000%d|r"):format(num);
|
Zerotorescue@0
|
219 end
|
Zerotorescue@0
|
220 end
|
Zerotorescue@0
|
221
|
Zerotorescue@0
|
222 function mod:NumberFormat(num)
|
Zerotorescue@0
|
223 local formatted = string.gsub(num, "(%d)(%d%d%d)$", "%1,%2", 1);
|
Zerotorescue@0
|
224
|
Zerotorescue@0
|
225 while true do
|
Zerotorescue@0
|
226 formatted, matches = string.gsub(formatted, "(%d)(%d%d%d),", "%1,%2,", 1);
|
Zerotorescue@0
|
227
|
Zerotorescue@0
|
228 if matches == 0 then
|
Zerotorescue@0
|
229 break;
|
Zerotorescue@0
|
230 end
|
Zerotorescue@0
|
231 end
|
Zerotorescue@0
|
232
|
Zerotorescue@0
|
233 return formatted;
|
Zerotorescue@0
|
234 end |