PageRenderTime 203ms CodeModel.GetById 50ms app.highlight 108ms RepoModel.GetById 33ms app.codeStats 1ms

/Tukui/modules/datatext/time.lua

http://github.com/Asphyxia/Tukui
Lua | 206 lines | 179 code | 23 blank | 4 comment | 55 complexity | f9d50d224ad8f710360ca5b0be91d80d MD5 | raw file
  1--------------------------------------------------------------------
  2-- TIME
  3--------------------------------------------------------------------
  4local T, C, L = unpack(select(2, ...)) -- Import Functions/Constants, Config, Locales
  5
  6if not C["datatext"].wowtime or C["datatext"].wowtime == 0 then return end
  7
  8local europeDisplayFormat = string.join("", "%02d", T.datacolor,":|r%02d")
  9local ukDisplayFormat = string.join("", "", "%d", T.datacolor, ":|r%02d", T.datacolor, " %s|r")
 10local timerLongFormat = "%d:%02d:%02d"
 11local timerShortFormat = "%d:%02d"
 12local lockoutInfoFormat = "%s |cffaaaaaa(%s%s, %s/%s)"
 13local formatBattleGroundInfo = "%s: "
 14local lockoutColorExtended, lockoutColorNormal = { r=0.3,g=1,b=0.3 }, { r=1,g=1,b=1 }
 15local difficultyInfo = { "N", "N", "H", "H" }
 16local curHr, curMin, curAmPm
 17
 18local Stat = CreateFrame("Frame")
 19Stat:EnableMouse(true)
 20Stat:SetFrameStrata("MEDIUM")
 21Stat:SetFrameLevel(3)
 22
 23local Text = Stat:CreateFontString(nil, "OVERLAY")
 24Text:SetFont(C.media.pixelfont, C["datatext"].fontsize, "MONOCHROMEOUTLINE")
 25T.PP(C["datatext"].wowtime, Text)
 26
 27local APM = { TIMEMANAGER_PM, TIMEMANAGER_AM }
 28
 29local function CalculateTimeValues(tt)
 30	if tt == nil then tt = false end
 31	local Hr, Min, AmPm
 32	if tt == true then
 33		if C["datatext"].localtime == true then
 34			Hr, Min = GetGameTime()
 35			if C["datatext"].time24 == true then
 36				return Hr, Min, -1
 37			else
 38				if Hr>=12 then
 39					if Hr>12 then Hr = Hr - 12 end
 40					AmPm = 1
 41				else
 42					if Hr == 0 then Hr = 12 end
 43					AmPm = 2
 44				end
 45				return Hr, Min, AmPm
 46			end			
 47		else
 48			local Hr24 = tonumber(date("%H"))
 49			Hr = tonumber(date("%I"))
 50			Min = tonumber(date("%M"))
 51			if C["datatext"].time24 == true then
 52				return Hr24, Min, -1
 53			else
 54				if Hr24>=12 then AmPm = 1 else AmPm = 2 end
 55				return Hr, Min, AmPm
 56			end
 57		end
 58	else
 59		if C["datatext"].localtime == true then
 60			local Hr24 = tonumber(date("%H"))
 61			Hr = tonumber(date("%I"))
 62			Min = tonumber(date("%M"))
 63			if C["datatext"].time24 == true then
 64				return Hr24, Min, -1
 65			else
 66				if Hr24>=12 then AmPm = 1 else AmPm = 2 end
 67				return Hr, Min, AmPm
 68			end
 69		else
 70			Hr, Min = GetGameTime()
 71			if C["datatext"].time24 == true then
 72				return Hr, Min, -1
 73			else
 74				if Hr>=12 then
 75					if Hr>12 then Hr = Hr - 12 end
 76					AmPm = 1
 77				else
 78					if Hr == 0 then Hr = 12 end
 79					AmPm = 2
 80				end
 81				return Hr, Min, AmPm
 82			end
 83		end	
 84	end
 85end
 86
 87local function CalculateTimeLeft(time)
 88		local hour = floor(time / 3600)
 89		local min = floor(time / 60 - (hour*60))
 90		local sec = time - (hour * 3600) - (min * 60)
 91		
 92		return hour, min, sec
 93end
 94
 95local function formatResetTime(sec,table)
 96	local table = table or {}
 97	local d,h,m,s = ChatFrame_TimeBreakDown(floor(sec))
 98	local string = gsub(gsub(format(" %dd %dh %dm "..((d==0 and h==0) and "%ds" or ""),d,h,m,s)," 0[dhms]"," "),"%s+"," ")
 99	local string = strtrim(gsub(string, "([dhms])", {d=table.days or L.datatext_time_day,h=table.hours or L.datatext_time_hour,m=table.minutes or L.datatext_time_min,s=table.seconds or L.datatext_time_sec})," ")
100	return strmatch(string,"^%s*$") and "0"..(table.seconds or L"s") or string
101end
102
103local int = 1
104local function Update(self, t)
105	int = int - t
106	if int > 0 then return end
107	
108	local Hr, Min, AmPm = CalculateTimeValues()
109	
110	if GameTimeFrame.flashInvite then
111		T.Flash(Text, 0.4)
112	else
113		T.StopFlash(Text)
114	end
115	
116	-- no update quick exit
117	if (Hr == curHr and Min == curMin and AmPm == curAmPm) then
118		int = 2
119		return
120	end
121	
122	curHr = Hr
123	curMin = Min
124	curAmPm = AmPm
125		
126	if AmPm == -1 then
127		Text:SetFormattedText(europeDisplayFormat, Hr, Min)
128	else
129		Text:SetFormattedText(ukDisplayFormat, Hr, Min, APM[AmPm])
130	end
131
132	self:SetAllPoints(Text)
133	int = 2
134end
135
136Stat:SetScript("OnEnter", function(self)
137	OnLoad = function(self) RequestRaidInfo() end
138	local anchor, panel, xoff, yoff = T.DataTextTooltipAnchor(Text)
139	GameTooltip:SetOwner(panel, anchor, xoff, yoff)
140	GameTooltip:ClearLines()
141
142	local localizedName, isActive, canQueue, startTime, canEnter
143	for i = 1, GetNumWorldPVPAreas() do
144		_, localizedName, isActive, canQueue, startTime, canEnter = GetWorldPVPAreaInfo(i)
145		if canEnter then
146			if isActive then
147				startTime = WINTERGRASP_IN_PROGRESS
148			elseif startTime == nil then
149				startTime = QUEUE_TIME_UNAVAILABLE
150			else
151				local hour, min, sec = CalculateTimeLeft(startTime)
152				if hour > 0 then 
153					startTime = string.format(timerLongFormat, hour, min, sec) 
154				else 
155					startTime = string.format(timerShortFormat, min, sec)
156				end
157			end
158			GameTooltip:AddDoubleLine(format(formatBattleGroundInfo, localizedName), startTime)	
159		end
160	end	
161
162	local timeText
163	local Hr, Min, AmPm = CalculateTimeValues(true)
164
165	if C["datatext"].localtime == true then
166		timeText = TIMEMANAGER_TOOLTIP_REALMTIME
167	else
168		timeText = TIMEMANAGER_TOOLTIP_LOCALTIME
169	end
170	
171	if AmPm == -1 then
172		GameTooltip:AddDoubleLine(timeText, string.format(europeDisplayFormat, Hr, Min))
173	else
174		GameTooltip:AddDoubleLine(timeText, string.format(ukDisplayFormat, Hr, Min, APM[AmPm]))
175	end
176	
177	local oneraid, lockoutColor
178	for i = 1, GetNumSavedInstances() do
179		local name, _, reset, difficulty, locked, extended, _, isRaid, maxPlayers, _, numEncounters, encounterProgress  = GetSavedInstanceInfo(i)
180		if isRaid and (locked or extended) then
181			local tr,tg,tb,diff
182			if not oneraid then
183				GameTooltip:AddLine(" ")
184				GameTooltip:AddLine(L.datatext_savedraid)
185				oneraid = true
186			end
187			if extended then lockoutColor = lockoutColorExtended else lockoutColor = lockoutColorNormal end
188			GameTooltip:AddDoubleLine(format(lockoutInfoFormat, name, maxPlayers, difficultyInfo[difficulty],encounterProgress,numEncounters), formatResetTime(reset), 1,1,1, lockoutColor.r,lockoutColor.g,lockoutColor.b)
189		end
190	end
191	GameTooltip:Show()
192end)
193
194Stat:SetScript("OnLeave", function() GameTooltip:Hide() end)
195Stat:RegisterEvent("CALENDAR_UPDATE_PENDING_INVITES")
196Stat:RegisterEvent("PLAYER_ENTERING_WORLD")
197Stat:SetScript("OnUpdate", Update)
198Stat:RegisterEvent("UPDATE_INSTANCE_INFO")
199Stat:SetScript("OnMouseDown", function(self, btn)
200	if btn == 'RightButton'  then
201		ToggleTimeManager()
202	else
203		GameTimeFrame:Click()
204	end
205end)
206Update(Stat, 10)