-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathUtility.lua
More file actions
183 lines (157 loc) · 5.74 KB
/
Utility.lua
File metadata and controls
183 lines (157 loc) · 5.74 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
--[[
AdiBags - Adirelle's bag addon.
Copyright 2010-2011 Adirelle (adirelle@tagada-team.net)
All rights reserved.
--]]
-- Various utility functions
local addonName, addon = ...
local L = addon.L
--<GLOBALS
local _G = _G
local band = _G.bit.band
local floor = _G.floor
local GameTooltip = _G.GameTooltip
local GetContainerNumFreeSlots = _G.GetContainerNumFreeSlots
local geterrorhandler = _G.geterrorhandler
local GetItemFamily = _G.GetItemFamily
local GetItemInfo = _G.GetItemInfo
local ITEM_QUALITY_POOR = _G.ITEM_QUALITY_POOR
local ITEM_QUALITY_UNCOMMON = _G.ITEM_QUALITY_UNCOMMON
local pcall = _G.pcall
local select = _G.select
local setmetatable = _G.setmetatable
local strjoin = _G.strjoin
local strmatch = _G.strmatch
local tonumber = _G.tonumber
local tostring = _G.tostring
local type = _G.type
--GLOBALS>
--------------------------------------------------------------------------------
-- (bag,slot) <=> slotId conversion
--------------------------------------------------------------------------------
function addon.GetSlotId(bag, slot)
if bag and slot then
return bag * 100 + slot
end
end
function addon.GetBagSlotFromId(slotId)
if slotId then
return floor(slotId / 100), slotId % 100
end
end
--------------------------------------------------------------------------------
-- Safe call
--------------------------------------------------------------------------------
local function safecall_return(success, ...)
if success then
return ...
else
geterrorhandler()((...))
end
end
function addon.safecall(funcOrSelf, argOrMethod, ...)
local func, arg
if type(funcOrSelf) == "table" and type(argOrMethod) == "string" then
func, arg = funcOrSelf[argOrMethod], funcOrSelf
else
func, arg = funcOrSelf, argOrMethod
end
if type(func) == "function" then
return safecall_return(pcall(func, arg, ...))
end
end
--------------------------------------------------------------------------------
-- Attaching tooltip to widgets
--------------------------------------------------------------------------------
local function WidgetTooltip_OnEnter(self)
GameTooltip:SetOwner(self, self.tooltipAnchor, self.tootlipAnchorXOffset, self.tootlipAnchorYOffset)
self:UpdateTooltip()
end
local function WidgetTooltip_OnLeave(self)
if GameTooltip:GetOwner() == self then
GameTooltip:Hide()
end
end
local function WidgetTooltip_Update(self)
GameTooltip:ClearLines()
addon.safecall(self, "tooltipCallback", GameTooltip)
GameTooltip:Show()
end
function addon.SetupTooltip(widget, content, anchor, xOffset, yOffset)
if type(content) == "string" then
widget.tooltipCallback = function(self, tooltip)
tooltip:AddLine(content)
end
elseif type(content) == "table" then
widget.tooltipCallback = function(self, tooltip)
tooltip:AddLine(tostring(content[1]), 1, 1, 1)
for i = 2, #content do
tooltip:AddLine(tostring(content[i]))
end
end
elseif type(content) == "function" then
widget.tooltipCallback = content
else
return
end
widget.tooltipAnchor = anchor or "ANCHOR_TOPLEFT"
widget.tootlipAnchorXOffset = xOffset or 0
widget.tootlipAnchorYOffset = yOffset or 0
widget.UpdateTooltip = WidgetTooltip_Update
widget:HookScript('OnEnter', WidgetTooltip_OnEnter)
widget:HookScript('OnLeave', WidgetTooltip_OnLeave)
end
--------------------------------------------------------------------------------
-- Item link checking
--------------------------------------------------------------------------------
function addon.IsValidItemLink(link)
if type(link) == "string" and strmatch(link, 'item:[-:%d]+') and not strmatch(link, 'item:%d+:0:0:0:0:0:0:0:0:0') then
return true
end
end
--------------------------------------------------------------------------------
-- Get distinct item IDs from item links
--------------------------------------------------------------------------------
local function __GetDistinctItemID(link)
if not link or not addon.IsValidItemLink(link) then return end
local itemString, id, enchant, gem1, gem2, gem3, gem4, suffix, reforge = strmatch(link, '(item:(%-?%d+):(%-?%d+):(%-?%d+):(%-?%d+):(%-?%d+):(%-?%d+):(%-?%d+):%-?%d+:%-?%d+:(%-?%d+))')
id = tonumber(id)
local equipSlot = id and select(9, GetItemInfo(id))
if equipSlot and equipSlot ~= "" and equipSlot ~= "INVTYPE_BAG" then
-- Rebuild an item link without noise
id = strjoin(':', 'item', id, enchant, gem1, gem2, gem3, gem4, suffix, "0", "0", reforge)
end
return id
end
local distinctIDs = setmetatable({}, {__index = function(t, link)
local result = __GetDistinctItemID(link)
if result then
t[link] = result
return result
else
return link
end
end})
function addon.GetDistinctItemID(link)
return link and distinctIDs[link]
end
--------------------------------------------------------------------------------
-- Basic junk test
--------------------------------------------------------------------------------
local JUNK = addon.BI['Junk']
function addon:IsJunk(itemId)
local _, _, quality, _, _, class, subclass = GetItemInfo(itemId)
return quality == ITEM_QUALITY_POOR or (quality and quality < ITEM_QUALITY_UNCOMMON and (class == JUNK or subclass == JUNK))
end
--------------------------------------------------------------------------------
-- Item and container family
--------------------------------------------------------------------------------
local GetItemFamily, GetItemInfo, GetContainerNumFreeSlots = GetItemFamily, GetItemInfo, GetContainerNumFreeSlots
function addon.GetItemFamily(item)
return select(9, GetItemInfo(item)) == "INVTYPE_BAG" and 0 or GetItemFamily(item)
end
function addon.CanPutItemInContainer(item, container)
local freeSlots, containerFamily = GetContainerNumFreeSlots(container)
local itemFamily = addon.GetItemFamily(item)
return freeSlots > 0 and (containerFamily == 0 or band(itemFamily, containerFamily) ~= 0), freeSlots, itemFamily, containerFamily
end