blanc = Color.new(255,255,255)
rouge = Color.new(255,0,0)

characters = { "1", "2", "3", "4", "5", "6", "7", "8", "9", "0", "a", "z", "e", "r", "t", "y", "u", "i", "o", "p", "q", "s", "d", "f", "g", "h", "j", "k", "l", "m", "w", "x", "c", "v", "b", "n", "-", ".", "_", " " }

selector = Image.createEmpty(10,10)
selector:clear(rouge)

charX = 315
charY= 55

currentX = 323
currentY = 54

positionX = 0
positionY = 0

currentLetter = 1

uppercase = true

oldpad = Controls.read()

addX = 0
addY = -10
addRow = -10 

enpause = 1
stop = 0 
lecture = "w" 
text = Font.load("text.ttf") 
text:setPixelSizes(0, 16) 
chrono = 100000

mnu_inscription = Image.load("images/mnu_inscription.png")
mnu_creerjoueur = Image.load("images/mnu_creerjoueur.png")
mnu_validinscrip = Image.load("images/mnu_validinscrip.png")
img_background = Image.load("images/img_background.png")

function drawLetters()
for a = 1, 4 do
addX = 0
addY = addY + 10
addRow = addRow + 10
for b = 1, 10 do
addX = addX + 10
if uppercase == false then
screen:print(charX + addX,charY + addY, characters[b + addRow],blanc)
else
screen:print(charX + addX,charY + addY,string.upper(characters[b + addRow]),blanc)
end
end
end
addX = 0
addY = -10
addRow = -10
end

function savename()
mes_sauvnom = Image.load("images/mes_sauvnom.png")
minuteursauv = Timer.new()
minuteursauv:start()
while true do
currentTime1 = minuteursauv:time()
if currentTime1 < 500 then
screen:clear()
screen:blit(positionX, positionY, img_background)
screen:blit(positionX, positionY, mnu_inscription)
screen:print(310,47,name,blanc)
screen:blit(positionX, positionY, mes_sauvnom)
end
if currentTime1 > 500 then
break
end
screen.waitVblankStart()
screen.flip()
end
file = io.open("sauvegarde/"..name..".txt", "w")
screen:print(305,139,name,blanc)
file:write(chrono)
file:close()
dofile("intro.lua")
end

if io.open("savename.txt", "r") ~= nil then 

	while true do 
	file = io.open("savename.txt", "r")
	ourline = file:read()
	if ourline == nil then
	ourline = "..."
	end
	file:close()
	screen:clear() 
	screen:blit(positionX, positionY, img_background)
	screen:blit(positionX, positionY, mnu_inscription)
	screen:print(310,47,ourline,blanc)
	pad = Controls.read() 
		if pad:start() and oldpad:start() ~= pad:start() then 
		lecture = "r+" 
		break 
		elseif pad:select() and oldpad:select() ~= pad:select() then 
		dofile("intro.lua")
		break 
		end 
	screen.waitVblankStart() 
	screen.flip() 
	oldpad = pad 
	end
end
name = ""

while true do
pad = Controls.read()
screen:clear()
screen:blit(positionX, positionY, img_background)
screen:blit(positionX, positionY, mnu_creerjoueur)
screen:print(305,39," "..name,blanc)
screen:blit(currentX,currentY, selector)

if string.len(name) > 15 then 
name = string.sub(name, 1, string.len(name) - 1)
end

drawLetters() 
 
if pad:square() and oldpad:square() ~= pad:square() then
if uppercase == false then
name = name .. characters[currentLetter]
else
name = name .. string.upper(characters[currentLetter])
end
end
if pad:circle() and oldpad:circle() ~= pad:circle() then
name = string.sub(name, 1, string.len(name) - 1)
end
if pad:triangle() and oldpad:triangle() ~= pad:triangle() then
if uppercase == false then
uppercase = true
else
uppercase = false
end
end
if pad:right() and currentLetter ~= 10 and currentLetter ~= 20 and currentLetter ~= 30 and currentLetter ~= 40 and oldpad:right() ~= pad:right() then
currentLetter = currentLetter + 1
currentX = currentX + 10
elseif pad:right() and oldpad:right() ~= pad:right() then
if currentLetter == 10 or currentLetter == 20 or currentLetter == 30 or currentLetter == 40 then
currentLetter = currentLetter - 9
currentX = 323
end
end
if pad:left() and currentLetter ~= 1 and currentLetter ~= 11 and currentLetter ~= 21 and currentLetter ~= 31 and oldpad:left() ~= pad:left() then
currentLetter = currentLetter - 1
currentX = currentX - 10
elseif pad:left() and oldpad:left() ~= pad:left() then
if currentLetter == 1 or currentLetter == 11 or currentLetter == 21 or currentLetter == 31 then
currentX = 323 + 90
currentLetter = currentLetter + 9
end
end
if pad:down() and currentLetter < 31 and oldpad:down() ~= pad:down() then
currentLetter = currentLetter + 10
currentY = currentY + 10
elseif pad:down() and currentLetter > 30 and oldpad:down() ~= pad:down() then
currentY = 54
currentLetter = currentLetter - 30
end
if pad:up() and currentLetter > 10 and oldpad:up() ~= pad:up() then
currentLetter = currentLetter - 10
currentY = currentY - 10
elseif pad:up() and currentLetter < 30 and oldpad:up() ~= pad:up() then
currentY = 54 + 30
currentLetter = currentLetter + 30
end

if pad:select() and oldpad:select() ~= pad:select() then
    savename()
if io.open("sauvegarde/savegame.txt", "r") ~= nil then 
	  screen:clear() 
	  screen:blit(positionX, positionY, img_background)
	  screen:blit(positionX, positionY, mnu_inscription)
	  screen:print(310,47,name,blanc)
      if pad:select() and oldpad:select() ~= pad:select() then
      dofile("intro.lua")
	  elseif pad:start() and oldpad:start() ~= pad:start() then 
	  dofile("inscription.lua")
	  end
end
end

screen.flip()
screen.waitVblankStart()
oldpad = pad
end

mnu_inscription = nil
mnu_creerjoueur = nil
mnu_validinscrip = nil
img_background = nil
mes_sauvnom = nil

