function IconController._updateSelectionGroup(C,M)if IconController._navigationEnabled then guiService:RemoveSelectionGroup("\084\111\112\098\097\114\080\108\117\115\073\099\111\110\115")end if M then guiService.CoreGuiNavigationEnabled=IconController._originalCoreGuiNavigationEnabled guiService.GuiNavigationEnabled=IconController._originalGuiNavigationEnabled IconController._navigationEnabled=nil elseif IconController.controllerModeEnabled then local M=IconController.getIcons()local C={}for M,F in pairs(M)do local Q=F.joinedFeatureName if not Q or F._parentIcon[F.joinedFeatureName.."\079\112\101\110"]==true then table.insert(C,F.instances.iconButton)end end guiService:AddSelectionTuple("\084\111\112\098\097\114\080\108\117\115\073\099\111\110\115",table.unpack(C))if not IconController._navigationEnabled then IconController._originalCoreGuiNavigationEnabled=guiService.CoreGuiNavigationEnabled IconController._originalGuiNavigationEnabled=guiService.GuiNavigationEnabled guiService.CoreGuiNavigationEnabled=false guiService.GuiNavigationEnabled=true IconController._navigationEnabled=true end end end local function M()if guiService:IsTenFootInterface()then return 3 else return 1.3 end end function IconController._setControllerSelectedObject(M)local C=IconController._controllerSetCount and IconController._controllerSetCount+1 or 0 IconController._controllerSetCount=C guiService.SelectedObject=M delay(.1,function()local F=IconController._controllerSetCount if C==F then guiService.SelectedObject=M end end)end function IconController._enableControllerMode(C)local F=TopbarPlusGui.Indicator local Q=IconController.getIcon("\095\084\111\112\098\097\114\067\111\110\116\114\111\108\108\101\114\079\112\116\105\111\110")if IconController.controllerModeEnabled==C then return end IconController.controllerModeEnabled=C if C then TopbarPlusGui.TopbarContainer.Position=UDim2.new(0,0,0,5)TopbarPlusGui.TopbarContainer.Visible=false local C=M()F.Position=UDim2.new(.5,0,0,5)F.Size=UDim2.new(0,18*C,0,18*C)F.Image="\114\098\120\097\115\115\101\116\105\100\058\047\047\053\050\055\056\049\053\049\053\053\054"F.Visible=checkTopbarEnabledAccountingForMimic()F.Position=UDim2.new(.5,0,0,5)F.Active=true controllerMenuOverride=F.InputBegan:Connect(function(M)if M.UserInputType==Enum.UserInputType.MouseButton1 then IconController.setTopbarEnabled(true,false)end end)else TopbarPlusGui.TopbarContainer.Position=UDim2.new(0,0,0,0)TopbarPlusGui.TopbarContainer.Visible=checkTopbarEnabledAccountingForMimic()F.Visible=false IconController._setControllerSelectedObject(nil)end for M,F in pairs(topbarIcons)do IconController._enableControllerModeForIcon(M,C)end end function IconController._enableControllerModeForIcon(C,F)local Q=C._parentIcon local t=C.joinedFeatureName if Q then C:leave()end if F then local F=M()local Q=C:get("\105\099\111\110\083\105\122\101","\100\101\115\101\108\101\099\116\101\100")local t=C:get("\105\099\111\110\083\105\122\101","\115\101\108\101\099\116\101\100")local b=C:getHovering("\105\099\111\110\083\105\122\101")C:set("\105\099\111\110\083\105\122\101",UDim2.new(0,Q.X.Offset*F,0,Q.Y.Offset*F),"\100\101\115\101\108\101\099\116\101\100","\099\111\110\116\114\111\108\108\101\114\077\111\100\101")C:set("\105\099\111\110\083\105\122\101",UDim2.new(0,t.X.Offset*F,0,t.Y.Offset*F),"\115\101\108\101\099\116\101\100","\099\111\110\116\114\111\108\108\101\114\077\111\100\101")if b then C:set("\105\099\111\110\083\105\122\101",UDim2.new(0,t.X.Offset*F,0,t.Y.Offset*F),"\104\111\118\101\114\105\110\103","\099\111\110\116\114\111\108\108\101\114\077\111\100\101")end C:set("\097\108\105\103\110\109\101\110\116","\109\105\100","\100\101\115\101\108\101\099\116\101\100","\099\111\110\116\114\111\108\108\101\114\077\111\100\101")C:set("\097\108\105\103\110\109\101\110\116","\109\105\100","\115\101\108\101\099\116\101\100","\099\111\110\116\114\111\108\108\101\114\077\111\100\101")else local M={"\100\101\115\101\108\101\099\116\101\100";"\115\101\108\101\099\116\101\100";"\104\111\118\101\114\105\110\103"}for M,F in pairs(M)do local Q,t=C:get("\097\108\105\103\110\109\101\110\116",F,"\099\111\110\116\114\111\108\108\101\114\077\111\100\101")if t then C:set("\097\108\105\103\110\109\101\110\116",t,F)end local b,v=C:get("\105\099\111\110\083\105\122\101",F,"\099\111\110\116\114\111\108\108\101\114\077\111\100\101")if v then C:set("\105\099\111\110\083\105\122\101",v,F)end end end if Q then C:join(Q,t)end end local C=false function IconController.setupHealthbar()if C then return end C=true task.defer(function()runService.Heartbeat:Wait()local M=require(script.Parent);((((((((M.new()):setProperty("\105\110\116\101\114\110\097\108\073\099\111\110",true)):setName("\095\070\097\107\101\072\101\097\108\116\104\098\097\114")):setRight()):setOrder(-420)):setSize(80,32)):lock()):set("\105\099\111\110\066\097\099\107\103\114\111\117\110\100\084\114\097\110\115\112\097\114\101\110\099\121",1)):give(function(M)local C=Instance.new("\070\114\097\109\101")C.Name="\072\101\097\108\116\104\067\111\110\116\097\105\110\101\114"C.BackgroundColor3=Color3.fromRGB(0,0,0)C.BorderSizePixel=0 C.AnchorPoint=Vector2.new(0,.5)C.Position=UDim2.new(0,0,.5,0)C.Size=UDim2.new(1,0,.2,0)C.Visible=true C.ZIndex=11 C.Parent=M.instances.iconButton local F=Instance.new("\085\073\067\111\114\110\101\114")F.CornerRadius=UDim.new(1,0)F.Parent=C local Q=C:Clone()Q.Name="\072\101\097\108\116\104\070\114\097\109\101"Q.BackgroundColor3=Color3.fromRGB(167,167,167)Q.BorderSizePixel=0 Q.AnchorPoint=Vector2.new(.5,.5)Q.Position=UDim2.new(.5,0,.5,0)Q.Size=UDim2.new(1,-2,1,-2)Q.Visible=true Q.ZIndex=12 Q.Parent=C local t=Q:Clone()t.Name="\072\101\097\108\116\104\066\097\114"t.BackgroundColor3=Color3.fromRGB(255,255,255)t.BorderSizePixel=0 t.AnchorPoint=Vector2.new(0,.5)t.Position=UDim2.new(0,0,.5,0)t.Size=UDim2.new(.5,0,1,0)t.Visible=true t.ZIndex=13 t.Parent=Q local b=Color3.fromRGB(27,252,107)local v=Color3.fromRGB(250,235,0)local a=Color3.fromRGB(255,28,0)local function I(M,C)return Color3.new(math.pow(M.R,C),math.pow(M.G,C),math.pow(M.B,C))end local function G(M,C,F,Q)Q=Q or 2.0 local t=I(M,Q)local b=I(C,Q)return I(t:Lerp(b,F),1/Q)end local K=true local function j(C)if not C then return end local F=C:WaitForChild("\072\117\109\097\110\111\105\100",10)if not F then return end local function Q()local C=starterGui:GetCoreGuiEnabled(Enum.CoreGuiType.Health)local Q=F.Health/F.MaxHealth if Q==1 or IconController.healthbarDisabled or K and C==false then if M.enabled then M:setEnabled(false)end return elseif Q<1 then if not M.enabled then M:setEnabled(true)end K=false if C then starterGui:SetCoreGuiEnabled(Enum.CoreGuiType.Health,false)end end local I=.9 local j=.1 local P=1/(I-j)local B=-P*j local H=P*Q+B local Y=H>1 and 1 or H<0 and 0 or H local V=Q>.5 and b or v local f=Q>.5 and v or a local X=(1-Y)*2 local Z=Q>.5 and 1-X or 2-X local m=G(f,V,Z)local i=UDim2.new(Q,0,1,0)t.BackgroundColor3=m t.Size=i end F.HealthChanged:Connect(Q)IconController.healthbarDisabledSignal:Connect(Q)Q()end localPlayer.CharacterAdded:Connect(function(M)j(M)end)task.spawn(j,localPlayer.Character)end)end)end

function IconController:_updateSelectionGroup(clearAll)
	if IconController._navigationEnabled then
		guiService:RemoveSelectionGroup("TopbarPlusIcons")
	end
	if clearAll then
		guiService.CoreGuiNavigationEnabled = IconController._originalCoreGuiNavigationEnabled
		guiService.GuiNavigationEnabled = IconController._originalGuiNavigationEnabled
		IconController._navigationEnabled = nil
	elseif IconController.controllerModeEnabled then
		local icons = IconController.getIcons()
		local iconContainers = {}
		for i, otherIcon in pairs(icons) do
			local featureName = otherIcon.joinedFeatureName
			if not featureName or otherIcon._parentIcon[otherIcon.joinedFeatureName.."Open"] == true then
				table.insert(iconContainers, otherIcon.instances.iconButton)
			end
		end
		guiService:AddSelectionTuple("TopbarPlusIcons", table.unpack(iconContainers))
		if not IconController._navigationEnabled then
			IconController._originalCoreGuiNavigationEnabled = guiService.CoreGuiNavigationEnabled
			IconController._originalGuiNavigationEnabled = guiService.GuiNavigationEnabled
			guiService.CoreGuiNavigationEnabled = false
			guiService.GuiNavigationEnabled = true
			IconController._navigationEnabled = true
		end
	end
end

local function getScaleMultiplier()
	if guiService:IsTenFootInterface() then
		return 3
	else
		return 1.3
	end
end

function IconController._setControllerSelectedObject(object)
	local startId = (IconController._controllerSetCount and IconController._controllerSetCount + 1) or 0
	IconController._controllerSetCount = startId
	guiService.SelectedObject = object
	delay(0.1, function() -- blame the roblox guiservice its a piece of doo doo
		local finalId = IconController._controllerSetCount
		if startId == finalId then
			guiService.SelectedObject = object
		end
	end)
end

function IconController._enableControllerMode(bool)
	local indicator = TopbarPlusGui.Indicator
	local controllerOptionIcon = IconController.getIcon("_TopbarControllerOption")
	if IconController.controllerModeEnabled == bool then
		return
	end
	IconController.controllerModeEnabled = bool
	if bool then
		TopbarPlusGui.TopbarContainer.Position = UDim2.new(0,0,0,5)
		TopbarPlusGui.TopbarContainer.Visible = false
		local scaleMultiplier = getScaleMultiplier()
		indicator.Position = UDim2.new(0.5,0,0,5)
		indicator.Size = UDim2.new(0, 18*scaleMultiplier, 0, 18*scaleMultiplier)
		indicator.Image = "rbxassetid://5278151556"
		indicator.Visible = checkTopbarEnabledAccountingForMimic()
		indicator.Position = UDim2.new(0.5,0,0,5)
		indicator.Active = true
		controllerMenuOverride = indicator.InputBegan:Connect(function(input)
			if input.UserInputType == Enum.UserInputType.MouseButton1 then
				IconController.setTopbarEnabled(true,false)
			end
		end)
	else
		TopbarPlusGui.TopbarContainer.Position = UDim2.new(0,0,0,0)
		TopbarPlusGui.TopbarContainer.Visible = checkTopbarEnabledAccountingForMimic()
		indicator.Visible = false
		IconController._setControllerSelectedObject(nil)
	end
	for icon, _ in pairs(topbarIcons) do
		IconController._enableControllerModeForIcon(icon, bool)
	end
end

function IconController._enableControllerModeForIcon(icon, bool)
	local parentIcon = icon._parentIcon
	local featureName = icon.joinedFeatureName
	if parentIcon then
		icon:leave()
	end
	if bool then
		local scaleMultiplier = getScaleMultiplier()
		local currentSizeDeselected = icon:get("iconSize", "deselected")
		local currentSizeSelected = icon:get("iconSize", "selected")
		local currentSizeHovering = icon:getHovering("iconSize")
		icon:set("iconSize", UDim2.new(0, currentSizeDeselected.X.Offset*scaleMultiplier, 0, currentSizeDeselected.Y.Offset*scaleMultiplier), "deselected", "controllerMode")
		icon:set("iconSize", UDim2.new(0, currentSizeSelected.X.Offset*scaleMultiplier, 0, currentSizeSelected.Y.Offset*scaleMultiplier), "selected", "controllerMode")
		if currentSizeHovering then
			icon:set("iconSize", UDim2.new(0, currentSizeSelected.X.Offset*scaleMultiplier, 0, currentSizeSelected.Y.Offset*scaleMultiplier), "hovering", "controllerMode")
		end
		icon:set("alignment", "mid", "deselected", "controllerMode")
		icon:set("alignment", "mid", "selected", "controllerMode")
	else
		local states = {"deselected", "selected", "hovering"}
		for _, iconState in pairs(states) do
			local _, previousAlignment = icon:get("alignment", iconState, "controllerMode")
			if previousAlignment then
				icon:set("alignment", previousAlignment, iconState)
			end
			local currentSize, previousSize = icon:get("iconSize", iconState, "controllerMode")
			if previousSize then
				icon:set("iconSize", previousSize, iconState)
			end
		end
	end
	if parentIcon then
		icon:join(parentIcon, featureName)
	end
end

local createdFakeHealthbarIcon = false
function IconController.setupHealthbar()

	if createdFakeHealthbarIcon then
		return
	end
	createdFakeHealthbarIcon = true

	-- Create a fake healthbar icon to mimic the core health gui
	task.defer(function()
		runService.Heartbeat:Wait()
		local Icon = require(script.Parent)

		Icon.new()
			:setProperty("internalIcon", true)
			:setName("_FakeHealthbar")
			:setRight()
			:setOrder(-420)
			:setSize(80, 32)
			:lock()
			:set("iconBackgroundTransparency", 1)
			:give(function(icon)

				local healthContainer = Instance.new("Frame")
				healthContainer.Name = "HealthContainer"
				healthContainer.BackgroundColor3 = Color3.fromRGB(0, 0, 0)
				healthContainer.BorderSizePixel = 0
				healthContainer.AnchorPoint = Vector2.new(0, 0.5)
				healthContainer.Position = UDim2.new(0, 0, 0.5, 0)
				healthContainer.Size = UDim2.new(1, 0, 0.2, 0)
				healthContainer.Visible = true
				healthContainer.ZIndex = 11
				healthContainer.Parent = icon.instances.iconButton

				local corner = Instance.new("UICorner")
				corner.CornerRadius = UDim.new(1, 0)
				corner.Parent = healthContainer

				local healthFrame = healthContainer:Clone()
				healthFrame.Name = "HealthFrame"
				healthFrame.BackgroundColor3 = Color3.fromRGB(167, 167, 167)
				healthFrame.BorderSizePixel = 0
				healthFrame.AnchorPoint = Vector2.new(0.5, 0.5)
				healthFrame.Position = UDim2.new(0.5, 0, 0.5, 0)
				healthFrame.Size = UDim2.new(1, -2, 1, -2)
				healthFrame.Visible = true
				healthFrame.ZIndex = 12
				healthFrame.Parent = healthContainer

				local healthBar = healthFrame:Clone()
				healthBar.Name = "HealthBar"
				healthBar.BackgroundColor3 = Color3.fromRGB(255, 255, 255)
				healthBar.BorderSizePixel = 0
				healthBar.AnchorPoint = Vector2.new(0, 0.5)
				healthBar.Position = UDim2.new(0, 0, 0.5, 0)
				healthBar.Size = UDim2.new(0.5, 0, 1, 0)
				healthBar.Visible = true
				healthBar.ZIndex = 13
				healthBar.Parent = healthFrame

				local START_HEALTHBAR_COLOR = Color3.fromRGB(27, 252, 107)
				local MID_HEALTHBAR_COLOR = Color3.fromRGB(250, 235, 0)
				local END_HEALTHBAR_COLOR = Color3.fromRGB(255, 28, 0)

				local function powColor3(color, pow)
					return Color3.new(
						math.pow(color.R, pow),
						math.pow(color.G, pow),
						math.pow(color.B, pow)
					)
				end

				local function lerpColor(colorA, colorB, frac, gamma)
					gamma = gamma or 2.0
					local CA = powColor3(colorA, gamma)
					local CB = powColor3(colorB, gamma)
					return powColor3(CA:Lerp(CB, frac), 1/gamma)
				end

				local firstTimeEnabling = true
				local function listenToHealth(character)
					if not character then
						return
					end
					local humanoid = character:WaitForChild("Humanoid", 10)
					if not humanoid then
						return
					end

					local function updateHealthBar()
						local realHealthbarEnabled = starterGui:GetCoreGuiEnabled(Enum.CoreGuiType.Health)
						local healthInterval = humanoid.Health / humanoid.MaxHealth
						if healthInterval == 1 or IconController.healthbarDisabled or (firstTimeEnabling and realHealthbarEnabled == false) then
							if icon.enabled then
								icon:setEnabled(false)
							end
							return
						elseif healthInterval < 1 then
							if not icon.enabled then
								icon:setEnabled(true)
							end
							firstTimeEnabling = false
							if realHealthbarEnabled then
								starterGui:SetCoreGuiEnabled(Enum.CoreGuiType.Health, false)
							end
						end
						local startInterval = 0.9
						local endInterval = 0.1
						local m = 1/(startInterval - endInterval)
						local c = -m*endInterval
						local colorIntervalAbsolute = (m*healthInterval) + c
						local colorInterval = (colorIntervalAbsolute > 1 and 1) or (colorIntervalAbsolute < 0 and 0) or colorIntervalAbsolute
						local firstColor = (healthInterval > 0.5 and START_HEALTHBAR_COLOR) or MID_HEALTHBAR_COLOR
						local lastColor = (healthInterval > 0.5 and MID_HEALTHBAR_COLOR) or END_HEALTHBAR_COLOR
						local doubleSubtractor = (1-colorInterval)*2
						local modifiedColorInterval = (healthInterval > 0.5 and (1-doubleSubtractor)) or (2-doubleSubtractor)
						local newHealthFillColor = lerpColor(lastColor, firstColor, modifiedColorInterval)
						local newHealthFillSize = UDim2.new(healthInterval, 0, 1, 0)
						healthBar.BackgroundColor3 = newHealthFillColor
						healthBar.Size = newHealthFillSize
					end

					humanoid.HealthChanged:Connect(updateHealthBar)
					IconController.healthbarDisabledSignal:Connect(updateHealthBar)
					updateHealthBar()
				end
				localPlayer.CharacterAdded:Connect(function(character)
					listenToHealth(character)
				end)
				task.spawn(listenToHealth, localPlayer.Character)
			end)
	end)
end


