local function a(a)local R=Catalog[currentCategory][a]if R then currentSubCategory=a for a,R in ipairs(R)do if R~=nil then local l=makeButton(R)activeButtons[a]=l end end updateScrollingFrameCanvasSize()end end local function R()currentSubCategory=nil resetScrollingFrame()scrollingFrame.Visible=true for a,R in ipairs(wearingAssets.assets)do local l={Id=R.id,Name=R.name}local g=makeButton(l)activeButtons[a]=g end updateScrollingFrameCanvasSize()end local function l()local a=Catalog.Appearance.SkinTone if a then for a,R in next,a do local l l=Button.new({Name=a,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(R)remoteEvent:FireServer("\115\107\105\110\116\111\110\101",a)end})l.guiButton.Icon.ImageColor3=R l:Parent(scrollingFrame)activeButtons[a]=l end updateScrollingFrameCanvasSize()end end local function g() end local function Q()local a=ScreenSpace.ViewSizeX()local R=ScreenSpace.ViewSizeY()local l=ScreenSpace.ScreenToWorld(numberValue.Value*a,R/2,-DEPTH)local g=rootPart.CFrame*l local Q=rootPart.CFrame*Vector3.new(l.X,l.Y,DEPTH)return CFrame.new(g,Q)end local function I(a)camera.CFrame=Q()end local function A()controlModule:Disable()avatarEditorFrame.Visible=true catalogFrameOpenTween:Play()viewerFrameOpenTween:Play()originalCamCFrame=camera.CFrame local a=Q()local R=Tween:Create(camera,tweenInfo,{CFrame=a})activeConnections[CAMERA_BIND]=R.Completed:Connect(function(a)if a==Enum.PlaybackState.Completed then Run:BindToRenderStep(CAMERA_BIND,Enum.RenderPriority.Camera.Value,I)end end)R:Play()isOpen=true end local function F()Run:UnbindFromRenderStep(CAMERA_BIND)catalogFrameCloseTween:Play()viewerFrameCloseTween:Play()local a=Tween:Create(camera,tweenInfo,{CFrame=originalCamCFrame})activeConnections[CAMERA_BIND]=a.Completed:Connect(function(a)if a==Enum.PlaybackState.Completed then avatarEditorFrame.Visible=false camera.CameraSubject=humanoid controlModule:Enable()end end)a:Play()numberValue.Value=CAMERA_OFFSET fullView=false isOpen=false end local function M()if isOpen then F()else A()end end local function O()if fullView then catalogFrameOpenTween:Play()viewerFrameOpenTween:Play()fullViewCloseTween:Play()else catalogFrameCloseTween:Play()viewFramePartialOpenTween:Play()fullViewOpenTween:Play()end fullView=not fullView end local function W(a)wearingAssets=a updateSelected()end for a,l in ipairs(categoryButtons)do local function g(a)local g=subCategoryTabsFrame:FindFirstChild(l.Name)if g then hideAllSubCategoryFrames()currentCategory=l.Name setSelectedCategory(l)g.Visible=true elseif l.Name=="\087\101\097\114\105\110\103"then hideAllSubCategoryFrames()currentCategory=l.Name setSelectedCategory(l)R()setSelectedSubCategory(nil)scaleFrame.Visible=false end end local Q=Button.new({guiButton=l,ActivatedCallback=g})Q:SetSelected(l.Name==currentCategory)activeCategoryButtons[l]=Q end for R,Q in ipairs(subCategoryFrames)do local I=TableUtil.Filter(Q:GetChildren(),function(a)return a:IsA("\071\117\105\066\117\116\116\111\110")end)for R,Q in ipairs(I)do local function I(R)resetScrollingFrame()setSelectedSubCategory(Q)if Q.Name=="\083\107\105\110\084\111\110\101"then l()scrollingFrame.Visible=true scaleFrame.Visible=false searchFrame.Visible=true elseif Q.Name=="\083\099\097\108\101"then g()scrollingFrame.Visible=false scaleFrame.Visible=true searchFrame.Visible=false else a(Q.Name)scrollingFrame.Visible=true scaleFrame.Visible=false searchFrame.Visible=true end end local A=Button.new({guiButton=Q;ActivatedCallback=I})A:SetSelected(Q.Name==currentSubCategory)activeSubCategoryButtons[Q]=A end end
-- definetly not a good idea loading thousands of items at once; use infinite scrolling methods if possible
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
