local d={}function LightingTool.Equip()ShowUI()EnableSurfaceClickSelection()end function LightingTool.Unequip()HideUI()ClearConnections()end function ClearConnections()for C,G in pairs(d)do G:Disconnect()d[C]=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)local d=(LightingTool.UI:WaitForChild("\084\105\116\108\101")):WaitForChild("\083\105\103\110\097\116\117\114\101")ListenForManualWindowTrigger(LightingTool.ManualText,LightingTool.Color.Color,d)UIUpdater=Support.ScheduleRecurringTask(UpdateUI,.1)end function EnableSurfaceClickSelection(C)if d.SurfaceClickSelection then d.SurfaceClickSelection:Disconnect()d.SurfaceClickSelection=nil end d.SurfaceClickSelection=Core.Mouse.Button1Down:Connect(function()local d,G=Core.Targeting:UpdateTarget()if Selection.IsSelected(G)then SetSurface(C,Core.Mouse.TargetSurface)end end)end function EnableLightSettingsUI(d)local C=d.Name local G=d.Options local W=G.RangeOption.Input.TextBox local q=G.BrightnessOption.Input.TextBox local Y=G.ColorOption.HSVPicker local D=G.ShadowsOption.Checkbox local E=d.AddButton local Q=d.RemoveButton local h=d.ArrowButton W.FocusLost:Connect(function()SetRange(C,tonumber(W.Text))end)q.FocusLost:Connect(function()SetBrightness(C,tonumber(q.Text))end)local B=nil Y.MouseButton1Click:Connect(function()local d=Support.IdentifyCommonProperty(GetLights(C),"\067\111\108\111\114")local G=Roact.createElement(ColorPicker,{InitialColor=d or Color3.fromRGB(255,255,255),SetPreviewColor=function(d)PreviewColor(C,d)end,OnConfirm=function(d)SetColor(C,d)B=Roact.unmount(B)end;OnCancel=function()B=Roact.unmount(B)end})B=B and Roact.update(B,G)or Roact.mount(G,Core.UI,"\067\111\108\111\114\080\105\099\107\101\114")end)D.MouseButton1Click:Connect(function()ToggleShadows(C)end)E.MouseButton1Click:Connect(function()AddLights(C)end)Q.MouseButton1Click:Connect(function()RemoveLights(C)end)h.MouseButton1Click:Connect(function()OpenLightOptions(C)end)if C=="\083\112\111\116\076\105\103\104\116"or C=="\083\117\114\102\097\099\101\076\105\103\104\116"then local d={"\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="\067\117\114\114\101\110\116"..(C.."\083\105\100\101")local function q()return Roact.createElement(Dropdown,{Position=UDim2.new(0,30,0,0),Size=UDim2.new(0,72,0,25),MaxRows=3,Options=d,CurrentOption=LightingTool[W]and LightingTool[W].Name;OnOptionSelected=function(d)SetSurface(C,Enum.NormalId[d])end})end local Y=Roact.mount(q(),G.SideOption,"\068\114\111\112\100\111\119\110")LightingTool.OnSideChanged:Connect(function()Roact.update(Y,q())end)local D=G.AngleOption.Input.TextBox D.FocusLost:Connect(function()SetAngle(C,tonumber(D.Text))end)end end function HideUI()if not LightingTool.UI then return end LightingTool.UI.Visible=false UIUpdater:Stop()end function GetLights(d)local C={}for G,W in pairs(Selection.Parts)do table.insert(C,Support.GetChildOfClass(W,d))end return C 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)

	-- Hook up manual triggering
	local SignatureButton = LightingTool.UI:WaitForChild("Title"):WaitForChild("Signature")
	ListenForManualWindowTrigger(LightingTool.ManualText, LightingTool.Color.Color, SignatureButton)

	-- 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 ColorPickerButton = 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
	local ColorPickerHandle = nil
	ColorPickerButton.MouseButton1Click:Connect(function()
		local CommonColor = Support.IdentifyCommonProperty(GetLights(LightType), "Color")
		local ColorPickerElement = Roact.createElement(ColorPicker, {
			InitialColor = CommonColor or Color3.fromRGB(255, 255, 255),
			SetPreviewColor = function(Color)
				PreviewColor(LightType, Color)
			end,
			OnConfirm = function(Color)
				SetColor(LightType, Color)
				ColorPickerHandle = Roact.unmount(ColorPickerHandle)
			end,
			OnCancel = function()
				ColorPickerHandle = Roact.unmount(ColorPickerHandle)
			end,
		})
		ColorPickerHandle = ColorPickerHandle and Roact.update(ColorPickerHandle, ColorPickerElement)
			or Roact.mount(ColorPickerElement, Core.UI, "ColorPicker")
	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
		local Surfaces = {
			"Top",
			"Bottom",
			"Front",
			"Back",
			"Left",
			"Right",
		}
		local SurfaceKey = "Current" .. LightType .. "Side"
		local function BuildDropdown()
			return Roact.createElement(Dropdown, {
				Position = UDim2.new(0, 30, 0, 0),
				Size = UDim2.new(0, 72, 0, 25),
				MaxRows = 3,
				Options = Surfaces,
				CurrentOption = LightingTool[SurfaceKey] and LightingTool[SurfaceKey].Name,
				OnOptionSelected = function(Option)
					SetSurface(LightType, Enum.NormalId[Option])
				end,
			})
		end
		local DropdownHandle = Roact.mount(BuildDropdown(), Options.SideOption, "Dropdown")

		-- Keep dropdown updated
		LightingTool.OnSideChanged:Connect(function()
			Roact.update(DropdownHandle, BuildDropdown())
		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
