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