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.
]]

local addonName, addon = ...
local core = LibStub("Libra"):NewAddon(addonName, addon)

addon.display = LibStub("EasyDisplay-1.0")
addon.name = addonName:gsub("Broker_", "")

local defaults = {
	profile = {
		mouseButton = 1,
		numAddOns = 5,
		hideTooltip = false,
		hideTooltipAccessibilityBindings = false,
		hideLogoutButtons = true,
		hideReloadUIButton = false,
		hideOptionsButton = false,
		menu = {
			character = 1,
			spellbook = 2,
			talents = 3,
			achievements = 4,
			questlog = 5,
			guild = 6,
			pvp = 7,
			lfd = 8,
			companions = 9,
			journal = 10,
			mainmenu = 11,
			help = 12
		},
		accessibility = {
			{
				name = "bags",
				modifier = 4
			},
			{
				name = "social",
				modifier = 2
			},
			{
				name = "lfd",
				modifier = 1
			},
			{
				name = "questlog",
				modifier = 3
			}
		}
	}
}

addon.buttons = {
	KEY_BUTTON1,
	KEY_BUTTON2,
}

addon.modifiers = {
	NONE,
	SHIFT_KEY,
	CTRL_KEY,
	ALT_KEY
}

core:SetOnUpdate(function(self, elapsed)
	if addon:IsMenuVisible() or not addon.tooltip then
		return
	end

	local t = self.elapsed or 0
	self.elapsed = t + elapsed

	t = self.elapsed

	if t >= 1 then
		addon:UpdateTooltip()
		self.elapsed = nil
	end
end)

function core:OnInitialize()
	addon.updateMenu = true

	addon.db = LibStub("AceDB-3.0"):New("Broker_StartMenuDb", defaults, true)
	addon.db.RegisterCallback(addon, "OnProfileChanged", "BuildMenu")
	addon.db.RegisterCallback(addon, "OnProfileCopied", "BuildMenu")
	addon.db.RegisterCallback(addon, "OnProfileReset", "BuildMenu")

	addon.dataobj = LibStub("LibDataBroker-1.1"):NewDataObject(addon.name, {
		type = "data source",
		icon = "Interface\\GroupFrame\\UI-Group-LeaderIcon",
		text = addon.name,
		OnClick = function(self, button)
			addon:BuildMenu(self)
			if not addon.db.profile.blockInCombat or not UnitAffectingCombat("player") then
				addon:OpenMenu(self, button)
			end
			GameTooltip:Hide()
		end,
		OnEnter = function(self)
			local _, y = GetCursorPosition()
			local _, y2 = UIParent:GetCenter()
			GameTooltip:SetOwner(self, "ANCHOR_NONE")
			if y > y2 then
				GameTooltip:SetPoint("TOP", self, "BOTTOM")
			else
				GameTooltip:SetPoint("BOTTOM", self, "TOP")
			end
			addon:UpdateTooltip()
			addon.tooltip = true
		end,
		OnLeave = function(self)
			GameTooltip:Hide()
			addon.tooltip = nil
		end
	})
end