local E=(Utility:Create("\070\114\097\109\101"))({Name="\071\097\109\101\112\097\100\072\105\110\116\115\070\114\097\109\101";Size=UDim2.new(0,HotbarFrame.Size.X.Offset,0,IsTenFootInterface and 95 or 60),BackgroundTransparency=1,Visible=false;Parent=MainFrame})local function R(R,U,A)local J=(Utility:Create("\070\114\097\109\101"))({Name="\072\105\110\116\070\114\097\109\101",Size=UDim2.new(1,0,1,-5),Position=UDim2.new(0,0,0,0);BackgroundTransparency=1,Parent=E})local g=(Utility:Create("\073\109\097\103\101\076\097\098\101\108"))({Name="\072\105\110\116\073\109\097\103\101",Size=IsTenFootInterface and UDim2.new(0,90,0,90)or UDim2.new(0,60,0,60);BackgroundTransparency=1;Image=IsTenFootInterface and U or R;Parent=J})local q=(Utility:Create("\084\101\120\116\076\097\098\101\108"))({Name="\072\105\110\116\084\101\120\116";Position=UDim2.new(0,IsTenFootInterface and 100 or 70,0,0),Size=UDim2.new(1,-(IsTenFootInterface and 100 or 70),1,0);Font=Enum.Font.SourceSansBold;FontSize=IsTenFootInterface and Enum.FontSize.Size36 or Enum.FontSize.Size24;BackgroundTransparency=1;Text=A,TextColor3=Color3.new(1,1,1),TextXAlignment=Enum.TextXAlignment.Left;TextWrapped=true,Parent=J})local a=Instance.new("\085\073\084\101\120\116\083\105\122\101\067\111\110\115\116\114\097\105\110\116",q)a.MaxTextSize=q.TextSize end local function U()E.Size=UDim2.new(HotbarFrame.Size.X.Scale,HotbarFrame.Size.X.Offset,0,IsTenFootInterface and 95 or 60)E.Position=UDim2.new(HotbarFrame.Position.X.Scale,HotbarFrame.Position.X.Offset,InventoryFrame.Position.Y.Scale,InventoryFrame.Position.Y.Offset-E.Size.Y.Offset)local R=0 local U=E:GetChildren()for E=1,#U,1 do U[E].Size=UDim2.new(1,0,1,-5)U[E].Position=UDim2.new(0,0,0,0)R=R+(U[E].HintText.Position.X.Offset+U[E].HintText.TextBounds.X)end local A=(E.AbsoluteSize.X-R)/(#U-1)for E=1,#U,1 do U[E].Position=E==1 and UDim2.new(0,0,0,0)or UDim2.new(0,(U[E-1].Position.X.Offset+U[E-1].Size.X.Offset)+A,0,0)U[E].Size=UDim2.new(0,U[E].HintText.Position.X.Offset+U[E].HintText.TextBounds.X,1,-5)end end R("\114\098\120\097\115\115\101\116\058\047\047\116\101\120\116\117\114\101\115\047\117\105\047\083\101\116\116\105\110\103\115\047\072\101\108\112\047\088\066\117\116\116\111\110\068\097\114\107\046\112\110\103","\114\098\120\097\115\115\101\116\058\047\047\116\101\120\116\117\114\101\115\047\117\105\047\083\101\116\116\105\110\103\115\047\072\101\108\112\047\088\066\117\116\116\111\110\068\097\114\107\064\050\120\046\112\110\103","\082\101\109\111\118\101 \070\114\111\109 \072\111\116\098\097\114")R("\114\098\120\097\115\115\101\116\058\047\047\116\101\120\116\117\114\101\115\047\117\105\047\083\101\116\116\105\110\103\115\047\072\101\108\112\047\065\066\117\116\116\111\110\068\097\114\107\046\112\110\103","\114\098\120\097\115\115\101\116\058\047\047\116\101\120\116\117\114\101\115\047\117\105\047\083\101\116\116\105\110\103\115\047\072\101\108\112\047\065\066\117\116\116\111\110\068\097\114\107\064\050\120\046\112\110\103","\083\101\108\101\099\116\047\083\119\097\112")R("\114\098\120\097\115\115\101\116\058\047\047\116\101\120\116\117\114\101\115\047\117\105\047\083\101\116\116\105\110\103\115\047\072\101\108\112\047\066\066\117\116\116\111\110\068\097\114\107\046\112\110\103","\114\098\120\097\115\115\101\116\058\047\047\116\101\120\116\117\114\101\115\047\117\105\047\083\101\116\116\105\110\103\115\047\072\101\108\112\047\066\066\117\116\116\111\110\068\097\114\107\064\050\120\046\112\110\103","\067\108\111\115\101 \066\097\099\107\112\097\099\107")do local E=NewGui("\070\114\097\109\101","\083\101\097\114\099\104")E.BackgroundColor3=SEARCH_BACKGROUND_COLOR E.BackgroundTransparency=SEARCH_BACKGROUND_FADE E.Size=UDim2.new(0,SEARCH_WIDTH-SEARCH_BUFFER*2,0,INVENTORY_HEADER_SIZE-SEARCH_BUFFER*2)E.Position=UDim2.new(1,-E.Size.X.Offset-SEARCH_BUFFER,0,SEARCH_BUFFER)local R=Instance.new("\085\073\067\111\114\110\101\114")R.CornerRadius=UDim.new(1,0)R.Parent=E E.Parent=InventoryFrame local U=NewGui("\084\101\120\116\066\111\120","\084\101\120\116\066\111\120")U.PlaceholderText=SEARCH_TEXT U.TextScaled=true U.ClearTextOnFocus=false U.Font=Enum.Font.Oswald U.FontSize=Enum.FontSize.Size14 U.TextXAlignment=Enum.TextXAlignment.Center U.Size=UDim2.new(.87,0,1,0)U.AnchorPoint=Vector2.new(.5,.5)U.Position=UDim2.new(.5,SEARCH_TEXT_OFFSET_FROMLEFT,.5,0)local A=Instance.new("\085\073\067\111\114\110\101\114")A.CornerRadius=UDim.new(1,0)A.Parent=U U.Parent=E local J=NewGui("\084\101\120\116\066\117\116\116\111\110","\088")J.Text="\120"J.Font=Enum.Font.Oswald J.ZIndex=10 J.TextColor3=SLOT_EQUIP_COLOR J.FontSize=Enum.FontSize.Size24 J.TextYAlignment=Enum.TextYAlignment.Bottom J.BackgroundColor3=SEARCH_BACKGROUND_COLOR J.BackgroundTransparency=1 J.AnchorPoint=Vector2.new(1,.5)J.Size=UDim2.new(.15,0,.5,0)J.Position=UDim2.new(.98,0,.5,0)J.Visible=false J.ZIndex=0 J.BorderSizePixel=0 J.Parent=E local function g()local E={}for R in U.Text:gmatch("\037\083\043")do E[R:lower()]=true end local R={}for U=NumberOfHotbarSlots+1,#Slots,1 do local A=Slots[U]local J=A:CheckTerms(E)table.insert(R,{A,J})A.Frame.Visible=false A.Frame.Parent=InventoryFrame end table.sort(R,function(E,R)return E[2]>R[2]end)ViewingSearchResults=true local A=0 for E,R in ipairs(R)do local U,J=R[1],R[2]if J>0 then U.Frame.Visible=true U.Frame.Parent=UIGridFrame U.Frame.LayoutOrder=NumberOfHotbarSlots+A A=A+1 end end ScrollingFrame.CanvasPosition=Vector2.new(0,0)UpdateScrollingFrameCanvasSize()J.ZIndex=3 end local function q()if J.ZIndex>0 then ViewingSearchResults=false for E=NumberOfHotbarSlots+1,#Slots,1 do local R=Slots[E]R.Frame.LayoutOrder=R.Index R.Frame.Parent=UIGridFrame R.Frame.Visible=true end J.ZIndex=0 end UpdateScrollingFrameCanvasSize()end local function a()q()U.Text=""end local function l(E)if E=="\084\101\120\116"then local E=U.Text if E==""then q()elseif E~=SEARCH_TEXT then g()end J.Visible=E~=""and E~=SEARCH_TEXT end end local function T(E)if E then g()end end J.MouseButton1Click:Connect(a)U.Changed:Connect(l)U.FocusLost:Connect(T)BackpackScript.StateChanged.Event:Connect(function(E)(InventoryIcon:getInstance("\105\099\111\110\066\117\116\116\111\110")).Modal=E if not E then a()end end)HotkeyFns[Enum.KeyCode.Escape.Value]=function(E)if E then a()elseif InventoryFrame.Visible then InventoryIcon:deselect()end end local function w(R)if R==Enum.UserInputType.Gamepad1 and not UserInputService.VREnabled then E.Visible=false else E.Visible=true end end UserInputService.LastInputTypeChanged:Connect(w)end GuiService.MenuOpened:Connect(function()if InventoryFrame.Visible then InventoryIcon:deselect()end end)do local R=function(E,R,U)if R~=Enum.UserInputState.Begin then return end if not GuiService.SelectedObject then return end for E=1,NumberOfHotbarSlots,1 do if Slots[E].Frame==GuiService.SelectedObject and Slots[E].Tool then Slots[E]:MoveToInventory()return end end end local function A()if not next(Dragging)then InventoryFrame.Visible=not InventoryFrame.Visible local E=InventoryFrame.Visible AdjustHotbarFrames()HotbarFrame.Active=not HotbarFrame.Active for R=1,NumberOfHotbarSlots,1 do Slots[R]:SetClickability(not E)end end if InventoryFrame.Visible then if GamepadEnabled then if GAMEPAD_INPUT_TYPES[UserInputService:GetLastInputType()]then U()E.Visible=not UserInputService.VREnabled end enableGamepadInventoryControl()end if BackpackPanel and VRService.VREnabled then BackpackPanel:SetVisible(true)BackpackPanel:RequestPositionUpdate()end else if GamepadEnabled then E.Visible=false end disableGamepadInventoryControl()end if InventoryFrame.Visible then ContextActionService:BindAction("\082\066\088\082\101\109\111\118\101\083\108\111\116",R,false,Enum.KeyCode.ButtonX)else ContextActionService:UnbindAction("\082\066\088\082\101\109\111\118\101\083\108\111\116")end BackpackScript.IsOpen=InventoryFrame.Visible BackpackScript.StateChanged:Fire(InventoryFrame.Visible)end StarterGui:SetCoreGuiEnabled(Enum.CoreGuiType.Backpack,false)BackpackScript.OpenClose=A end

local gamepadHintsFrame = Utility:Create'Frame'
{
	Name = "GamepadHintsFrame",
	Size = UDim2.new(0, HotbarFrame.Size.X.Offset, 0, (IsTenFootInterface and 95 or 60)),
	BackgroundTransparency = 1,
	Visible = false,
	Parent = MainFrame
}

local function addGamepadHint(hintImage, hintImageLarge, hintText)
	local hintFrame = Utility:Create'Frame'
	{
		Name = "HintFrame",
		Size = UDim2.new(1, 0, 1, -5),
		Position = UDim2.new(0, 0, 0, 0),
		BackgroundTransparency = 1,
		Parent = gamepadHintsFrame
	}

	local hintImage = Utility:Create'ImageLabel'
	{
		Name = "HintImage",
		Size = (IsTenFootInterface and UDim2.new(0,90,0,90) or UDim2.new(0,60,0,60)),
		BackgroundTransparency = 1,
		Image = (IsTenFootInterface and hintImageLarge or hintImage),
		Parent = hintFrame
	}

	local hintText = Utility:Create'TextLabel'
	{
		Name = "HintText",
		Position = UDim2.new(0, (IsTenFootInterface and 100 or 70), 0, 0),
		Size = UDim2.new(1, -(IsTenFootInterface and 100 or 70), 1, 0),
		Font = Enum.Font.SourceSansBold,
		FontSize = (IsTenFootInterface and Enum.FontSize.Size36 or Enum.FontSize.Size24),
		BackgroundTransparency = 1,
		Text = hintText,
		TextColor3 = Color3.new(1,1,1),
		TextXAlignment = Enum.TextXAlignment.Left,
		TextWrapped = true,
		Parent = hintFrame
	}
	local textSizeConstraint = Instance.new("UITextSizeConstraint", hintText)
	textSizeConstraint.MaxTextSize = hintText.TextSize
end

local function resizeGamepadHintsFrame()
	gamepadHintsFrame.Size = UDim2.new(HotbarFrame.Size.X.Scale, HotbarFrame.Size.X.Offset, 0, (IsTenFootInterface and 95 or 60))
	gamepadHintsFrame.Position = UDim2.new(HotbarFrame.Position.X.Scale, HotbarFrame.Position.X.Offset, InventoryFrame.Position.Y.Scale, InventoryFrame.Position.Y.Offset - gamepadHintsFrame.Size.Y.Offset)

	local spaceTaken = 0

	local gamepadHints = gamepadHintsFrame:GetChildren()
	--First get the total space taken by all the hints
	for i = 1, #gamepadHints do
		gamepadHints[i].Size = UDim2.new(1, 0, 1, -5)
		gamepadHints[i].Position = UDim2.new(0, 0, 0, 0)
		spaceTaken = spaceTaken + (gamepadHints[i].HintText.Position.X.Offset + gamepadHints[i].HintText.TextBounds.X)
	end

	--The space between all the frames should be equal
	local spaceBetweenElements = (gamepadHintsFrame.AbsoluteSize.X - spaceTaken)/(#gamepadHints - 1)
	for i = 1, #gamepadHints do
		gamepadHints[i].Position = (i == 1 and UDim2.new(0, 0, 0, 0) or UDim2.new(0, gamepadHints[i-1].Position.X.Offset + gamepadHints[i-1].Size.X.Offset + spaceBetweenElements, 0, 0))
		gamepadHints[i].Size = UDim2.new(0, (gamepadHints[i].HintText.Position.X.Offset + gamepadHints[i].HintText.TextBounds.X), 1, -5)
	end
end

addGamepadHint("rbxasset://textures/ui/Settings/Help/XButtonDark.png", "rbxasset://textures/ui/Settings/Help/XButtonDark@2x.png", "Remove From Hotbar")
addGamepadHint("rbxasset://textures/ui/Settings/Help/AButtonDark.png", "rbxasset://textures/ui/Settings/Help/AButtonDark@2x.png", "Select/Swap")
addGamepadHint("rbxasset://textures/ui/Settings/Help/BButtonDark.png", "rbxasset://textures/ui/Settings/Help/BButtonDark@2x.png", "Close Backpack")

do -- Search stuff
	local searchFrame = NewGui('Frame', 'Search')
	searchFrame.BackgroundColor3 = SEARCH_BACKGROUND_COLOR
	searchFrame.BackgroundTransparency = SEARCH_BACKGROUND_FADE
	searchFrame.Size = UDim2.new(0, SEARCH_WIDTH - (SEARCH_BUFFER * 2), 0, INVENTORY_HEADER_SIZE - (SEARCH_BUFFER * 2))
	searchFrame.Position = UDim2.new(1, -searchFrame.Size.X.Offset - SEARCH_BUFFER, 0, SEARCH_BUFFER)
	
	local searchFrameUIC = Instance.new("UICorner")
	searchFrameUIC.CornerRadius = UDim.new(1,0)
	searchFrameUIC.Parent = searchFrame
	
	searchFrame.Parent = InventoryFrame
	
	local searchBox = NewGui('TextBox', 'TextBox')
	searchBox.PlaceholderText = SEARCH_TEXT
	searchBox.TextScaled = true
	searchBox.ClearTextOnFocus = false
	searchBox.Font = Enum.Font.Oswald
	searchBox.FontSize = Enum.FontSize.Size14
	searchBox.TextXAlignment = Enum.TextXAlignment.Center
	searchBox.Size = UDim2.new(0.87, 0, 1, 0)
	searchBox.AnchorPoint = Vector2.new(0.5,0.5)
	searchBox.Position = UDim2.new(0.5, SEARCH_TEXT_OFFSET_FROMLEFT, 0.5, 0)
	
	local searchBoxUIC = Instance.new("UICorner")
	searchBoxUIC.CornerRadius = UDim.new(1,0)
	searchBoxUIC.Parent = searchBox
	
	searchBox.Parent = searchFrame
	
	local xButton = NewGui('TextButton', 'X')
	xButton.Text = 'x'
	xButton.Font = Enum.Font.Oswald
	xButton.ZIndex = 10
	xButton.TextColor3 = SLOT_EQUIP_COLOR
	xButton.FontSize = Enum.FontSize.Size24
	xButton.TextYAlignment = Enum.TextYAlignment.Bottom
	xButton.BackgroundColor3 = SEARCH_BACKGROUND_COLOR
	xButton.BackgroundTransparency = 1
	xButton.AnchorPoint = Vector2.new(1,0.5)
	xButton.Size = UDim2.new(0.15, 0, 0.5, 0)
	xButton.Position = UDim2.new(0.98, 0, 0.5, 0)
	xButton.Visible = false
	xButton.ZIndex = 0
	xButton.BorderSizePixel = 0
	xButton.Parent = searchFrame

	local function search()
		local terms = {}
		for word in searchBox.Text:gmatch('%S+') do
			terms[word:lower()] = true
		end

		local hitTable = {}
		for i = NumberOfHotbarSlots + 1, #Slots do -- Only search inventory slots
			local slot = Slots[i]
			local hits = slot:CheckTerms(terms)
			table.insert(hitTable, {slot, hits})
			slot.Frame.Visible = false
			slot.Frame.Parent = InventoryFrame
		end

		table.sort(hitTable, function(left, right)
			return left[2] > right[2]
		end)
		ViewingSearchResults = true

		local hitCount = 0
		for i, data in ipairs(hitTable) do
			local slot, hits = data[1], data[2]
			if hits > 0 then
				slot.Frame.Visible = true
				slot.Frame.Parent = UIGridFrame
				slot.Frame.LayoutOrder = NumberOfHotbarSlots + hitCount
				hitCount = hitCount + 1
			end
		end

		ScrollingFrame.CanvasPosition = Vector2.new(0, 0)
		UpdateScrollingFrameCanvasSize()

		xButton.ZIndex = 3
	end

	local function clearResults()
		if xButton.ZIndex > 0 then
			ViewingSearchResults = false
			for i = NumberOfHotbarSlots + 1, #Slots do
				local slot = Slots[i]
				slot.Frame.LayoutOrder = slot.Index
				slot.Frame.Parent = UIGridFrame
				slot.Frame.Visible = true
			end
			xButton.ZIndex = 0
		end
		UpdateScrollingFrameCanvasSize()
	end

	local function reset()
		clearResults()
		searchBox.Text = ''
	end

	local function onChanged(property)
		if property == 'Text' then
			local text = searchBox.Text
			if text == '' then
				clearResults()
			elseif text ~= SEARCH_TEXT then
				search()
			end
			xButton.Visible = text ~= '' and text ~= SEARCH_TEXT
		end
	end

	local function focusLost(enterPressed)
		if enterPressed then
			--TODO: Could optimize
			search()
		end
	end

	xButton.MouseButton1Click:Connect(reset)
	searchBox.Changed:Connect(onChanged)
	searchBox.FocusLost:Connect(focusLost)

	BackpackScript.StateChanged.Event:Connect(function(isNowOpen)
		InventoryIcon:getInstance("iconButton").Modal = isNowOpen -- Allows free mouse movement even in first person

		if not isNowOpen then
			reset()
		end
	end)
	
	HotkeyFns[Enum.KeyCode.Escape.Value] = function(isProcessed)
		if isProcessed then -- Pressed from within a TextBox
			reset()
		elseif InventoryFrame.Visible then
			InventoryIcon:deselect()
		end
	end

	local function detectGamepad(lastInputType)
		if lastInputType == Enum.UserInputType.Gamepad1 and not UserInputService.VREnabled then
			searchFrame.Visible = false
		else
			searchFrame.Visible = true
		end
	end
	UserInputService.LastInputTypeChanged:Connect(detectGamepad)
end

GuiService.MenuOpened:Connect(function()
	if InventoryFrame.Visible then
		InventoryIcon:deselect()
	end
end)


do -- Make the Inventory expand/collapse arrow (unless TopBar)
	local removeHotBarSlot = function(name, state, input)
		if state ~= Enum.UserInputState.Begin then return end
		if not GuiService.SelectedObject then return end

		for i = 1, NumberOfHotbarSlots do
			if Slots[i].Frame == GuiService.SelectedObject and Slots[i].Tool then
				Slots[i]:MoveToInventory()
				return
			end
		end
	end

	local function openClose()
		if not next(Dragging) then -- Only continue if nothing is being dragged
			InventoryFrame.Visible = not InventoryFrame.Visible
			local nowOpen = InventoryFrame.Visible
			AdjustHotbarFrames()
			HotbarFrame.Active = not HotbarFrame.Active
			for i = 1, NumberOfHotbarSlots do
				Slots[i]:SetClickability(not nowOpen)
			end
		end

		if InventoryFrame.Visible then
			if GamepadEnabled then
				if GAMEPAD_INPUT_TYPES[UserInputService:GetLastInputType()] then
					resizeGamepadHintsFrame()
					gamepadHintsFrame.Visible = not UserInputService.VREnabled
				end
				enableGamepadInventoryControl()
			end
			if BackpackPanel and VRService.VREnabled then
				BackpackPanel:SetVisible(true)
				BackpackPanel:RequestPositionUpdate()
			end
		else
			if GamepadEnabled then
				gamepadHintsFrame.Visible = false
			end
			disableGamepadInventoryControl()
		end

		if InventoryFrame.Visible then
			ContextActionService:BindAction("RBXRemoveSlot", removeHotBarSlot, false, Enum.KeyCode.ButtonX)
		else
			ContextActionService:UnbindAction("RBXRemoveSlot")
		end

		BackpackScript.IsOpen = InventoryFrame.Visible
		BackpackScript.StateChanged:Fire(InventoryFrame.Visible)
	end

	StarterGui:SetCoreGuiEnabled(Enum.CoreGuiType.Backpack, false)
	BackpackScript.OpenClose = openClose -- Exposed
end
