local Q={}function NewPartTool.Equip(Q)Q:ShowUI()EnableClickCreation()Q:SetType(NewPartTool.Type)end function NewPartTool.Unequip(Q)Q:HideUI()ClearConnections()ContextActionService:UnbindAction("\066\084\058 \067\114\101\097\116\101 \112\097\114\116")end function ClearConnections()for i,N in pairs(Q)do N:Disconnect()Q[i]=nil end end function NewPartTool.ShowUI(Q)if Q.UI then Q.UI.Visible=true return end Q.UI=Core.Tool.Interfaces.BTNewPartToolGUI:Clone()Q.UI.Parent=Core.UI Q.UI.Visible=true Types={"\078\111\114\109\097\108","\084\114\117\115\115";"\087\101\100\103\101";"\067\111\114\110\101\114";"\067\121\108\105\110\100\101\114";"\066\097\108\108","\083\101\097\116","\086\101\104\105\099\108\101 \083\101\097\116";"\083\112\097\119\110"}local function i()return Roact.createElement(Dropdown,{Position=UDim2.new(0,70,0,0);Size=UDim2.new(0,140,0,25),Options=Types,MaxRows=5,CurrentOption=Q.Type,OnOptionSelected=function(i)Q:SetType(i)end})end local N=Roact.mount(i(),Q.UI.TypeOption,"\068\114\111\112\100\111\119\110")Q.OnTypeChanged:Connect(function()Roact.update(N,i())end)local B=(Q.UI:WaitForChild("\084\105\116\108\101")):WaitForChild("\083\105\103\110\097\116\117\114\101")ListenForManualWindowTrigger(NewPartTool.ManualText,NewPartTool.Color.Color,B)end function NewPartTool.HideUI(Q)if not Q.UI then return end Q.UI.Visible=false end function NewPartTool.SetType(i,Q)if i.Type~=Q then i.Type=Q i.OnTypeChanged:Fire(Q)end end function EnableClickCreation()local function Q(Q,i,N)if i.Name=="\066\101\103\105\110"then DragNewParts=true Core.Targeting.CancelSelecting()CreatePart(NewPartTool.Type)elseif i.Name=="\069\110\100"then DragNewParts=nil end end ContextActionService:BindAction("\066\084\058 \067\114\101\097\116\101 \112\097\114\116",Q,false,Enum.UserInputType.MouseButton1,Enum.UserInputType.Touch)end function CreatePart(Q)local i=Core.SyncAPI:Invoke("\067\114\101\097\116\101\080\097\114\116",Q,CFrame.new(Core.Mouse.Hit.p),Core.Targeting.Scope)if not i then return end local N={Part=i,Unapply=function(Q)Core.SyncAPI:Invoke("\082\101\109\111\118\101",{Q.Part})end,Apply=function(Q)Core.SyncAPI:Invoke("\085\110\100\111\082\101\109\111\118\101",{Q.Part})end}Core.History.Add(N)Selection.Replace({i})local B=require(Core.Tool.Tools.Move)Core.EquipTool(B)if DragNewParts then B.FreeDragging:SetUpDragging(i)end end
-- Container for temporary connections (disconnected automatically)
local Connections = {}

function NewPartTool:Equip()
	-- Enables the tool's equipped functionality

	-- Start up our interface
	self:ShowUI()
	EnableClickCreation()

	-- Set our current type
	self:SetType(NewPartTool.Type)
end

function NewPartTool:Unequip()
	-- Disables the tool's equipped functionality

	-- Clear unnecessary resources
	self:HideUI()
	ClearConnections()
	ContextActionService:UnbindAction("BT: Create part")
end

function ClearConnections()
	-- Clears out temporary connections

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

function NewPartTool:ShowUI()
	-- Creates and reveals the UI

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

		-- Skip UI creation
		return
	end

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

	-- Creatable part types
	Types = {
		"Normal",
		"Truss",
		"Wedge",
		"Corner",
		"Cylinder",
		"Ball",
		"Seat",
		"Vehicle Seat",
		"Spawn",
	}

	-- Create type dropdown
	local function BuildTypeDropdown()
		return Roact.createElement(Dropdown, {
			Position = UDim2.new(0, 70, 0, 0),
			Size = UDim2.new(0, 140, 0, 25),
			Options = Types,
			MaxRows = 5,
			CurrentOption = self.Type,
			OnOptionSelected = function(Option)
				self:SetType(Option)
			end,
		})
	end

	-- Mount type dropdown
	local TypeDropdownHandle = Roact.mount(BuildTypeDropdown(), self.UI.TypeOption, "Dropdown")
	self.OnTypeChanged:Connect(function()
		Roact.update(TypeDropdownHandle, BuildTypeDropdown())
	end)

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

function NewPartTool:HideUI()
	-- Hides the tool UI

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

	-- Hide the UI
	self.UI.Visible = false
end

function NewPartTool:SetType(Type)
	if self.Type ~= Type then
		self.Type = Type
		self.OnTypeChanged:Fire(Type)
	end
end

function EnableClickCreation()
	-- Allows the user to click anywhere and create a new part

	local function CreateAtTarget(Action, State, Input)
		-- Drag new parts
		if State.Name == "Begin" then
			DragNewParts = true
			Core.Targeting.CancelSelecting()

			-- Create new part
			CreatePart(NewPartTool.Type)

		-- Disable dragging on release
		elseif State.Name == "End" then
			DragNewParts = nil
		end
	end

	-- Register input handler
	ContextActionService:BindAction(
		"BT: Create part",
		CreateAtTarget,
		false,
		Enum.UserInputType.MouseButton1,
		Enum.UserInputType.Touch
	)
end

function CreatePart(Type)
	-- Send the creation request to the server
	local Part = Core.SyncAPI:Invoke("CreatePart", Type, CFrame.new(Core.Mouse.Hit.p), Core.Targeting.Scope)

	-- Make sure the part creation succeeds
	if not Part then
		return
	end

	-- Put together the history record
	local HistoryRecord = {
		Part = Part,

		Unapply = function(HistoryRecord)
			-- Reverts this change

			-- Remove the part
			Core.SyncAPI:Invoke("Remove", { HistoryRecord.Part })
		end,

		Apply = function(HistoryRecord)
			-- Reapplies this change

			-- Restore the part
			Core.SyncAPI:Invoke("UndoRemove", { HistoryRecord.Part })
		end,
	}

	-- Register the history record
	Core.History.Add(HistoryRecord)

	-- Select the part
	Selection.Replace({ Part })

	-- Switch to the move tool
	local MoveTool = require(Core.Tool.Tools.Move)
	Core.EquipTool(MoveTool)

	-- Enable dragging to allow easy positioning of the created part
	if DragNewParts then
		MoveTool.FreeDragging:SetUpDragging(Part)
	end
end
