Selection.Changed:connect(FocusOnLastSelectedPart)function GetCore()return require(script.Parent.Core)end function CreateSelectionBoxes(E)local F=GetCore()if F.Mode~="\084\111\111\108"then return end local S={}for E,F in pairs(E)do if not Selection.Outlines[F]then local E=Instance.new("\083\101\108\101\099\116\105\111\110\066\111\120")E.Name="\066\084\083\101\108\101\099\116\105\111\110\066\111\120"E.Color=Selection.Color E.Adornee=F E.LineThickness=.025 E.Transparency=.5 Selection.Outlines[F]=E table.insert(S,E)end end for E,S in pairs(S)do S.Parent=F.UIContainer end end function RemoveSelectionBoxes(E)if(GetCore()).Mode~="\084\111\111\108"then return end for E,F in pairs(E)do local S=Selection.Outlines[F]if S then S:Destroy()end Selection.Outlines[F]=nil end end function Selection.RecolorOutlines(E)Selection.Color=E for E,F in pairs(Selection.Outlines)do F.Color=Selection.Color end end function Selection.FlashOutlines()for E=1,.5,-0.1 do for F,S in pairs(Selection.Outlines)do S.Transparency=E end wait(.1)end end function Selection.EnableOutlines()local E=(GetCore()).UIContainer for F,S in pairs(Selection.Outlines)do S.Parent=E end;(GetCore()).Connections.HideOutlinesOnDisable=(GetCore()).Disabling:connect(Selection.HideOutlines)end function Selection.EnableMultiselectionHotkeys()local E=Support.FlipTable({"\076\101\102\116\083\104\105\102\116","\082\105\103\104\116\083\104\105\102\116","\076\101\102\116\067\111\110\116\114\111\108";"\082\105\103\104\116\067\111\110\116\114\111\108"})local F=GetCore()F.Connections.MultiselectionHotkeys=Support.AddUserInputListener("\066\101\103\097\110","\075\101\121\098\111\097\114\100",false,function(F)if E[F.KeyCode.Name]then Selection.Multiselecting=true end end)F.Connections.MultiselectingReleaseHotkeys=Support.AddUserInputListener("\069\110\100\101\100","\075\101\121\098\111\097\114\100",true,function(F)local S=Support.GetListMembers(Support.GetListMembers((Game:GetService("\085\115\101\114\073\110\112\117\116\083\101\114\118\105\099\101")):GetKeysPressed(),"\075\101\121\067\111\100\101"),"\078\097\109\101")for F,S in pairs(S)do if E[S]then return end end Selection.Multiselecting=false end)end function Selection.HideOutlines()for E,F in pairs(Selection.Outlines)do F.Parent=nil end end function TrackSelectionChange(E)if History.Index~=#History.Stack then return end History.Add({Before=E;After=Selection.Items,Unapply=function(E)Selection.Replace(E.Before)end;Apply=function(E)Selection.Replace(E.After)end})end return Selection

Selection.Changed:connect(FocusOnLastSelectedPart);

function GetCore()
	-- Returns the core API
	return require(script.Parent.Core);
end;

function CreateSelectionBoxes(Items)
	-- Creates a SelectionBox for each given item

	-- Get the core API
	local Core = GetCore();

	-- Only create selection boxes if in tool mode
	if Core.Mode ~= 'Tool' then
		return;
	end;

	-- Track new selection boxes
	local SelectionBoxes = {};

	-- Create an outline for each part
	for _, Item in pairs(Items) do

		-- Avoid duplicate selection boxes
		if not Selection.Outlines[Item] then

			-- Create the selection box
			local SelectionBox = Instance.new 'SelectionBox';
			SelectionBox.Name = 'BTSelectionBox';
			SelectionBox.Color = Selection.Color;
			SelectionBox.Adornee = Item;
			SelectionBox.LineThickness = 0.025;
			SelectionBox.Transparency = 0.5;

			-- Register the outline
			Selection.Outlines[Item] = SelectionBox;
			table.insert(SelectionBoxes, SelectionBox);

		end;

	end;

	-- Parent the selection boxes
	for _, SelectionBox in pairs(SelectionBoxes) do
		SelectionBox.Parent = Core.UIContainer;
	end;

end;

function RemoveSelectionBoxes(Items)
	-- Removes the given item's selection box

	-- Only proceed if in tool mode
	if GetCore().Mode ~= 'Tool' then
		return;
	end;

	-- Remove each item's outline
	for _, Item in pairs(Items) do

		-- Get the item's selection box
		local SelectionBox = Selection.Outlines[Item];

		-- Remove the selection box if found
		if SelectionBox then
			SelectionBox:Destroy();
		end;

		-- Deregister the selection box
		Selection.Outlines[Item] = nil;

	end;

end;

function Selection.RecolorOutlines(Color)
	-- Updates selection outline colors

	-- Set `Color` as the new color
	Selection.Color = Color;

	-- Recolor existing outlines
	for _, Outline in pairs(Selection.Outlines) do
		Outline.Color = Selection.Color;
	end;

end;

function Selection.FlashOutlines()
	-- Flashes selection outlines for emphasis

	-- Fade in from complete to normal transparency
	for Transparency = 1, 0.5, -0.1 do

		-- Update each outline
		for _, Outline in pairs(Selection.Outlines) do
			Outline.Transparency = Transparency;
		end;

		-- Fade over time
		wait(0.1);

	end;

end;

function Selection.EnableOutlines()
	-- Shows selection outlines

	local UIContainer = GetCore().UIContainer;

	-- Show each outline
	for _, Outline in pairs(Selection.Outlines) do
		Outline.Parent = UIContainer;
	end;

	-- Hide outlines when tool is disabled
	GetCore().Connections.HideOutlinesOnDisable = GetCore().Disabling:connect(Selection.HideOutlines);

end;

function Selection.EnableMultiselectionHotkeys()
	-- Enables hotkeys for multiselecting

	-- Determine multiselection hotkeys
	local Hotkeys = Support.FlipTable { 'LeftShift', 'RightShift', 'LeftControl', 'RightControl' };

	-- Get core API
	local Core = GetCore();

	-- Listen for matching key presses
	Core.Connections.MultiselectionHotkeys = Support.AddUserInputListener('Began', 'Keyboard', false, function (Input)
		if Hotkeys[Input.KeyCode.Name] then
			Selection.Multiselecting = true;
		end;
	end);

	-- Listen for matching key releases
	Core.Connections.MultiselectingReleaseHotkeys = Support.AddUserInputListener('Ended', 'Keyboard', true, function (Input)

		-- Get currently pressed keys
		local PressedKeys = Support.GetListMembers(Support.GetListMembers(Game:GetService('UserInputService'):GetKeysPressed(), 'KeyCode'), 'Name');

		-- Continue multiselection if a hotkey is still pressed
		for _, PressedKey in pairs(PressedKeys) do
			if Hotkeys[PressedKey] then
				return;
			end;
		end;

		-- Disable multiselection if matching key not found
		Selection.Multiselecting = false;

	end);

end;

function Selection.HideOutlines()
	-- Hides selection outlines

	-- Hide each outline
	for _, Outline in pairs(Selection.Outlines) do
		Outline.Parent = nil;
	end;

end;

function TrackSelectionChange(OldSelection)
	-- Registers a history record for a change in the selection

	-- Avoid overwriting history for selection actions
	if History.Index ~= #History.Stack then
		return;
	end;

	-- Add the history record
	History.Add({

		Before = OldSelection;
		After = Selection.Items;

		Unapply = function (HistoryRecord)
			-- Reverts this change

			-- Restore the old selection
			Selection.Replace(HistoryRecord.Before);

		end;

		Apply = function (HistoryRecord)
			-- Reapplies this change

			-- Restore the new selection
			Selection.Replace(HistoryRecord.After);

		end;
	});

end;

return Selection;
