view HotCorners.lua @ 1:018fc9f0c471

continuing the development.
author tercio
date Sat, 09 Aug 2014 17:28:04 -0300
parents fc346da3afd9
children a6fb0ff113b1
line wrap: on
line source

LibHotCorners = LibStub ("AceAddon-3.0"):NewAddon ("HotCorners", "AceConsole-3.0", "AceEvent-3.0", "AceTimer-3.0")
_G.HotCorners = LibHotCorners
local LibHotCorners = LibHotCorners

local LBD = LibStub ("LibDataBroker-1.1")

local debug = false
local tinsert = tinsert

local default_db = {
	profile = {
		is_enabled = true,
		topleft_enabled = true,
		topleft_quickfunc = false,
		clicks = {}, 
		disabled = {}
	},
}

LibHotCorners.RegistredQuickFunctions = {}
LibHotCorners.QuickFunctions = {topleft = false}

function LibHotCorners:OnEnable()
	
end

function LibHotCorners:OnDisable()
	
end

local refresh_topleft = function()
	LibHotCorners ["topleft"].is_enabled = LibHotCorners.db.profile.topleft_enabled
end

local OptionsTable = {
	name = "HotCorners",
	type = "group",
	args = {
		Enabled = {
			type = "toggle",
			name = "Enabled",
			desc = "Enable or Disable this addon.",
			order = 1,
			get = function() return LibHotCorners.db.profile.is_enabled end,
			set = function (self, val) LibHotCorners.db.profile.is_enabled = not LibHotCorners.db.profile.is_enabled; --[[ do something]] end,
		},
		TopLeftEnabled = {
			type = "toggle",
			name = "Top Left",
			desc = "Enable or Disable the Top Left bar.",
			order = 2,
			get = function() return LibHotCorners.db.profile.topleft_enabled end,
			set = function (self, val) LibHotCorners.db.profile.topleft_enabled = not LibHotCorners.db.profile.topleft_enabled; refresh_topleft() end,
		},
		QuickClickFunc = {
			type = "select",
			name = "Quick Click",
			desc = "Select the behavior when clicking over the absolute topleft corner.",
			values = function()
					local options = {}
					for index, quickfunc in ipairs (LibHotCorners.RegistredQuickFunctions) do
						options [quickfunc.name] = quickfunc.name
					end
					return options
				end,
			get = function() return LibHotCorners.db.profile.topleft_quickfunc or "" end,
			set = function (self, funcname) 
						LibHotCorners.db.profile.topleft_quickfunc = funcname;
						for index, quickfunc in ipairs (LibHotCorners.RegistredQuickFunctions) do
							if (quickfunc.name == funcname) then
								LibHotCorners.QuickFunctions.topleft = quickfunc.func
								break
							end
						end
				end,
			order = 4,
		}
	}
}

function LibHotCorners:OnInitialize()

	--declarar primeiro o db usando a global que é declarada no toc.
  	self.db = LibStub ("AceDB-3.0"):New ("HotCornersDB", default_db, true)
	
	--declara agora as opções da tab raiz
	LibStub("AceConfig-3.0"):RegisterOptionsTable ("HotCorners", OptionsTable)
	LibHotCorners.OptionsFrame1 = LibStub ("AceConfigDialog-3.0"):AddToBlizOptions ("HotCorners", "HotCorners")
	--sub tab
	LibStub ("AceConfig-3.0"):RegisterOptionsTable ("HotCorners-Profiles", LibStub ("AceDBOptions-3.0"):GetOptionsTable (self.db))
	LibHotCorners.OptionsFrame2 = LibStub ("AceConfigDialog-3.0"):AddToBlizOptions ("HotCorners-Profiles", "Profiles", "HotCorners")

	LibHotCorners ["topleft"].is_enabled = self.db.topleft_enabled
	refresh_topleft()
	
	SLASH_HOTCORNER1, SLASH_HOTCORNER2 = "/hotcorners", "/hotcorner"
	function SlashCmdList.HOTCORNER (msg, editbox)
		HotCornersOpenOptions (self);
	end
	
	for name, dataobj in LBD:DataObjectIterator() do
		if (dataobj.type and dataobj.icon and dataobj.OnClick) then
			LibHotCorners:RegisterHotCornerButton (name, "TopLeft", nil, name .. "HotCornerLauncher", dataobj.icon, dataobj.OnTooltipShow, dataobj.OnClick, nil, nil, dataobj.OnEnter, dataobj.OnLeave)
		end
	end
	for k, v in pairs (LBD.attributestorage) do 
		--print (k, v)
		--print ("----------------")
		--vardump (v)
	end
	
end

------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
--> main function

	LibHotCorners.embeds = LibHotCorners.embeds or {}
	local embed_functions = {
		"RegisterHotCornerButton",
		"HideHotCornerButton",
	}
	
	function LibHotCorners:Embed (target)
		for k, v in pairs (embed_functions) do
			target[v] = self[v]
		end
		self.embeds [target] = true
		return target
	end

	local CallbackHandler = LibStub:GetLibrary ("CallbackHandler-1.0")
	LibHotCorners.callbacks = LibHotCorners.callbacks or CallbackHandler:New (LibHotCorners)

	LibHotCorners.topleft = LibHotCorners.topleft or {widgets = {}, quickclick = false, is_enabled = false, map = {}}
	LibHotCorners.bottomleft = {}
	LibHotCorners.topright = {}
	LibHotCorners.bottomright = {}

	local function test (corner)
		assert (corner == "topleft" or corner == "bottomleft" or corner == "topright" or corner == "bottomright", "LibHotCorners:RegisterAddon expects a corner on #1 argument.")
	end
	
	function LibHotCorners:AddQuickFunction (quickfunc, corner)

		local current_quickfunc = LibHotCorners.db.profile [corner .. "_quickfunc"] or ""
	
		--> passed only one table
		if (quickfunc.name) then
			--> check if already exists
			local already_exists = false
			for _, registred in ipairs (LibHotCorners.RegistredQuickFunctions) do
				if (registred.name == quickfunc.name) then
					registred.func = quickfunc.func
					if (current_quickfunc == quickfunc.name) then
						LibHotCorners.QuickFunctions [corner] = quickfunc.func
					end
					already_exists = true
					break
				end
			end
			--> add table
			if (not already_exists) then
				table.insert (LibHotCorners.RegistredQuickFunctions, quickfunc)
				if (current_quickfunc == quickfunc.name) then
					LibHotCorners.QuickFunctions [corner] = quickfunc.func
				end
			end
			--> check if there is a quickfunc to be use
			if (current_quickfunc == "") then
				LibHotCorners.db.profile [corner .. "_quickfunc"] = quickfunc.name
				LibHotCorners.QuickFunctions [corner] = quickfunc.func
			end
		else
			--> passed a table of tables
			for _, this_quickfunc in ipairs (quickfunc) do
				--> check if already exists
				local already_exists = false
				for _, registred in ipairs (LibHotCorners.RegistredQuickFunctions) do
					if (registred.name == this_quickfunc.name) then
						registred.func = this_quickfunc.func
						if (current_quickfunc == this_quickfunc.name) then
							LibHotCorners.QuickFunctions [corner] = this_quickfunc.func
						end
						already_exists = true
						break
					end
				end
				--> add table
				if (not already_exists) then
					table.insert (LibHotCorners.RegistredQuickFunctions, this_quickfunc)
					if (current_quickfunc == this_quickfunc.name) then
						LibHotCorners.QuickFunctions [corner] = this_quickfunc.func
					end
				end
				--> check if there is a quickfunc to be use
				if (current_quickfunc == "") then
					LibHotCorners.db.profile [corner .. "_quickfunc"] = this_quickfunc.name
					LibHotCorners.QuickFunctions [corner] = this_quickfunc.func
					current_quickfunc = this_quickfunc.name
				end
			end
		end

	end
	
	function LibHotCorners:RegisterHotCornerButton (name, corner, optionstable, fname, icon, tooltip, clickfunc, menus, quickfunc, onenter, onleave, is_install)
		corner = string.lower (corner)
		test (corner)

		optionstable = optionstable or {hide = false}
		
		if (is_install) then
			--> overwrite if already exists a widget
			for i, widget in ipairs (LibHotCorners [corner]) do
				if (widget.name == name) then
					table.remove (LibHotCorners [corner], i)
					table.insert (LibHotCorners [corner], i, {name = name, fname = fname, optionstable = optionstable, icon = icon, tooltip = tooltip, click = clickfunc, menus = menus, quickfunc = quickfunc, onenter = onenter, onleave = onleave, is_install = true})
					if (quickfunc) then
						LibHotCorners:AddQuickFunction (quickfunc, corner)
					end
					return LibHotCorners [corner].map [name]
				end
			end
			--> add
			table.insert (LibHotCorners [corner], {name = name, fname = fname, optionstable = optionstable, icon = icon, tooltip = tooltip, click = clickfunc, menus = menus, quickfunc = quickfunc, onenter = onenter, onleave = onleave, is_install = true})
			LibHotCorners [corner].map [name] = #LibHotCorners [corner]
			if (quickfunc) then
				LibHotCorners:AddQuickFunction (quickfunc, corner)
			end
			return LibHotCorners [corner].map [name]
		else
			--> check if already exists
			for i, widget in ipairs (LibHotCorners [corner]) do
				if (widget.name == name) then
					return
				end
			end
			--> add
			table.insert (LibHotCorners [corner], {name = name, fname = fname, optionstable = optionstable, icon = icon, tooltip = tooltip, click = clickfunc, menus = menus, quickfunc = quickfunc, onenter = onenter, onleave = onleave})
			LibHotCorners [corner].map [name] = #LibHotCorners [corner]
			if (quickfunc) then
				LibHotCorners:AddQuickFunction (quickfunc, corner)
			end
			return LibHotCorners [corner].map [name]
		end
	end
	
	function LibHotCorners:HideHotCornerButton (name, corner, value)
		
		corner = string.lower (corner)
		test (corner)
		
		local corner_table = LibHotCorners [corner]
		local addon_table = corner_table [corner_table.map [name]]
		
		if (addon_table) then
			addon_table.optionstable.hide = value
		end

		LibHotCorners [corner].is_enabled = false
		
		for index, button_table in ipairs (corner_table) do
			if (not button_table.optionstable.hide) then
				LibHotCorners [corner].is_enabled = true
				break
			end
		end
		
		return true
	end
	
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
--> data broker stuff
	function LibHotCorners:DataBrokerCallback (event, name, dataobj)
		if (not name or not dataobj or not dataobj.type) then
			return
		end
		if (dataobj.icon and dataobj.OnClick) then
			LibHotCorners:RegisterHotCornerButton (name, "TopLeft", nil, name .. "HotCornerLauncher", dataobj.icon, dataobj.OnTooltipShow, dataobj.OnClick, nil, nil, dataobj.OnEnter, dataobj.OnLeave)
		end
	end
	LBD.RegisterCallback (LibHotCorners, "DataBrokerCallback")
	
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
--> scripts

	--> background (window mode fix)
	function HotCornersBackgroundOnEnter (self)
		if (LibHotCornersTopLeft and LibHotCornersTopLeft:IsShown()) then
			if (LibHotCornersTopLeft:GetWidth() > 2) then
				HotCornersOnLeave (LibHotCornersTopLeft)
			end
		end
		self:EnableMouse (false)
	end

	--> set size
		local function set_size (self)
			if (self.position == "topleft" or self.position == "topright") then
				self:SetSize (40, GetScreenHeight())
			else
				self:SetSize (GetScreenWidth(), 40)
			end
		end
		
	--> show tooltip
		local show_tooltip = function (self)
			if (self.table.tooltip) then
				if (type (self.table.tooltip) == "function") then
					GameTooltip:SetOwner (self, "ANCHOR_RIGHT")
					self.table.tooltip (GameTooltip)
					GameTooltip:Show()
				elseif (type (self.table.tooltip) == "string") then
					GameTooltip:SetOwner (self, "ANCHOR_RIGHT")
					GameTooltip:AddLine (self.table.tooltip)
					GameTooltip:Show()
				end
			elseif (self.table.onenter) then
				self.table.onenter (self)
			end
		end
		
	--> corner frame on enter
		local more_clicked = function (t1, t2)
			return t1[1] > t2[1]
		end

		function HotCornersOnEnter (self)
			
			if (not LibHotCorners.db.profile.is_enabled) then
				return
			end
			
			if (not LibHotCorners.db.profile [self.position .. "_enabled"]) then
				return
			end
	
			set_size (self)
			
			HotCornersBackgroundFrame:EnableMouse (true)
			
			local i = 1
			
			local sort = {}
			local clicks = LibHotCorners.db.profile.clicks
			for index, button_table in ipairs (LibHotCorners [self.position]) do 
				tinsert (sort, {clicks [button_table.name] or 0, button_table})
			end
			table.sort (sort, more_clicked)

			local last_button
			
			local disabled = LibHotCorners.db.profile.disabled
			
			for index, button_table in ipairs (sort) do 
				button_table = button_table [2]
				if (not button_table.widget) then
					LibHotCorners:CreateAddonWidget (self, button_table, index, self.position)
				end
				
				button_table.widget:ClearAllPoints()
				
				if (not disabled [button_table.name] and not button_table.optionstable.hide) then
					if (self.position == "topleft" or self.position == "topright") then
						local y = i * 35 * -1
						button_table.widget:SetPoint ("topleft", self, "topleft", 4, y)
						button_table.widget.y = y
					else
						local x = i * 35
						button_table.widget:SetPoint ("topleft", self, "topleft", x, -4)
						button_table.widget.x = x
					end

					button_table.widget:Show()
					last_button = button_table.widget
					
					i = i + 1
				else
					button_table.widget:Hide()
				end
			end
			
			local OptionsButton = LibHotCorners [self.position].optionsbutton
			local y = i * 35 * -1
			OptionsButton:SetPoint ("top", self, "top", 0, y)
			OptionsButton:Show()
			
		end

	--> corner frame on leave
		function HotCornersOnLeave (self)
			self:SetSize (1, 1)
			for index, button_table in ipairs (LibHotCorners [self.position]) do 
				if (button_table.widget) then
					button_table.widget:Hide()
				end
			end
			local OptionsButton = LibHotCorners [self.position].optionsbutton
			OptionsButton:Hide()
		end
		
	--> quick corner on click
		function HotCornersOnQuickClick (self, button)
			local parent_position = self:GetParent().position
			local func = LibHotCorners.QuickFunctions [parent_position]
			if (func) then
				func (self, button)
			end
		end

	--> options button onenter
		function HotCornersOptionsButtonOnEnter (self)
			set_size (self:GetParent())
			for index, button_table in ipairs (LibHotCorners [self:GetParent().position]) do 
				if (not LibHotCorners.db.profile.disabled [button_table.name]) then
					button_table.widget:Show()
				end
			end
			self:Show()
		end
		
		function HotCornersOpenOptions (self)
			InterfaceOptionsFrame_OpenToCategory ("HotCorners")
			InterfaceOptionsFrame_OpenToCategory ("HotCorners")
		end
		
		function HotCornersSetEnabled (state)
			LibHotCorners.db.profile.is_enabled = state
		end
	
	--> options button onleave
		function HotCornersOptionsButtonOnLeave (self)
			self:GetParent():GetScript("OnLeave")(self:GetParent())
		end
	
	--> button onenter
		function HotCornersButtonOnEnter (self)
			set_size (self:GetParent())
			for index, button_table in ipairs (LibHotCorners [self:GetParent().position]) do 
				if (not LibHotCorners.db.profile.disabled [button_table.name]) then
					button_table.widget:Show()
				end
			end
			show_tooltip (self)
			local OptionsButton = LibHotCorners [self:GetParent().position].optionsbutton
			OptionsButton:Show()
		end
	
	--> button onleave
		function HotCornersButtonOnLeave (self)
			GameTooltip:Hide()
			if (self.table.onleave) then
				self.table.onleave (self)
			end
			self:GetParent():GetScript("OnLeave")(self:GetParent())
			local OptionsButton = LibHotCorners [self:GetParent().position].optionsbutton
			OptionsButton:Hide()
		end

	--> button onmousedown
		function HotCornersButtonOnMouseDown (self, button)
			if (self:GetParent().position == "topleft" or self:GetParent().position == "topright") then
				self:SetPoint ("topleft", self:GetParent(), "topleft", 5, self.y - 1)
			else
				self:SetPoint ("topleft", self:GetParent(), "topleft", self.x+1, -6)
			end
		end
		
	--> button onmouseup
		function HotCornersButtonOnMouseUp (self, button)
			if (self:GetParent().position == "topleft" or self:GetParent().position == "topright") then
				self:SetPoint ("topleft", self:GetParent(), "topleft", 4, self.y)
			else
				self:SetPoint ("topleft", self:GetParent(), "topleft", self.x, -4)
			end
			if (self.table.click) then
				local clicks = LibHotCorners.db.profile.clicks
				clicks [self.table.name] = clicks [self.table.name] or 0
				clicks [self.table.name] = clicks [self.table.name] + 1
				self.table.click (self, button)
			end
		end

------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
--> create top left corner

	local TopLeftCorner = CreateFrame ("Frame", "LibHotCornersTopLeft", nil, "HotCornersFrameCornerTemplate")
	TopLeftCorner:SetPoint ("topleft", UIParent, "topleft", 0, 0)
	TopLeftCorner.position = "topleft"
	
	--fast corner button
	local QuickClickButton = CreateFrame ("button", "LibHotCornersTopLeftFastButton", TopLeftCorner, "HotCornersQuickCornerButtonTemplate")
	
	--options button
	local OptionsButton = CreateFrame ("button", "LibHotCornersTopLeftOptionsButton", TopLeftCorner, "HotCornersOptionsButtonTemplate")
	
	if (debug) then
		QuickClickButton:SetSize (20, 20)
		QuickClickButton:SetBackdrop ({bgFile = [[Interface\DialogFrame\UI-DialogBox-Gold-Background]], tile = true, tileSize = 40})
		QuickClickButton:SetBackdropColor (1, 0, 0, 1)
	end
	
	LibHotCorners.topleft.quickbutton = QuickClickButton
	LibHotCorners.topleft.optionsbutton = OptionsButton

------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
--> buttons 

	function LibHotCorners:CreateAddonWidget (frame, button_table, index, side)
	
		--> create the button
		local button = CreateFrame ("button", "LibHotCorners" .. side .. button_table.fname, frame, "HotCornersButtonTemplate")
		
		--> write some attributes
		button.index = index
		button.table = button_table
		button.parent = frame
		button_table.widget = button
		
		--> set the icon
		button:SetNormalTexture (button_table.icon)
		button:SetHighlightTexture (button_table.icon)
		
		if (string.lower (button_table.icon):find ([[\icons\]])) then
			button:GetNormalTexture():SetTexCoord (0.078125, 0.9375, 0.078125, 0.9375)
			button:GetHighlightTexture():SetTexCoord (0.078125, 0.9375, 0.078125, 0.9375)
		end
		
		return button
	end