--[[
	Useful for hovering over ui elements
--]]


local UserInputService = game:GetService("UserInputService")
local GuiService = game:GetService("GuiService")

local Hover = {}
Hover.__index = Hover

	-- pass the element, and two functions (1=enter, 2=leave)
	function Hover.Connect(element,enter,leave, call_leave_on_selection_lost)
		local self = {}
		setmetatable(self,Hover)
		self.Connections = {}
		self.Active = false;
		if element and (enter or leave) then -- support both, or just one function (maybe we just want a sound to play on leave, etc)
			if call_leave_on_selection_lost then
				table.insert(self.Connections,
					element.SelectionGained:Connect(function()
						self.Active = true
						if enter then enter() end
					end)
				)
				table.insert(self.Connections,
					element.SelectionLost:Connect(function()
						self.Active = true
						if leave then leave() end
					end)
				)
			end
			table.insert(self.Connections,
				element.InputBegan:Connect(function(input)
					if input.UserInputType == Enum.UserInputType.MouseMovement then
						self.Active = true
						if enter then enter() end
					end
				end)
			)
			table.insert(self.Connections, 
				element.InputEnded:Connect(function(input)
					if input.UserInputType == Enum.UserInputType.MouseMovement then
						self.Active = false
						if leave then leave() end
					end
				end)
			)
		end
				
		return self
	end
		
	-- disconnects the events.
	function Hover:Disconnect()
		if self and self.Connections then
			for i=1,#self.Connections do
				local connection = self.Connections[i]
				if connection then connection:Disconnect() end
			end
		end
		self = nil
	end
	
	

return Hover