PageRenderTime 28ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/Tukui/modules/extras/mail.lua

http://github.com/Asphyxia/Tukui
Lua | 138 lines | 121 code | 14 blank | 3 comment | 19 complexity | 0a0c2a74a8462111f155a46522e58a58 MD5 | raw file
  1. local T, C, L = unpack(select(2, ...)) -- Import: T - functions, constants, variables; C - config; L - locales
  2. ----------------------------------------------------------------------------------------
  3. -- Grab mail in 1 button(OpenAll by Kemayo)
  4. ----------------------------------------------------------------------------------------
  5. local COPPER_ICON = "|TInterface\\MONEYFRAME\\UI-CopperIcon:0:0:0:-1|t"
  6. local SILVER_ICON = "|TInterface\\MONEYFRAME\\UI-SilverIcon:0:0:0:-1|t"
  7. local GOLD_ICON = "|TInterface\\MONEYFRAME\\UI-GoldIcon:0:0:0:-1|t"
  8. local L_MAIL_STOPPED = ERR_INV_FULL
  9. local L_MAIL_COMPLETE = DONE
  10. local L_MAIL_NEED = L.mail_need
  11. local L_MAIL_MESSAGES = L.mail_messages
  12. local deletedelay, t = 0.5, 0
  13. local takingOnlyCash = false
  14. local button, button2, waitForMail, openAll, openAllCash, openMail, lastopened, stopOpening, onEvent, needsToWait, copper_to_pretty_money, total_cash
  15. local _G = _G
  16. local baseInboxFrame_OnClick
  17. function openAll()
  18. if GetInboxNumItems() == 0 then return end
  19. button:SetScript("OnClick", nil)
  20. button2:SetScript("OnClick", nil)
  21. baseInboxFrame_OnClick = InboxFrame_OnClick
  22. InboxFrame_OnClick = function() end
  23. button:RegisterEvent("UI_ERROR_MESSAGE")
  24. openMail(GetInboxNumItems())
  25. end
  26. function openAllCash()
  27. takingOnlyCash = true
  28. openAll()
  29. end
  30. function openMail(index)
  31. if not InboxFrame:IsVisible() then return stopOpening("|cffffff00"..L_MAIL_NEED) end
  32. if index == 0 then MiniMapMailFrame:Hide() stopOpening("|cffffff00"..L_MAIL_COMPLETE) return end
  33. local _, _, _, _, money, COD, _, numItems = GetInboxHeaderInfo(index)
  34. if money > 0 then
  35. TakeInboxMoney(index)
  36. needsToWait = true
  37. if total_cash then total_cash = total_cash - money end
  38. elseif (not takingOnlyCash) and numItems and (numItems > 0) and COD <= 0 then
  39. TakeInboxItem(index)
  40. needsToWait = true
  41. end
  42. local items = GetInboxNumItems()
  43. if (numItems and numItems > 1) or (items > 1 and index <= items) then
  44. lastopened = index
  45. t = 0
  46. button:SetScript("OnUpdate", waitForMail)
  47. else
  48. stopOpening("|cffffff00"..L_MAIL_COMPLETE)
  49. MiniMapMailFrame:Hide()
  50. TukuiMinimap:SetBackdropBorderColor(unpack(C.media.bordercolor))
  51. end
  52. end
  53. function waitForMail(self,elapsed)
  54. t = t + elapsed
  55. if (not needsToWait) or (t > deletedelay) then
  56. needsToWait = false
  57. button:SetScript("OnUpdate", nil)
  58. local _, _, _, _, money, COD, _, numItems = GetInboxHeaderInfo(lastopened)
  59. if money > 0 or ((not takingOnlyCash) and COD <= 0 and numItems and (numItems > 0)) then
  60. openMail(lastopened)
  61. else
  62. openMail(lastopened - 1)
  63. end
  64. end
  65. end
  66. function stopOpening(msg, ...)
  67. button:SetScript("OnUpdate", nil)
  68. button:SetScript("OnClick", openAll)
  69. button2:SetScript("OnClick", openAllCash)
  70. if baseInboxFrame_OnClick then
  71. InboxFrame_OnClick = baseInboxFrame_OnClick
  72. end
  73. button:UnregisterEvent("UI_ERROR_MESSAGE")
  74. takingOnlyCash = false
  75. total_cash = nil
  76. if msg then DEFAULT_CHAT_FRAME:AddMessage(msg, ...) end
  77. end
  78. function onEvent(frame, event, arg1, arg2, arg3, arg4)
  79. if event == "UI_ERROR_MESSAGE" then
  80. if arg1 == ERR_INV_FULL then
  81. stopOpening("|cffffff00"..L_MAIL_STOPPED)
  82. end
  83. end
  84. end
  85. local function makeButton(id, text, w, h, x, y)
  86. local button = CreateFrame("Button", id, InboxFrame, "UIPanelButtonTemplate")
  87. button:SetWidth(w)
  88. button:SetHeight(h)
  89. button:SetPoint("CENTER", InboxFrame, "TOP", x, y)
  90. button:SetText(text)
  91. return button
  92. end
  93. button = makeButton("TakeAll_Button", ALL, 55, 25, -55, -408)
  94. button:SetScript("OnClick", openAll)
  95. button:SetScript("OnEvent", onEvent)
  96. button2 = makeButton("TakeCash_Button", GUILDCONTROL_OPTION16, 85, 25, 18, -408)
  97. button2:SetScript("OnClick", openAllCash)
  98. button:SetScript("OnEnter", function()
  99. GameTooltip:SetOwner(button, "ANCHOR_RIGHT")
  100. GameTooltip:AddLine(string.format("%d "..L_MAIL_MESSAGES, GetInboxNumItems()), 1, 1, 1)
  101. GameTooltip:Show()
  102. end)
  103. button:SetScript("OnLeave", function() GameTooltip:Hide() end)
  104. function copper_to_pretty_money(c)
  105. if c > 10000 then
  106. return ("%d|cffffd700"..GOLD_ICON.."|r %d|cffc7c7cf"..SILVER_ICON.."|r %d|cffeda55f"..COPPER_ICON.."|r"):format(c/10000, (c/100)%100, c%100)
  107. elseif c > 100 then
  108. return ("%d|cffc7c7cf"..SILVER_ICON.."|r %d|cffeda55f"..COPPER_ICON.."|r"):format((c/100)%100, c%100)
  109. else
  110. return ("%d|cffeda55f"..COPPER_ICON.."|r"):format(c%100)
  111. end
  112. end
  113. button2:SetScript("OnEnter", function()
  114. if not total_cash then
  115. total_cash = 0
  116. for index = 0, GetInboxNumItems() do
  117. total_cash = total_cash + select(5, GetInboxHeaderInfo(index))
  118. end
  119. end
  120. GameTooltip:SetOwner(button, "ANCHOR_RIGHT")
  121. GameTooltip:AddLine(copper_to_pretty_money(total_cash), 1, 1, 1)
  122. GameTooltip:Show()
  123. end)
  124. button2:SetScript("OnLeave", function() GameTooltip:Hide() end)