-- finished the demo level
local menu = Container:new(0,0,300,240)
local function addlabel(x,y,w,h,tx,to,center)
	local text = (to or menu):add(Label:new(x,y,w,h,tx))
	text:setFontColor(0,1,0,1)
	if center then
		text:setAlignment(Label.LABEL_ALIGNCENTERED,Label.LABEL_ALIGNCENTERED)
		--text:getSkin():setFontColor(0,1,0,1)
	else
		text:setAlignment(nil,Label.LABEL_ALIGNTOP)
		--text:getSkin():setFontColor(0,1,0,1)
	end
	return text
end
local function addrect (x,y,w,h,to)
	local line = (to or menu):add(ImageComponent:new(x,y,w,h,matsurface.vertexcolor()))
	line.l2d.image:color(0,1,0,1)
	return line
end

local function addbutton(x,y,w,h,to,tx,fun)
	local btn = (to or menu):add(Button:new(x,y,w,h,tx))
	btn.onClicked = fun
	btn:getSkin():setSkinColor(0,1,0,.3)
	btn:getSkin():setFontColor(0,1,0,1)
end

local points = game.player.extracounter
local place = highscores:place(points)

addlabel(0,0,300,50,"Congratulations, you finished the demo level.\n\vCYou collected "..
	points.." scrapparts, which places you at #"..place..
	"\n\vCEnter your callsign:",nil,true)
local sign = addlabel(0,50,300,30,"_--",nil,true)

local function finish()
	local nick = sign:getText()
	highscores:enter(nick,points)
	startmenu()
end

function sign:isFocusable() return true end
sign:addKeyListener(KeyListener.new(
	function(kl,ke) 
		if ke.key:match("[a-zA-z0-9]") then
			local tx = sign:getText()
			tx = tx:gsub("_%-?",ke.key.."_"):sub(1,3)
			sign:setText(tx)
		end
		if ke.key=='\n' then
			finish()
		end	
		if ke.key == '\b' then
			local tx = sign:getText()
			if tx:match("_") then
				tx = tx:gsub("._","_-"):sub(1,3)
			else
				tx = tx:sub(1,2).."_"
			end
			sign:setText(tx)
		end
	end,true))
sign:focus()
function sign:transferFocusOnTab () return false end
function sign:transferFocusOnArrows () return false end

local tx = {"Highscores\n"}
for i,v in ipairs(highscores.scores) do 
	tx[#tx+1] = ("%d. %s\vx100;%s\vx200;%d\n"):format(i,v.date,v.name,v.points)
end
local scores = addlabel(0,80,300,160,table.concat(tx))


game:showMenu(menu,finish)