local W={}function LightingTool.Equip()ShowUI()EnableSurfaceClickSelection()end function LightingTool.Unequip()HideUI()ClearConnections()end function ClearConnections()for k,w in pairs(W)do w:Disconnect()W[k]=nil end end function ShowUI()if LightingTool.UI then LightingTool.UI.Visible=true UIUpdater=Support.ScheduleRecurringTask(UpdateUI,.1)return end LightingTool.UI=Core.Tool.Interfaces.BTLightingToolGUI:Clone()LightingTool.UI.Parent=Core.UI LightingTool.UI.Visible=true EnableLightSettingsUI(LightingTool.UI.PointLight)EnableLightSettingsUI(LightingTool.UI.SpotLight)EnableLightSettingsUI(LightingTool.UI.SurfaceLight)UIUpdater=Support.ScheduleRecurringTask(UpdateUI,.1)end function EnableSurfaceClickSelection(k)if W.SurfaceClickSelection then W.SurfaceClickSelection:Disconnect()W.SurfaceClickSelection=nil end W.SurfaceClickSelection=Core.Mouse.Button1Down:Connect(function()local W,w=Core.Targeting:UpdateTarget()if Selection.IsSelected(w)then SetSurface(k,Core.Mouse.TargetSurface)end end)end function EnableLightSettingsUI(W)local k=W.Name local w=W.Options local m=w.RangeOption.Input.TextBox local z=w.BrightnessOption.Input.TextBox local B=w.ColorOption.HSVPicker local O=w.ShadowsOption.Checkbox local S=W.AddButton local o=W.RemoveButton local f=W.ArrowButton m.FocusLost:Connect(function()SetRange(k,tonumber(m.Text))end)z.FocusLost:Connect(function()SetBrightness(k,tonumber(z.Text))end)B.MouseButton1Click:Connect(function()(Core.Cheer(Core.Tool.Interfaces.BTHSVColorPicker,Core.UI)).Start(Support.IdentifyCommonProperty(GetLights(k),"\067\111\108\111\114")or Color3.new(1,1,1),function(W)SetColor(k,W)end,Core.Targeting.CancelSelecting)end)O.MouseButton1Click:Connect(function()ToggleShadows(k)end)S.MouseButton1Click:Connect(function()AddLights(k)end)o.MouseButton1Click:Connect(function()RemoveLights(k)end)f.MouseButton1Click:Connect(function()OpenLightOptions(k)end)if k=="\083\112\111\116\076\105\103\104\116"or k=="\083\117\114\102\097\099\101\076\105\103\104\116"then Surfaces={"\084\111\112";"\066\111\116\116\111\109","\070\114\111\110\116","\066\097\099\107","\076\101\102\116";"\082\105\103\104\116"}local W=(Core.Cheer(w.SideOption.Dropdown)).Start(Surfaces,"",function(W)SetSurface(k,Enum.NormalId[W])end)local m=w.AngleOption.Input.TextBox m.FocusLost:Connect(function()SetAngle(k,tonumber(m.Text))end)end end function HideUI()if not LightingTool.UI then return end LightingTool.UI.Visible=false UIUpdater:Stop()end function GetLights(W)local k={}for w,m in pairs(Selection.Parts)do table.insert(k,Support.GetChildOfClass(m,W))end return k end
-- Container for temporary connections (disconnected automatically)
local Connections = {}

function LightingTool.Equip()
	-- Enables the tool's equipped functionality

	-- Start up our interface
	ShowUI()
	EnableSurfaceClickSelection()
end

function LightingTool.Unequip()
	-- Disables the tool's equipped functionality

	-- Clear unnecessary resources
	HideUI()
	ClearConnections()
end

function ClearConnections()
	-- Clears out temporary connections

	for ConnectionKey, Connection in pairs(Connections) do
		Connection:Disconnect()
		Connections[ConnectionKey] = nil
	end
end

function ShowUI()
	-- Creates and reveals the UI

	-- Reveal UI if already created
	if LightingTool.UI then
		-- Reveal the UI
		LightingTool.UI.Visible = true

		-- Update the UI every 0.1 seconds
		UIUpdater = Support.ScheduleRecurringTask(UpdateUI, 0.1)

		-- Skip UI creation
		return
	end

	-- Create the UI
	LightingTool.UI = Core.Tool.Interfaces.BTLightingToolGUI:Clone()
	LightingTool.UI.Parent = Core.UI
	LightingTool.UI.Visible = true

	-- Enable each light type UI
	EnableLightSettingsUI(LightingTool.UI.PointLight)
	EnableLightSettingsUI(LightingTool.UI.SpotLight)
	EnableLightSettingsUI(LightingTool.UI.SurfaceLight)

	-- Update the UI every 0.1 seconds
	UIUpdater = Support.ScheduleRecurringTask(UpdateUI, 0.1)
end

function EnableSurfaceClickSelection(LightType)
	-- Allows for the setting of the face for the given light type by clicking

	-- Clear out any existing connection
	if Connections.SurfaceClickSelection then
		Connections.SurfaceClickSelection:Disconnect()
		Connections.SurfaceClickSelection = nil
	end

	-- Add the new click connection
	Connections.SurfaceClickSelection = Core.Mouse.Button1Down:Connect(function()
		local _, ScopeTarget = Core.Targeting:UpdateTarget()
		if Selection.IsSelected(ScopeTarget) then
			SetSurface(LightType, Core.Mouse.TargetSurface)
		end
	end)
end

function EnableLightSettingsUI(LightSettingsUI)
	-- Sets up the UI for the given light type settings UI

	-- Get the type of light this settings UI is for
	local LightType = LightSettingsUI.Name

	-- Option input references
	local Options = LightSettingsUI.Options
	local RangeInput = Options.RangeOption.Input.TextBox
	local BrightnessInput = Options.BrightnessOption.Input.TextBox
	local ColorPicker = Options.ColorOption.HSVPicker
	local ShadowsCheckbox = Options.ShadowsOption.Checkbox

	-- Add/remove/show button references
	local AddButton = LightSettingsUI.AddButton
	local RemoveButton = LightSettingsUI.RemoveButton
	local ShowButton = LightSettingsUI.ArrowButton

	-- Enable range input
	RangeInput.FocusLost:Connect(function()
		SetRange(LightType, tonumber(RangeInput.Text))
	end)

	-- Enable brightness input
	BrightnessInput.FocusLost:Connect(function()
		SetBrightness(LightType, tonumber(BrightnessInput.Text))
	end)

	-- Enable color input
	ColorPicker.MouseButton1Click:Connect(function()
		Core.Cheer(Core.Tool.Interfaces.BTHSVColorPicker, Core.UI)
			.Start(Support.IdentifyCommonProperty(GetLights(LightType), "Color") or Color3.new(1, 1, 1), function(Color)
				SetColor(LightType, Color)
			end, Core.Targeting.CancelSelecting)
	end)

	-- Enable shadows input
	ShadowsCheckbox.MouseButton1Click:Connect(function()
		ToggleShadows(LightType)
	end)

	-- Enable light addition button
	AddButton.MouseButton1Click:Connect(function()
		AddLights(LightType)
	end)

	-- Enable light removal button
	RemoveButton.MouseButton1Click:Connect(function()
		RemoveLights(LightType)
	end)

	-- Enable light options UI show button
	ShowButton.MouseButton1Click:Connect(function()
		OpenLightOptions(LightType)
	end)

	-- Enable light type-specific features
	if LightType == "SpotLight" or LightType == "SurfaceLight" then
		-- Create a surface selection dropdown
		Surfaces = { "Top", "Bottom", "Front", "Back", "Left", "Right" }
		local SurfaceDropdown = Core.Cheer(Options.SideOption.Dropdown).Start(Surfaces, "", function(Surface)
			SetSurface(LightType, Enum.NormalId[Surface])
		end)

		-- Enable angle input
		local AngleInput = Options.AngleOption.Input.TextBox
		AngleInput.FocusLost:Connect(function()
			SetAngle(LightType, tonumber(AngleInput.Text))
		end)
	end
end

function HideUI()
	-- Hides the tool UI

	-- Make sure there's a UI
	if not LightingTool.UI then
		return
	end

	-- Hide the UI
	LightingTool.UI.Visible = false

	-- Stop updating the UI
	UIUpdater:Stop()
end

function GetLights(LightType)
	-- Returns all the lights of the given type in the selection

	local Lights = {}

	-- Get any lights from any selected parts
	for _, Part in pairs(Selection.Parts) do
		table.insert(Lights, Support.GetChildOfClass(Part, LightType))
	end

	-- Return the lights
	return Lights
end
