-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathItemContainer.lua
More file actions
238 lines (191 loc) · 5.16 KB
/
ItemContainer.lua
File metadata and controls
238 lines (191 loc) · 5.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
local _, Inventorian = ...
local L = LibStub("AceLocale-3.0"):GetLocale("Inventorian")
local ItemCache = LibStub("LibItemCache-1.1-Inventorian")
local ItemContainerMixin = {}
local Events = Inventorian:GetModule("Events")
Inventorian.ItemContainer = {}
Inventorian.ItemContainer.defaults = {}
function Inventorian.ItemContainer:Create(parent)
local frame = Mixin(CreateFrame("Frame", nil, parent), ItemContainerMixin)
-- settings
frame.items = {}
frame.itemCount = 0
frame.bagSizes = {}
-- scripts
frame:SetScript("OnShow", frame.OnShow)
frame:SetScript("OnHide", frame.OnHide)
return frame
end
local function ToIndex(bag, slot)
return (bag < 0 and bag * 100 - slot) or (bag * 100 + slot)
end
local function ToBag(index)
return (index > 0 and floor(index / 100)) or ceil(index / 100)
end
function ItemContainerMixin:SetBags(bags)
self.bags = bags
self:UpdateBags()
end
function ItemContainerMixin:OnShow()
self:GenerateItemButtons()
Events.Register(self, "ITEM_SLOT_ADD", "ITEM_SLOT_UPDATE")
Events.Register(self, "ITEM_SLOT_UPDATE")
Events.Register(self, "ITEM_SLOT_REMOVE")
Events.Register(self, "ITEM_SLOT_UPDATE_COOLDOWN")
Events.Register(self, "ITEM_LOCK_CHANGED")
end
function ItemContainerMixin:OnHide()
Events.UnregisterAll(self)
end
function ItemContainerMixin:ITEM_SLOT_UPDATE(event, bag, slot)
if self:UpdateSlot(bag, slot) then
self:Layout()
end
end
function ItemContainerMixin:ITEM_SLOT_REMOVE(event, bag, slot)
if self:RemoveSlot(bag, slot) then
self:Layout()
end
end
function ItemContainerMixin:ITEM_SLOT_UPDATE_COOLDOWN(event, bag, slot)
local item = self.items[ToIndex(bag, slot)]
if item then
item:UpdateCooldown()
end
end
function ItemContainerMixin:ITEM_LOCK_CHANGED(event, bag, slot)
if not slot then return end
local item = self.items[ToIndex(bag, slot)]
if item then
item:UpdateLocked()
end
end
function ItemContainerMixin:RemoveAllItems()
local items = self.items
for i, item in pairs(items) do
item:Free()
items[i] = nil
end
self.itemCount = 0
end
function ItemContainerMixin:ItemFilter(bag, slot, link)
-- check for the bag
local hasBag = false
for _, bagID in pairs(self.bags) do
if bag == bagID then
hasBag = true
break
end
end
if not hasBag then return false end
-- TODO: possible item filtering
return true
end
function ItemContainerMixin:Search(text)
if text == "" then text = nil end
self.searchText = text
self:UpdateSearch()
end
function ItemContainerMixin:UpdateSearch()
for idx, item in pairs(self.items) do
item:UpdateSearch(self.searchText)
end
end
function ItemContainerMixin:HighlightBag(bag)
self.highlightBag = bag
for _, item in pairs(self.items) do
item:Highlight(item:GetBag() == bag)
end
end
function ItemContainerMixin:UpdateBags()
self:RemoveAllItems()
self:GenerateItemButtons()
end
function ItemContainerMixin:UpdateSlot(bag, slot)
if self:ItemFilter(bag, slot) then
return self:AddSlot(bag, slot)
end
return self:RemoveSlot(bag, slot)
end
function ItemContainerMixin:AddSlot(bag, slot)
local index = ToIndex(bag, slot)
if self.items[index] then
self.items[index]:Update()
else
self.items[index] = Inventorian.Item:Create()
self.items[index]:Set(self, bag, slot)
self.itemCount = self.itemCount + 1
return true
end
end
function ItemContainerMixin:RemoveSlot(bag, slot)
local index = ToIndex(bag, slot)
if self.items[index] then
self.items[index]:Free()
self.items[index] = nil
self.itemCount = self.itemCount - 1
return true
end
end
function ItemContainerMixin:Layout()
local width, height = self:GetWidth(), self:GetHeight()
local spacing = 2
local count = self.itemCount
local size = 36 + spacing*2
local cols = 0
local scale, rows
if count <= 0 then return end
repeat
cols = cols + 1
scale = width / (size*cols)
rows = floor(height / (size*scale))
until (scale <= 1.33 and cols*rows >= count)
--layout the items
local items = self.items
local i = 0
for _, bag in ipairs(self.bags) do
for slot = 1, self:GetBagSize(bag) do
local item = items[ToIndex(bag, slot)]
if item then
i = i + 1
local row = mod(i-1, cols)
local col = ceil(i / cols) - 1
item:ClearAllPoints()
item:SetScale(scale)
item:SetPoint("TOPLEFT", self, "TOPLEFT", size*row + spacing, -(size*col + spacing))
item:Show()
end
end
end
end
function ItemContainerMixin:GenerateItemButtons()
if not self:IsVisible() then return end
-- track if anything changed
local slotChanged = false
for _, bag in ipairs(self.bags) do
local bagSize = self:GetBagSize(bag)
-- check if the size changed
if (self.bagSizes[bag] or 0) > bagSize then
slotChanged = true
for slot = bagSize, self.bagSizes[bag] do
self:RemoveSlot(bag, slot)
end
end
-- update slots
for slot = 1, bagSize do
if self:UpdateSlot(bag, slot) then
slotChanged = true
end
end
self.bagSizes[bag] = bagSize
end
if slotChanged then
self:Layout()
end
end
-----------------------------------------------------------------------
-- Various information getters
function ItemContainerMixin:GetBagSize(bag)
local link, numFreeSlots, icon, slot, numSlots = ItemCache:GetBagInfo(self:GetParent():GetPlayerName(), bag)
return numSlots >= 0 and numSlots or 0
end