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