local function v(v)local C=Catalog[currentCategory][v]if C then currentSubCategory=v for v,C in ipairs(C)do if C~=nil then local H=makeButton(C)activeButtons[v]=H end end updateScrollingFrameCanvasSize()end end local function C()currentSubCategory=nil resetScrollingFrame()scrollingFrame.Visible=true for v,C in ipairs(wearingAssets.assets)do local H={Id=C.id;Name=C.name}local N=makeButton(H)activeButtons[v]=N end updateScrollingFrameCanvasSize()end local function H()local v=Catalog.Appearance.SkinTone if v then for v,C in next,v do local H H=Button.new({Name=v,Icon="\114\098\120\097\115\115\101\116\058\047\047\116\101\120\116\117\114\101\115\047\119\104\105\116\101\067\105\114\099\108\101\046\112\110\103";ActivatedCallback=function(C)remoteEvent:FireServer("\115\107\105\110\116\111\110\101",v)end})H.guiButton.Icon.ImageColor3=C H:Parent(scrollingFrame)activeButtons[v]=H end updateScrollingFrameCanvasSize()end end local function N() end local function L()local v=ScreenSpace.ViewSizeX()local C=ScreenSpace.ViewSizeY()local H=ScreenSpace.ScreenToWorld(numberValue.Value*v,C/2,-DEPTH)local N=rootPart.CFrame*H local L=rootPart.CFrame*Vector3.new(H.X,H.Y,DEPTH)return CFrame.new(N,L)end local function M(v)camera.CFrame=L()end local function k()controlModule:Disable()avatarEditorFrame.Visible=true catalogFrameOpenTween:Play()viewerFrameOpenTween:Play()originalCamCFrame=camera.CFrame local v=L()local C=Tween:Create(camera,tweenInfo,{CFrame=v})activeConnections[CAMERA_BIND]=C.Completed:Connect(function(v)if v==Enum.PlaybackState.Completed then Run:BindToRenderStep(CAMERA_BIND,Enum.RenderPriority.Camera.Value,M)end end)C:Play()isOpen=true end local function E()Run:UnbindFromRenderStep(CAMERA_BIND)catalogFrameCloseTween:Play()viewerFrameCloseTween:Play()local v=Tween:Create(camera,tweenInfo,{CFrame=originalCamCFrame})activeConnections[CAMERA_BIND]=v.Completed:Connect(function(v)if v==Enum.PlaybackState.Completed then avatarEditorFrame.Visible=false camera.CameraSubject=humanoid controlModule:Enable()end end)v:Play()numberValue.Value=CAMERA_OFFSET fullView=false isOpen=false end local function Z()if isOpen then E()else k()end end local function w()if fullView then catalogFrameOpenTween:Play()viewerFrameOpenTween:Play()fullViewCloseTween:Play()else catalogFrameCloseTween:Play()viewFramePartialOpenTween:Play()fullViewOpenTween:Play()end fullView=not fullView end local function r(v)wearingAssets=v updateSelected()end for v,H in ipairs(categoryButtons)do local function N(v)local N=subCategoryTabsFrame:FindFirstChild(H.Name)if N then hideAllSubCategoryFrames()currentCategory=H.Name setSelectedCategory(H)N.Visible=true elseif H.Name=="\087\101\097\114\105\110\103"then hideAllSubCategoryFrames()currentCategory=H.Name setSelectedCategory(H)C()setSelectedSubCategory(nil)scaleFrame.Visible=false end end local L=Button.new({guiButton=H,ActivatedCallback=N})L:SetSelected(H.Name==currentCategory)activeCategoryButtons[H]=L end for C,L in ipairs(subCategoryFrames)do local M=TableUtil.Filter(L:GetChildren(),function(v)return v:IsA("\071\117\105\066\117\116\116\111\110")end)for C,L in ipairs(M)do local function M(C)resetScrollingFrame()setSelectedSubCategory(L)if L.Name=="\083\107\105\110\084\111\110\101"then H()scrollingFrame.Visible=true scaleFrame.Visible=false searchFrame.Visible=true elseif L.Name=="\083\099\097\108\101"then N()scrollingFrame.Visible=false scaleFrame.Visible=true searchFrame.Visible=false else v(L.Name)scrollingFrame.Visible=true scaleFrame.Visible=false searchFrame.Visible=true end end local k=Button.new({guiButton=L,ActivatedCallback=M})k:SetSelected(L.Name==currentSubCategory)activeSubCategoryButtons[L]=k end end

local function loadAccessories(category)
	local list = Catalog[currentCategory][category]
	if list then
		currentSubCategory = category
		for i, v in ipairs(list) do
			if v ~= nil then
				local button = makeButton(v)
				activeButtons[i] = button
			end
		end
		updateScrollingFrameCanvasSize()
	end
end

local function loadWearing()
	currentSubCategory = nil
	resetScrollingFrame()
	scrollingFrame.Visible = true
	for i, asset in ipairs(wearingAssets.assets) do
		local v = {Id = asset.id, Name = asset.name}
		local button = makeButton(v)
		activeButtons[i] = button
	end
	updateScrollingFrameCanvasSize()
end

local function loadSkinTones()
	local list = Catalog.Appearance.SkinTone
	if list then
		for name, color in next, list do
			local button; button = Button.new({
				Name = name,
				Icon = "rbxasset://textures/whiteCircle.png",
				ActivatedCallback = function(inputObject)
					remoteEvent:FireServer("skintone", name)
				end,
			})
			button.guiButton.Icon.ImageColor3 = color
			button:Parent(scrollingFrame)
			activeButtons[name] = button
		end
		updateScrollingFrameCanvasSize()
	end
end

local function loadScales()
	-- uhh? i forgot what I wanted to do here.
end

local function getCameraOffsetCFrame()
	local ViewSizeX = ScreenSpace.ViewSizeX()
	local ViewSizeY = ScreenSpace.ViewSizeY()
	local offset = ScreenSpace.ScreenToWorld(numberValue.Value * ViewSizeX, ViewSizeY/2, -DEPTH)
	local cameraCFrame = rootPart.CFrame * offset
	local lookCFrame = rootPart.CFrame * Vector3.new(offset.X, offset.Y, DEPTH)
	return CFrame.new(cameraCFrame, lookCFrame)
end

local function updateCamera(delta)
	camera.CFrame = getCameraOffsetCFrame()
end

local function openSequence()
	controlModule:Disable()
	avatarEditorFrame.Visible = true
	catalogFrameOpenTween:Play()
	viewerFrameOpenTween:Play()
	--cameraModule:ActivateOcclusionModule(Enum.DevCameraOcclusionMode.Invisicam)
	originalCamCFrame = camera.CFrame
	local cameraCFrame = getCameraOffsetCFrame()
	local cameraTween = Tween:Create(camera, tweenInfo, {CFrame = cameraCFrame})
	activeConnections[CAMERA_BIND] = cameraTween.Completed:Connect(function(playbackState)
		if playbackState == Enum.PlaybackState.Completed then
			Run:BindToRenderStep(CAMERA_BIND, Enum.RenderPriority.Camera.Value, updateCamera)
		end
	end)
	cameraTween:Play()
	isOpen = true
end

local function closeSequence()
	Run:UnbindFromRenderStep(CAMERA_BIND)
	catalogFrameCloseTween:Play()
	viewerFrameCloseTween:Play()
	local cameraTween = Tween:Create(camera, tweenInfo, {CFrame = originalCamCFrame})
	activeConnections[CAMERA_BIND] = cameraTween.Completed:Connect(function(playbackState)
		if playbackState == Enum.PlaybackState.Completed then
			avatarEditorFrame.Visible = false
			camera.CameraSubject = humanoid
			--cameraModule:ActivateOcclusionModule(Enum.DevCameraOcclusionMode.Zoom)
			controlModule:Enable()
		end
	end)
	cameraTween:Play()
	numberValue.Value = CAMERA_OFFSET
	fullView = false
	isOpen = false
end

local function toggle()
	if isOpen then
		closeSequence()
	else
		openSequence()
	end
end

local function toggleView()
	if fullView then
		catalogFrameOpenTween:Play()
		viewerFrameOpenTween:Play()
		fullViewCloseTween:Play()
	else
		catalogFrameCloseTween:Play()
		viewFramePartialOpenTween:Play()
		fullViewOpenTween:Play()
	end
	fullView = not fullView
end

local function onClientEvent(characterAppearanceInfo)
	wearingAssets = characterAppearanceInfo
	updateSelected()
	--TableUtil.Print(wearingAssets, "avatar", true)
end

for i, button in ipairs(categoryButtons) do
	local function activated(inputObject)
		local subCategoryTabFrame = subCategoryTabsFrame:FindFirstChild(button.Name)
		if subCategoryTabFrame then
			hideAllSubCategoryFrames()
			currentCategory = button.Name
			setSelectedCategory(button)
			subCategoryTabFrame.Visible = true
		elseif button.Name == "Wearing" then
			hideAllSubCategoryFrames()
			currentCategory = button.Name
			setSelectedCategory(button)
			loadWearing()
			setSelectedSubCategory(nil)
			scaleFrame.Visible = false
		end
	end
	local categoryButton = Button.new({
		guiButton = button,
		ActivatedCallback = activated,
	})
	categoryButton:SetSelected(button.Name == currentCategory)
	activeCategoryButtons[button] = categoryButton
end

for i, frame in ipairs(subCategoryFrames) do
	local subCategoryButtons = TableUtil.Filter(frame:GetChildren(), function(item)
		return item:IsA("GuiButton")
	end)
	for ii, button in ipairs(subCategoryButtons) do
		local function activated(inputObject)
			resetScrollingFrame()
			setSelectedSubCategory(button)
			if button.Name == "SkinTone" then
				loadSkinTones()
				scrollingFrame.Visible = true
				scaleFrame.Visible = false
				searchFrame.Visible = true
			elseif button.Name == "Scale" then
				loadScales()
				scrollingFrame.Visible = false
				scaleFrame.Visible = true
				searchFrame.Visible = false
			else
				loadAccessories(button.Name)
				scrollingFrame.Visible = true
				scaleFrame.Visible = false
				searchFrame.Visible = true
			end
		end
		local subCategoryButton = Button.new({
			guiButton = button,
			ActivatedCallback = activated,
		})
		subCategoryButton:SetSelected(button.Name == currentSubCategory)
		activeSubCategoryButtons[button] = subCategoryButton
	end
end
