Connections={}function ClearConnections()for v,R in pairs(Connections)do R:Disconnect()Connections[v]=nil end end function InitializeUI()if UI then return end UI=Instance.new("\083\099\114\101\101\110\071\117\105")UI.Name="\066\117\105\108\100\105\110\103 \084\111\111\108\115 \098\121 \070\051\088 \040\085\073\041"local v={}local R=require((Tool:WaitForChild("\085\073")):WaitForChild("\068\111\099\107"))local E=Roact.createElement(R,{Core=Core,Tools=v})local e=Roact.mount(E,UI,"\068\111\099\107")local function U(E,U,Z)table.insert(v,{IconAssetId=E;HotkeyLabel=U;Tool=Z})Roact.update(e,Roact.createElement(R,{Core=Core,Tools=Cryo.List.join(v)}))end Core.AddToolButton=U UIMaid=Maid.new()Tool.AncestryChanged:Connect(function(v,R)if R==nil then UIMaid:Destroy()end end)end local v=Tool:WaitForChild("\085\073")local R=require(v:WaitForChild("\069\120\112\108\111\114\101\114"))Core.ExplorerVisibilityChanged=Signal.new()Core.ExplorerVisible=false function ToggleExplorer()if not Core.ExplorerVisible then OpenExplorer()else CloseExplorer()end end function OpenExplorer()if ExplorerHandle then return end Explorer=Roact.createElement(R,{Core=getfenv(0);Close=CloseExplorer,Scope=Targeting.Scope})ExplorerHandle=Roact.mount(Explorer,UI,"\069\120\112\108\111\114\101\114")Core.ExplorerVisible=true UIMaid.Explorer=Support.Call(Roact.unmount,ExplorerHandle)UIMaid.ExplorerScope=Targeting.ScopeChanged:Connect(function(v)local E=Support.Merge({},Explorer.props,{Scope=v})local e=Roact.createElement(R,E)ExplorerHandle=Roact.update(ExplorerHandle,e)end)Core.ExplorerVisibilityChanged:Fire()end function CloseExplorer()UIMaid.Explorer=nil UIMaid.ExplorerScope=nil ExplorerHandle=nil Core.ExplorerVisible=false Core.ExplorerVisibilityChanged:Fire()end
-- Core connections
Connections = {}

function ClearConnections()
	-- Clears and disconnects temporary connections
	for Index, Connection in pairs(Connections) do
		Connection:Disconnect()
		Connections[Index] = nil
	end
end

function InitializeUI()
	-- Sets up the UI

	-- Ensure UI has not yet been initialized
	if UI then
		return
	end

	-- Create the root UI
	UI = Instance.new("ScreenGui")
	UI.Name = "Building Tools by F3X (UI)"

	-- Create dock
	local ToolList = {}
	local DockComponent = require(Tool:WaitForChild("UI"):WaitForChild("Dock"))
	local DockElement = Roact.createElement(DockComponent, {
		Core = Core,
		Tools = ToolList,
	})
	local DockHandle = Roact.mount(DockElement, UI, "Dock")

	-- Provide API for adding tool buttons to dock
	local function AddToolButton(IconAssetId, HotkeyLabel, Tool)
		table.insert(ToolList, {
			IconAssetId = IconAssetId,
			HotkeyLabel = HotkeyLabel,
			Tool = Tool,
		})

		-- Update dock
		Roact.update(
			DockHandle,
			Roact.createElement(DockComponent, {
				Core = Core,
				Tools = Cryo.List.join(ToolList),
			})
		)
	end
	Core.AddToolButton = AddToolButton

	-- Clean up UI on tool teardown
	UIMaid = Maid.new()
	Tool.AncestryChanged:Connect(function(Item, Parent)
		if Parent == nil then
			UIMaid:Destroy()
		end
	end)
end

local UIElements = Tool:WaitForChild("UI")
local ExplorerTemplate = require(UIElements:WaitForChild("Explorer"))
Core.ExplorerVisibilityChanged = Signal.new()
Core.ExplorerVisible = false

function ToggleExplorer()
	if not Core.ExplorerVisible then
		OpenExplorer()
	else
		CloseExplorer()
	end
end

function OpenExplorer()
	-- Ensure explorer not already open
	if ExplorerHandle then
		return
	end

	-- Initialize explorer
	Explorer = Roact.createElement(ExplorerTemplate, {
		Core = getfenv(0),
		Close = CloseExplorer,
		Scope = Targeting.Scope,
	})

	-- Mount explorer
	ExplorerHandle = Roact.mount(Explorer, UI, "Explorer")
	Core.ExplorerVisible = true

	-- Unmount explorer on tool cleanup
	UIMaid.Explorer = Support.Call(Roact.unmount, ExplorerHandle)
	UIMaid.ExplorerScope = Targeting.ScopeChanged:Connect(function(Scope)
		local UpdatedProps = Support.Merge({}, Explorer.props, { Scope = Scope })
		local UpdatedExplorer = Roact.createElement(ExplorerTemplate, UpdatedProps)
		ExplorerHandle = Roact.update(ExplorerHandle, UpdatedExplorer)
	end)

	-- Fire signal
	Core.ExplorerVisibilityChanged:Fire()
end

function CloseExplorer()
	-- Clean up explorer
	UIMaid.Explorer = nil
	UIMaid.ExplorerScope = nil
	ExplorerHandle = nil
	Core.ExplorerVisible = false

	-- Fire signal
	Core.ExplorerVisibilityChanged:Fire()
end
