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

UI = {}

UI.clickCount = 0
UI.iconToggled = false

function UI.start()
	UILib.init()

	-- cursor
	UI.cursorSprite = allocSprite()
	UI.cursorSpriteData = {}
	UI.cursorSpriteData.position = vec3(0.0, 0.0, 999.0)
	UI.cursorSpriteData.size = vec2(16.0, 16.0)
	UI.cursorSpriteData.color = vec4(255.0, 255.0, 255.0, 255.0)
	UI.cursorSpriteData.textureId = TEXTURE_ICONS
	UI.cursorSpriteData.texturePos = vec2(8.0 * 5, 8.0 * 4)
	UI.cursorSpriteData.textureSize = vec2(16.0, 16.0)

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

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

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

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

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

	-- buttonIcon
	UI.iconToggled = false
	UI.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()
			UI.iconToggled = not UI.iconToggled
			UILib.setButtonPressed(UI.iconButton, UI.iconToggled)
		end)
	UI.iconButton.desc = "toggle icon"

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

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

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

	-- section: bare sprite hover
	UI.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)
	UI.spriteLabel.textOffset = vec2(4.0, 1.0)

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

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

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

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

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

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

	-- cursor
	UI.cursorSpriteData.position.x = round(mousePos.x) - 8
	UI.cursorSpriteData.position.y = round(mousePos.y) - 8
	setSprite(UI.cursorSprite, UI.cursorSpriteData)
	drawSprite2D(UI.cursorSprite)
end
