---Controls UI

local controlsOpen = false
local cInputB = nil
local cInputT = nil
local cInput = false
for i,v in pairs(_Tune.Controls) do
	local button = script.Parent.Controls.Window.Content[i]
	button.Text = v.Name
	button.MouseButton1Click:connect(function()
		_InControls = true
		script.Parent.Controls.Window.Overlay.Visible = true
		cInput = true
		repeat wait() until cInputB~=nil
		if cInputB == Enum.KeyCode.Return or cInputB == Enum.KeyCode.KeypadEnter then
			--do nothing
		elseif string.find(i,"Contlr")~=nil then
			if cInputT.Name:find("Gamepad") then
				_Tune.Controls[i] = cInputB
				button.Text = cInputB.Name
			else
				script.Parent.Controls.Window.Error.Visible = true
			end
		elseif i=="MouseThrottle" or i=="MouseBrake" then
			if cInputT == Enum.UserInputType.MouseButton1 or cInputT == Enum.UserInputType.MouseButton2 then
				_Tune.Controls[i] = cInputT
				button.Text = cInputT.Name
			else
				script.Parent.Controls.Window.Error.Visible = true
			end	
		else
			if cInputT == Enum.UserInputType.Keyboard then
				_Tune.Controls[i] = cInputB
				button.Text = cInputB.Name
			else
				script.Parent.Controls.Window.Error.Visible = true
			end
		end
		cInputB = nil
		cInputT = nil
		cInput = false
		wait(.2)
		script.Parent.Controls.Window.Overlay.Visible = false
		_InControls = false
	end)
end

script.Parent.Controls.Window.Error.Changed:connect(function(property)
	if property == "Visible" then
		wait(3)
		script.Parent.Controls.Window.Error.Visible = false
	end
end)

UserInputService.InputBegan:connect(function(input) if cInput then cInputB = input.KeyCode cInputT = input.UserInputType end end)
UserInputService.InputChanged:connect(function(input) if cInput and (input.KeyCode==Enum.KeyCode.Thumbstick1 or input.KeyCode==Enum.KeyCode.Thumbstick2) then cInputB = input.KeyCode cInputT = input.UserInputType end end)


script.Parent.Controls.Toggle.MouseButton1Click:connect(function()
	controlsOpen = not controlsOpen
	if controlsOpen then
		script.Parent.Controls.Toggle.BackgroundColor3 = Color3.new(1,85/255,.5)
		script.Parent.Controls.Window:TweenPosition(UDim2.new(0.5, -250,0.5, -250),Enum.EasingDirection.Out,Enum.EasingStyle.Quad,.7,true)
	else
		script.Parent.Controls.Toggle.BackgroundColor3 = Color3.new(1,170/255,0)
		script.Parent.Controls.Window:TweenPosition(UDim2.new(0.5, -250,0, -500),Enum.EasingDirection.Out,Enum.EasingStyle.Quad,.7,true)
	end
end)

script.Parent.Controls.Window.Tabs.Keyboard.MouseButton1Click:connect(function()
	script.Parent.Controls.Window.Content:TweenPosition(UDim2.new(0, 0, 0, 60),Enum.EasingDirection.Out,Enum.EasingStyle.Quad,.4,true)
	script.Parent.Controls.Window.Tabs.Keyboard:TweenPosition(UDim2.new(0, 5, 0, 0),Enum.EasingDirection.Out,Enum.EasingStyle.Quad,.4,true)
	script.Parent.Controls.Window.Tabs.Mouse:TweenPosition(UDim2.new(0, 120, 0, -5),Enum.EasingDirection.Out,Enum.EasingStyle.Quad,.4,true)
	script.Parent.Controls.Window.Tabs.Controller:TweenPosition(UDim2.new(0, 235, 0, -5),Enum.EasingDirection.Out,Enum.EasingStyle.Quad,.4,true)
end)

script.Parent.Controls.Window.Tabs.Mouse.MouseButton1Click:connect(function()
	script.Parent.Controls.Window.Content:TweenPosition(UDim2.new(-1, 0, 0, 60),Enum.EasingDirection.Out,Enum.EasingStyle.Quad,.4,true)
	script.Parent.Controls.Window.Tabs.Keyboard:TweenPosition(UDim2.new(0, 5, 0, -5),Enum.EasingDirection.Out,Enum.EasingStyle.Quad,.4,true)
	script.Parent.Controls.Window.Tabs.Mouse:TweenPosition(UDim2.new(0, 120, 0, 0),Enum.EasingDirection.Out,Enum.EasingStyle.Quad,.4,true)
	script.Parent.Controls.Window.Tabs.Controller:TweenPosition(UDim2.new(0, 235, 0, -5),Enum.EasingDirection.Out,Enum.EasingStyle.Quad,.4,true)
end)

script.Parent.Controls.Window.Tabs.Controller.MouseButton1Click:connect(function()
	script.Parent.Controls.Window.Content:TweenPosition(UDim2.new(-2, 0, 0, 60),Enum.EasingDirection.Out,Enum.EasingStyle.Quad,.4,true)
	script.Parent.Controls.Window.Tabs.Keyboard:TweenPosition(UDim2.new(0, 5, 0, -5),Enum.EasingDirection.Out,Enum.EasingStyle.Quad,.4,true)
	script.Parent.Controls.Window.Tabs.Mouse:TweenPosition(UDim2.new(0, 120, 0, -5),Enum.EasingDirection.Out,Enum.EasingStyle.Quad,.4,true)
	script.Parent.Controls.Window.Tabs.Controller:TweenPosition(UDim2.new(0, 235, 0, 0),Enum.EasingDirection.Out,Enum.EasingStyle.Quad,.4,true)
end)

wait(.5)
script.Parent.Controls.Toggle:TweenPosition(UDim2.new(0, 50, 1, -30),Enum.EasingDirection.Out,Enum.EasingStyle.Quad,.4,false)	
for i=1,6 do
	script.Parent.Controls.Toggle.BackgroundColor3 = Color3.new(100/255,100/255,100/255)
	wait(.2)
	if controlsOpen then
		script.Parent.Controls.Toggle.BackgroundColor3 = Color3.new(1,85/255,.5)
	else
		script.Parent.Controls.Toggle.BackgroundColor3 = Color3.new(1,170/255,0)
	end
	wait(.2)
end