
local keys_y={20,32,44,56,68}        
local keys_x={3,14,24,34,44,54,64,74,84,94,104,114,124,134,144,154} 

local keymap={}
keymap[1]="_0123456789****"
keymap[2]="ABCDEFGHIJKL---"
keymap[3]="MNOPQRSTUVXYZ--"
keymap[4]="     @@@@@     "
--      *=CANC
--      -=OK
--      @=SPACE
local sPlayer="PLAYER"

local keyb_top,keyb_left

-----------------------------------------------------
-- Ask for player name screen
function doInsertName()

--      "_0123456789****"
--      "ABCDEFGHIJKL---"
--      "MNOPQRSTUVXYZ--"
--      "     @@@@@     "
--      *=CANC
--      -=OK
--      @=SPACE
        local string_left
        
        keyb_left=(screenwidth()-imagewidth(IMG_KEYB))/2
        keyb_top=(screenheight()-imageheight(IMG_KEYB))/2
        drawimage(IMG_KEYB, keyb_left,keyb_top,0,0,imagewidth(IMG_KEYB),imageheight(IMG_KEYB))

        setfont(font, fontSize1, 0, 0, 0)
        
        string_left = (screenwidth() - string.len(sPlayer)*fontSize1)/2
        drawtext(font, string_left, keyb_top+16, sPlayer)
end

-----------------------------------------------------
-- Function called during insert name screen to detect
-- player input
function keyHit(x,y)

        local ix,iy
        local car
        
        ix= getIndexX(x)   -- detect key under the stylus
        iy= getIndexY(y)   
           
        car= string.sub(keymap[iy], ix, ix) -- this is the key on the virtual keyboard 
        if (car=="*") then
                -- *= canc
                sPlayer= string.sub(sPlayer,1,string.len(sPlayer)-1)    -- delete 1 char
        elseif (car=="-") then          -- this is like an "enter" 
                -- -= ok
                gameHiScores[game_score_rank]['player']= sPlayer                -- set name and score
                gameHiScores[game_score_rank]['score']= game_score_record               
                saveHiscores()          -- save hiscore table
                CURRENT_STATE = STATE_HIGHSCORES        -- change game state to hiscores
        elseif (car==" ") then
                -- click outside keys   
        elseif (string.len(sPlayer)>8) then
                -- no more than 8 char 
        elseif (car=="@") then 
                -- @= space
                sPlayer= sPlayer.." "   -- add space char           
        else
                sPlayer= sPlayer..car   -- add the type char to player name
        end     
end

------------------------------------------------------
-- Get x-index of the key under the stylus
function getIndexX(x) 

        local i
        if (x<keyb_left+keys_x[1]) then 
                return 0
        end
        if (x>keyb_left+keys_x[#keys_x]) then 
                return 0
        end
        for i=1, #keys_x do
           if (x<keyb_left+keys_x[i]) then
                return i-1
           end
        end
        return 0
end

------------------------------------------------------
-- Get y-index of the key under the stylus
function getIndexY(x) 
 
        local i
        if (x<keyb_top+keys_y[1]) then 
                return 0
        end
        if (x>keyb_top+keys_y[#keys_y]) then 
                return 0
        end
        for i=1, #keys_y do
           if (x<keyb_top+keys_y[i]) then
                return i-1
           end
        end
        return 0
end
