red = Color.new(255,0,0)
yellow = Color.new (255,255,0)

characters = { "1", "2", "3", "4", "5", "6", "7", "8", "", "9", "0", "a", "b", "c", "d", "e", "f", "", "", "", "", "", "", "", "", "", "" }

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

charX = 200
charY= 120

currentX = 197
currentY = 118



currentLetter = 1 


uppercase = true 
name = ""
oldpad = Controls.read() 



addX = 0 
addY = -10
addRow = -9 



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





while true do



pad = Controls.read() 

screen:clear() 
screen:blit(0,0,rech)
screen:print(190,100,"Code:"..name,yellow) 
screen:blit(currentX,currentY, selector) 
screen:blit(197,118,clavier) 

drawLetters() 


if pad:cross() and oldpad:cross() ~= pad:cross() then 
if uppercase == true then 
name = name .. characters[currentLetter] 
else 
name = name .. string.upper(characters[currentLetter]) 
end 
end 


if pad:square() and oldpad:square() ~= pad:square() then 
name = string.sub(name, 1, string.len(name) - 1) 
end 


if pad:circle() then
dofile("recherche2.lua")

end

if pad:start() then
screen.flip() 
screen.waitVblankStart(7) 
dofile("menu.lua")
end

if pad:triangle() then
page = "1/197"
num = "00000000"
name = "00000000"
trad = "Pas d'erreur"
solu = ""
m = "8002044d"
p = "80020001"
screen.flip() 
screen.waitVblankStart(7) 
dofile("page.lua")
end

--MOVE SELECTOR RIGHT :
if pad:right() and currentLetter ~= 9 and currentLetter ~= 18 and currentLetter ~= 27 and oldpad:right() ~= pad:right() then 
currentLetter = currentLetter + 1 
currentX = currentX + 10 
end
if pad:right() and currentLetter == 9 or currentLetter == 18 or currentLetter == 27 then 
currentLetter = currentLetter - 8
currentX = currentX - 80
end 



-- MOVE SELECTOR LEFT 
if pad:left() and currentLetter ~= 0 and currentLetter ~= 9 and currentLetter ~= 18 and oldpad:left() ~= pad:left() then 
currentLetter = currentLetter - 1 
currentX = currentX - 10 
end
if pad:left() and currentLetter == 0 or currentLetter == 9 or currentLetter == 18 then 
currentX = currentX + 80 
currentLetter = currentLetter + 8
end

-- MOVE SELECTOR DOWN 
if pad:down() and currentLetter < 19 and currentLetter > 8 and oldpad:down() ~= pad:down() then 
currentLetter = currentLetter - 9 
currentY = currentY - 10 
elseif pad:down() and currentLetter < 9 and oldpad:down() ~= pad:down() then 
currentY = 128 
currentLetter = currentLetter + 9 
end 

-- MOVE SELECTOR UP 
if pad:up() and currentLetter < 19 and currentLetter > 8 and oldpad:up() ~= pad:up() then 
currentLetter = currentLetter - 9 
currentY = currentY - 10 
elseif pad:up() and currentLetter < 9 and oldpad:up() ~= pad:up() then 
currentY = 128 
currentLetter = currentLetter + 9
end 







 

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