
-- UILib was imported by gameMain.lua, don't forget to import it yourself in your game

UIExample = {}

UIExample.clickCount = 0
UIExample.iconToggled = false

function UIExample.start()
	UILib.init()

	-- background
	UIExample.background = UILib.panel("", vec3(0.0, 0.0, -1.0), vec2(320.0, 280.0), Config.paletteNames.black)

	-- title
	UIExample.title = UILib.panel("ui example", vec3(0.0, 4.0, 0.0), vec2(320.0, 16.0), Config.paletteNames.black, Config.paletteNames.white)
	UIExample.title.textOffset = vec2(120.0, 4.0)

	-- section: UILib widgets
	UIExample.sectionLabel = UILib.panel("UILib widgets", vec3(16.0, 26.0, 0.0), vec2(140.0, 10.0), Config.paletteNames.black, Config.paletteNames.grey)
	UIExample.sectionLabel.textOffset = vec2(4.0, 1.0)

	-- button
	UIExample.clickCount = 0
	UIExample.counterLabel = UILib.panel("clicks: 0", vec3(120.0, 42.0, 0.0), vec2(80.0, 16.0), Config.paletteNames.black, Config.paletteNames.yellow)
	UIExample.counterLabel.textOffset = vec2(4.0, 4.0)

	UIExample.clickButton = UILib.button("click me", vec3(16.0, 42.0, 0.0), vec2(96.0, 16.0), Config.palette[5],
		function()
			UIExample.clickCount = UIExample.clickCount + 1
			UIExample.counterLabel.text = "clicks: " .. UIExample.clickCount
			UIExample.counterLabel.changed = true
		end)
	UIExample.clickButton.textOffset = vec2(4.0, 4.0)
	UIExample.clickButton.roundCorner = 0.15

	-- buttonIcon
	UIExample.iconToggled = false
	UIExample.iconButton = UILib.buttonIcon(
		vec3(16.0, 64.0, 0.0), vec2(16.0, 16.0),
		Config.palette[9],
		vec2(0.0, 0.0), vec2(16.0, 16.0),
		vec2(8.0 * 10.0, 8.0 * 2.0), vec2(16.0, 16.0),
		Config.paletteNames.white,
		function()
			UIExample.iconToggled = not UIExample.iconToggled
			UILib.setButtonPressed(UIExample.iconButton, UIExample.iconToggled)
		end)
	UIExample.iconButton.desc = "toggle icon"

	UIExample.iconLabel = UILib.panel("buttonIcon (toggle)", vec3(38.0, 64.0, 0.0), vec2(120.0, 16.0), Config.paletteNames.black, Config.paletteNames.grey)
	UIExample.iconLabel.textOffset = vec2(4.0, 4.0)

	-- textLine
	UIExample.typedLabel = UILib.panel("typed:", vec3(16.0, 102.0, 0.0), vec2(200.0, 10.0), Config.paletteNames.black, Config.paletteNames.grey)
	UIExample.typedLabel.textOffset = vec2(4.0, 1.0)

	UIExample.textField = UILib.textLine(vec3(16.0, 86.0, 0.0), vec2(140.0, 10.0),
		function()
			UIExample.typedLabel.text = "typed: " .. UIExample.textField.text
			UIExample.typedLabel.changed = true
		end)
	UIExample.textField.text = "edit me"
	UIExample.textField.changed = true

	-- section: bare sprite hover
	UIExample.spriteLabel = UILib.panel("bare sprite (hover me)", vec3(16.0, 122.0, 0.0), vec2(160.0, 10.0), Config.paletteNames.black, Config.paletteNames.grey)
	UIExample.spriteLabel.textOffset = vec2(4.0, 1.0)

	-- show how to do UIs with bare 2D sprites
	UIExample.hoverSprite = allocSprite()
	UIExample.hoverSpriteData = {}
	UIExample.hoverSpriteData.position = vec3(16.0, 138.0, 0.0)
	UIExample.hoverSpriteData.size = vec2(64.0, 32.0)
	UIExample.hoverColorBase = vec4(56.0, 152.0, 255.0, 255.0)
	UIExample.hoverColorHover = vec4(110.0, 238.0, 255.0, 255.0)
	UIExample.hoverSpriteData.color = UIExample.hoverColorBase
	UIExample.hoverSpriteData.text = "hover"
	UIExample.hoverSpriteData.textColor = vec4(255.0, 255.0, 255.0, 255.0)
	UIExample.hoverSpriteData.textOffset = vec2(12.0, 12.0)
end

function UIExample.update()
	-- UILib will update the sprite hovers and clicks
	UILib.update()

	-- bare sprite hover detection
	local mousePos = getMousePosition()

	local spritePos = vec2(UIExample.hoverSpriteData.position.x, UIExample.hoverSpriteData.position.y)

	if intersectRect(mousePos, spritePos, UIExample.hoverSpriteData.size) then
		UIExample.hoverSpriteData.color = UIExample.hoverColorHover
	else
		UIExample.hoverSpriteData.color = UIExample.hoverColorBase
	end

	setSprite(UIExample.hoverSprite, UIExample.hoverSpriteData)
	drawSprite2D(UIExample.hoverSprite)

	UILib.drawCursor(mousePos)
end
