PageRenderTime 185ms CodeModel.GetById 80ms app.highlight 18ms RepoModel.GetById 83ms app.codeStats 0ms

/Tukui/modules/skins/bags.lua

http://github.com/Asphyxia/Tukui
Lua | 347 lines | 288 code | 48 blank | 11 comment | 43 complexity | ba5f8bf1666beb10ba780ce01e5f2b72 MD5 | raw file
  1local T, C, L = unpack( select( 2, ... ) )
  2
  3if C["bags"].enable == true then return end
  4
  5local function LoadSkin()
  6	local QUEST_ITEM_STRING = select( 10, GetAuctionItemClasses() )
  7
  8	local function UpdateBorderColors( button )
  9		button:SetBackdropBorderColor( unpack( C["media"].bordercolor ) )
 10
 11		if button.type and button.type == QUEST_ITEM_STRING then
 12			button:SetBackdropBorderColor( 1, 0.2, 0.2 )
 13		elseif button.quality and button.quality > 1 then
 14			local r, g, b = GetItemQualityColor( button.quality )
 15			button:SetBackdropBorderColor( r, g, b )
 16		end
 17	end
 18	
 19	local function SkinButton( button )
 20		if not button.skinned then
 21			button:StripTextures()
 22			button:SetTemplate( "Default", true )
 23			button:StyleButton()
 24
 25			local icon = _G[button:GetName() .. "IconTexture"]
 26			icon:ClearAllPoints()
 27			icon:Point( "TOPLEFT", 2, -2 )
 28			icon:Point( "BOTTOMRIGHT", -2, 2 )
 29			icon:SetTexCoord( .08, .92, .08, .92 )
 30
 31			if _G[button:GetName() .. "IconQuestTexture"] then
 32				_G[button:GetName() .. "IconQuestTexture"]:Kill()
 33			end
 34			button.skinned = true
 35		end
 36	end
 37
 38	local function SkinBagButtons( container, button )
 39		SkinButton( button )
 40
 41		local texture, _, _, _, _, _, itemLink = GetContainerItemInfo( container:GetID(), button:GetID() )
 42		local isQuestItem = GetContainerItemQuestInfo( container:GetID(), button:GetID() )
 43		_G[button:GetName() .. "IconTexture"]:SetTexture( texture )
 44		button.type = nil
 45		button.quality = nil
 46		button.ilink = itemLink
 47		if button.ilink then
 48			button.name, _, button.quality, _, _, button.type = GetItemInfo( button.ilink )
 49		end
 50
 51		if isQuestItem then
 52			button.type = QUEST_ITEM_STRING
 53		end
 54
 55		UpdateBorderColors( button )
 56	end
 57
 58	local function SkinBags()
 59		for i = 1, NUM_CONTAINER_FRAMES, 1 do
 60			local container = _G["ContainerFrame" .. i]
 61			if container and not container.backdrop then
 62				container:SetFrameStrata( "HIGH" )
 63				container:StripTextures( true )
 64				container:CreateBackdrop( "Transparent" )
 65				--container.backdrop:Point( "TOPLEFT", 2, -2 )
 66				--container.backdrop:Point( "BOTTOMRIGHT", -2, 2 )
 67				--T.SkinCloseButton( _G[container:GetName() .. "CloseButton"] )
 68				
 69				container.backdrop:Point("TOPLEFT", 6, -2)
 70				container.backdrop:Point("BOTTOMRIGHT", -4, 4)
 71				T.SkinCloseButton(_G[container:GetName().."CloseButton"])
 72				_G[container:GetName().."CloseButton"]:Point("TOPRIGHT",container.backdrop,"TOPRIGHT",0,3)
 73
 74				container:HookScript( "OnShow", function( self )
 75					if self and self.size then
 76						for b = 1, self.size, 1 do
 77							local button = _G[self:GetName() .. "Item" .. b]
 78							SkinBagButtons( self, button )
 79						end
 80					end
 81				end )
 82
 83				if i == 1 then
 84					BackpackTokenFrame:StripTextures( true )
 85					for i = 1, MAX_WATCHED_TOKENS do
 86						_G["BackpackTokenFrameToken" .. i].icon:SetTexCoord( .08, .92, .08, .92 )
 87						_G["BackpackTokenFrameToken" .. i]:CreateBackdrop( "Default" )
 88						_G["BackpackTokenFrameToken" .. i].backdrop:Point( "TOPLEFT", _G["BackpackTokenFrameToken" .. i].icon, "TOPLEFT", -2, 2 )
 89						_G["BackpackTokenFrameToken" .. i].backdrop:Point( "BOTTOMRIGHT", _G["BackpackTokenFrameToken" .. i].icon, "BOTTOMRIGHT", 2, -2 )
 90						_G["BackpackTokenFrameToken" .. i].icon:Point("LEFT", _G["BackpackTokenFrameToken" .. i].count, "RIGHT", 2, 0 )
 91					end
 92				end
 93			end
 94
 95			if container and container.size then
 96				for b = 1, container.size, 1 do
 97					local button = _G[container:GetName() .. "Item" .. b]
 98					SkinBagButtons( container, button )
 99				end
100			end
101		end
102	end
103	
104	-- Backpack + Bags bar
105		for i = 0, 3 do
106			local backpack = MainMenuBarBackpackButton
107			local bags = _G["CharacterBag"..i.."Slot"]
108			local bags2 = _G["CharacterBag"..(i-1).."Slot"]
109			backpack:StyleButton(true)
110			bags:StyleButton(true)
111			T.SkinButton(backpack)
112			T.SkinButton(bags)
113			backpack:SetParent(UIParent)
114			bags:SetParent(backpack)
115			
116			if TukuiCubeRight then
117				backpack:ClearAllPoints()
118				backpack:Point("BOTTOMRIGHT", TukuiInfoRight, "TOPRIGHT", 0, 130)
119				backpack:Hide()		-- Show/Hide bags when clicking on the right cube
120				TukuiCubeRight:EnableMouse(true)
121				TukuiCubeRight:SetScript("OnMouseDown", function(self)
122					if MainMenuBarBackpackButton:IsShown() then
123						MainMenuBarBackpackButton:Hide()
124					else
125						MainMenuBarBackpackButton:Show()
126					end
127				end)
128			elseif C.chat.background then
129				backpack:SetSize(30,30)
130				backpack:ClearAllPoints()
131				backpack:Point("BOTTOMRIGHT", TukuiChatBackgroundRight, "BOTTOMLEFT", -5, 0)
132			else
133				backpack:Hide()
134			end
135			
136			bags:ClearAllPoints()
137			if i == 0 then
138				bags:Point("BOTTOM", backpack,"TOP", 0, 4)
139			else
140				bags:Point("BOTTOM", bags2, "TOP", 0, 4)
141			end
142
143			bags:SetSize(backpack:GetWidth(), backpack:GetHeight())
144			
145			local icons = { "MainMenuBarBackpackButtonIconTexture", "CharacterBag0SlotIconTexture", 
146			"CharacterBag1SlotIconTexture", "CharacterBag2SlotIconTexture", "CharacterBag3SlotIconTexture",}
147			for _, object in pairs(icons) do
148				_G[object]:SetTexCoord(.08, .92, .08, .92)
149				_G[object]:ClearAllPoints()
150				_G[object]:SetAllPoints()
151				_G[object]:Point("TOPLEFT", 2, -2)
152				_G[object]:Point("BOTTOMRIGHT", -2, 2)
153			end
154		end
155
156	hooksecurefunc( "BankFrameItemButton_Update", function( button )
157		if not BankFrame.backdrop then
158			BankFrame:StripTextures( true )
159			BankFrame:CreateBackdrop( "Transparent" )
160			BankFrame.backdrop:Point( "TOPLEFT", 0, 0 )
161			BankFrame.backdrop:Point( "BOTTOMRIGHT", -4, 70 )
162			T.SkinButton( BankFramePurchaseButton, true )
163			T.SkinCloseButton( BankCloseButton )
164
165			BankFrame.backdrop2 = CreateFrame( "Frame", nil, BankFrame )
166			BankFrame.backdrop2:SetTemplate( "Default" )
167			BankFrame.backdrop2:Point( "TOPLEFT", BankFrameItem1, "TOPLEFT", -8, 8 )
168			BankFrame.backdrop2:Point( "BOTTOMRIGHT", BankFrameItem28, "BOTTOMRIGHT", 8, -8 )
169
170			BankFrame.backdrop3 = CreateFrame( "Frame", nil, BankFrame )
171			BankFrame.backdrop3:SetTemplate( "Default" )
172			BankFrame.backdrop3:Point( "TOPLEFT", BankFrameBag1, "TOPLEFT", -8, 8 )
173			BankFrame.backdrop3:Point( "BOTTOMRIGHT", BankFrameBag7, "BOTTOMRIGHT", 8, -8 )
174		end
175
176		SkinButton( button )
177
178		local inventoryID = button:GetInventorySlot()
179		local textureName = GetInventoryItemTexture( "player", inventoryID )
180
181		if( textureName ) then
182			_G[button:GetName() .. "IconTexture"]:SetTexture( textureName )
183		elseif( button.isBag ) then
184			local _, slotTextureName = GetInventorySlotInfo( strsub( button:GetName(), 10 ) )
185			_G[button:GetName() .. "IconTexture"]:SetTexture( slotTextureName )
186		end
187
188		if not button.isBag then
189			local texture, _, _, _, _, _, itemLink = GetContainerItemInfo( BANK_CONTAINER, button:GetID() )
190			local isQuestItem = GetContainerItemQuestInfo( BANK_CONTAINER, button:GetID() )
191			button.type = nil
192			button.ilink = itemLink
193			button.quality = nil
194
195			if button.ilink then
196				button.name, _, button.quality, _, _, button.type = GetItemInfo( button.ilink )
197			end
198
199			if isQuestItem then
200				button.type = QUEST_ITEM_STRING
201			end
202
203			UpdateBorderColors( button )
204		end
205
206		local highlight = _G[button:GetName() .. "HighlightFrameTexture"]
207		if highlight and not highlight.skinned then
208			highlight:SetTexture(0,1,0,0.3)
209			highlight.SetTexture = T.dummy
210			highlight:ClearAllPoints()
211			highlight:Point( "TOPLEFT", 2, -2 )
212			highlight:Point( "BOTTOMRIGHT", -2, 2 )
213			highlight.skinned = true
214		end
215	end )
216
217	hooksecurefunc( "updateContainerFrameAnchors", function()
218		local frame, xOffset, yOffset, screenHeight, freeScreenHeight, leftMostPoint, column
219		local screenWidth = GetScreenWidth()
220		local containerScale = 1
221		local leftLimit = 0
222		if( BankFrame:IsShown() ) then
223			leftLimit = BankFrame:GetRight() - 25
224		end
225
226		while( containerScale > CONTAINER_SCALE ) do
227			screenHeight = GetScreenHeight() / containerScale
228			xOffset = CONTAINER_OFFSET_X / containerScale
229			yOffset = CONTAINER_OFFSET_Y / containerScale
230			freeScreenHeight = screenHeight - yOffset
231			leftMostPoint = screenWidth - xOffset
232			column = 1
233
234			local frameHeight
235			for index, frameName in ipairs( ContainerFrame1.bags ) do
236				frameHeight = _G[frameName]:GetHeight()
237				if( freeScreenHeight < frameHeight ) then
238					column = column + 1
239					leftMostPoint = screenWidth - ( column * CONTAINER_WIDTH * containerScale ) - xOffset
240					freeScreenHeight = screenHeight - yOffset
241				end
242				freeScreenHeight = freeScreenHeight - frameHeight - VISIBLE_CONTAINER_SPACING
243			end
244			if( leftMostPoint < leftLimit ) then
245				containerScale = containerScale - 0.01
246			else
247				break
248			end
249		end
250
251		if( containerScale < CONTAINER_SCALE ) then
252			containerScale = CONTAINER_SCALE
253		end
254
255		screenHeight = GetScreenHeight() / containerScale;
256		-- Adjust the start anchor for bags depending on the multibars
257		xOffset = CONTAINER_OFFSET_X / containerScale;
258		yOffset = CONTAINER_OFFSET_Y / containerScale;
259		-- freeScreenHeight determines when to start a new column of bags
260		freeScreenHeight = screenHeight - yOffset;
261		column = 0;		
262		
263		local bagsPerColumn = 0
264		for index, frameName in ipairs(ContainerFrame1.bags) do
265			frame = _G[frameName];
266			frame:SetScale(1);
267			if ( index == 1 ) then
268				-- First bag
269				frame:SetPoint("BOTTOMRIGHT", TukuiInfoRight, "BOTTOMRIGHT", 4, TukuiInfoRight:GetHeight()+2 );
270				bagsPerColumn = bagsPerColumn + 1
271			elseif ( freeScreenHeight < frame:GetHeight() ) then
272				-- Start a new column
273				column = column + 1;
274				freeScreenHeight = screenHeight - yOffset;
275				if column > 1 then
276					frame:SetPoint("BOTTOMRIGHT", ContainerFrame1.bags[(index - bagsPerColumn) - 1], "BOTTOMLEFT", -(CONTAINER_SPACING -6), 0 );
277				else
278					frame:SetPoint("BOTTOMRIGHT", ContainerFrame1.bags[index - bagsPerColumn], "BOTTOMLEFT", -(CONTAINER_SPACING -6 ), 0 );
279				end
280				bagsPerColumn = 0
281			else
282				-- Anchor to the previous bag
283				frame:SetPoint("BOTTOMRIGHT", ContainerFrame1.bags[index - 1], "TOPRIGHT", 0, CONTAINER_SPACING);	
284				bagsPerColumn = bagsPerColumn + 1
285			end
286			freeScreenHeight = freeScreenHeight - frame:GetHeight() - VISIBLE_CONTAINER_SPACING;
287		end		
288	end)
289
290	local bags = CreateFrame( "Frame" )
291	bags:RegisterEvent( "BAG_UPDATE" )
292	bags:RegisterEvent( "ITEM_LOCK_CHANGED" )
293	bags:RegisterEvent( "BAG_CLOSED")
294	bags:SetScript( "OnEvent", SkinBags )
295	SkinBags()
296end
297
298local SearchBags = function(self)
299	local Search = string.lower(self:GetText())
300	
301	for i = 1, NUM_CONTAINER_FRAMES, 1 do
302		local Container = _G["ContainerFrame"..i]
303		
304		if Container and Container.size then
305			for b = 1, Container.size, 1 do
306				local Button = _G[Container:GetName().."Item"..b]
307				local ItemLink = select(7, GetContainerItemInfo(Container:GetID(), Button:GetID()))
308				
309				if ItemLink then
310					local ItemName = string.lower(GetItemInfo(ItemLink))
311
312					if string.find(ItemName, Search) then
313						_G[Button:GetName().."IconTexture"]:SetDesaturated(false)
314					else
315						_G[Button:GetName().."IconTexture"]:SetDesaturated(true)
316					end
317				end
318			end
319		end
320	end
321end
322
323local OnEscapePressed = function(self)
324	self:SetAutoFocus(false)
325	self:ClearFocus()
326	SearchBags(self)
327end
328
329-- Bar
330local frame = CreateFrame("Frame", nil, ContainerFrame1)
331frame:CreatePanel(nil, 164, 16, "TOP", ContainerFrame1, "TOP", 2, -28)
332frame:SetFrameLevel(ContainerFrame1:GetFrameLevel() + 1)
333frame:SetFrameStrata("HIGH")
334
335-- Editbox
336frame.edit = CreateFrame("Editbox", nil, frame)
337frame.edit:Point("TOPLEFT", 2, 0)
338frame.edit:Point("BOTTOMRIGHT", -2, -1)
339frame.edit:SetFrameLevel(frame:GetFrameLevel() + 1)
340frame.edit:SetMaxLetters(20)
341frame.edit:SetAutoFocus(false)
342frame.edit:SetFont(C.media.font, 12)
343frame.edit:SetScript("OnEscapePressed", OnEscapePressed)
344frame.edit:SetScript("OnEnterPressed", OnEscapePressed)
345frame.edit:SetScript("OnTextChanged", SearchBags)
346
347tinsert( T.SkinFuncs["Tukui"], LoadSkin )