Quantcast
--[[
	Copyright (c) 2014 Eyal Shilony <Lynxium>

	Permission is hereby granted, free of charge, to any person obtaining
	a copy of this software and associated documentation files (the
	"Software"), to deal in the Software without restriction, including
	without limitation the rights to use, copy, modify, merge, publish,
	distribute, sublicense, and/or sell copies of the Software, and to
	permit persons to whom the Software is furnished to do so, subject to
	the following conditions:

	The above copyright notice and this permission notice shall be
	included in all copies or substantial portions of the Software.

	THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
	EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
	MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
	NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
	LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
	OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
	WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

	-- A library that provides API to display the game interfaces.
]]

assert(LibStub, "EasyDisplay-1.0 requires LibStub")

local lib, minor = LibStub:NewLibrary("EasyDisplay-1.0", 6)
if not lib then return end
minor = minor or 0

local GetUIPanel = GetUIPanel
local HideUIPanel = HideUIPanel
local ShowUIPanel = ShowUIPanel
local UIPanelWindows = UIPanelWindows

--[[ LOCALIZATION ]]

local BAGS = "Bags"
local CALENDAR = "Calendar"
local CHARACTER_MSG = "|cffFFFF00Cannot display frame '%s' because the character level is too low.|r"

--[[ MANAGED FRAMES ]]

-- Determines whether the GameMenuFrame should be hidden when closing one of the listed interfaces.
GameMenu_OptionFrames = {
	["VideoOptionsFrame"] = true,
	["AudioOptionsFrame"] = true,
	["InterfaceOptionsFrame"] = true,
	["MacOptionsFrame"] = true,
	["KeyBindingFrame"] = true,
}

-- Must be loaded before the interface is displayed.
LoD_AddonFrames = {
	["PlayerTalentFrame"] = TalentFrame_LoadUI,
	["AchievementFrame"] = AchievementFrame_LoadUI,
	["CalendarFrame"] = Calendar_LoadUI,
	["KeyBindingFrame"] = KeyBindingFrame_LoadUI,
	["MacroFrame"] = MacroFrame_LoadUI,
	["TimeManagerFrame"] = TimeManager_LoadUI,
	["GuildFrame"] = GuildFrame_LoadUI,
	["EncounterJournal"] = EncounterJournal_LoadUI(),
}

--[[ PREDEFINED INTERFACES ]]

--[[
	name - (string) The name to access the interface.
	title - (string) The title that represents the interface.
	alias - (string) An optional name to access the interface.
	frameName - (string) The name of the frame as it appears in the UIPanelWindows table to manage the display or the name of the frame as it appears in the ProtectedFrames table to determine whether the frame is protected.
	button (frame) - The button to click to open the interface.
	level - (number) The minimum level of the player required to display the interface.
	func - (function) The function (handler) to display the interface.
]]
local interfaces = {
	{
		name = "character",
		title = CHARACTER_BUTTON,
		alias = "char",
		frameName = "CharacterFrame",
		--button = CharacterMicroButton,
		func = function() ToggleCharacter("PaperDollFrame") end,
	},
	{
		name = "spellbook",
		title = SPELLBOOK_ABILITIES_BUTTON,
		alias = "sb",
		frameName = "SpellBookFrame",
		button = SpellbookMicroButton,
		func = function() ToggleFrame(SpellBookFrame) end,
	},
	{
		name = "talents",
		title = TALENTS_BUTTON,
		alias = "tal",
		frameName = "PlayerTalentFrame",
		level = SHOW_TALENT_LEVEL,
		button = TalentMicroButton,
		func = function() ToggleTalentFrame() end,
	},
	{
		name = "achievements",
		title = ACHIEVEMENT_BUTTON,
		alias = "achi",
		frameName = "AchievementFrame",
		button = AchievementMicroButton,
		func = function() ToggleAchievementFrame() end,
	},
	{
		name = "calendar",
		title = CALENDAR,
		alias = "cal",
		frameName = "CalendarFrame",
		func = function() ToggleCalendar() end,
	},
	{
		name = "questlog",
		title = QUESTLOG_BUTTON,
		alias = "ql",
		frameName = "QuestLogFrame",
		button = QuestLogMicroButton,
		func = function() ToggleFrame(QuestLogFrame) end,
	},
	{
		name = "social",
		title = SOCIAL_BUTTON,
		alias = "soc",
		frameName = "FriendsFrame",
		func = function() ToggleFriendsFrame() end,
	},
	{
		name = "pvp",
		title = PLAYER_V_PLAYER,
		frameName = "PVPFrame",
		level = SHOW_PVP_LEVEL,
		--button = PVPMicroButton,
		func = function() TogglePVPUI() end,
	},
	{
		name = "lfd",
		title = DUNGEONS_BUTTON,
		frameName = "LFDParentFrame",
		level = SHOW_LFD_LEVEL,
		button = LFDMicroButton,
		func = function() ToggleLFDParentFrame() end,
	},
	{
		name = "lfr",
		title = RAID_FINDER,
		frameName = "LFRParentFrame",
		level = SHOW_LFD_LEVEL,
		func = function() ToggleRaidFrame() end,
	},
	{
		name = "companions",
		title = MOUNTS_AND_PETS,
		alias = "pets",
		frameName = "PetJournalParent",
		button = CompanionsMicroButton,
		func = function() TogglePetJournal() end,
	},
	{
		name = "journal",
		title = ENCOUNTER_JOURNAL,
		frameName = "EncounterJournal",
		level = SHOW_LFD_LEVEL,
		button = EJMicroButton,
		func = function() ToggleEncounterJournal() end,
	},
	{
		name = "guild",
		title = GUILD,
		frameName = "GuildFrame",
		button = GuildMicroButton,
		func = function() ToggleGuildFrame() end,
	},
	{
		name = "backpack",
		title = BACKPACK_TOOLTIP,
		func = function()
			if IsModifierKeyDown() then
				ToggleAllBags()
			else
				ToggleBackpack()
			end
		end,
	},
	{
		name = "bags",
		title = BAGS,
		func = function()
			ToggleAllBags()
		end,
	},
	{
		name = "keyring",
		title = KEYRING,
		func = function()
			ToggleKeyRing()
		end,
	},
	{
		name = "time",
		title = TIMEMANAGER_TITLE,
		frameName = "TimeManagerFrame",
		func = function() ToggleTimeManager() end,
	},
	{
		name = "help",
		title = HELP_BUTTON,
		frameName = "HelpFrame",
		button = HelpMicroButton,
		func = function() ToggleHelpFrame() end,
	},
	{
		name = "options",
		title = SYSTEMOPTIONS_MENU,
		alias = "system",
		frameName = "VideoOptionsFrame",
		func = function()
			if not VideoOptionsFrame:IsVisible() then
				ShowUIPanel(VideoOptionsFrame)
				VideoOptionsFrame.lastFrame = GameMenuFrame
				VideoOptionsFrame.hideMenu = true
			else
				HideUIPanel(VideoOptionsFrame)
			end
		end,
	},
	{
		name = "interface",
		title = UIOPTIONS_MENU,
		frameName = "InterfaceOptionsFrame",
		func = function()
			if not InterfaceOptionsFrame:IsVisible() then
				ShowUIPanel(InterfaceOptionsFrame)
				InterfaceOptionsFrame.lastFrame = GameMenuFrame
				InterfaceOptionsFrame.hideMenu = true
			else
				HideUIPanel(InterfaceOptionsFrame)
			end
		end,
	},
	{
		name = "mac",
		title = MAC_OPTIONS,
		frameName = "MacOptionsFrame",
		func = function()
			if not MacOptionsFrame:IsVisible() then
				ShowUIPanel(MacOptionsFrame)
				MacOptionsFrame.hideMenu = true
			else
				HideUIPanel(MacOptionsFrame)
			end
		end,
	},
	{
		name = "keybindings",
		title = KEY_BINDINGS,
		alias = "keys",
		frameName = "KeyBindingFrame",
		func = function()
			if not KeyBindingFrame:IsVisible() then
				ShowUIPanel(KeyBindingFrame)
				KeyBindingFrame.hideMenu = true
			else
				HideUIPanel(KeyBindingFrame)
			end
		end,
	},
	{
		name = "macro",
		title = MACROS,
		frameName = "MacroFrame",
		func = function()
			if not MacroFrame:IsVisible() then
				ShowUIPanel(MacroFrame)
			else
				HideUIPanel(MacroFrame)
			end
		end,
	},
	{
		name = "mainmenu",
		title = MAINMENU_BUTTON,
		alias = "menu",
		frameName = "GameMenuFrame",
		--button = MainMenuMicroButton,
		func = function()
            -- Calling ToggleGameMenu() causes the UI to taint.
			if not GameMenuFrame:IsShown() then
				if VideoOptionsFrame:IsShown() then
					VideoOptionsFrameCancel:Click()
				elseif AudioOptionsFrame:IsShown() then
					AudioOptionsFrameCancel:Click()
				elseif InterfaceOptionsFrame:IsShown() then
					InterfaceOptionsFrameCancel:Click()
				end
				CloseMenus()
				CloseAllWindows()
				ShowUIPanel(GameMenuFrame)
			else
				HideUIPanel(GameMenuFrame)
			end
		end,
	}
}

-- if we're not using a mac client, remove the interface from the table.
if IsMacClient() ~= 1 then
	for i, v in ipairs(interfaces) do
		if v.name == "mac" then
			table.remove(interfaces, i)
			break
		end
	end
end

local function comp(a, b)
	return a.title:lower() < b.title:lower()
end

table.sort(interfaces, comp)

local count = #interfaces

-- Due to a reason unknown to me yet the SpellBookFrame cannot be opened in combat, at least on my own machines,
-- probably because some variables and frames aren't fully set prior to combat so it taints and throws this error 'Interface action failed because of an AddOn'
-- the following hack seems to solve it by opening and closing it immediately at login.
local frame = CreateFrame("Frame")
frame:RegisterEvent("PLAYER_LOGIN")
local function onEvent(self, event, ...)
	ShowUIPanel(SpellBookFrame)
	HideUIPanel(SpellBookFrame)
end
frame:SetScript('OnEvent', onEvent)

-- Hides the GameMenuFrame and resets the hideMenu flag.
-- The hideMenu flag dictates whether the GameMenuFrame should be hidden rather than opened when the interface is closed.
local function hideUIPanel(self)
	if self.hideMenu then
		HideUIPanel(GameMenuFrame)
		self.hideMenu = nil
	end
end

--[[ APIs ]]

--[[ GetInterface(name) - Gets the interface by a given name, alias or title.

	Arguments.
	name - (string) The name, alias or title of the interface to display.

	Returns.
	interface - (table) The interface entry.
	index - (number) The index of interface in the table.

]]
function lib:GetInterface(name)
	local interface, index  = nil, 0
	if  name and type(name) == "string" then
		for i, v in ipairs(interfaces) do
			if v.name == name or v.alias == name or v.title == name then
				interface = v
				index = i
				break
			end
		end
	end
	return interface, index
end

--[[ GetInterfaceByIndex(index) - Gets the interface by a given index

	Arguments.
	index - (number) The index of the interface to display.

	Returns.
	interface - (table) The interface entry.

]]
function lib:GetInterfaceByIndex(index)
	local interface
	if  index and type(index) == "number" then
		interface = interfaces[index]
	end
	return interface
end

--[[ Interfaces() - Traverses over the interfaces.

	Returns.
	iterator - (function) An iterator to traverse over the interfaces.

]]
function lib:Interfaces()
	return ipairs(interfaces)
end

--[[ InterfacesCount() - Counts the amount if interfaces we have.

	Returns.
	iterator - (function) An iterator to traverse over the interfaces.

]]
function lib:InterfacesCount()
	return count
end


--[[ DisplayInterface(name) - Displays the interface.

	Arguments.
	name - (string) The key, alias  or title of the interface to display.

]]
function lib:DisplayInterface(name)
	local interface = self:GetInterface(name)
	if interface and interface.func and type(interface.func) == "function" then
		if interface.frameName then

			local playerLevel = UnitLevel("player")
			-- Check whether the player is in appropriate level for the interface to be displayed.
			if interface.level and tonumber(interface.level) and playerLevel < interface.level  then
				print(CHARACTER_MSG:format(interface.frameName))
				return
			end

			local loadLoD = LoD_AddonFrames[interface.frameName]
			-- Check whether it should preload the associated addon.
			if loadLoD then loadLoD() end

			-- Manages the display of the interfaces.
			local panel = UIPanelWindows[interface.frameName]
			local centerFrame = not (GetUIPanel("left") or GetUIPanel("right") or GetUIPanel("doublewide")) and GetUIPanel("center")

			if panel and panel.area ~= "center" and centerFrame and centerFrame:IsShown() then
				HideUIPanel(centerFrame)
			end

			-- Display the interface.
			interface.func()

			-- This keeps the GameMenuFrame hidden when an interface was opened through this action.
			if GameMenu_OptionFrames[interface.frameName] then
				local frame = _G[interface.frameName]
				if frame then
					frame:HookScript("OnHide", hideUIPanel)
				end
				GameMenu_OptionFrames[interface.frameName] = nil
			end

		-- The interface does not have a frameName entry so just run the function.
		else
			interface.func()
		end
	end
end