LoadFile("DATA:GUI/Dialogs_Screen.lua")

-----------------------------------------------------
-- MAIN MENU
-----------------------------------------------------

--Main Logo
local MainMenuTitle = CreateImage("MainMenuTitle", [[GUI\main_menu\eets_title.anim]], { SCREEN_WIDTH / 2, 185 })

--Crumbs animation first frame is empty, so we need to override dimensions
local Crumbs = CreateImage("Crumbs", [[GUI\main_menu\eets_run_crumbs.anim]])
Crumbs.width = 1
Crumbs.height = 1
Crumbs.anim_ext.auto_hitregion = false

--Cake and rope animations are rotated to save atlas space
local CAKE_ANGLE = 35 * math.pi / 180
local ROPE_ANGLE = 30 * math.pi / 180

--Ground and BG rotation parameters
GroundRotationParams =
{
	num_slices = 2,
	slice_angle = math.pi,
	rotate_offset = 0,
	rotate_delta = -2 * math.pi / 280,
}
LightRotationParams =
{
	num_slices = 3,
	slice_angle = math.pi / 2,
	rotate_offset = math.pi / -2,
	rotate_delta = -0.25 * GroundRotationParams.rotate_delta,
}

function CreateSlices(name, anim, params, pos, scale, blend)
	local slices = CreateGroup(name, {}, 1, 1, pos)
	local angle = params.rotate_offset
	for i = 1, params.num_slices do
		local slice = CreateImage(name..tostring(angle), anim, { 0, 0 }, { math.cos(angle), math.sin(angle) })
		slice.anim_ext.additional_scale = scale
		slice.anim_ext.blend = blend
		angle = angle + params.slice_angle
		table.insert(slices.parent_ext.children, slice)
	end
	return slices
end

--Eets Running Animation
local EetsRunAnim = CreateGroup("EetsRunAnim",
{
	CreateGroup("CakeGroup",
	{
		Crumbs,
		CreateImage("Cake", [[GUI\main_menu\eets_run_cake.anim]], nil, { math.cos(CAKE_ANGLE), math.sin(CAKE_ANGLE) }),
		CreateImage("Rope", [[GUI\main_menu\eets_run_rope.anim]], nil, { math.cos(ROPE_ANGLE), math.sin(ROPE_ANGLE) }),
	},
	1, 1, { 760, 260 }),
	CreateSlices("Ground", [[GUI\main_menu\eets_run_ground.anim]], GroundRotationParams, { 445, 1165 }),
	CreateImage("Eets", [[GUI\main_menu\eets_run.anim]], { 380, 425 }),
	CreateImage("Dust", [[GUI\main_menu\eets_run_dust.anim]], { 335, 620 }),
},
SCREEN_WIDTH, SCREEN_HEIGHT, { 0, 55 })

--Animated BG
local MainMenuLight = CreateSlices("MainMenuLight", [[GUI\main_menu\lighting\title_light_ray.anim]], LightRotationParams, { 512, -60 }, 4, true)

--Static BG
local MainMenuBGBack = CreateImage("MainMenuBGBack", [[GUI\main_menu\title_bg_back.anim]], { SCREEN_WIDTH / 2, SCREEN_HEIGHT / 2 })

--Main buttons
local BUTTON_MARGIN = 25
local BUTTON_SPACING = 10
local right_x = SCREEN_WIDTH / 2 - BUTTON_MARGIN
local right_y = SCREEN_HEIGHT / 2 - BUTTON_MARGIN + BUTTON_SPACING
local left_x = -right_x
local left_y = right_y

local MainMenuUI = CreateGroup("MainMenuUI", {}, SCREEN_WIDTH, SCREEN_HEIGHT, { SCREEN_WIDTH / 2, SCREEN_HEIGHT / 2 })

--Adds botton right aligned buttons to the screen
function AddRightButton(name, anim, width, height, label, badge_offs)
	right_y = right_y - height - BUTTON_SPACING
	local btn = CreatePushButton(name, anim, { right_x - width / 2, right_y + height / 2 }, 1.1)
	btn.button_ext.additional_scale = 1.03
	if label then
		btn.text_ext =
		{
			text = label,
			font = "dark_label",
			style = "centre centy",
			colour = { r = 66, g = 21, b = 18, alpha = 255 },
			hover_colour = { r = 66, g = 21, b = 18, alpha = 255 },
			offset = { 0, 58 },
		}
	end
	table.insert(MainMenuUI.parent_ext.children, btn)
	if badge_offs then
		table.insert(MainMenuUI.parent_ext.children, 1, CreateBadge(name.."Badge", [[GUI\buttons\badge.anim]], { right_x - width / 2 + badge_offs[1], right_y + height / 2 + badge_offs[2] }))
	end
end

--Adds bottom left aligned buttons to the screen
function AddLeftButton(name, anim, width, height, badge_offs)
	left_y = left_y - height - BUTTON_SPACING
	local btn = CreatePushButton(name, anim, { left_x + width / 2, left_y + height / 2 }, 1.1)
	btn.button_ext.additional_scale = 1.1
	table.insert(MainMenuUI.parent_ext.children, btn)
	if badge_offs then
		table.insert(MainMenuUI.parent_ext.children, 1, CreateBadge(name.."Badge", [[GUI\buttons\badge.anim]], { left_x + width / 2 + badge_offs[1], left_y + height / 2 + badge_offs[2] }))
	end
end

--Right side buttons
AddRightButton("EditorButton", [[GUI\main_menu\leveleditor_button.anim]], 180, 180, "$00481")
AddRightButton("StartButton", [[GUI\main_menu\storymode_button.anim]], 180, 180, "$00485")
if GetVersion() ~= "IOS" then
	local WorkshopButton = inherit(MainMenuUI.parent_ext.children[1]) { name = "WorkshopButton" }
	WorkshopButton.position[1] = WorkshopButton.position[1] - 200
	WorkshopButton.anim_ext.animation = [[GUI\main_menu\steam\workshop_button.anim]]
	WorkshopButton.text_ext.text = "$00483"
	table.insert(MainMenuUI.parent_ext.children, 1, WorkshopButton)
end

--Left side buttons
AddLeftButton("OptionsButton", [[GUI\main_menu\options_button.anim]], 100, 86)

-------------------------------------------------

widgets =
{
	-- Dialogs
	WebLinkButton,
	OptionsDialog,
	CreditsDialog,
	MainMenuUI,
	MainMenuLight,
	EetsRunAnim,
	MainMenuTitle,
	MainMenuBGBack,
}

if GetVersion() == "IOS" then
	AddLeftButton("GameCenterButton", [[GUI\main_menu\challenge_button.anim]], 100, 86, { 32, -32 })
else
	local ExitButton = CreatePushButton("ExitButton", [[GUI\main_menu\steam\btn_delete.anim]], { SCREEN_WIDTH - 30, 30 }, 1.1)
	ExitButton.button_ext.additional_scale = 1.1
	table.insert(widgets, 1, ExitButton)
end