Advertisement
Guest User

stuffing

a guest
Dec 29th, 2014
270
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 38.60 KB | None | 0 0
  1. -- texture used to skin buttons, when skinning is enabled.
  2. local BORDER_TEXTURE = "Interface\\AddOns\\Stuffing\\border"
  3.  
  4. local BAGS_BACKPACK = {0, 1, 2, 3, 4}
  5. local BAGS_BANK = {-1, 5, 6, 7, 8, 9, 10, 11}
  6.  
  7. local ST_NORMAL = 1
  8. local ST_SOULBAG = 2
  9. local ST_SPECIAL = 3
  10. local ST_QUIVER = 4
  11.  
  12. Stuffing = CreateFrame ("Frame", nil, UIParent)
  13. Stuffing:RegisterEvent("ADDON_LOADED")
  14. Stuffing:SetScript("OnEvent", function(this, event, ...)
  15. Stuffing[event](this, ...)
  16. end)
  17.  
  18. --
  19. -- config
  20. --
  21. StuffingDB = {}
  22.  
  23. local function SetDefaults()
  24. StuffingDB.cols = 10 -- columns to use for bags.
  25. StuffingDB.bankCols = 15 -- columns to use for bank.
  26. StuffingDB.bsize = 37 -- height and width of the item buttons (default: 37).
  27. StuffingDB.spacing = 4 -- spacing between buttons.
  28. StuffingDB.padding = 10 -- left, right and bottom padding.
  29. StuffingDB.clamp = 1 -- clamp bag window to screen.
  30. StuffingDB.rarity_glow = 1 -- color item borders by rarity.
  31. StuffingDB.search_glow = {0.8, 0.8, 0.3} -- glow color used for search highlighting.
  32. StuffingDB.locked = 0 -- lock bag frame position.
  33. StuffingDB.font = "Interface\\Addons\\Stuffing\\ARIAL.TTF"
  34. StuffingDB.fontSize = 12
  35. StuffingDB.background = [=[Interface\Tooltips\UI-Tooltip-Background]=]
  36. StuffingDB.edge = "Interface\\Buttons\\WHITE8x8"
  37. StuffingDB.edgeSize = 1
  38. StuffingDB.backdrop_color = {0.1, 0.1, 0.1, 0.7}
  39. StuffingDB.border_color = {65/255, 74/255, 79/255, 1}
  40. StuffingDB.quest_glow = 1
  41. StuffingDB.quest_glow_color = {1.0, 0.3, 0.3}
  42. StuffingDB.custom_skin = 0
  43. StuffingDB.hide_soulbag = 0
  44.  
  45. StuffingDB.force_defaults = 0
  46.  
  47. StuffingDB.soulbag_color = {0.8, 0.2, 0.2, 1}
  48. StuffingDB.special_color = {0.2, 0.2, 0.8, 1}
  49. StuffingDB.quiver_color = {0.8, 0.8, 0.2, 1}
  50.  
  51. StuffingDB.bag_bars = 1
  52.  
  53. StuffingDB.custom_stackfont = 0
  54. StuffingDB.stackfont = "Interface\\Addons\\Stuffing\\ARIAL.TTF"
  55. StuffingDB.stackfont_size = 8
  56. end
  57.  
  58. SetDefaults()
  59.  
  60. -- stub for localization.
  61. local L = setmetatable({}, {
  62. __index = function (t, v)
  63. t[v] = v
  64. return v
  65. end
  66. })
  67.  
  68.  
  69. local function Print (x)
  70. DEFAULT_CHAT_FRAME:AddMessage("|cFFFF6633Stuffing:|r " .. x)
  71. end
  72.  
  73. local function Stuffing_Sort(args)
  74. if not args then
  75. args = ""
  76. end
  77.  
  78. Stuffing.itmax = 0
  79. Print(L["Starting sorting, please be patient."])
  80. Stuffing:SetBagsForSorting(args)
  81. Stuffing:SortBags()
  82. end
  83.  
  84. function frameBD(f)
  85. if f.frameBD==nil then
  86. local frameBD = CreateFrame("Frame", nil, f)
  87. frameBD:SetFrameLevel(f:GetFrameLevel()-1)
  88. frameBD = CreateFrame("Frame", nil, f)
  89. frameBD:SetFrameLevel(1)
  90. frameBD:SetFrameStrata(f:GetFrameStrata())
  91. frameBD:SetPoint("TOPLEFT", 1, -1)
  92. frameBD:SetPoint("BOTTOMLEFT", 1, -1)
  93. frameBD:SetPoint("TOPRIGHT", -1, -1)
  94. frameBD:SetPoint("BOTTOMRIGHT", -1, -1)
  95. frameBD:SetBackdrop( {
  96. edgeFile = "Interface\\Addons\\Stuffing\\glowTex", edgeSize = 0.5,
  97. insets = {left = 1, right = -1, top = 1, bottom = -1},
  98. tile = false, tileSize = 0,
  99. })
  100.  
  101. frameBD:SetBackdropColor(0, 0, 0, 0)
  102. frameBD:SetBackdropBorderColor(0, 0, 0, 0.9)
  103. f.frameBD = frameBD
  104. end
  105. end
  106.  
  107.  
  108. local function Stuffing_OpenConfig()
  109. local l, r = LoadAddOn ("Stuffing_Config")
  110. if l == nil then
  111. Print(string.format(L["Can't open config: %s"], _G["ADDON_" .. r]))
  112. return
  113. end
  114. Stuffing_Config:ShowConfig()
  115. end
  116.  
  117.  
  118. local function Stuffing_OnShow()
  119. Stuffing:PLAYERBANKSLOTS_CHANGED(29) -- XXX: hack to force bag frame update
  120.  
  121. Stuffing:Layout()
  122. Stuffing:SearchReset()
  123. end
  124.  
  125.  
  126. local function StuffingBank_OnHide()
  127. CloseBankFrame()
  128. end
  129.  
  130.  
  131. local function Stuffing_OnHide()
  132. if Stuffing.bankFrame and Stuffing.bankFrame:IsShown() then
  133. Stuffing.bankFrame:Hide()
  134. end
  135. end
  136.  
  137.  
  138. local function Stuffing_Open()
  139. Stuffing.frame:Show()
  140. end
  141.  
  142.  
  143. local function Stuffing_Close()
  144. Stuffing.frame:Hide()
  145. end
  146.  
  147.  
  148. local function Stuffing_Toggle()
  149. if Stuffing.frame:IsShown() then
  150. Stuffing.frame:Hide()
  151. else
  152. Stuffing.frame:Show()
  153. end
  154. end
  155.  
  156.  
  157. local function Stuffing_ToggleBag(id)
  158. if id == -2 then
  159. ToggleKeyRing()
  160. return
  161. end
  162. Stuffing_Toggle()
  163. end
  164.  
  165.  
  166. local function StartMoving(self)
  167. self:StartMoving()
  168. local n = self:GetName()
  169. end
  170.  
  171.  
  172. local function StopMoving(self)
  173. self:StopMovingOrSizing()
  174. self:SetUserPlaced(false)
  175.  
  176. local n = self:GetName()
  177. local x, y = self:GetCenter()
  178. StuffingDB[n .. "PosX"] = x
  179. StuffingDB[n .. "PosY"] = y
  180. end
  181.  
  182.  
  183. --
  184. -- bag slot stuff
  185. --
  186. local trashParent = CreateFrame("Frame", nil, UIParent)
  187. local trashButton = {}
  188. local trashBag = {}
  189.  
  190. -- for the tooltip frame used to scan item tooltips
  191. local StuffingTT = nil
  192.  
  193. -- mostly from cargBags_Aurora
  194. local QUEST_ITEM_STRING = nil
  195.  
  196. function Stuffing:SlotUpdate(b)
  197. local texture, count, locked = GetContainerItemInfo (b.bag, b.slot)
  198. local clink = GetContainerItemLink(b.bag, b.slot)
  199.  
  200. if b.Cooldown then
  201. local cd_start, cd_finish, cd_enable = GetContainerItemCooldown(b.bag, b.slot)
  202. CooldownFrame_SetTimer(b.Cooldown, cd_start, cd_finish, cd_enable)
  203. end
  204.  
  205. if(clink) then
  206. local iType
  207. b.name, _, b.rarity, _, _, iType = GetItemInfo(clink)
  208.  
  209. if StuffingDB.quest_glow == 1 then
  210. if not StuffingTT then
  211. StuffingTT = CreateFrame("GameTooltip", "StuffingTT", nil, "GameTooltipTemplate")
  212. StuffingTT:Hide()
  213. end
  214.  
  215. if QUEST_ITEM_STRING == nil then
  216. -- GetItemInfo returns a localized item type.
  217. -- this is to figure out what that string is.
  218. local t = {GetAuctionItemClasses()}
  219. QUEST_ITEM_STRING = t[#t] -- #t == 12
  220. end
  221.  
  222. -- load tooltip, check if ITEM_BIND_QUEST ("Quest Item") is in it.
  223. -- If the tooltip says its a quest item, we assume it is a quest item
  224. -- and ignore the item type from GetItemInfo.
  225. StuffingTT:SetOwner(WorldFrame, "ANCHOR_NONE")
  226. StuffingTT:ClearLines()
  227. StuffingTT:SetBagItem(b.bag, b.slot)
  228. for i = 1, StuffingTT:NumLines() do
  229. local txt = getglobal("StuffingTTTextLeft" .. i)
  230. if txt then
  231. local text = txt:GetText()
  232. if string.find (txt:GetText(), ITEM_BIND_QUEST) then
  233. --print (txt:GetText())
  234. --print (b.name)
  235. --if iType then print(iType) end
  236. iType = QUEST_ITEM_STRING
  237. end
  238. end
  239. end
  240.  
  241. if iType and iType == QUEST_ITEM_STRING then
  242. --print (iType .. " " .. b.name)
  243. b.qitem = true
  244. else
  245. b.qitem = nil
  246. end
  247. end
  248. else
  249. b.name, b.rarity, b.qitem = nil, nil, nil
  250. end
  251.  
  252. SetItemButtonTexture(b.frame, texture)
  253. SetItemButtonCount(b.frame, count)
  254. SetItemButtonDesaturated(b.frame, locked, 0.5, 0.5, 0.5)
  255.  
  256. if b.Glow then
  257. b.Glow:Hide()
  258. if b.rarity then
  259. if b.rarity > 1 then
  260. b.Glow:SetVertexColor(GetItemQualityColor(b.rarity))
  261. b.Glow:Show()
  262. elseif b.qitem and StuffingDB.quest_glow == 1 then
  263. b.Glow:SetVertexColor(unpack(StuffingDB.quest_glow_color))
  264. b.Glow:Show()
  265. end
  266. end
  267. end
  268.  
  269. b.frame:Show()
  270. end
  271.  
  272.  
  273. function Stuffing:BagSlotUpdate(bag)
  274. if not self.buttons then
  275. return
  276. end
  277.  
  278. for _, v in ipairs (self.buttons) do
  279. if v.bag == bag then
  280. self:SlotUpdate(v)
  281. end
  282. end
  283. end
  284.  
  285.  
  286. function Stuffing:BagFrameSlotNew (slot, p)
  287. for _, v in ipairs(self.bagframe_buttons) do
  288. if v.slot == slot then
  289. --print ("found " .. slot)
  290. return v, false
  291. end
  292. end
  293.  
  294. --print ("new " .. slot)
  295. local ret = {}
  296. local tpl
  297.  
  298. if slot > 3 then
  299. ret.slot = slot
  300. slot = slot - 4
  301. tpl = "BankItemButtonBagTemplate"
  302. ret.frame = CreateFrame("CheckButton", "StuffingBBag" .. slot, p, tpl)
  303. ret.frame:SetID(slot + 4)
  304. table.insert(self.bagframe_buttons, ret)
  305.  
  306. --[[
  307. local is = ret.frame:GetInventorySlot()
  308. local texture = GetInventoryItemTexture("player", is)
  309.  
  310. if texture == nil then
  311. texture = "interface\\paperdoll\\UI-PaperDoll-Slot-Bag.blp"
  312. end
  313.  
  314. SetItemButtonTexture(ret.frame, texture)
  315. ret.frame:Update()
  316. --]]
  317.  
  318. BankFrameItemButton_Update(ret.frame)
  319. BankFrameItemButton_UpdateLocked(ret.frame)
  320.  
  321. if not ret.frame.tooltipText then
  322. ret.frame.tooltipText = ""
  323. end
  324. else
  325. tpl = "BagSlotButtonTemplate"
  326. ret.frame = CreateFrame("CheckButton", "StuffingFBag" .. slot .. "Slot", p, tpl)
  327. ret.slot = slot
  328. table.insert(self.bagframe_buttons, ret)
  329. end
  330.  
  331. return ret
  332. end
  333.  
  334.  
  335. function Stuffing:SlotNew (bag, slot)
  336. for _, v in ipairs(self.buttons) do
  337. if v.bag == bag and v.slot == slot then
  338. return v, false
  339. end
  340. end
  341.  
  342. local tpl = "ContainerFrameItemButtonTemplate"
  343.  
  344. if bag == -1 then
  345. tpl = "BankItemButtonGenericTemplate"
  346. end
  347.  
  348. local ret = {}
  349.  
  350. if #trashButton > 0 then
  351. local f = -1
  352. for i, v in ipairs(trashButton) do
  353. local b, s = v:GetName():match("(%d+)_(%d+)")
  354.  
  355. b = tonumber(b)
  356. s = tonumber(s)
  357.  
  358. --print (b .. " " .. s)
  359. if b == bag and s == slot then
  360. f = i
  361. break
  362. end
  363. end
  364.  
  365. if f ~= -1 then
  366. --print("found it")
  367. ret.frame = trashButton[f]
  368. table.remove(trashButton, f)
  369. end
  370. end
  371.  
  372. if not ret.frame then
  373. ret.frame = CreateFrame("Button", "StuffingBag" .. bag .. "_" .. slot, self.bags[bag], tpl)
  374. end
  375. local ni = _G["StuffingBag" .. bag .. "_" .. slot.."NewItemTexture"]
  376. if ni then
  377. ni:SetAlpha(0)
  378. ni:Hide()
  379. end
  380.  
  381. ret.bag = bag
  382. ret.slot = slot
  383. ret.frame:SetID(slot)
  384.  
  385. if StuffingDB.rarity_glow == 1 and not ret.Glow then
  386. -- from cargBags_Aurora
  387. local glow = ret.frame:CreateTexture(nil, "OVERLAY")
  388. glow:SetTexture("Interface\\Buttons\\UI-ActionButton-Border")
  389. glow:SetBlendMode("ADD")
  390. glow:SetAlpha(.8)
  391. glow:SetPoint("CENTER", ret.frame)
  392. ret.Glow = glow
  393. end
  394.  
  395. ret.Cooldown = _G[ret.frame:GetName() .. "Cooldown"]
  396. ret.Cooldown:Show()
  397.  
  398. self:SlotUpdate (ret)
  399.  
  400. return ret, true
  401. end
  402.  
  403.  
  404. -- from OneBag
  405. local BAGTYPE_QUIVER = 0x0001 + 0x0002
  406. local BAGTYPE_SOUL = 0x004
  407. local BAGTYPE_PROFESSION = 0x0008 + 0x0010 + 0x0020 + 0x0040 + 0x0080 + 0x0200 + 0x0400
  408.  
  409. function Stuffing:BagType(bag)
  410. local bagType = select(2, GetContainerNumFreeSlots(bag))
  411.  
  412. if bit.band(bagType, BAGTYPE_QUIVER) > 0 then
  413. return ST_QUIVER
  414. elseif bit.band(bagType, BAGTYPE_SOUL) > 0 then
  415. return ST_SOULBAG
  416. elseif bit.band(bagType, BAGTYPE_PROFESSION) > 0 then
  417. return ST_SPECIAL
  418. end
  419.  
  420. return ST_NORMAL
  421. end
  422.  
  423.  
  424. function Stuffing:BagNew (bag, f)
  425. for i, v in pairs(self.bags) do
  426. if v:GetID() == bag then
  427. v.bagType = self:BagType(bag)
  428. return v
  429. end
  430. end
  431.  
  432. local ret
  433.  
  434. if #trashBag > 0 then
  435. local f = -1
  436. for i, v in pairs(trashBag) do
  437. if v:GetID() == bag then
  438. f = i
  439. break
  440. end
  441. end
  442.  
  443. if f ~= -1 then
  444. --print("found bag " .. bag)
  445. ret = trashBag[f]
  446. table.remove(trashBag, f)
  447. ret:Show()
  448. ret.bagType = self:BagType(bag)
  449. return ret
  450. end
  451. end
  452.  
  453. --print("new bag " .. bag)
  454. ret = CreateFrame("Frame", "StuffingBag" .. bag, f)
  455. ret.bagType = self:BagType(bag)
  456.  
  457. ret:SetID(bag)
  458. return ret
  459. end
  460.  
  461.  
  462. function Stuffing:SearchUpdate(str)
  463. str = string.lower(str)
  464.  
  465. for _, b in ipairs(self.buttons) do
  466. if b.name then
  467. if not string.find (string.lower(b.name), str) then
  468. SetItemButtonDesaturated(b.frame, 1, 1, 1, 1)
  469. if b.Glow then
  470. b.Glow:Hide()
  471. end
  472. else
  473. SetItemButtonDesaturated(b.frame, 0, 1, 1, 1)
  474. if b.Glow then
  475. b.Glow:Show()
  476. b.Glow:SetVertexColor(unpack(StuffingDB.search_glow))
  477. end
  478. end
  479. end
  480. end
  481. end
  482.  
  483.  
  484. function Stuffing:SearchReset()
  485. for _, b in ipairs(self.buttons) do
  486. SetItemButtonDesaturated(b.frame, 0, 1, 1, 1)
  487. if b.Glow then
  488. b.Glow:Hide()
  489. if b.rarity then
  490. if b.rarity > 1 then
  491. b.Glow:SetVertexColor(GetItemQualityColor(b.rarity))
  492. b.Glow:Show()
  493. elseif b.qitem and StuffingDB.quest_glow == 1 then
  494. b.Glow:SetVertexColor(unpack(StuffingDB.quest_glow_color))
  495. b.Glow:Show()
  496. end
  497. end
  498. end
  499. end
  500. end
  501.  
  502. -- drop down menu stuff from Postal
  503. local Stuffing_DDMenu = CreateFrame("Frame", "Stuffing_DropDownMenu")
  504. Stuffing_DDMenu.displayMode = "MENU"
  505. Stuffing_DDMenu.info = {}
  506. Stuffing_DDMenu.HideMenu = function()
  507. if UIDROPDOWNMENU_OPEN_MENU == Stuffing_DDMenu then
  508. CloseDropDownMenus()
  509. end
  510. end
  511.  
  512.  
  513. function Stuffing.Menu(self, level)
  514. if not level then
  515. return
  516. end
  517.  
  518. local info = self.info
  519.  
  520. wipe(info)
  521.  
  522. if level ~= 1 then
  523. return
  524. end
  525.  
  526. wipe(info)
  527. info.isTitle = 1
  528. info.text = "Stuffing"
  529. info.notCheckable = 1
  530. UIDropDownMenu_AddButton(info, level)
  531.  
  532. wipe(info)
  533. info.disabled = 1
  534. info.text = nil
  535. info.func = nil
  536. UIDropDownMenu_AddButton(info, level)
  537.  
  538. wipe(info)
  539. info.text = "Sort"
  540. info.notCheckable = 1
  541. info.func = function()
  542. Stuffing_Sort("d")
  543. end
  544. UIDropDownMenu_AddButton(info, level)
  545.  
  546. wipe(info)
  547. info.text = "Sort Special"
  548. info.notCheckable = 1
  549. info.func = function()
  550. Stuffing_Sort("c/p")
  551. end
  552. UIDropDownMenu_AddButton(info, level)
  553.  
  554. wipe(info)
  555. info.text = "Stack"
  556. info.notCheckable = 1
  557. info.func = function()
  558. Stuffing:SetBagsForSorting("d")
  559. Stuffing:Restack()
  560. end
  561. UIDropDownMenu_AddButton(info, level)
  562.  
  563. wipe(info)
  564. info.text = "Stack Special"
  565. info.notCheckable = 1
  566. info.func = function()
  567. Stuffing:SetBagsForSorting("c/p")
  568. Stuffing:Restack()
  569. end
  570. UIDropDownMenu_AddButton(info, level)
  571.  
  572. wipe(info)
  573. info.disabled = 1
  574. info.notCheckable = 1
  575. info.text = nil
  576. info.func = nil
  577. UIDropDownMenu_AddButton(info, level)
  578.  
  579. wipe(info)
  580. info.text = "Show Bags"
  581. info.checked = function()
  582. return StuffingDB.bag_bars == 1
  583. end
  584.  
  585. info.func = function()
  586. if StuffingDB.bag_bars == 1 then
  587. StuffingDB.bag_bars = 0
  588. else
  589. StuffingDB.bag_bars = 1
  590. end
  591. Stuffing:Layout()
  592. if Stuffing.bankFrame and Stuffing.bankFrame:IsShown() then
  593. Stuffing:Layout(true)
  594. end
  595.  
  596. end
  597. UIDropDownMenu_AddButton(info, level)
  598.  
  599. wipe(info)
  600. info.disabled = 1
  601. info.text = nil
  602. info.func = nil
  603. UIDropDownMenu_AddButton(info, level)
  604.  
  605. wipe(info)
  606. info.text = "Show Keyring"
  607. info.notCheckable = 1
  608. info.func = ToggleKeyRing
  609. UIDropDownMenu_AddButton(info, level)
  610.  
  611. wipe(info)
  612. info.text = "Open Config"
  613. info.notCheckable = 1
  614. info.func = Stuffing_OpenConfig
  615. UIDropDownMenu_AddButton(info, level)
  616.  
  617. wipe(info)
  618. info.disabled = 1
  619. info.text = nil
  620. info.func = nil
  621. UIDropDownMenu_AddButton(info, level)
  622.  
  623. wipe(info)
  624. info.disabled = nil
  625. info.notCheckable = 1
  626. info.text = CLOSE
  627. info.func = self.HideMenu
  628. info.tooltipTitle = CLOSE
  629. UIDropDownMenu_AddButton(info, level)
  630. end
  631.  
  632.  
  633. function Stuffing:CreateBagFrame(w)
  634. local n = "StuffingFrame" .. w
  635. local f = CreateFrame ("Frame", n, UIParent)
  636. f:EnableMouse(1)
  637. f:SetMovable(1)
  638. f:SetToplevel(1)
  639. f:SetFrameStrata("HIGH")
  640. frameBD(f)
  641. if w == "Bank" then
  642. f:SetScript("OnMouseDown", StartMoving)
  643. f:SetScript("OnMouseUp", StopMoving)
  644. else
  645. if StuffingDB.locked == 0 then
  646. f:SetScript("OnMouseDown", StartMoving)
  647. f:SetScript("OnMouseUp", StopMoving)
  648. end
  649. end
  650.  
  651. local x = StuffingDB[n .. "PosX"] or 0
  652. local y = StuffingDB[n .. "PosY"] or 0
  653. f:SetPoint ("CENTER", UIParent, "BOTTOMLEFT", x, y)
  654.  
  655. -- close button
  656. f.b_close = CreateFrame("Button", "Stuffing_CloseButton" .. w, f, "UIPanelCloseButton")
  657. f.b_close:SetWidth(32)
  658. f.b_close:SetHeight(32)
  659. f.b_close:SetPoint("TOPRIGHT", -3, -3)
  660. f.b_close:SetScript("OnClick", function(self, btn)
  661. if self:GetParent():GetName() == "StuffingFrameBags" and btn == "RightButton" then
  662. if Stuffing_DDMenu.initialize ~= Stuffing.Menu then
  663. CloseDropDownMenus()
  664. Stuffing_DDMenu.initialize = Stuffing.Menu
  665. end
  666. ToggleDropDownMenu(1, nil, Stuffing_DDMenu, self:GetName(), 0, 0)
  667. return
  668. end
  669. self:GetParent():Hide()
  670. end)
  671. f.b_close:RegisterForClicks("AnyUp")
  672. f.b_close:GetNormalTexture():SetDesaturated(1)
  673.  
  674. -- create the bags frame
  675. local fb = CreateFrame ("Frame", n .. "BagsFrame", f)
  676. fb:SetPoint("BOTTOMRIGHT", f, "TOPRIGHT", 0, 5)
  677. fb:SetFrameStrata("HIGH")
  678. f.bags_frame = fb
  679. frameBD(fb)
  680. return f
  681. end
  682.  
  683.  
  684. function Stuffing:InitBank()
  685. if self.bankFrame then
  686. return
  687. end
  688.  
  689. local f = self:CreateBagFrame("Bank")
  690. f:SetScript("OnHide", StuffingBank_OnHide)
  691. self.bankFrame = f
  692. end
  693.  
  694.  
  695. local parent_startmoving = function(self)
  696. StartMoving(self:GetParent())
  697. end
  698.  
  699.  
  700. local parent_stopmovingorsizing = function (self)
  701. StopMoving(self:GetParent())
  702. end
  703.  
  704.  
  705. function Stuffing:LockUnlock()
  706. local f = self.frame
  707.  
  708. if StuffingDB.locked == 0 then
  709. f:SetScript("OnMouseDown", StartMoving)
  710. f:SetScript("OnMouseUp", StopMovingOrSizing)
  711. f.button:SetScript("OnMouseDown", parent_startmoving)
  712. f.button:SetScript("OnMouseUp", parent_stopmovingorsizing)
  713. else
  714. f:SetScript("OnMouseDown", nil)
  715. f:SetScript("OnMouseUp", nil)
  716. f.button:SetScript("OnMouseDown", nil)
  717. f.button:SetScript("OnMouseUp", nil)
  718. end
  719. end
  720.  
  721.  
  722. function Stuffing:InitBags()
  723. if self.frame then
  724. return
  725. end
  726.  
  727. self.buttons = {}
  728. self.bags = {}
  729. self.bagframe_buttons = {}
  730.  
  731. local f = self:CreateBagFrame("Bags")
  732. f:SetScript("OnShow", Stuffing_OnShow)
  733. f:SetScript("OnHide", Stuffing_OnHide)
  734.  
  735. -- search editbox (tekKonfigAboutPanel.lua)
  736. local editbox = CreateFrame("EditBox", nil, f)
  737. editbox:Hide()
  738. editbox:SetAutoFocus(true)
  739. editbox:SetHeight(32)
  740.  
  741. local left = editbox:CreateTexture(nil, "BACKGROUND")
  742. left:SetWidth(8) left:SetHeight(20)
  743. left:SetPoint("LEFT", -5, 0)
  744. left:SetTexture("Interface\\Common\\Common-Input-Border")
  745. left:SetTexCoord(0, 0.0625, 0, 0.625)
  746.  
  747. local right = editbox:CreateTexture(nil, "BACKGROUND")
  748. right:SetWidth(8) right:SetHeight(20)
  749. right:SetPoint("RIGHT", 0, 0)
  750. right:SetTexture("Interface\\Common\\Common-Input-Border")
  751. right:SetTexCoord(0.9375, 1, 0, 0.625)
  752.  
  753. local center = editbox:CreateTexture(nil, "BACKGROUND")
  754. center:SetHeight(20)
  755. center:SetPoint("RIGHT", right, "LEFT", 0, 0)
  756. center:SetPoint("LEFT", left, "RIGHT", 0, 0)
  757. center:SetTexture("Interface\\Common\\Common-Input-Border")
  758. center:SetTexCoord(0.0625, 0.9375, 0, 0.625)
  759.  
  760. local resetAndClear = function (self)
  761. self:GetParent().detail:Show()
  762. self:GetParent().gold:Show()
  763. self:ClearFocus()
  764. Stuffing:SearchReset()
  765. end
  766.  
  767. local updateSearch = function(self, t)
  768. if t == true then
  769. Stuffing:SearchUpdate(self:GetText())
  770. end
  771. end
  772.  
  773. editbox:SetScript("OnEscapePressed", resetAndClear)
  774. editbox:SetScript("OnEnterPressed", resetAndClear)
  775. editbox:SetScript("OnEditFocusLost", editbox.Hide)
  776. editbox:SetScript("OnEditFocusGained", editbox.HighlightText)
  777. editbox:SetScript("OnTextChanged", updateSearch)
  778. editbox:SetText("Search")
  779.  
  780.  
  781. local detail = f:CreateFontString(nil, "ARTWORK", "GameFontHighlightLarge")
  782. detail:SetPoint("TOPLEFT", f, StuffingDB.padding, -10)
  783. detail:SetPoint("RIGHT", -(16 + 24), 0)
  784. detail:SetJustifyH("LEFT")
  785. detail:SetText("|cffebebeb" .. "Search")
  786. editbox:SetAllPoints(detail)
  787.  
  788. local gold = f:CreateFontString(nil, "ARTWORK", "GameFontHighlightLarge")
  789. gold:SetJustifyH("RIGHT")
  790. gold:SetPoint("RIGHT", f.b_close, "LEFT", -3, 0)
  791.  
  792. f:SetScript("OnEvent", function (self, e)
  793. self.gold:SetText (GetCoinTextureString(GetMoney(), StuffingDB.fontSize))
  794. end)
  795.  
  796. f:RegisterEvent("PLAYER_MONEY")
  797. f:RegisterEvent("PLAYER_LOGIN")
  798. f:RegisterEvent("PLAYER_TRADE_MONEY")
  799. f:RegisterEvent("TRADE_MONEY_CHANGED")
  800.  
  801. local OpenEditbox = function(self)
  802. self:GetParent().detail:Hide()
  803. self:GetParent().gold:Hide()
  804. self:GetParent().editbox:Show()
  805. self:GetParent().editbox:HighlightText()
  806. end
  807.  
  808. local button = CreateFrame("Button", nil, f)
  809. button:EnableMouse(1)
  810. button:RegisterForClicks("LeftButtonUp", "RightButtonUp")
  811. button:SetAllPoints(detail)
  812. button:SetScript("OnClick", function(self, btn)
  813. if btn == "RightButton" then
  814. OpenEditbox(self)
  815. else
  816. if self:GetParent().editbox:IsShown() then
  817. self:GetParent().editbox:Hide()
  818. self:GetParent().editbox:ClearFocus()
  819. self:GetParent().detail:Show()
  820. self:GetParent().gold:Show()
  821. Stuffing:SearchReset()
  822. end
  823. end
  824. end)
  825.  
  826. local tooltip_hide = function()
  827. GameTooltip:Hide()
  828. end
  829.  
  830. local tooltip_show = function (self)
  831. GameTooltip:SetOwner(self, "ANCHOR_CURSOR")
  832. GameTooltip:ClearLines()
  833. GameTooltip:SetText("Right-click to search.")
  834. end
  835.  
  836. button:SetScript("OnEnter", tooltip_show)
  837. button:SetScript("OnLeave", tooltip_hide)
  838.  
  839. if StuffingDB.locked == 0 then
  840. button:SetScript("OnMouseDown", parent_startmoving)
  841. button:SetScript("OnMouseUp", parent_stopmovingorsizing)
  842. end
  843.  
  844. f.editbox = editbox
  845. f.detail = detail
  846. f.button = button
  847. f.gold = gold
  848. self.frame = f
  849. f:Hide()
  850. end
  851.  
  852.  
  853. function Stuffing:Layout(lb)
  854. local slots = 0
  855. local rows = 0
  856. local off = 26
  857. local cols
  858. local f
  859. local bs
  860.  
  861. if lb then
  862. bs = BAGS_BANK
  863. cols = StuffingDB.bankCols
  864. f = self.bankFrame
  865. else
  866. bs = BAGS_BACKPACK
  867. cols = StuffingDB.cols
  868. f = self.frame
  869.  
  870. f.gold:SetText (GetCoinTextureString(GetMoney(), StuffingDB.fontSize))
  871. f.editbox:SetFont(StuffingDB.font, StuffingDB.fontSize)
  872. f.detail:SetFont(StuffingDB.font, StuffingDB.fontSize)
  873. f.gold:SetFont(StuffingDB.font, StuffingDB.fontSize)
  874.  
  875. f.detail:ClearAllPoints()
  876. f.detail:SetPoint("TOPLEFT", f, StuffingDB.padding, -10)
  877. f.detail:SetPoint("RIGHT", -(16 + 24), 0)
  878. end
  879.  
  880. f:SetClampedToScreen(StuffingDB.clamp)
  881. f:SetBackdrop({
  882. bgFile = StuffingDB.background,
  883. edgeFile = StuffingDB.edge,
  884. edgeSize = StuffingDB.edgeSize,
  885. insets = {left = 1, right = 1, top = 1, bottom = 1}
  886. })
  887. f:SetBackdropColor(unpack(StuffingDB.backdrop_color))
  888. f:SetBackdropBorderColor(unpack(StuffingDB.border_color))
  889.  
  890.  
  891. -- bag frame stuff
  892. local fb = f.bags_frame
  893. if StuffingDB.bag_bars == 1 then
  894. fb:SetClampedToScreen(StuffingDB.clamp)
  895. fb:SetBackdrop({
  896. bgFile = StuffingDB.background,
  897. edgeFile = StuffingDB.edge,
  898. edgeSize = StuffingDB.edgeSize,
  899. insets = {left = 1, right = 1, top = 1, bottom = 1}
  900. })
  901. fb:SetBackdropColor(unpack(StuffingDB.backdrop_color))
  902. fb:SetBackdropBorderColor(unpack(StuffingDB.border_color))
  903.  
  904. local bsize = 30
  905. if lb then bsize = 37 end
  906.  
  907. local w = 2 * StuffingDB.padding
  908. w = w + ((#bs - 1) * bsize)
  909. w = w + (StuffingDB.spacing * (#bs - 2))
  910.  
  911. fb:SetHeight(2 * StuffingDB.padding + bsize)
  912. fb:SetWidth(w)
  913. fb:Show()
  914. else
  915. fb:Hide()
  916. end
  917.  
  918.  
  919. local idx = 0
  920. for _, v in ipairs(bs) do
  921. if (not lb and v <= 3 ) or (lb and v ~= -1) then
  922. local bsize = 30
  923. if lb then bsize = 37 end
  924.  
  925. local b = self:BagFrameSlotNew(v, fb)
  926.  
  927. local xoff = StuffingDB.padding
  928.  
  929. xoff = xoff + (idx * bsize) -- StuffingDB.bsize)
  930. xoff = xoff + (idx * StuffingDB.spacing)
  931.  
  932. b.frame:ClearAllPoints()
  933. b.frame:SetPoint("LEFT", fb, "LEFT", xoff, 0)
  934. b.frame:Show()
  935.  
  936.  
  937. idx = idx + 1
  938. end
  939. end
  940.  
  941.  
  942. for _, i in ipairs(bs) do
  943. local x = GetContainerNumSlots(i)
  944. if x > 0 then
  945. if not self.bags[i] then
  946. self.bags[i] = self:BagNew(i, f)
  947. end
  948.  
  949. if not (StuffingDB.hide_soulbag == 1 and self.bags[i].bagType == ST_SOULBAG) then
  950. slots = slots + GetContainerNumSlots(i)
  951. end
  952. end
  953. end
  954.  
  955.  
  956. rows = floor (slots / cols)
  957. if (slots % cols) ~= 0 then
  958. rows = rows + 1
  959. end
  960.  
  961. f:SetWidth(cols * StuffingDB.bsize
  962. + (cols - 1) * StuffingDB.spacing
  963. + StuffingDB.padding * 2)
  964.  
  965. f:SetHeight(rows * StuffingDB.bsize
  966. + (rows - 1) * StuffingDB.spacing
  967. + off + StuffingDB.padding * 2)
  968.  
  969.  
  970. local idx = 0
  971. for _, i in ipairs(bs) do
  972. local bag_cnt = GetContainerNumSlots(i)
  973.  
  974. if bag_cnt > 0 then
  975. self.bags[i] = self:BagNew(i, f)
  976. local bagType = self.bags[i].bagType
  977.  
  978. if not (StuffingDB.hide_soulbag == 1 and bagType == ST_SOULBAG) then
  979. self.bags[i]:Show()
  980. --print (i .. ": " .. GetContainerNumSlots(i) .. " slots.")
  981. for j = 1, bag_cnt do
  982. local b, isnew = self:SlotNew (i, j)
  983. local xoff
  984. local yoff
  985. local x = (idx % cols)
  986. local y = floor(idx / cols)
  987.  
  988. if isnew then
  989. table.insert(self.buttons, idx + 1, b)
  990. end
  991.  
  992. xoff = StuffingDB.padding + (x * StuffingDB.bsize)
  993. + (x * StuffingDB.spacing)
  994.  
  995. yoff = off + StuffingDB.padding + (y * StuffingDB.bsize)
  996. + ((y - 1) * StuffingDB.spacing)
  997. yoff = yoff * -1
  998.  
  999. b.frame:ClearAllPoints()
  1000. b.frame:SetPoint("TOPLEFT", f, "TOPLEFT", xoff, yoff)
  1001. b.frame:SetHeight(StuffingDB.bsize)
  1002. b.frame:SetWidth(StuffingDB.bsize)
  1003. b.frame:Show()
  1004.  
  1005. local normalTex = _G[b.frame:GetName() .. "NormalTexture"]
  1006. if StuffingDB.custom_skin == 1 then
  1007. normalTex:SetTexture(BORDER_TEXTURE)
  1008. normalTex:ClearAllPoints()
  1009. normalTex:SetAllPoints(b.frame)
  1010. end
  1011. normalTex:SetHeight(StuffingDB.bsize / 37 * 64)
  1012. normalTex:SetWidth(StuffingDB.bsize / 37 * 64)
  1013. normalTex:Show()
  1014. b.normalTex = normalTex
  1015.  
  1016. if bagType == ST_QUIVER then
  1017. normalTex:SetVertexColor(unpack(StuffingDB.quiver_color))
  1018. elseif bagType == ST_SOULBAG then
  1019. normalTex:SetVertexColor(unpack(StuffingDB.soulbag_color))
  1020. elseif bagType == ST_SPECIAL then
  1021. normalTex:SetVertexColor(unpack(StuffingDB.special_color))
  1022. elseif bagType == ST_NORMAL then
  1023. normalTex:SetVertexColor(1, 1, 1, 1)
  1024. end
  1025.  
  1026. local iconTex = _G[b.frame:GetName() .. "IconTexture"]
  1027. if StuffingDB.custom_skin == 1 then
  1028. iconTex:SetTexCoord(0.1, 0.9, 0.1, 0.9)
  1029. iconTex:ClearAllPoints()
  1030. iconTex:SetPoint("TOPLEFT", b.frame, "TOPLEFT", 2, -2)
  1031. iconTex:SetPoint("BOTTOMRIGHT", b.frame, "BOTTOMRIGHT", -2, 2)
  1032. end
  1033. iconTex:Show()
  1034. b.iconTex = iconTex
  1035.  
  1036. if b.Glow then
  1037. b.Glow:SetWidth(StuffingDB.bsize / 37 * 65)
  1038. b.Glow:SetHeight(StuffingDB.bsize / 37 * 65)
  1039. end
  1040.  
  1041. if StuffingDB.custom_stackfont == 1 then
  1042. local scount = _G[b.frame:GetName() .. "Count"]
  1043. --scount:GetFontObject():SetFont (StuffingDB.stackfont, StuffingDB.stackfont_size, "OUTLINE")
  1044. scount:SetFont (StuffingDB.stackfont, StuffingDB.stackfont_size, "OUTLINE")
  1045. b.scount = scount
  1046. end
  1047.  
  1048. idx = idx + 1
  1049. end
  1050. else
  1051. -- XXX
  1052. self.bags[i]:Hide()
  1053. end
  1054. end
  1055. end
  1056. end
  1057.  
  1058.  
  1059. function Stuffing:SetBagsForSorting(c)
  1060. Stuffing_Open()
  1061.  
  1062. self.sortBags = {}
  1063.  
  1064. local cmd = ((c == nil or c == "") and {"d"} or {strsplit("/", c)})
  1065.  
  1066. for _, s in ipairs(cmd) do
  1067. if s == "c" then
  1068. self.sortBags = {}
  1069. elseif s == "d" then
  1070. if not self.bankFrame or not self.bankFrame:IsShown() then
  1071. for _, i in ipairs(BAGS_BACKPACK) do
  1072. if self.bags[i] and self.bags[i].bagType == ST_NORMAL then
  1073. table.insert(self.sortBags, i)
  1074. end
  1075. end
  1076. else
  1077. for _, i in ipairs(BAGS_BANK) do
  1078. if self.bags[i] and self.bags[i].bagType == ST_NORMAL then
  1079. table.insert(self.sortBags, i)
  1080. end
  1081. end
  1082. end
  1083. elseif s == "p" then
  1084. if not self.bankFrame or not self.bankFrame:IsShown() then
  1085. for _, i in ipairs(BAGS_BACKPACK) do
  1086. if self.bags[i] and self.bags[i].bagType == ST_SPECIAL then
  1087. table.insert(self.sortBags, i)
  1088. end
  1089. end
  1090. else
  1091. for _, i in ipairs(BAGS_BANK) do
  1092. if self.bags[i] and self.bags[i].bagType == ST_SPECIAL then
  1093. table.insert(self.sortBags, i)
  1094. end
  1095. end
  1096. end
  1097. else
  1098. if tonumber(s) == nil then
  1099. Print(string.format(L["Error: don't know what \"%s\" means."], s))
  1100. end
  1101.  
  1102. table.insert(self.sortBags, tonumber(s))
  1103. end
  1104. end
  1105.  
  1106. local bids = "Using bags: "
  1107. for _, i in ipairs(self.sortBags) do
  1108. bids = bids .. i .. " "
  1109. end
  1110.  
  1111. Print(bids)
  1112. end
  1113.  
  1114.  
  1115. -- slash command handler
  1116. local function StuffingSlashCmd(Cmd)
  1117. local cmd, args = strsplit(" ", Cmd:lower(), 2)
  1118.  
  1119. if cmd == "config" then
  1120. Stuffing_OpenConfig()
  1121. elseif cmd == "sort" then
  1122. Stuffing_Sort(args)
  1123. elseif cmd == "psort" then
  1124. Stuffing_Sort("c/p")
  1125. elseif cmd == "stack" then
  1126. Print(L["Restacking, please be patient."])
  1127. Stuffing:SetBagsForSorting(args)
  1128. Stuffing:Restack()
  1129. elseif cmd == "test" then
  1130. Stuffing:SetBagsForSorting(args)
  1131. elseif cmd == "purchase" then
  1132. -- XXX
  1133. if Stuffing.bankFrame and Stuffing.bankFrame:IsShown() then
  1134. local cnt, full = GetNumBankSlots()
  1135. if full then
  1136. Print("can't buy anymore slots")
  1137. return
  1138. end
  1139.  
  1140. if args == "yes" then
  1141. PurchaseSlot()
  1142. return
  1143. end
  1144.  
  1145. Print(string.format("Cost: %.2f gold", GetBankSlotCost() / 10000))
  1146. Print("Buy new slot with /stuffing purchase yes")
  1147. else
  1148. Print("You need to open your bank first")
  1149. end
  1150. else
  1151. Print(string.format(L["Version: %s."], GetAddOnMetadata("Stuffing", "Version")))
  1152. Print(L["Commands:"])
  1153. Print("sort - " .. L["sort your bags or your bank, if open."])
  1154. Print("stack - " .. L["fill up partial stacks in your bags or bank, if open."])
  1155. end
  1156. end
  1157.  
  1158.  
  1159. function Stuffing:ADDON_LOADED(addon)
  1160. if addon ~= "Stuffing" then
  1161. return nil
  1162. end
  1163.  
  1164. -- new options
  1165. if not StuffingDB.font then
  1166. StuffingDB.font = "Interface\\Addons\\Stuffing\\ARIAL.TTF"
  1167. end
  1168.  
  1169. if not StuffingDB.fontSize then
  1170. StuffingDB.fontSize = 14
  1171. end
  1172.  
  1173. if not StuffingDB.quest_glow then
  1174. StuffingDB.quest_glow = 1
  1175. end
  1176.  
  1177. if not StuffingDB.quest_glow_color then
  1178. StuffingDB.quest_glow_color = {0.3, 0.3, 0.8}
  1179. end
  1180.  
  1181. if not StuffingDB.custom_skin then
  1182. StuffingDB.custom_skin = 0
  1183. end
  1184.  
  1185. if not StuffingDB.hide_soulbag then
  1186. StuffingDB.hide_soulbag = 0
  1187. end
  1188.  
  1189. if StuffingDB.force_defaults then
  1190. if StuffingDB.force_defaults == 1 then
  1191. SetDefaults()
  1192. StuffingDB.force_defaults = 1
  1193. end
  1194. end
  1195.  
  1196. if not StuffingDB.bag_bars then
  1197. StuffingDB.bag_bars = 0
  1198. end
  1199.  
  1200. if not StuffingDB.soulbag_color then
  1201. StuffingDB.soulbag_color = {0.8, 0.2, 0.2, 1}
  1202. StuffingDB.special_color = {0.2, 0.2, 0.8, 1}
  1203. StuffingDB.quiver_color = {0.8, 0.8, 0.2, 1}
  1204. end
  1205.  
  1206. if not StuffingDB.stackfont then
  1207. StuffingDB.custom_stackfont = 0
  1208. StuffingDB.stackfont = "Interface\\Addons\\Stuffing\\ARIAL.TTF"
  1209. StuffingDB.stackfont_size = 8
  1210. end
  1211.  
  1212.  
  1213. self:UnregisterEvent("ADDON_LOADED")
  1214.  
  1215. self:RegisterEvent("BAG_UPDATE")
  1216. self:RegisterEvent("ITEM_LOCK_CHANGED")
  1217.  
  1218. self:RegisterEvent("BANKFRAME_OPENED")
  1219. self:RegisterEvent("BANKFRAME_CLOSED")
  1220. self:RegisterEvent("PLAYERBANKSLOTS_CHANGED")
  1221.  
  1222. self:RegisterEvent("BAG_CLOSED")
  1223.  
  1224. SlashCmdList["STUFFING"] = StuffingSlashCmd
  1225. SLASH_STUFFING1 = "/stuffing"
  1226.  
  1227. self:InitBags()
  1228.  
  1229. tinsert(UISpecialFrames,"StuffingFrameBags")
  1230.  
  1231. --
  1232. -- hook functions
  1233. --
  1234. ToggleBackpack = Stuffing_Toggle
  1235. OpenAllBags = Stuffing_Toggle
  1236. OpenBackpack = Stuffing_Toggle
  1237. CloseBackpack = Stuffing_Close
  1238. CloseAllBags = Stuffing_Close
  1239.  
  1240. BankFrame:UnregisterAllEvents()
  1241. end
  1242.  
  1243.  
  1244. function Stuffing:PLAYERBANKSLOTS_CHANGED(id)
  1245. if id > 28 then
  1246. for _, v in ipairs(self.bagframe_buttons) do
  1247. --local texture = nil
  1248.  
  1249. if v.frame and v.frame.GetInventorySlot then
  1250.  
  1251. BankFrameItemButton_Update(v.frame)
  1252. BankFrameItemButton_UpdateLocked(v.frame)
  1253.  
  1254. if not v.frame.tooltipText then
  1255. v.frame.tooltipText = ""
  1256. end
  1257.  
  1258. --[[
  1259. local is = v.frame:GetInventorySlot()
  1260. texture = GetInventoryItemTexture("player", is)
  1261. --]]
  1262. --print(texture)
  1263.  
  1264. --[[
  1265. if texture ~= nil then
  1266. texture = "interface\\paperdoll\\UI-PaperDoll-Slot-Bag.blp"
  1267. end
  1268. --]]
  1269. end
  1270.  
  1271. --SetItemButtonTexture(v.frame, texture)
  1272. end
  1273. end
  1274.  
  1275. if self.bankFrame and self.bankFrame:IsShown() then
  1276. self:BagSlotUpdate(-1)
  1277. end
  1278. end
  1279.  
  1280.  
  1281. function Stuffing:BAG_UPDATE(id)
  1282. self:BagSlotUpdate(id)
  1283. end
  1284.  
  1285.  
  1286. function Stuffing:ITEM_LOCK_CHANGED(bag, slot)
  1287. if slot == nil then
  1288. return
  1289. end
  1290.  
  1291. for _, v in ipairs(self.buttons) do
  1292. if v.bag == bag and v.slot == slot then
  1293. self:SlotUpdate(v)
  1294. break
  1295. end
  1296. end
  1297. end
  1298.  
  1299.  
  1300. function Stuffing:BANKFRAME_OPENED()
  1301. if not self.bankFrame then
  1302. self:InitBank()
  1303. end
  1304.  
  1305. self:Layout(true)
  1306. for _, x in ipairs(BAGS_BANK) do
  1307. self:BagSlotUpdate(x)
  1308. end
  1309. self.bankFrame:Show()
  1310. Stuffing_Open()
  1311. end
  1312.  
  1313.  
  1314. function Stuffing:BANKFRAME_CLOSED()
  1315. if not self.bankFrame then
  1316. return
  1317. end
  1318.  
  1319. self.bankFrame:Hide()
  1320. end
  1321.  
  1322.  
  1323. function Stuffing:BAG_CLOSED(id)
  1324. --Print("BAG_CLOSED: " .. id)
  1325.  
  1326. local b = self.bags[id]
  1327. if b then
  1328. table.remove(self.bags, id)
  1329. b:Hide()
  1330. table.insert (trashBag, #trashBag + 1, b)
  1331. -- else
  1332. -- print("BAG_CLOSED: no such bag: " .. id)
  1333. end
  1334.  
  1335. while true do
  1336. local changed = false
  1337.  
  1338. for i, v in ipairs(self.buttons) do
  1339. if v.bag == id then
  1340. v.frame:Hide()
  1341. v.normalTex:Hide()
  1342. v.iconTex:Hide()
  1343.  
  1344. if v.Glow then
  1345. v.Glow:Hide()
  1346. end
  1347.  
  1348. table.insert (trashButton, #trashButton + 1, v.frame)
  1349. table.remove(self.buttons, i)
  1350.  
  1351. v = nil
  1352. changed = true
  1353. end
  1354. end
  1355.  
  1356. if not changed then
  1357. break
  1358. end
  1359. end
  1360. end
  1361.  
  1362.  
  1363. function Stuffing:SortOnUpdate(e)
  1364. if not self.elapsed then
  1365. self.elapsed = 0
  1366. end
  1367.  
  1368. if not self.itmax then
  1369. self.itmax = 0
  1370. end
  1371.  
  1372. self.elapsed = self.elapsed + e
  1373.  
  1374. if self.elapsed < 0.1 then
  1375. return
  1376. end
  1377.  
  1378. self.elapsed = 0
  1379. self.itmax = self.itmax + 1
  1380.  
  1381. local changed, blocked = false, false
  1382.  
  1383. if self.sortList == nil or next(self.sortList, nil) == nil then
  1384. -- wait for all item locks to be released.
  1385. local locks = false
  1386.  
  1387. for i, v in pairs(self.buttons) do
  1388. local _, _, l = GetContainerItemInfo(v.bag, v.slot)
  1389. if l then
  1390. locks = true
  1391. else
  1392. v.block = false
  1393. end
  1394. end
  1395.  
  1396. if locks then
  1397. -- something still locked. wait some more.
  1398. return
  1399. else
  1400. -- all unlocked. get a new table.
  1401. self:SetScript("OnUpdate", nil)
  1402. self:SortBags()
  1403.  
  1404. if self.sortList == nil then
  1405. return
  1406. end
  1407. end
  1408. end
  1409.  
  1410. -- go through the list and move stuff if we can.
  1411. for i, v in ipairs (self.sortList) do
  1412. repeat
  1413. if v.ignore then
  1414. blocked = true
  1415. break
  1416. end
  1417.  
  1418. if v.srcSlot.block then
  1419. changed = true
  1420. break
  1421. end
  1422.  
  1423. if v.dstSlot.block then
  1424. changed = true
  1425. break
  1426. end
  1427.  
  1428. local _, _, l1 = GetContainerItemInfo(v.dstSlot.bag, v.dstSlot.slot)
  1429. local _, _, l2 = GetContainerItemInfo(v.srcSlot.bag, v.srcSlot.slot)
  1430.  
  1431. if l1 then
  1432. v.dstSlot.block = true
  1433. end
  1434.  
  1435. if l2 then
  1436. v.srcSlot.block = true
  1437. end
  1438.  
  1439. if l1 or l2 then
  1440. break
  1441. end
  1442.  
  1443. if v.sbag ~= v.dbag or v.sslot ~= v.dslot then
  1444. if v.srcSlot.name ~= v.dstSlot.name then
  1445. v.srcSlot.block = true
  1446. v.dstSlot.block = true
  1447. PickupContainerItem (v.sbag, v.sslot)
  1448. PickupContainerItem (v.dbag, v.dslot)
  1449. changed = true
  1450. break
  1451. end
  1452. end
  1453. until true
  1454. end
  1455.  
  1456. self.sortList = nil
  1457.  
  1458. if (not changed and not blocked) or self.itmax > 250 then
  1459. self:SetScript("OnUpdate", nil)
  1460. self.sortList = nil
  1461. Print (L["Sorting finished."])
  1462. end
  1463. end
  1464.  
  1465.  
  1466. local function InBags(x)
  1467. if not Stuffing.bags[x] then
  1468. return false
  1469. end
  1470.  
  1471. for _, v in ipairs(Stuffing.sortBags) do
  1472. if x == v then
  1473. return true
  1474. end
  1475. end
  1476. return false
  1477. end
  1478.  
  1479.  
  1480. function Stuffing:SortBags()
  1481. local bs = self.sortBags
  1482. if #bs < 1 then
  1483. Print (L["Nothing to sort."])
  1484. return
  1485. end
  1486.  
  1487. local st = {}
  1488. local bank = false
  1489.  
  1490.  
  1491. Stuffing_Open()
  1492.  
  1493. -- get a list of all buttons we want to sort and construct
  1494. -- a string to sort them by.
  1495. for i, v in pairs(self.buttons) do
  1496. if InBags(v.bag) then
  1497. self:SlotUpdate(v)
  1498.  
  1499. if v.name then
  1500. local tex, cnt, _, _, _, _, clink = GetContainerItemInfo(v.bag, v.slot)
  1501. local n, _, q, iL, rL, c1, c2, _, Sl = GetItemInfo(clink)
  1502. table.insert(st, {
  1503. srcSlot = v,
  1504. sslot = v.slot,
  1505. sbag = v.bag,
  1506. --sort = q .. iL .. c1 .. c2 .. rL .. Sl .. n .. i,
  1507. --sort = q .. iL .. c1 .. c2 .. rL .. Sl .. n .. (#self.buttons - i),
  1508. sort = q .. c1 .. c2 .. rL .. n .. iL .. Sl .. (#self.buttons - i),
  1509. --sort = q .. (#self.buttons - i) .. n,
  1510. })
  1511. end
  1512. end
  1513. end
  1514.  
  1515. -- sort them
  1516. table.sort (st, function(a, b)
  1517. return a.sort > b.sort
  1518. end)
  1519.  
  1520. -- for each button we want to sort, get a destination button
  1521. local st_idx = 1
  1522. local dbag = bs[st_idx]
  1523. local dslot = 1
  1524. local max_dslot = GetContainerNumSlots(dbag)
  1525.  
  1526. for i, v in ipairs (st) do
  1527. v.dbag = dbag
  1528. v.dslot = dslot
  1529. v.dstSlot = self:SlotNew(dbag, dslot)
  1530.  
  1531. dslot = dslot + 1
  1532.  
  1533. if dslot > max_dslot then
  1534. dslot = 1
  1535.  
  1536. while true do
  1537. st_idx = st_idx + 1
  1538.  
  1539. if st_idx > #bs then
  1540. break
  1541. end
  1542.  
  1543. dbag = bs[st_idx]
  1544.  
  1545. if Stuffing:BagType(dbag) == ST_NORMAL or dbag > 4 then
  1546. break
  1547. end
  1548. end
  1549.  
  1550. max_dslot = GetContainerNumSlots(dbag)
  1551. end
  1552. end
  1553.  
  1554. -- throw various stuff out of the search list
  1555. local changed = true
  1556. while changed do
  1557. changed = false
  1558. -- XXX why doesn't this remove all x->x moves in one pass?
  1559.  
  1560. for i, v in ipairs (st) do
  1561.  
  1562. -- source is same as destination
  1563. if (v.sslot == v.dslot) and (v.sbag == v.dbag) then
  1564. table.remove (st, i)
  1565. changed = true
  1566.  
  1567. -- same item
  1568. --[[
  1569. elseif v.srcSlot.name == v.dstSlot.name then
  1570. table.remove (st, i) -- XXX
  1571. changed = true
  1572. --]]
  1573. end
  1574. --
  1575. end
  1576. end
  1577.  
  1578. -- if the destination of something is the source of
  1579. -- something else, don't move it this round.
  1580. --[[
  1581. for i, v in ipairs (st) do
  1582. if not v.ignore then
  1583. for j, w in ipairs (st) do
  1584. if not w.ignore then
  1585. if v ~= w and v.dbag == w.sbag and v.dslot == w.sslot then
  1586. if not (v.sbag == w.dbag and v.sslot == w.dslot) then
  1587. w.ignore = true
  1588. end
  1589. end
  1590. end
  1591. end
  1592. end
  1593. end
  1594. --]]
  1595.  
  1596.  
  1597. --[[
  1598. print(#st)
  1599. for i, v in ipairs (st) do
  1600. if not v.ignore then
  1601. print("OK " .. v.sbag .. ":" .. v.sslot .. " -> " .. v.dbag .. ":" .. v.dslot)
  1602. else
  1603. print("IG " .. v.sbag .. ":" .. v.sslot .. " -> " .. v.dbag .. ":" .. v.dslot)
  1604. end
  1605. end
  1606. --]]
  1607.  
  1608. -- kick off moving of stuff, if needed.
  1609. if st == nil or next(st, nil) == nil then
  1610. Print(L["Sorting finished."])
  1611. self:SetScript("OnUpdate", nil)
  1612. else
  1613. self.sortList = st
  1614. self:SetScript("OnUpdate", Stuffing.SortOnUpdate)
  1615. end
  1616. end
  1617.  
  1618.  
  1619. function Stuffing:RestackOnUpdate(e)
  1620. if not self.elapsed then
  1621. self.elapsed = 0
  1622. end
  1623.  
  1624. self.elapsed = self.elapsed + e
  1625.  
  1626. if self.elapsed < 0.1 then
  1627. return
  1628. end
  1629.  
  1630. self.elapsed = 0
  1631. self:Restack()
  1632. end
  1633.  
  1634.  
  1635. function Stuffing:Restack()
  1636. local st = {}
  1637.  
  1638. Stuffing_Open()
  1639.  
  1640. for i, v in pairs(self.buttons) do
  1641. if InBags(v.bag) then
  1642. local tex, cnt, _, _, _, _, clink = GetContainerItemInfo(v.bag, v.slot)
  1643. if clink then
  1644. local n, _, _, _, _, _, _, s = GetItemInfo(clink)
  1645.  
  1646. if cnt ~= s then
  1647. if not st[n] then
  1648. st[n] = {{
  1649. item = v,
  1650. size = cnt,
  1651. max = s
  1652. }}
  1653. else
  1654. table.insert(st[n], {
  1655. item = v,
  1656. size = cnt,
  1657. max = s
  1658. })
  1659. end
  1660. end
  1661. end
  1662. end
  1663. end
  1664.  
  1665. local did_restack = false
  1666.  
  1667. for i, v in pairs(st) do
  1668. if #v > 1 then
  1669. for j = 2, #v, 2 do
  1670. local a, b = v[j - 1], v[j]
  1671. local _, _, l1 = GetContainerItemInfo(a.item.bag, a.item.slot)
  1672. local _, _, l2 = GetContainerItemInfo(b.item.bag, b.item.slot)
  1673.  
  1674. if l1 or l2 then
  1675. did_restack = true
  1676. else
  1677. PickupContainerItem (a.item.bag, a.item.slot)
  1678. PickupContainerItem (b.item.bag, b.item.slot)
  1679. did_restack = true
  1680. end
  1681. end
  1682. end
  1683. end
  1684.  
  1685. if did_restack then
  1686. self:SetScript("OnUpdate", Stuffing.RestackOnUpdate)
  1687. else
  1688. self:SetScript("OnUpdate", nil)
  1689. Print (L["Restacking finished."])
  1690. end
  1691. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement