
PolyEditorUI = {}
PolyEditorUI.buttonHeight = 8
PolyEditorUI.toolButtons = {}
PolyEditorUI.toolButtonsName = {}
PolyEditorUI.savedAssetTimer = 0

function makeToolButton(posX, posY, iconUV, toolName, callback, mode)
	local button = UILib.buttonIcon(
		vec3(posX, posY, 0.0), 
		vec2(PolyEditorUI.buttonHeight + 4.0, PolyEditorUI.buttonHeight + 4.0), 
		Config.uiColors.button,
		vec2(2.0, 2.0), vec2(8.0, 8.0),
		iconUV, vec2(8.0, 8.0),
		Config.uiColors.icon, callback)
	button.roundCorner = 0.3
	button.toolName = toolName

	local toolButtons = PolyEditorUI.toolButtons[mode]
	toolButtons[#toolButtons + 1] = button

	PolyEditorUI.toolButtonsName[toolName .. mode] = button

	return button
end

function buildModelUI()
	local col1 = PolyEditor.leftSideGUIOffset
	local col2 = PolyEditor.leftSideGUIOffset + (PolyEditorUI.buttonHeight + 6.0)
	
	local buttonSize = vec2(PolyEditorUI.buttonHeight + 4.0, PolyEditorUI.buttonHeight + 4.0)

	local row = 8.0

	PolyEditorUI.cubeButton = makeToolButton(col1, row, vec2(0.0, 0.0), "new cube", function() PolyEditor.newCubeToolStart() end, MODEL_MODE)
	PolyEditorUI.cubeButton.desc = "box primitive"

	PolyEditorUI.sphereButton = makeToolButton(col2, row, vec2(8.0, 0.0), "new sphere", function() PolyEditor.newSphereToolStart() end, MODEL_MODE)
	PolyEditorUI.sphereButton.desc = "sphere primitive"

	row = row + 14.0
	PolyEditorUI.cylinderButton = makeToolButton(col1, row, vec2(8.0 * 2, 0.0), "new cylinder", function() PolyEditor.newCylinderToolStart() end, MODEL_MODE)
	PolyEditorUI.cylinderButton.desc = "cylinder primitive"

	PolyEditorUI.coneButton = makeToolButton(col2, row, vec2(8.0 * 3, 0.0), "new cone", function() PolyEditor.newConeToolStart() end, MODEL_MODE)
	PolyEditorUI.cylinderButton.desc = "cone primitive"

	local toolButtons = PolyEditorUI.toolButtons[MODEL_MODE]

	row = row + 14.0
	PolyEditorUI.lowResButton = UILib.buttonIcon(
		vec3(col1 - 1, row, 0.0), 
		vec2(8, 8), 
		Config.uiColors.panel,
		vec2(0.0, 0.0), vec2(8.0, 8.0),
		vec2(8 * 13, 0), vec2(8.0, 8.0),
		Config.uiColors.panelLight, function() PolyEditor.setPrimitiveResolution(1) end)
	PolyEditorUI.lowResButton.desc = "low res primitives"

	toolButtons[#toolButtons + 1] = PolyEditorUI.lowResButton

	PolyEditorUI.midResButton = UILib.buttonIcon(
		vec3(col1 - 2 + 9, row, 0.0), 
		vec2(9, 9), 
		Config.uiColors.panel,
		vec2(1.0, 1.0), vec2(8.0, 8.0),
		vec2(0, 8 * 7), vec2(8.0, 8.0),
		Config.uiColors.panelLight, function() PolyEditor.setPrimitiveResolution(2) end)
	PolyEditorUI.midResButton.desc = "mid res primitives"

	toolButtons[#toolButtons + 1] = PolyEditorUI.midResButton

	PolyEditorUI.highResButton = UILib.buttonIcon(
		vec3(col1 -2 + 9 + 9, row, 0.0), 
		vec2(11, 11), 
		Config.uiColors.panel,
		vec2(1.0, 1.0), vec2(9.0, 9.0),
		vec2(8, 8 * 7), vec2(9.0, 9.0),
		Config.uiColors.panelLight, function() PolyEditor.setPrimitiveResolution(3) end)
	PolyEditorUI.highResButton.desc = "high res primitives"

	toolButtons[#toolButtons + 1] = PolyEditorUI.highResButton

	local row = row + (14.0 * 2.0) - 4

	PolyEditorUI.selectButton = makeToolButton(col1, row, vec2(8.0 * 2, 8.0), "select", function() PolyEditor.selectToolStart() end, MODEL_MODE)
	PolyEditorUI.selectButton.desc = "select"

	PolyEditorUI.moveButton = makeToolButton(col2, row, vec2(8.0, 8.0), "move", function() PolyEditor.moveToolStart() end, MODEL_MODE)
	PolyEditorUI.moveButton.desc = "move"

	row = row + 14.0	
	PolyEditorUI.rotateButton = makeToolButton(col1, row, vec2(8.0 * 4, 0.0), "rotate", function() PolyEditor.rotateToolStart() end, MODEL_MODE)
	PolyEditorUI.rotateButton.desc = "rotate"

	PolyEditorUI.scaleButton = makeToolButton(col2, row, vec2(8.0 * 5, 0.0), "scale", function() PolyEditor.scaleToolStart() end, MODEL_MODE)
	PolyEditorUI.scaleButton.desc = "scale"
	PolyEditorUI.scaleButton.desc2 = "hold CTRL to uniform"

	row = row + 14.0

	PolyEditorUI.snapButton = UILib.buttonIcon(
		vec3(col1, row, 0.0), 
		vec2(PolyEditorUI.buttonHeight + 4.0, PolyEditorUI.buttonHeight + 4.0), 
		Config.uiColors.panel,
		vec2(2.0, 2.0), vec2(8.0, 8.0),
		vec2(8.0 * 15, 8.0 * 4), vec2(8.0, 8.0),
		Config.uiColors.panelLight, function() PolyEditor.snapSwitch() end)
	PolyEditorUI.snapButton.desc = "grid snap ON/OFF"

	toolButtons[#toolButtons + 1] = PolyEditorUI.snapButton

	PolyEditorUI.scissorButton = UILib.buttonIcon(
		vec3(col2, row, 0.0), 
		vec2(PolyEditorUI.buttonHeight + 4.0, PolyEditorUI.buttonHeight + 4.0), 
		Config.uiColors.panel,
		vec2(2.0, 2.0), vec2(8.0, 8.0),
		vec2(8.0 * 12, 0.0), vec2(8.0, 8.0),
		Config.uiColors.panelLight, function() PolyEditor.scissorSwitch() end)
	PolyEditorUI.scissorButton.desc = "unstitch vertices when moved"

	toolButtons[#toolButtons + 1] = PolyEditorUI.scissorButton

	row = row + (14.0 * 2)

	PolyEditorUI.newQuadButton = makeToolButton(col1, row, vec2(8.0 * 8, 0.0), "new quad", function() PolyEditor.quadToolStart() end, MODEL_MODE)
	PolyEditorUI.newQuadButton.desc = "new quad"

	PolyEditorUI.extrudeButton = makeToolButton(col2, row, vec2(8.0 * 7, 0.0), "extrude", function() PolyEditor.extrudeToolStart() end, MODEL_MODE)
	PolyEditorUI.extrudeButton.desc = "extrude"

	row = row + 14.0

	PolyEditorUI.verticalSplitButton = makeToolButton(col1, row, vec2(8.0 * 9, 0.0), "split vertical", function() PolyEditor.verticalSplitToolStart() end, MODEL_MODE)
	PolyEditorUI.verticalSplitButton.desc = "split vertical"

	PolyEditorUI.horizontalSplitButton = makeToolButton(col2, row, vec2(8.0 * 10, 0.0), "split horizontal", function() PolyEditor.horizontalSplitToolStart() end, MODEL_MODE)
	PolyEditorUI.horizontalSplitButton.desc = "split horizontal"

	row = row + 14.0

	PolyEditorUI.mirrorButton = makeToolButton(col1, row, vec2(8.0 * 4, 8.0 * 5), "mirror", function() PolyEditor.mirrorToolStart() end, MODEL_MODE)
	PolyEditorUI.mirrorButton.desc = "mirror"

	PolyEditorUI.flipNormalButton = makeToolButton(col2, row, vec2(8.0 * 6, 0.0), "flip normal", function() PolyEditor.flipNormalToolStart() end, MODEL_MODE)
	PolyEditorUI.flipNormalButton.desc = "flip normal"

	row = row + 14.0

	PolyEditorUI.sharpNormalButton = makeToolButton(col1, row, vec2(8.0 * 15, 8.0), "sharp normal", function() PolyEditor.sharpNormalToolStart() end, MODEL_MODE)
	PolyEditorUI.sharpNormalButton.desc = "sharp normals"

	PolyEditorUI.smoothNormalButton = makeToolButton(col2, row, vec2(8.0 * 4, 8.0 * 4), "smooth normal", function() PolyEditor.smoothNormalToolStart() end, MODEL_MODE)
	PolyEditorUI.smoothNormalButton.desc = "sharp normals"
	
	PolyEditorUI.primitiveResolutionChanged()
	PolyEditorUI.shadingTypeChanged()
end

function vertexColorPressed()	
	if PolyEditorUI.colorPicker.visible == false then
		PolyEditorUI.colorPicker.position.x = PolyEditor.screenWidth - PolyEditorUI.colorPicker.size.x
		PolyEditorUI.colorPicker.position.y = PolyEditorUI.vertexColorButton.position.y + 12.0

		PolyEditorUI.colorPicker:setColor(PolyEditorUI.vertexColorButton.colorBase)

		PolyEditorUI.colorPicker:setVisible(true)

		PolyEditorUI.colorPicker.updateColorCallback = function()
			PolyEditor.setVertexColor(PolyEditorUI.colorPicker.color)
		end

		PolyEditorUI.colorPicker.startColorPickCallback = function()
			PolyEditor.vertexPickColorStart()
		end
	else
		PolyEditorUI.colorPicker:setVisible(false)
	end
end

function pencilColorPressed()
	if PolyEditorUI.colorPicker.visible == false then
		PolyEditorUI.colorPicker.position.x = PolyEditor.screenWidth - PolyEditorUI.colorPicker.size.x
		PolyEditorUI.colorPicker.position.y = PolyEditorUI.pencilColorButton.position.y + 12.0

		PolyEditorUI.colorPicker:setColor(PolyEditorUI.pencilColorButton.colorBase)

		PolyEditorUI.colorPicker:setVisible(true)

		PolyEditorUI.colorPicker.updateColorCallback = function()
			PolyEditor.setTexturePencilColor(PolyEditorUI.colorPicker.color)
		end

		PolyEditorUI.colorPicker.startColorPickCallback = function()
			PolyEditor.texturePickColorStart()
		end
	else
		PolyEditorUI.colorPicker:setVisible(false)
	end
end

function buildTextureUI()
	local toolButtons = PolyEditorUI.toolButtons[TEXTURE_MODE]

	local col1 = PolyEditor.leftSideGUIOffset
	local col2 = PolyEditor.leftSideGUIOffset + (PolyEditorUI.buttonHeight + 6.0)
	
	local buttonSize = vec2(PolyEditorUI.buttonHeight + 4.0, PolyEditorUI.buttonHeight + 4.0)

	local row = 8.0

	PolyEditorUI.vertexColorButton = UILib.button("",
		vec3(col1, row, 0.0), 
		vec2(PolyEditorUI.buttonHeight + 4.0, PolyEditorUI.buttonHeight + 4.0), 
		Config.uiColors.icon, vertexColorPressed)
	PolyEditorUI.vertexColorButton.roundCorner = 0.3
	PolyEditorUI.vertexColorButton.border = 1.0
	PolyEditorUI.vertexColorButton.borderColorBase = vec4(Config.paletteNames.white.x * 0.8, Config.paletteNames.white.y * 0.8, Config.paletteNames.white.z * 0.8, 255)
	PolyEditorUI.vertexColorButton.toolName = "vertex color"
	PolyEditorUI.vertexColorButton.desc = "vertex color"
	toolButtons[#toolButtons + 1] = PolyEditorUI.vertexColorButton

	PolyEditorUI.textureScissorButton = UILib.buttonIcon(
		vec3(col2, row, 0.0),
		vec2(PolyEditorUI.buttonHeight + 4.0, PolyEditorUI.buttonHeight + 4.0),
		Config.uiColors.panel,
		vec2(2.0, 2.0), vec2(8.0, 8.0),
		vec2(8.0 * 12, 0.0), vec2(8.0, 8.0),
		Config.uiColors.panelLight, function() PolyEditor.textureScissorSwitch() end)
	PolyEditorUI.textureScissorButton.desc = "unstitch vertices when coloring"

	toolButtons[#toolButtons + 1] = PolyEditorUI.textureScissorButton

	row = row + 14.0 + 14.0

	PolyEditorUI.uvSelectButton = makeToolButton(col1, row, vec2(8.0 * 2, 8.0), "select", function() PolyEditor.selectToolStart() end, TEXTURE_MODE)
	PolyEditorUI.uvSelectButton.desc = "UV select"

	PolyEditorUI.uvMoveButton = makeToolButton(col2, row, vec2(8.0, 8.0), "move uv", function() PolyEditor.moveUVToolStart() end, TEXTURE_MODE)
	PolyEditorUI.uvMoveButton.desc = "UV move"

	row = row + 14.0
	PolyEditorUI.uvRotateButton = makeToolButton(col1, row, vec2(8.0 * 4, 0.0), "rotate uv", function() PolyEditor.rotateUVToolStart() end, TEXTURE_MODE)
	PolyEditorUI.uvRotateButton.desc = "UV rotate"

	PolyEditorUI.uvScaleButton = makeToolButton(col2, row, vec2(8.0 * 5, 0.0), "scale uv", function() PolyEditor.scaleUVToolStart() end, TEXTURE_MODE)
	PolyEditorUI.uvScaleButton.desc = "UV scale"

	row = row + 14.0

	PolyEditorUI.uvSnapshotButton = makeToolButton(col1, row, vec2(8.0 * 3, 8.0 * 7), "UV snapshot", function() PolyEditor.startCaptureUVSnapshot() end, TEXTURE_MODE)
	PolyEditorUI.uvSnapshotButton.desc = "UV snapshot"

	row = row + (14.0 * 2)

	PolyEditorUI.textureBrushSizeButton = UILib.button("", vec3(col1, row, 0.0), vec2(PolyEditorUI.buttonHeight + 4.0, PolyEditorUI.buttonHeight + 4.0), PolyEditor.texturePencilColor, brushSizeButtonPressed)
	PolyEditorUI.textureBrushSizeButton.roundCorner = 1.0
	PolyEditorUI.textureBrushSizeButton.border = 1.0
	PolyEditorUI.textureBrushSizeButton.borderColorBase = vec4(Config.paletteNames.black.x * 0.8, Config.paletteNames.black.y * 0.8, Config.paletteNames.black.z * 0.8, 255)
	PolyEditorUI.textureBrushSizeButton.desc = "brush size"
	toolButtons[#toolButtons + 1] = PolyEditorUI.textureBrushSizeButton

	UILib.setCollectSpritePool(false)

	PolyEditorUI.brushSizePanel = UILib.panel("", vec3(col1 - 40.0, row, 1.0), vec2(40.0, PolyEditorUI.buttonHeight + 4.0), Config.uiColors.panel)
	PolyEditorUI.brushSizePanel.visible = false

	PolyEditorUI.brushSizeLine = UILib.panel("", vec3(col1 - 34.0, row + 5.0, 1.1), vec2(28.0, 2.0), Config.uiColors.handle)
	PolyEditorUI.brushSizeLine.visible = false

	PolyEditorUI.brushSizeHandle = UILib.button("", vec3(0.0, 0.0, 1.2), vec2(1.0, 1.0), Config.uiColors.handle)
	PolyEditorUI.brushSizeHandle.roundCorner = 1.0
	PolyEditorUI.brushSizeHandle.colorPressed = Config.uiColors.handle + 100
	PolyEditorUI.brushSizeHandle.visible = false

	UILib.setCollectSpritePool(true)

	PolyEditorUI.pencilColorButton = UILib.button("",
		vec3(col2, row, 0.0), 
		vec2(PolyEditorUI.buttonHeight + 4.0, PolyEditorUI.buttonHeight + 4.0), 
		vec4(255, 255, 255, 255), pencilColorPressed)
	PolyEditorUI.pencilColorButton.roundCorner = 0.3
	PolyEditorUI.pencilColorButton.border = 1.0
	PolyEditorUI.pencilColorButton.borderColorBase = vec4(Config.paletteNames.white.x * 0.8, Config.paletteNames.white.y * 0.8, Config.paletteNames.white.z * 0.8, 255)
	PolyEditorUI.pencilColorButton.toolName = "pencil color"
	PolyEditorUI.pencilColorButton.desc = "brush color"
	toolButtons[#toolButtons + 1] = PolyEditorUI.pencilColorButton

	row = row + 14.0

	PolyEditorUI.texturePencilButton = makeToolButton(col1, row, vec2(8.0 * 8, 8.0), "pencil", function() PolyEditor.pencilToolStart() end, TEXTURE_MODE)
	PolyEditorUI.texturePencilButton.desc = "pencil"

	PolyEditorUI.texturePickColorButton = makeToolButton(col2, row, vec2(8.0 * 7, 8.0), "textureColorPick", function() PolyEditor.texturePickColorStart() end, TEXTURE_MODE)
	PolyEditorUI.texturePickColorButton.desc = "pick color"

	row = row + 14.0

	PolyEditorUI.textureLineButton = makeToolButton(col1, row, vec2(8.0 * 12, 8.0), "line", function() PolyEditor.lineToolStart() end, TEXTURE_MODE)
	PolyEditorUI.textureLineButton.desc = "line"

	PolyEditorUI.paintBucketButton = makeToolButton(col2, row, vec2(8.0 * 10, 8.0), "bucket", function() PolyEditor.bucketToolStart() end, TEXTURE_MODE)
	PolyEditorUI.paintBucketButton.desc = "bucket"

	row = row + 14.0

	PolyEditorUI.textureRectFillButton = makeToolButton(col1, row, vec2(8.0 * 14, 0.0), "rect fill", function() PolyEditor.rectFillToolStart() end, TEXTURE_MODE)
	PolyEditorUI.textureRectFillButton.desc = "rect fill"

	PolyEditorUI.textureStampButton = makeToolButton(col2, row, vec2(8.0 * 11, 8.0), "stamp", function() PolyEditor.stampToolStart() end, TEXTURE_MODE)
	PolyEditorUI.textureStampButton.desc = "stamp"

	row = row + 14.0

	PolyEditorUI.textureGradientButton = makeToolButton(col1, row, vec2(8.0 * 14, 8.0), "gradient", function() PolyEditor.gradientToolStart() end, TEXTURE_MODE)
	PolyEditorUI.textureGradientButton.desc = "gradient"
	
	PolyEditorUI.textureMagnifyButton = UILib.buttonIcon(
		vec3(col2, row, 0.0), 
		vec2(PolyEditorUI.buttonHeight + 4.0, PolyEditorUI.buttonHeight + 4.0), 
		Config.uiColors.panel,
		vec2(2.0, 2.0), vec2(8.0, 8.0),
		vec2(8.0 * 9, 8.0), vec2(8.0, 8.0),
		Config.uiColors.panelLight, function() PolyEditor.magnifyTexture() end)
	PolyEditorUI.textureMagnifyButton.desc = "magnified region ON/OFF"
	PolyEditorUI.textureMagnifyButton.pressed = false
	PolyEditorUI.textureMagnifyButton.colorPressed = vec4(Config.uiColors.panel)

	UILib.setButtonPressed(PolyEditorUI.textureMagnifyButton, true)

	toolButtons[#toolButtons + 1] = PolyEditorUI.textureMagnifyButton

	local v3pos = PolyEditor.view3SpriteData.position

	PolyEditorUI.prevUVAnimFrameButton = UILib.button(
		"<",
		vec3(v3pos.x, v3pos.y, v3pos.z + 0.1),
		vec2(8.0, 8.0),
		Config.uiColors.button,
		function() PolyEditor.prevUVAnimFrame() end)
	PolyEditorUI.prevUVAnimFrameButton.roundCorner = 0.0
	toolButtons[#toolButtons + 1] = PolyEditorUI.prevUVAnimFrameButton
	PolyEditorUI.prevUVAnimFrameButton.desc = "previous uv anim frame"

	PolyEditorUI.nextUVAnimFrameButton = UILib.button(
		">",
		vec3(v3pos.x + 8.0, v3pos.y, v3pos.z + 0.1),
		vec2(8.0, 8.0),
		Config.uiColors.button,
		function() PolyEditor.nextUVAnimFrame() end)
	PolyEditorUI.nextUVAnimFrameButton.roundCorner = 0.0
	toolButtons[#toolButtons + 1] = PolyEditorUI.nextUVAnimFrameButton
	PolyEditorUI.nextUVAnimFrameButton.desc = "next uv anim frame"

	PolyEditorUI.playUVAnimButton = UILib.buttonIcon(
		vec3(v3pos.x + 16.0, v3pos.y, v3pos.z + 0.1),
		vec2(8.0, 8.0),
		Config.uiColors.button,
		vec2(0.0, 0.0), vec2(8.0, 8.0),
		vec2(8 * 4, 8 * 6), vec2(8.0, 8.0),
		vec4(255, 255, 255, 255),
		function() PolyEditor.playUVAnim() end)
	PolyEditorUI.playUVAnimButton.roundCorner = 0.0
	toolButtons[#toolButtons + 1] = PolyEditorUI.playUVAnimButton
	PolyEditorUI.playUVAnimButton.desc = "play/stop uv anim"

	PolyEditorUI.uvAnimFramePanel = UILib.panel("0",
		vec3(v3pos.x + 24.0, v3pos.y, v3pos.z + 0.1),
		vec2(8.0, 8.0),
		Config.uiColors.panel)
	toolButtons[#toolButtons + 1] = PolyEditorUI.uvAnimFramePanel

	PolyEditorUI.storeUVAnimKeyButton = UILib.button(
		"+",
		vec3(v3pos.x + 32.0, v3pos.y, v3pos.z + 0.1),
		vec2(8.0, 8.0),
		Config.uiColors.button,
		function() PolyEditor.setUVAnimFrame() end)
	PolyEditorUI.storeUVAnimKeyButton.roundCorner = 0.0
	toolButtons[#toolButtons + 1] = PolyEditorUI.storeUVAnimKeyButton
	PolyEditorUI.storeUVAnimKeyButton.desc = "store uv anim key"

	PolyEditorUI.deleteUVAnimFrameButton = UILib.button(
		"x",
		vec3(v3pos.x + 40.0, v3pos.y, v3pos.z + 0.1),
		vec2(8.0, 8.0),
		Config.uiColors.button,
		function() PolyEditor.deleteUVAnimFrame() end)
	PolyEditorUI.deleteUVAnimFrameButton.roundCorner = 0.0
	toolButtons[#toolButtons + 1] = PolyEditorUI.deleteUVAnimFrameButton
	PolyEditorUI.deleteUVAnimFrameButton.desc = "delete uv anim frame"

	PolyEditorUI.colorPicker = UILib.colorPicker()
	PolyEditorUI.colorPicker:setVisible(false)

	PolyEditorUI.colorPicker.copyColorCallback = function()
		UILib.colorPickerClipboardColor = vec4(PolyEditorUI.colorPicker.color)
	end

	PolyEditorUI.colorPicker.pasteColorCallback = function()
		if UILib.colorPickerClipboardColor == nil then
			return
		end
		
		PolyEditorUI.colorPicker:setColor(UILib.colorPickerClipboardColor)

		PolyEditorUI.colorPicker:updateColorCallback()
	end

	PolyEditorUI.colorPicker.paletteChangeCallback = function()
		local paletteTable = {}

		local palette = PolyEditorUI.colorPicker.palette
		if palette ~= nil then
			for i = 1, 24 do
				local col = {}
				col[1] = palette[i].x
				col[2] = palette[i].y
				col[3] = palette[i].z
				col[4] = palette[i].w

				paletteTable[i] = col
			end

			invokeProgram(MAIN_PROGRAMID, "setProjectPalette", paletteTable)
		end
	end
end

function buildRigUI()
	local toolButtons = PolyEditorUI.toolButtons[RIG_MODE]

	local col1 = PolyEditor.leftSideGUIOffset
	local col2 = PolyEditor.leftSideGUIOffset + (PolyEditorUI.buttonHeight + 6.0)
	
	local buttonSize = vec2(PolyEditorUI.buttonHeight + 4.0, PolyEditorUI.buttonHeight + 4.0)

	local row = 8.0

	PolyEditorUI.movePivotButton = makeToolButton(col1, row, vec2(8.0, 8.0), "move pivot", function() PolyEditor.movePivotToolStart() end, RIG_MODE)
	PolyEditorUI.movePivotButton.desc = "move pivot"

	PolyEditorUI.hierarchyButton = makeToolButton(col2, row, vec2(8.0 * 3, 8.0 * 6), "edit hierarchy", function() PolyEditor.hierarchyEditorStart() end, RIG_MODE)
	PolyEditorUI.hierarchyButton.desc = "edit hierarchy"

	local screenWidth = 340
	local hierarchyInspectorWidth = 8 * 18
	local hierarchyInspectorHeight = 8 * 32
	local rightBarWidth = 8 * 4
	local hierarchyInspectorX = screenWidth - hierarchyInspectorWidth - rightBarWidth + 2
	local hierarchyInspectorY = 8 * 2

	PolyEditorUI.hierarchyInspector = UILib.hierarchyInspector()
	PolyEditorUI.hierarchyInspector.position.x = hierarchyInspectorX
	PolyEditorUI.hierarchyInspector.position.y = hierarchyInspectorY
	PolyEditorUI.hierarchyInspector.size.x = hierarchyInspectorWidth
	PolyEditorUI.hierarchyInspector.size.y = hierarchyInspectorHeight
	PolyEditorUI.hierarchyInspector:setVisible(false)
end

function PolyEditorUI.updatePalette()
	local paletteTable = invokeProgram(MAIN_PROGRAMID, "getProjectPalette")

	local palette = {}
	for i = 1, 24 do
		local col = paletteTable[i]
		palette[i] = vec4(col[1], col[2], col[3], col[4])
	end
	
	PolyEditorUI.colorPicker:setPalette(palette)
end

function updateBrushSize()
	if PolyEditor.mode ~= TEXTURE_MODE then
		setBrushSizeSliderVisible(false)

		return
	end

	if getLastKeyPressed() == KEY_ESCAPE then
		setBrushSizeSliderVisible(false)

		return
	end

	local mousePos = getMousePosition()
	local mousePressed = getMouseButtonPressed(MOUSE_BUTTON_LEFT)
	local mouseReleased = getMouseButtonReleased(MOUSE_BUTTON_LEFT)
	local mouseDown = getMouseButtonDown(MOUSE_BUTTON_LEFT)
	
	if mouseReleased == true and PolyEditorUI.closeBrushSizeOnClickOut == true and PolyEditorUI.brushSizePanel.hovered == false then
		setBrushSizeSliderVisible(false)

		return
	end

	if mouseDown == true and PolyEditorUI.brushSizePanel.hovered == true then
		local mousePos = getMousePosition()

		local r = clamp(
			(mousePos.x - PolyEditorUI.brushSizeLine.position.x) / (PolyEditorUI.brushSizeLine.size.x - 2.0),
			0.0, 1.0)

		PolyEditor.paintBrushRadius = round(lerp(PolyEditor.paintBrushRadiusMin, PolyEditor.paintBrushRadiusMax, r))
	end

	PolyEditorUI.brushSizeHandle.size.x = PolyEditorUI.buttonHeight
	PolyEditorUI.brushSizeHandle.size.y = PolyEditorUI.buttonHeight

	local t = (PolyEditor.paintBrushRadius - PolyEditor.paintBrushRadiusMin) / (PolyEditor.paintBrushRadiusMax - 1)

	local handleX = lerp(
		PolyEditorUI.brushSizeLine.position.x - (PolyEditorUI.brushSizeHandle.size.x * 0.5), 
		PolyEditorUI.brushSizeLine.position.x + PolyEditorUI.brushSizeLine.size.x - (PolyEditorUI.brushSizeHandle.size.x * 0.5),
		t)

	PolyEditorUI.brushSizeHandle.position.x = handleX
	
	local handleSize = round(lerp(4.0, 8.0, t))
	PolyEditorUI.brushSizeHandle.size.x = handleSize
	PolyEditorUI.brushSizeHandle.size.y = handleSize

	PolyEditorUI.brushSizeHandle.position.y = (PolyEditorUI.brushSizePanel.position.y + (PolyEditorUI.brushSizePanel.size.y * 0.5)) - (handleSize * 0.5)

	PolyEditorUI.brushSizePanel.changed = true
	UILib.updateUISprite(PolyEditorUI.brushSizePanel, mousePos, mousePressed, mouseDown)

	PolyEditorUI.brushSizeLine.changed = true
	UILib.updateUISprite(PolyEditorUI.brushSizeLine, mousePos, mousePressed, mouseDown)

	PolyEditorUI.brushSizeHandle.changed = true
	UILib.updateUISprite(PolyEditorUI.brushSizeHandle, mousePos, mousePressed, mouseDown)

	if mouseReleased == true and PolyEditorUI.closeBrushSizeOnClickOut == nil then
		PolyEditorUI.closeBrushSizeOnClickOut = true
	end
end

function setBrushSizeSliderVisible(state)
	if PolyEditorUI.closeBrushSizeOnClickOut == true then
		state = false
	end

	PolyEditorUI.brushSizePanel.visible = state
	PolyEditorUI.brushSizeHandle.visible = state
	PolyEditorUI.brushSizeLine.visible = state

	PolyEditor.previewBrushSize = state

	PolyEditorUI.closeBrushSizeOnClickOut = nil
end

function brushSizeButtonPressed()
	setBrushSizeSliderVisible(not PolyEditorUI.brushSizePanel.visible)
end

function PolyEditorUI._tileSizeMenuPressed()
	UILib.setCollectSpritePool(false)

	local labelW = UILib.panel("width", vec3(0, 0, 0), vec2(8 * 5, 8), vec4(0, 0, 0, 0), Config.uiColors.text)
	labelW.hoverEnabled = false

	local fieldW = UILib.textLine(vec3(0, 0, 0), vec2(8 * 4, 8))
	fieldW.text = tostring(PolyEditor.tileSize.x)
	fieldW:setFocusColor(Config.uiColors.panelLight)

	local labelH = UILib.panel("height", vec3(0, 0, 0), vec2(8 * 6, 8), vec4(0, 0, 0, 0), Config.uiColors.text)
	labelH.hoverEnabled = false

	local fieldH = UILib.textLine(vec3(0, 0, 0), vec2(8 * 4, 8))
	fieldH.text = tostring(PolyEditor.tileSize.y)
	fieldH:setFocusColor(Config.uiColors.panelLight)

	local labelD = UILib.panel("depth", vec3(0, 0, 0), vec2(8 * 5, 8), vec4(0, 0, 0, 0), Config.uiColors.text)
	labelD.hoverEnabled = false

	local fieldD = UILib.textLine(vec3(0, 0, 0), vec2(8 * 4, 8))
	fieldD.text = tostring(PolyEditor.tileSize.z)
	fieldD:setFocusColor(Config.uiColors.panelLight)

	UILib.setCollectSpritePool(true)

	local children = { labelW, fieldW, labelH, fieldH, labelD, fieldD }

	UILib.openModal(PolyEditorUI.backgroundSpriteData.size.x, PolyEditorUI.backgroundSpriteData.size.y, "set tile size",
		"ok", function()
			local w = tonumber(fieldW.text) or 1
			local h = tonumber(fieldH.text) or 1
			local d = tonumber(fieldD.text) or 1
			PolyEditor.tileSize = vec3(w, h, d)
		end,
		"cancel", nil,
		children)
end

function PolyEditorUI._newModelMenuPressed()
	UILib.openModal(PolyEditorUI.backgroundSpriteData.size.x, PolyEditorUI.backgroundSpriteData.size.y, "all unsaved changes will be lost", 
		 "ok", PolyEditor.newModel, 
		 "cancel", function() UILib.closeModal() end)
end

function PolyEditorUI._saveModelMeuPressed()
	PolyEditor.save()
end

function PolyEditorUI._saveNewMenuPressed()
	PolyEditor.saveNew()
end

function PolyEditorUI.build()
	PolyEditorUI.toolButtons[MODEL_MODE] = {}
	PolyEditorUI.toolButtons[TEXTURE_MODE] = {}
	PolyEditorUI.toolButtons[RIG_MODE] = {}

	PolyEditorUI.backgroundSprite = allocSprite()
	PolyEditorUI.backgroundSpriteData = {}
	PolyEditorUI.backgroundSpriteData.position = vec3(0.0, 0.0, -0.1)
	PolyEditorUI.backgroundSpriteData.size = vec2(PolyEditor.screenWidth, PolyEditor.screenHeight)
	PolyEditorUI.backgroundSpriteData.color = Config.uiColors.polyEditorBackground

	setSprite(PolyEditorUI.backgroundSprite, PolyEditorUI.backgroundSpriteData)

	local offset = 0
	local label = "file"
	local fileMenuButton = UILib.button(label, vec3(0, 0, 0.0), vec2(8 * #label, PolyEditorUI.buttonHeight), Config.uiColors.panel)
	fileMenuButton.callback = function() 
		UILib.openMenu(fileMenuButton, {
			{"new model", PolyEditorUI._newModelMenuPressed},
			{"save", PolyEditorUI._saveModelMeuPressed},
			{"save new", PolyEditorUI._saveNewMenuPressed},
		})
	end

	offset = offset + 8 * #label + 8
	local label = "edit"
	local editMenuButton = UILib.button(label, vec3(offset, 0, 0.0), vec2(8 * #label, PolyEditorUI.buttonHeight), Config.uiColors.panel)
	editMenuButton.callback = function()
		UILib.openMenu(editMenuButton, {
			{"undo", function() _undo() end},
			{"redo", function() _redo() end},
			{"cut", PolyEditor.cutToClipboard},
			{"copy", PolyEditor.copyToClipboard},
			{"paste", PolyEditor.pasteFromClipboard},
			{"select connected", PolyEditor.selectAllConnected},
			{"clear layer", PolyEditor.clearLayer},
			{"frame selected", PolyEditor.frameSelectedAllViews},
			{"capture GIF", PolyEditor.startCaptureGif},
			{"tile size bounds", PolyEditorUI._tileSizeMenuPressed},
			{PolyEditor.tileBoundsEnabled and "disable tile bounds" or "enable tile bounds", function() PolyEditor.tileBoundsEnabledSwitch() end},
		})
	end

	PolyEditorUI.modeButtonColor = vec4Copy(Config.uiColors.tab)
	PolyEditorUI.modeButtonColorHighlight = PolyEditorUI.modeButtonColor + 50
	PolyEditorUI.modeButtonIconColor = vec4Copy(Config.uiColors.tabIcon)
	PolyEditorUI.modeButtonSelectedColor = vec4Copy(Config.uiColors.accent)
	PolyEditorUI.modeButtonSelectedColorHighlight = PolyEditorUI.modeButtonSelectedColor + 50

	local modelButtonPosX = 8 * 10
	local label = "model"
	PolyEditorUI.modelButton = UILib.button(label, vec3(modelButtonPosX + 8.0, 0.0, 0.0), vec2(8 * #label, PolyEditorUI.buttonHeight), PolyEditorUI.modeButtonColor)
	PolyEditorUI.modelButton.callback = function() switchToModel() end
	PolyEditorUI.modelButton.desc = "model tools"

	modelButtonPosX = modelButtonPosX + (8 * #label) + 8.0
	local label = "texture"
	PolyEditorUI.textureButton = UILib.button(label, vec3(modelButtonPosX + 8.0, 0.0, 0.0), vec2(8 * #label, PolyEditorUI.buttonHeight), PolyEditorUI.modeButtonColor)
	PolyEditorUI.textureButton.callback = function() switchToTexture() end
	PolyEditorUI.textureButton.desc = "texture tools"

	modelButtonPosX = modelButtonPosX + (8 * #label) + 8.0
	local label = "rig"
	PolyEditorUI.rigButton = UILib.button(label, vec3(modelButtonPosX + 8.0, 0.0, 0.0), vec2(8 * #label, PolyEditorUI.buttonHeight), PolyEditorUI.modeButtonColor)
	PolyEditorUI.rigButton.callback = function() switchToRig() end
	PolyEditorUI.rigButton.desc = "rig tools"

	modelButtonPosX = modelButtonPosX + (8 * #label) + 8.0
	local label = "p"
	PolyEditorUI.cameraPerspButton = UILib.button(label, vec3(modelButtonPosX + (8.0 * 5), 0.0, 0.0), vec2(8 * #label, PolyEditorUI.buttonHeight), Config.uiColors.polyEditorBackground)
	PolyEditorUI.cameraPerspButton.textColor = Config.uiColors.panelLight
	PolyEditorUI.cameraPerspButton.changed = true
	PolyEditorUI.cameraPerspButton.callback = function() PolyEditor.switchCameraPerspective() end
	PolyEditorUI.cameraPerspButton.desc = "persp/ortho camera"

	local perspViewOptionsOffset = PolyEditor.leftSideGUIOffset - (8.0 * 4.0) - 8.0

	PolyEditorUI.shade4Button = UILib.buttonIcon(
		vec3(perspViewOptionsOffset + (8.0 * 3), 0.0, 0.0), vec2(8.0, 8.0), 
		Config.uiColors.panel,
		vec2(0.0, 0.0), vec2(8.0, 8.0),
		vec2(8.0 * 15.0, 8.0 * 5.0), vec2(8.0, 8.0),
		Config.uiColors.icon, function() PolyEditor.setShadingType(4) end)
	PolyEditorUI.shade4Button.roundCorner = 0.3
	PolyEditorUI.shade4Button.desc = "texture unlit"

	PolyEditorUI.shade1Button = UILib.buttonIcon(
		vec3(perspViewOptionsOffset + (8.0 * 2), 0.0, 0.0), vec2(8.0, 8.0), 
		Config.uiColors.panel,
		vec2(0.0, 0.0), vec2(8.0, 8.0),
		vec2(8.0 * 15.0, 0.0), vec2(8.0, 8.0),
		Config.uiColors.icon, function() PolyEditor.setShadingType(1) end)
	PolyEditorUI.shade1Button.roundCorner = 0.3
	PolyEditorUI.shade1Button.desc = "texture shaded"

	PolyEditorUI.shade2Button = UILib.buttonIcon(
		vec3(perspViewOptionsOffset + (8.0), 0.0, 0.0), vec2(8.0, 8.0), 
		Config.uiColors.panel,
		vec2(0.0, 0.0), vec2(8.0, 8.0),
		vec2(8.0 * 14.0, 0.0), vec2(8.0, 8.0),
		Config.uiColors.icon, function() PolyEditor.setShadingType(2) end)
	PolyEditorUI.shade2Button.roundCorner = 0.3
	PolyEditorUI.shade2Button.desc = "shaded"

	PolyEditorUI.shade3Button = UILib.buttonIcon(
		vec3(perspViewOptionsOffset, 0.0, 0.0), vec2(8.0, 8.0), 
		Config.uiColors.panel,
		vec2(0.0, 0.0), vec2(8.0, 8.0),
		vec2(8.0 * 13.0, 0.0), vec2(8.0, 8.0),
		Config.uiColors.icon, function() PolyEditor.setShadingType(3) end)
	PolyEditorUI.shade3Button.roundCorner = 0.3
	PolyEditorUI.shade3Button.desc = "wireframe"

	buildModelUI()
	buildTextureUI()
	buildRigUI()

	PolyEditorUI.showAllLayersButton = UILib.buttonIcon(
		vec3(PolyEditor.leftSideGUIOffset, 8 * 29, 0.0), vec2((8.0 * 3) + 2, 8.0), 
		Config.uiColors.modelLayer - 50, 
		vec2(0.0, 0.0), vec2(16.0, 8.0),
		vec2(0.0, 8 * 6), vec2(16.0, 8.0),
		Config.uiColors.icon * 0.5, function() PolyEditor.toggleShowAllLayers() end)
	PolyEditorUI.showAllLayersButton.rightPressCallback = function()
		PolyEditor.toggleAllLayersOverlay()
	end
	PolyEditorUI.showAllLayersButton.roundCorner = 0.3
	PolyEditorUI.showAllLayersButton.desc = "show all, right click for wireframe"

	PolyEditorUI.layersButton = {}

	local numLayers = 8
	local layerIt = 1
	for y = 1, numLayers do
		for x = 1, 3 do
			local xOffset = PolyEditor.leftSideGUIOffset + ((x - 1) * (7.0 + 3.0))
			local yOffset = 170.0 - (PolyEditorUI.buttonHeight - ((7.0 + 3.0) * numLayers)) + ((7.0 + 3.0) * (y - 1))

			local layerId = layerIt -- local scope capture for the callback
			local button = UILib.button(
				"",
				vec3(xOffset, yOffset, 0.0), 
				vec2(7.0, 7.0), 
				Config.uiColors.modelLayer,
				function()
					if getKeyDown(KEY_LEFT_SHIFT) or getKeyDown(KEY_LEFT_CONTROL) then
						PolyEditor.toggleLayerSelected(layerId)
					else
						PolyEditor.selectLayer(layerId)
					end
				end)
			button.roundCorner = 1.2
			button.desc = "layer " .. layerIt .. " (right click for wireframe)"

			button.rightPressCallback = function()
				PolyEditor.switchLayerOverlay(layerId)
			end

			PolyEditorUI.layersButton[layerIt] = button

			button.border = 0.0
			button.borderColorBase = Config.paletteNames.white

			layerIt = layerIt + 1
		end
	end

	PolyEditorUI.layerSelected(PolyEditor.currentLayerId)

	PolyEditorUI.descPanel = UILib.panel("", vec3(0.0, PolyEditor.screenHeight - 8, 1.0), vec2(PolyEditor.screenWidth - 30, 8.0), Config.uiColors.descPanel)
	PolyEditorUI.descPanel.visible = false
	PolyEditorUI.descPanel.changed = true

	switchToModel()
end

function PolyEditorUI.setLayerOverlay(layerId, state)
	local layerButton = PolyEditorUI.layersButton[layerId]

	if state == true then
		layerButton.border = 1.0
	else
		layerButton.border = 0.0
	end

	layerButton.changed = true
end

function PolyEditorUI.layerSelected(layerId)
	if PolyEditorUI.layersButton == nil then
		return
	end
	
	for i = 1, #PolyEditorUI.layersButton do
		local layerButton = PolyEditorUI.layersButton[i]

		if PolyEditor.layerIsEmpty(i) == true then
			layerButton.colorBase = Config.uiColors.modelLayer - 50
		else
			layerButton.colorBase = Config.uiColors.modelLayer
		end

		layerButton.color = layerButton.colorBase
		
		setSprite(layerButton.sprite, layerButton)
	end

	-- highlight every selected layer; the active layer gets the full accent,
	-- other selected layers a dimmer accent so the active one stands out
	for i = 1, #PolyEditor.selectedLayers do
		local selId = PolyEditor.selectedLayers[i]
		local layerButton = PolyEditorUI.layersButton[selId]

		if layerButton ~= nil then
			local accent = Config.uiColors.accent

			if selId ~= PolyEditor.currentLayerId then
				accent = Config.uiColors.accent - 60
			end

			layerButton.color = vec4(accent)
			layerButton.colorBase = vec4(accent)
			layerButton.colorHover = accent + 50
			layerButton.changed = true
		end
	end
end

function PolyEditorUI.toolActivated()
	local toolButtons = PolyEditorUI.toolButtons[PolyEditor.mode]
	for i = 1, #toolButtons do
		local button = toolButtons[i]
		UILib.setButtonPressed(button, false)
	end

	if PolyEditor.mode == MODEL_MODE then
		local toolName = PolyEditor.activeTool
		local button = PolyEditorUI.toolButtonsName[toolName .. PolyEditor.mode]
		if button ~= nil then
			UILib.setButtonPressed(button, true)
		end
	else
		local modelToolName = PolyEditor.activeTool
		local textureToolName = PolyEditor.activePaintTool

		local button1 = PolyEditorUI.toolButtonsName[modelToolName .. PolyEditor.mode]
		if button1 ~= nil then
			UILib.setButtonPressed(button1, true)
		end

		local button2 = PolyEditorUI.toolButtonsName[textureToolName .. PolyEditor.mode]
		if button2 ~= nil then
			UILib.setButtonPressed(button2, true)
		end
	end
end

function PolyEditorUI.primitiveResolutionChanged()
	PolyEditorUI.lowResButton.iconSpriteData.color = vec4Copy(Config.uiColors.panelLight)
	PolyEditorUI.midResButton.iconSpriteData.color = vec4Copy(Config.uiColors.panelLight)
	PolyEditorUI.highResButton.iconSpriteData.color = vec4Copy(Config.uiColors.panelLight)

	if PolyEditor.primitiveResolution == 1 then
		PolyEditorUI.lowResButton.iconSpriteData.color = vec4Copy(Config.uiColors.icon)
	elseif PolyEditor.primitiveResolution == 2 then
		PolyEditorUI.midResButton.iconSpriteData.color = vec4Copy(Config.uiColors.icon)
	elseif PolyEditor.primitiveResolution == 3 then
		PolyEditorUI.highResButton.iconSpriteData.color = vec4Copy(Config.uiColors.icon)
	end

	setSprite(PolyEditorUI.lowResButton.iconSprite, PolyEditorUI.lowResButton.iconSpriteData)
	setSprite(PolyEditorUI.midResButton.iconSprite, PolyEditorUI.midResButton.iconSpriteData)
	setSprite(PolyEditorUI.highResButton.iconSprite, PolyEditorUI.highResButton.iconSpriteData)
end

function PolyEditorUI.shadingTypeChanged()
	PolyEditorUI.shade1Button.iconSpriteData.color = vec4Copy(Config.uiColors.panelLight)
	PolyEditorUI.shade2Button.iconSpriteData.color = vec4Copy(Config.uiColors.panelLight)
	PolyEditorUI.shade3Button.iconSpriteData.color = vec4Copy(Config.uiColors.panelLight)
	PolyEditorUI.shade4Button.iconSpriteData.color = vec4Copy(Config.uiColors.panelLight)

	if PolyEditor.shadingType == 1 then
		PolyEditorUI.shade1Button.iconSpriteData.color = vec4Copy(Config.uiColors.icon)
	elseif PolyEditor.shadingType == 2 then
		PolyEditorUI.shade2Button.iconSpriteData.color = vec4Copy(Config.uiColors.icon)
	elseif PolyEditor.shadingType == 3 then
		PolyEditorUI.shade3Button.iconSpriteData.color = vec4Copy(Config.uiColors.icon)
	elseif PolyEditor.shadingType == 4 then
		PolyEditorUI.shade4Button.iconSpriteData.color = vec4Copy(Config.uiColors.icon)
	end

	setSprite(PolyEditorUI.shade1Button.iconSprite, PolyEditorUI.shade1Button.iconSpriteData)
	setSprite(PolyEditorUI.shade2Button.iconSprite, PolyEditorUI.shade2Button.iconSpriteData)
	setSprite(PolyEditorUI.shade3Button.iconSprite, PolyEditorUI.shade3Button.iconSpriteData)
	setSprite(PolyEditorUI.shade4Button.iconSprite, PolyEditorUI.shade4Button.iconSpriteData)
end

function switchToModel()
	UILib.setButtonPressed(PolyEditorUI.modelButton, true)
	UILib.setButtonPressed(PolyEditorUI.textureButton, false)
	UILib.setButtonPressed(PolyEditorUI.rigButton, false)

	PolyEditor.switchToModel()

	local modelToolButtons = PolyEditorUI.toolButtons[MODEL_MODE]
	for i = 1, #modelToolButtons do
		local toolButton = modelToolButtons[i] 
		toolButton.visible = true

		if toolButton.iconSpriteData ~= nil then
			toolButton.iconSpriteData.visible = true
		end
	end

	local textureToolButtons = PolyEditorUI.toolButtons[TEXTURE_MODE]
	for i = 1, #textureToolButtons do
		local toolButton = textureToolButtons[i] 
		toolButton.visible = false
		
		if toolButton.iconSpriteData ~= nil then
			toolButton.iconSpriteData.visible = false
		end
	end

	local rigToolButtons = PolyEditorUI.toolButtons[RIG_MODE]
	for i = 1, #rigToolButtons do
		local toolButton = rigToolButtons[i] 
		toolButton.visible = false

		if toolButton.iconSpriteData ~= nil then
			toolButton.iconSpriteData.visible = false
		end
	end

	PolyEditor.selectToolStart()
end

function switchToTexture()
	UILib.setButtonPressed(PolyEditorUI.modelButton, false)
	UILib.setButtonPressed(PolyEditorUI.textureButton, true)
	UILib.setButtonPressed(PolyEditorUI.rigButton, false)

	PolyEditor.switchToTexture()

	local modelToolButtons = PolyEditorUI.toolButtons[MODEL_MODE]
	for i = 1, #modelToolButtons do
		local toolButton = modelToolButtons[i] 
		toolButton.visible = false

		if toolButton.iconSpriteData ~= nil then
			toolButton.iconSpriteData.visible = false
		end
	end

	local textureToolButtons = PolyEditorUI.toolButtons[TEXTURE_MODE]
	for i = 1, #textureToolButtons do
		local toolButton = textureToolButtons[i] 
		toolButton.visible = true

		if toolButton.iconSpriteData ~= nil then
			toolButton.iconSpriteData.visible = true
		end
	end

	local rigToolButtons = PolyEditorUI.toolButtons[RIG_MODE]
	for i = 1, #rigToolButtons do
		local toolButton = rigToolButtons[i] 
		toolButton.visible = false

		if toolButton.iconSpriteData ~= nil then
			toolButton.iconSpriteData.visible = false
		end
	end

	PolyEditor.moveUVToolStart()
end

function switchToRig()
	UILib.setButtonPressed(PolyEditorUI.modelButton, false)
	UILib.setButtonPressed(PolyEditorUI.textureButton, false)
	UILib.setButtonPressed(PolyEditorUI.rigButton, true)

	PolyEditor.switchToRig()

	local modelToolButtons = PolyEditorUI.toolButtons[MODEL_MODE]
	for i = 1, #modelToolButtons do
		local toolButton = modelToolButtons[i] 
		toolButton.visible = false

		if toolButton.iconSpriteData ~= nil then
			toolButton.iconSpriteData.visible = false
		end
	end

	local textureToolButtons = PolyEditorUI.toolButtons[TEXTURE_MODE]
	for i = 1, #textureToolButtons do
		local toolButton = textureToolButtons[i] 
		toolButton.visible = false

		if toolButton.iconSpriteData ~= nil then
			toolButton.iconSpriteData.visible = false
		end
	end

	local rigToolButtons = PolyEditorUI.toolButtons[RIG_MODE]
	for i = 1, #rigToolButtons do
		local toolButton = rigToolButtons[i] 
		toolButton.visible = true

		if toolButton.iconSpriteData ~= nil then
			toolButton.iconSpriteData.visible = true
		end
	end

	PolyEditor.moveUVToolStart()
end

function updateModelModeUI()
	if PolyEditorUI.modelButton.pressed == false then
		UILib.setButtonPressed(PolyEditorUI.modelButton, true)
		UILib.setButtonPressed(PolyEditorUI.textureButton, false)
		UILib.setButtonPressed(PolyEditorUI.rigButton, false)
	end

	if PolyEditorUI.scissorButton.pressed ~= PolyEditor.scissorEnabled then
		PolyEditorUI.scissorButton.iconSpriteData.color = vec4Copy(Config.uiColors.panelLight)

		if PolyEditor.scissorEnabled == true then
			PolyEditorUI.scissorButton.iconSpriteData.color = vec4Copy(Config.uiColors.icon)
		end

		PolyEditorUI.scissorButton.pressed = PolyEditor.scissorEnabled

		setSprite(PolyEditorUI.scissorButton.iconSprite, PolyEditorUI.scissorButton.iconSpriteData)
	end

	if PolyEditorUI.snapButton.pressed ~= PolyEditor.snapEnabled then
		PolyEditorUI.snapButton.iconSpriteData.color = vec4Copy(Config.uiColors.panelLight)

		if PolyEditor.snapEnabled == true then
			PolyEditorUI.snapButton.iconSpriteData.color = vec4Copy(Config.uiColors.icon)
		end

		PolyEditorUI.snapButton.pressed = PolyEditor.snapEnabled

		setSprite(PolyEditorUI.snapButton.iconSprite, PolyEditorUI.snapButton.iconSpriteData)
	end
end

function updateTextureModeUI()
	if PolyEditorUI.textureButton.pressed == false then
		UILib.setButtonPressed(PolyEditorUI.modelButton, false)
		UILib.setButtonPressed(PolyEditorUI.textureButton, true)
		UILib.setButtonPressed(PolyEditorUI.rigButton, false)
	end

	if PolyEditorUI.textureScissorButton.pressed ~= PolyEditor.textureScissorEnabled then
		PolyEditorUI.textureScissorButton.iconSpriteData.color = vec4Copy(Config.uiColors.panelLight)

		if PolyEditor.textureScissorEnabled == true then
			PolyEditorUI.textureScissorButton.iconSpriteData.color = vec4Copy(Config.uiColors.icon)
		end

		PolyEditorUI.textureScissorButton.pressed = PolyEditor.textureScissorEnabled
		
		setSprite(PolyEditorUI.textureScissorButton.iconSprite, PolyEditorUI.textureScissorButton.iconSpriteData)
	end

	PolyEditorUI.vertexColorButton.colorBase.x = PolyEditor.currentVertexColor.x
	PolyEditorUI.vertexColorButton.colorBase.y = PolyEditor.currentVertexColor.y
	PolyEditorUI.vertexColorButton.colorBase.z = PolyEditor.currentVertexColor.z
	PolyEditorUI.vertexColorButton.colorBase.w = PolyEditor.currentVertexColor.w
	PolyEditorUI.vertexColorButton.changed = true

	PolyEditorUI.vertexColorButton.colorHover.x = PolyEditorUI.vertexColorButton.colorBase.x + 50.0
	PolyEditorUI.vertexColorButton.colorHover.y = PolyEditorUI.vertexColorButton.colorBase.y + 50.0
	PolyEditorUI.vertexColorButton.colorHover.z = PolyEditorUI.vertexColorButton.colorBase.z + 50.0
	PolyEditorUI.vertexColorButton.colorHover.w = PolyEditorUI.vertexColorButton.colorBase.w + 50.0

	PolyEditorUI.vertexColorButton.borderColorBase.x = PolyEditorUI.vertexColorButton.colorBase.x * 0.8
	PolyEditorUI.vertexColorButton.borderColorBase.y = PolyEditorUI.vertexColorButton.colorBase.y * 0.8
	PolyEditorUI.vertexColorButton.borderColorBase.z = PolyEditorUI.vertexColorButton.colorBase.z * 0.8

	PolyEditorUI.pencilColorButton.colorBase.x = PolyEditor.texturePencilColor.x
	PolyEditorUI.pencilColorButton.colorBase.y = PolyEditor.texturePencilColor.y
	PolyEditorUI.pencilColorButton.colorBase.z = PolyEditor.texturePencilColor.z
	PolyEditorUI.pencilColorButton.colorBase.w = PolyEditor.texturePencilColor.w
	PolyEditorUI.pencilColorButton.changed = true

	PolyEditorUI.pencilColorButton.colorHover.x = PolyEditorUI.pencilColorButton.colorBase.x + 50.0
	PolyEditorUI.pencilColorButton.colorHover.y = PolyEditorUI.pencilColorButton.colorBase.y + 50.0
	PolyEditorUI.pencilColorButton.colorHover.z = PolyEditorUI.pencilColorButton.colorBase.z + 50.0
	PolyEditorUI.pencilColorButton.colorHover.w = PolyEditorUI.pencilColorButton.colorBase.w + 50.0

	PolyEditorUI.pencilColorButton.borderColorBase.x = PolyEditorUI.pencilColorButton.colorBase.x * 0.8
	PolyEditorUI.pencilColorButton.borderColorBase.y = PolyEditorUI.pencilColorButton.colorBase.y * 0.8
	PolyEditorUI.pencilColorButton.borderColorBase.z = PolyEditorUI.pencilColorButton.colorBase.z * 0.8

	local brushRadiusT = PolyEditor.paintBrushRadius / PolyEditor.paintBrushRadiusMax
	PolyEditorUI.textureBrushSizeButton.size.x = round(lerp(6.0, PolyEditorUI.buttonHeight + 4.0, brushRadiusT))
	PolyEditorUI.textureBrushSizeButton.size.y = PolyEditorUI.textureBrushSizeButton.size.x
	PolyEditorUI.textureBrushSizeButton.changed = true

	if PolyEditor.previewBrushSize == true then
		updateBrushSize()
	end
	
	if PolyEditorUI.textureMagnifyButton.pressed ~= not PolyEditor.textureDrawMagnified then
		UILib.setButtonPressed(PolyEditorUI.textureMagnifyButton, not PolyEditor.textureDrawMagnified)
	end

	PolyEditorUI.uvAnimFramePanel.text = tostring(PolyEditor.uvAnimFrame)
	PolyEditorUI.uvAnimFramePanel.changed = true

	local meshData = PolyEditor.meshes[PolyEditor.currentLayerId]
	if meshData and PolyEditor.uvAnimFrame > 0 and hasMeshUVAnimFrame(meshData.mesh, PolyEditor.uvAnimFrame) then
		PolyEditorUI.deleteUVAnimFrameButton.colorBase = Config.paletteNames.redLight
		PolyEditorUI.deleteUVAnimFrameButton.colorHover = Config.paletteNames.redLight + 50
		PolyEditorUI.deleteUVAnimFrameButton.changed = true
	else
		PolyEditorUI.deleteUVAnimFrameButton.colorBase = Config.uiColors.button
		PolyEditorUI.deleteUVAnimFrameButton.colorHover = Config.uiColors.button + 50
		PolyEditorUI.deleteUVAnimFrameButton.changed = true
	end

	if PolyEditor.uvAnimPlaying then
		PolyEditorUI.playUVAnimButton.iconSpriteData.texturePos = vec2(8.0 * 14.0, 0.0)
	else
		PolyEditorUI.playUVAnimButton.iconSpriteData.texturePos = vec2(8.0 * 4.0, 8.0 * 6.0)
	end
	setSprite(PolyEditorUI.playUVAnimButton.iconSprite, PolyEditorUI.playUVAnimButton.iconSpriteData)
end

function updateRigModeUI()
	if PolyEditorUI.rigButton.pressed == false then
		UILib.setButtonPressed(PolyEditorUI.modelButton, false)
		UILib.setButtonPressed(PolyEditorUI.textureButton, false)
		UILib.setButtonPressed(PolyEditorUI.rigButton, true)
	end

	local hi = PolyEditorUI.hierarchyInspector
	if hi.visible == false then
		return
	end

	local hoveredLayerId = -1

	for i = 1, #hi.nodeData do
		local btn = hi.nodeButtons[i]
		if btn ~= nil and btn.visible and btn.hovered == true then
			local node = hi.nodeData[i].node
			if node.layerId ~= nil then
				hoveredLayerId = node.layerId
			end
			break
		end
	end

	for i = 1, #PolyEditorUI.layersButton do
		local layerButton = PolyEditorUI.layersButton[i]
		if i == hoveredLayerId then
			layerButton.forceHovered = true
		else
			layerButton.forceHovered = false
		end
		layerButton.changed = true
	end
end

function updateStatusBar()
	local showDesc = false

	local desc = nil

	local hovered = UILib.hoveredSpriteWidget
	if hovered ~= nil then
		if hovered.desc ~= nil then
			if PolyEditorUI.statusText ~= hovered.desc then
				desc = hovered.desc

				PolyEditorUI.statusText = desc

				local shortcut = Utils.registeredShortcuts[desc] 
				
				if shortcut ~= nil then
					if shortcut.modifier ~= nil then
						desc = desc .. " - ( " .. shortcut.modifier .. " + " .. shortcut.key .. " )"
					else
						desc = desc .. " - ( " .. shortcut.key .. " )"
					end
				end

				if hovered.desc2 ~= nil then
					desc = desc .. " " .. hovered.desc2
				end

				PolyEditorUI.descPanel.text = desc
				PolyEditorUI.descPanel.visible = true
				PolyEditorUI.descPanel.changed = true
			else
				desc = PolyEditorUI.statusText
			end
		end
	end

	if PolyEditorUI.savedAssetTimer > 0 then
		PolyEditorUI.descPanel.text = "saved model " .. PolyEditorUI.savedAsset
		PolyEditorUI.descPanel.visible = true
		PolyEditorUI.descPanel.changed = true

		PolyEditorUI.savedAssetTimer = PolyEditorUI.savedAssetTimer - 1

		return
	end

	if PolyEditorUI.descPanel.visible == true and desc == nil then
		PolyEditorUI.descPanel.text = ""
		PolyEditorUI.statusText = ""
		PolyEditorUI.descPanel.visible = false
		PolyEditorUI.descPanel.changed = true
	end
end

function PolyEditorUI.saved(assetName)
	PolyEditorUI.savedAsset = assetName
	PolyEditorUI.savedAssetTimer = 30
end

function PolyEditorUI.update()
	if PolyEditor.mode == MODEL_MODE then
		updateModelModeUI()
	elseif PolyEditor.mode == TEXTURE_MODE then
		updateTextureModeUI()
	elseif PolyEditor.mode == RIG_MODE then
		updateRigModeUI()
	end

	if PolyEditorUI.showAllLayersButton.pressed ~= PolyEditor.showAllLayers then
		PolyEditorUI.showAllLayersButton.iconSpriteData.color = vec4Copy(Config.uiColors.panelLight)

		if PolyEditor.showAllLayers == true then
			PolyEditorUI.showAllLayersButton.iconSpriteData.color = vec4Copy(Config.uiColors.icon)
		end

		PolyEditorUI.showAllLayersButton.pressed = PolyEditor.showAllLayers

		if PolyEditor.showAllLayers == true then
			PolyEditorUI.showAllLayersButton.iconSpriteData.size.x = 8 * 3
			PolyEditorUI.showAllLayersButton.iconSpriteData.textureSize.x = 8 * 3
		else
			PolyEditorUI.showAllLayersButton.iconSpriteData.size.x = 8 * 2
			PolyEditorUI.showAllLayersButton.iconSpriteData.textureSize.x = 8 * 2
		end

		setSprite(PolyEditorUI.showAllLayersButton.iconSprite, PolyEditorUI.showAllLayersButton.iconSpriteData)
	end

	

	if PolyEditor.cameraType == CAMERA_PERSPECTIVE then
		PolyEditorUI.cameraPerspButton.text = "p"
	else
		PolyEditorUI.cameraPerspButton.text = "o"
	end

	updateStatusBar()

	UILib.update()
end
