PageRenderTime 106ms CodeModel.GetById 92ms app.highlight 9ms RepoModel.GetById 1ms app.codeStats 0ms

/Tukui/modules/loot/lootframe.lua

http://github.com/Asphyxia/Tukui
Lua | 332 lines | 265 code | 61 blank | 6 comment | 24 complexity | c1633193cfc8b9b5dcf51742cabdcb1e MD5 | raw file
  1local T, C, L = unpack(select(2, ...)) -- Import: T - functions, constants, variables; C - config; L - locales
  2-- credits : Haste
  3
  4if not C["loot"].lootframe == true then return end
  5
  6local L = {
  7	fish = "Fishy loot",
  8	empty = "Empty slot",
  9}
 10
 11local addon = CreateFrame("Button", "Butsu")
 12local title = addon:CreateFontString(nil, "OVERLAY")
 13
 14local iconSize = 30
 15local frameScale = 1
 16
 17local sq, ss, sn
 18
 19local OnEnter = function(self)
 20	local slot = self:GetID()
 21	if(LootSlotIsItem(slot)) then
 22		GameTooltip:SetOwner(self, "ANCHOR_RIGHT")
 23		GameTooltip:SetLootItem(slot)
 24		CursorUpdate(self)
 25	end
 26	
 27	LootFrame.selectedSlot = self:GetID()
 28	self.drop:Show()
 29	self.drop:SetVertexColor(1, 1, 0)
 30end
 31
 32local OnLeave = function(self)
 33	if(self.quality > 1) then
 34		local color = ITEM_QUALITY_COLORS[self.quality]
 35		self.drop:SetVertexColor(color.r, color.g, color.b)
 36	else
 37		self.drop:Hide()
 38	end
 39
 40	GameTooltip:Hide()
 41	ResetCursor()
 42end
 43
 44local OnClick = function(self)
 45	if(IsModifiedClick()) then
 46		HandleModifiedItemClick(GetLootSlotLink(self:GetID()))
 47	else
 48		StaticPopup_Hide"CONFIRM_LOOT_DISTRIBUTION"
 49		ss = self:GetID()
 50		sq = self.quality
 51		sn = self.name:GetText()
 52		LootSlot(ss)
 53	end
 54end
 55
 56local OnUpdate = function(self)
 57	if(GameTooltip:IsOwned(self)) then
 58		GameTooltip:SetOwner(self, "ANCHOR_RIGHT")
 59		GameTooltip:SetLootItem(self:GetID())
 60		CursorOnUpdate(self)
 61	end
 62end
 63
 64local createSlot = function(id)
 65	local iconsize = iconSize-2
 66	local frame = CreateFrame("Button", 'ButsuSlot'..id, addon)
 67	frame:Point("LEFT", 8, 0)
 68	frame:Point("RIGHT", -8, 0)
 69	frame:Height(iconsize)
 70	frame:SetID(id)
 71
 72	frame:RegisterForClicks("LeftButtonUp", "RightButtonUp")
 73
 74	frame:SetScript("OnEnter", OnEnter)
 75	frame:SetScript("OnLeave", OnLeave)
 76	frame:SetScript("OnClick", OnClick)
 77	frame:SetScript("OnUpdate", OnUpdate)
 78
 79	local iconFrame = CreateFrame("Frame", nil, frame)
 80	iconFrame:Height(iconsize)
 81	iconFrame:Width(iconsize)
 82	iconFrame:ClearAllPoints()
 83	iconFrame:SetPoint("RIGHT", frame)
 84	
 85	iconFrame:SetTemplate("Default")
 86
 87	local icon = iconFrame:CreateTexture(nil, "ARTWORK")
 88	icon:SetAlpha(.8)
 89	icon:SetTexCoord(.07, .93, .07, .93)
 90	icon:Point("TOPLEFT", 2, -2)
 91	icon:Point("BOTTOMRIGHT", -2, 2)
 92	frame.icon = icon
 93
 94	local count = iconFrame:CreateFontString(nil, "OVERLAY")
 95	count:ClearAllPoints()
 96	count:SetJustifyH"RIGHT"
 97	count:Point("BOTTOMRIGHT", iconFrame, -1, 2)
 98	count:SetFont(C["media"].font, 12, "OUTLINE")
 99	count:SetShadowOffset(.8, -.8)
100	count:SetShadowColor(0, 0, 0, 1)
101	count:SetText(1)
102	frame.count = count
103
104	local name = frame:CreateFontString(nil, "OVERLAY")
105	name:SetJustifyH"LEFT"
106	name:ClearAllPoints()
107	name:SetPoint("LEFT", frame)
108	name:SetPoint("RIGHT", icon, "LEFT")
109	name:SetNonSpaceWrap(true)
110	name:SetFont(C["media"].font, 13, "OUTLINE")
111	name:SetShadowOffset(.8, -.8)
112	name:SetShadowColor(0, 0, 0, 1)
113	frame.name = name
114
115	local drop = frame:CreateTexture(nil, "ARTWORK")
116	drop:SetTexture"Interface\\QuestFrame\\UI-QuestLogTitleHighlight"
117
118	drop:SetPoint("LEFT", icon, "RIGHT", 0, 0)
119	drop:SetPoint("RIGHT", frame)
120	drop:SetAllPoints(frame)
121	drop:SetAlpha(.3)
122	frame.drop = drop
123
124	addon.slots[id] = frame
125	return frame
126end
127
128local anchorSlots = function(self)
129	local iconsize = iconSize
130	local shownSlots = 0
131	for i=1, #self.slots do
132		local frame = self.slots[i]
133		if(frame:IsShown()) then
134			shownSlots = shownSlots + 1
135
136			-- We don't have to worry about the previous slots as they're already hidden.
137			frame:Point("TOP", addon, 4, (-8 + iconsize) - (shownSlots * iconsize))
138		end
139	end
140
141	self:Height(math.max(shownSlots * iconsize + 16, 20))
142end
143
144title:SetFont(C["media"].font, 13, "OUTLINE")
145title:Point("BOTTOMLEFT", addon, "TOPLEFT", 4, 4)
146
147addon:SetScript("OnMouseDown", function(self) if(IsAltKeyDown()) then self:StartMoving() end end)
148addon:SetScript("OnMouseUp", function(self) self:StopMovingOrSizing() end)
149addon:SetScript("OnHide", function(self)
150	StaticPopup_Hide"CONFIRM_LOOT_DISTRIBUTION"
151	CloseLoot()
152end)
153addon:SetMovable(true)
154addon:RegisterForClicks"anyup"
155
156addon:SetParent(UIParent)
157--addon:SetUserPlaced(true)
158addon:Point("TOPLEFT", 0, -104)
159addon:SetTemplate("Default")
160addon:Width(256)
161addon:Height(64)
162addon:SetBackdropColor(0.1, 0.1, 0.1, 1)
163
164addon:SetClampedToScreen(true)
165addon:SetClampRectInsets(0, 0, T.Scale(14), 0)
166addon:SetHitRectInsets(0, 0, T.Scale(-14), 0)
167addon:SetFrameStrata"HIGH"
168addon:SetToplevel(true)
169
170addon.slots = {}
171addon.LOOT_OPENED = function(self, event, autoloot)
172	self:Show()
173
174	if(not self:IsShown()) then
175		CloseLoot(not autoLoot)
176	end
177
178	local items = GetNumLootItems()
179
180	if(IsFishingLoot()) then
181		title:SetText(L.fish)
182	elseif(not UnitIsFriend("player", "target") and UnitIsDead"target") then
183		title:SetText(UnitName"target")
184	else
185		title:SetText(LOOT)
186	end
187
188	-- Blizzard uses strings here
189	if(GetCVar("lootUnderMouse") == "1") then
190		local x, y = GetCursorPosition()
191		x = x / self:GetEffectiveScale()
192		y = y / self:GetEffectiveScale()
193
194		self:ClearAllPoints()
195		self:Point("TOPLEFT", nil, "BOTTOMLEFT", x - 40, y + 20)
196		self:GetCenter()
197		self:Raise()
198	else
199		self:ClearAllPoints()
200		self:SetUserPlaced(false)
201		self:Point("TOPLEFT", 0, -104)		
202	end
203
204	local m, w, t = 0, 0, title:GetStringWidth()
205	if(items > 0) then
206		for i=1, items do
207			local slot = addon.slots[i] or createSlot(i)
208			local texture, item, quantity, quality, locked = GetLootSlotInfo(i)
209			local color = ITEM_QUALITY_COLORS[quality]
210
211			if(LootSlotIsCoin(i)) then
212				item = item:gsub("\n", ", ")
213			end
214
215			if(quantity > 1) then
216				slot.count:SetText(quantity)
217				slot.count:Show()
218			else
219				slot.count:Hide()
220			end
221
222			if(quality > 1) then
223				slot.drop:SetVertexColor(color.r, color.g, color.b)
224				slot.drop:Show()
225			else
226				slot.drop:Hide()
227			end
228
229			slot.quality = quality
230			slot.name:SetText(item)
231			slot.name:SetTextColor(color.r, color.g, color.b)
232			slot.icon:SetTexture(texture)
233
234			m = math.max(m, quality)
235			w = math.max(w, slot.name:GetStringWidth())
236
237			slot:Enable()
238			slot:Show()
239		end
240	else
241		local slot = addon.slots[1] or createSlot(1)
242		local color = ITEM_QUALITY_COLORS[0]
243
244		slot.name:SetText(L.empty)
245		slot.name:SetTextColor(color.r, color.g, color.b)
246		slot.icon:SetTexture[[Interface\Icons\INV_Misc_Herb_AncientLichen]]
247
248		items = 1
249		w = math.max(w, slot.name:GetStringWidth())
250
251		slot.count:Hide()
252		slot.drop:Hide()
253		slot:Disable()
254		slot:Show()
255	end
256	anchorSlots(self)
257
258	w = w + 60
259	t = t + 5
260
261	local color = ITEM_QUALITY_COLORS[m]
262	self:SetBackdropBorderColor(color.r, color.g, color.b, .8)
263	self:Width(math.max(w, t))
264end
265
266addon.LOOT_SLOT_CLEARED = function(self, event, slot)
267	if(not self:IsShown()) then return end
268
269	addon.slots[slot]:Hide()
270	anchorSlots(self)
271end
272
273addon.LOOT_CLOSED = function(self)
274	StaticPopup_Hide"LOOT_BIND"
275	self:Hide()
276
277	for _, v in pairs(self.slots) do
278		v:Hide()
279	end
280end
281
282addon.OPEN_MASTER_LOOT_LIST = function(self)
283	ToggleDropDownMenu(1, nil, GroupLootDropDown, addon.slots[ss], 0, 0)
284end
285
286addon.UPDATE_MASTER_LOOT_LIST = function(self)
287	UIDropDownMenu_Refresh(GroupLootDropDown)
288end
289
290addon.ADDON_LOADED = function(self, event, addon)
291	if(addon == "Butsu") then
292		db = setmetatable({}, {__index = defaults})
293
294		self:SetScale(frameScale)
295
296		-- clean up.
297		self[event] = nil
298		self:UnregisterEvent(event)
299	end
300end
301
302addon:SetScript("OnEvent", function(self, event, ...)
303	self[event](self, event, ...)
304end)
305
306addon:RegisterEvent"LOOT_OPENED"
307addon:RegisterEvent"LOOT_SLOT_CLEARED"
308addon:RegisterEvent"LOOT_CLOSED"
309addon:RegisterEvent"OPEN_MASTER_LOOT_LIST"
310addon:RegisterEvent"UPDATE_MASTER_LOOT_LIST"
311addon:RegisterEvent"ADDON_LOADED"
312addon:Hide()
313
314-- Fuzz
315LootFrame:UnregisterAllEvents()
316table.insert(UISpecialFrames, "Butsu")
317
318function _G.GroupLootDropDown_GiveLoot(self)
319	if ( sq >= MASTER_LOOT_THREHOLD ) then
320		local dialog = StaticPopup_Show("CONFIRM_LOOT_DISTRIBUTION", ITEM_QUALITY_COLORS[sq].hex..sn..FONT_COLOR_CODE_CLOSE, self:GetText())
321		if (dialog) then
322			dialog.data = self.value
323		end
324	else
325		GiveMasterLoot(ss, self.value)
326	end
327	CloseDropDownMenus()
328end
329
330StaticPopupDialogs["CONFIRM_LOOT_DISTRIBUTION"].OnAccept = function(self, data)
331	GiveMasterLoot(ss, data)
332end