sync on : set window on
sch = screen height()
scw = screen width()
set display mode screen width()/4,sch/4,32
set window size scw/4,sch/4
set window position 0,sch-(sch/4)
 
sync
 
load dll "user32.dll",1
curwin = call dll(1,"GetForegroundWindow")
 
dim recog(-1) as pos2
 
global size# as double float
 
type string2
 
    s1 as string
    s2 as string
 
endtype
 
type pos2
 
    x as double float
    y as double float
    d as double float
 
endtype
 
dim characters(25) as string2
dim final(25) as string
dim error(25) as integer
dim people(-1) as string2
dim learn(4,10) as string
dim unusable(10) as integer
 
if file exist("people.ini")
    open to read 1,"people.ini"
    do
        read string 1,name$
        read string 1,fnam$
        array insert at top people(0)
        people(0).s1 = name$
        people(0).s2 = fnam$
        inc numb
        print "[",numb,"] ",name$
        if file end(1) = 1 then exit
    loop
    sync
    do
        slct = val(inkey$())
        if slct <= numb and slct <> 0
            fnam$ = people(numb-slct).s2
            if file exist(fnam$)
                filename$ = fnam$
                gosub load_array
                exit
            else
                print "File not found!"
            endif
        endif
        sync
    loop
    close file 1
endif
 
do
 
    if ontop = 0
        call dll 1,"SetForegroundWindow",curwin
        ontop = 1
    endif
    if msc = 0 and tog = 0 and ontop = 1
        nextwin = call dll(1,"GetWindow",curwin,2)
        call dll 1,"SetForegroundWindow",nextwin
        ontop = 0
    endif
    inc tog
    if tog = 2 then tog = 0
 
    omc = msc
    msc = mouseclick()
    mmx# = mousemovex()
    mmy# = mousemovey()
    msx# = mousex()
    msy# = mousey()
    if msc = 3 and omc <> 3
        cls
        sx# = msx#
        sy# = msy#
        dx# = msx#
        dy# = msy#
    endif
    drag = 0
    if msc = 3 and omc = 3
        drag = 1
        line sx#,sy#,msx#,msy#
        sx# = msx#
        sy# = msy#
        array insert at bottom recog(0)
        max = array count(recog(0))
        recog(max).x = msx#-dx#
        recog(max).y = msy#-dy#
        recog(max).d = sqrt(((recog(max).x*100.0)^2.0)+((recog(max).y*100.0)^2.0))
        dx# = msx#
        dy# = msy#
    endif
    if msc <> 3 and omc = 3
        line sx#,sy#,msx#,msy#
        array insert at bottom recog(0)
        max = array count(recog(0))
        recog(max).x = msx#-dx#
        recog(max).y = msy#-dy#
        recog(max).d = sqrt(((recog(max).x*100.0)^2.0)+((recog(max).y*100.0)^2.0))
        size# = 0.0
        print " "
        for d = 0 to max
            inc size#,recog(d).d
`            print recog(d).d
        next d
`        print size#
        gosub get_letter
        empty array recog(0)
        text 0,0,drawnchar$
        if caps = 0
            text 30,0,"Caps off"
        else
            text 30,0,"Caps on"
        endif
        text 0,(sch/4)-15,"Backspace"
        text 0,(sch/4)-30,"Delete"
        text 150,(sch/4)-30,"  []"
        text 150,(sch/4)-15,"[][][]"
        text 90,(sch/4)-15,"[====]"
        hide window
        if caps
            call dll 1,"keybd_event",20,0x45,1,0,0
        endif
        for d = 1 to len(drawnchar$)
            call dll 1,"keybd_event",(asc(mid$(drawnchar$,d))-32),0x45,1,0,0
        next d
        if caps
            call dll 1,"keybd_event",20,0x45,1,0,0
        endif
        show window
        linen = 0
    endif
    if backspace
        hide window
        call dll 1,"keybd_event",8,0x45,1,0,0
        show window
    endif
    if delete
        hide window
        call dll 1,"keybd_event",46,0x45,1,0,0
        show window
    endif
    if space
        hide window
        call dll 1,"keybd_event",32,0x45,1,0,0
        show window
    endif
    if arrow > 0
        hide window
        call dll 1,"keybd_event",36+arrow,0x45,1,0,0
        show window
    endif
    sleep 10
    backspace = 0
    space = 0
    delete = 0
    arrow = 0
    if msc = 1 and omc = 0
        if msx# > 30 and msy# < 18
            cls
            text 0,(sch/4)-15,"Backspace"
            text 0,(sch/4)-30,"Delete"
            text 150,(sch/4)-30,"  []"
            text 150,(sch/4)-15,"[][][]"
            text 90,(sch/4)-15,"[====]"
            if caps = 0
                caps = 1
                text 30,0,"Caps on"
            else
                caps = 0
                text 30,0,"Caps off"
            endif
        else
            if msy# > (sch/4)-30
                if msx# < 150
                    if msy# > (sch/4)-15
                        if msx# < 90
                            backspace = 1
                        else
                            space = 1
                        endif
                    else
                        delete = 1
                    endif
                else
                    if msy# > (sch/4)-15
                        if msx# < 160
                            arrow = 1
                        else
                            if msx# < 170
                                arrow = 4
                            else
                                arrow = 3
                            endif
                        endif
                    else
                        arrow = 2
                    endif
                endif
            endif
        endif
    endif
    if inkey$() = "l" and msc <> 3
        gosub learn
    endif
 
    sync
 
loop
 
get_letter:
 
    max = array count(recog(0))
    for d = 0 to array count(characters(0))
        final(d) = ""
        dist# = 0
        xm = 0
        ym = 0
        nsects# = len(characters(d).s1)
        sectss# = size# / nsects#
`        print sectss#
`        print size#
        for e = 0 to max
            inc dist#,recog(e).d
            inc xm,recog(e).x
            inc ym,recog(e).y
            if dist# >= sectss#
                angle = wrapvalue(atanfull(xm,0-ym))
                a2 = angle
                inc angle,22.5
                angle = int(angle/45)
                if int(angle) = 8 then angle = 0
                final(d) = final(d) + str$(int(angle))
                xm = 0
                ym = 0
                dist# = 0
            endif
        next e
        angle = wrapvalue(atanfull(xm,0-ym))
        a2 = angle
        inc angle,22.5
        angle = int(angle/45)
        if int(angle) = 8 then angle = 0
        final(d) = final(d) + str$(int(angle))
        xm = 0
        ym = 0
        dist# = 0
    next d
    for d = 0 to array count(characters(0))
        error(d) = get_sim(final(d),characters(d).s1)
    next d
    old = 0
    for d = 1 to array count(characters(0))
        if error(d) < error(old)
            old = d
        endif
    next d
    drawnchar$ = characters(old).s2
 
return
 
get_letter2:
 
    max = array count(recog(0))
    final(learnd) = ""
    dist# = 0
    xm = 0
    ym = 0
    nsects# = learnn
    sectss# = size# / nsects#
    for e = 0 to max
        inc dist#,recog(e).d
        inc xm,recog(e).x
        inc ym,recog(e).y
        if dist# >= sectss#
            angle = wrapvalue(atanfull(xm,0-ym))
            a2 = angle
            inc angle,22.5
            angle = int(angle/45)
            if int(angle) = 8 then angle = 0
            final(learnd) = final(learnd) + str$(int(angle))
            xm = 0
            ym = 0
            dist# = 0
        endif
    next e
    angle = wrapvalue(atanfull(xm,0-ym))
    a2 = angle
    inc angle,22.5
    angle = int(angle/45)
    if int(angle) = 8 then angle = 0
    final(learnd) = final(learnd) + str$(int(angle))
    xm = 0
    ym = 0
    dist# = 0
 
return
 
enter_letters:
 
    characters(0).s1 = "53043"
    characters(0).s2 = "a"
 
    characters(1).s1 = "440246"
    characters(1).s2 = "b"
 
    characters(2).s1 = "65322"
    characters(2).s2 = "c"
 
    characters(3).s1 = "5200420"
    characters(3).s2 = "d"
 
return
 
learn:
 
    cls
 
    letteron = 0
 
    print "Draw each letter three times..."
    sync
    wait key
    cls
    print "Draw a ",chr$(letteron+97),":"
    do
 
        omc = msc
        msc = mouseclick()
        mmx# = mousemovex()
        mmy# = mousemovey()
        msx# = mousex()
        msy# = mousey()
        if msc = 3 and omc <> 3
            cls
            sx# = msx#
            sy# = msy#
            dx# = msx#
            dy# = msy#
        endif
        if msc = 3 and omc = 3
            line sx#,sy#,msx#,msy#
            sx# = msx#
            sy# = msy#
            array insert at bottom recog(0)
            max = array count(recog(0))
            recog(max).x = msx#-dx#
            recog(max).y = msy#-dy#
            recog(max).d = sqrt(((recog(max).x*100.0)^2.0)+((recog(max).y*100.0)^2.0))
            dx# = msx#
            dy# = msy#
        endif
        if msc <> 3 and omc = 3
            line sx#,sy#,msx#,msy#
            array insert at bottom recog(0)
            max = array count(recog(0))
            recog(max).x = msx#-dx#
            recog(max).y = msy#-dy#
            recog(max).d = sqrt(((recog(max).x*100.0)^2.0)+((recog(max).y*100.0)^2.0))
            size# = 0.0
            print " "
            for d = 0 to max
                inc size#,recog(d).d
            next d
            learnd = letteron
            for t = 5 to 15
                learnn = t
                gosub get_letter2
                learn(goon,t-5) = final(learnd)
            next t
            if goon = 2
                for t = 5 to 15
                    unusable(t-5) = -100
                    for e = 1 to 4
                        err = get_sim(learn(0,t-5),learn(goon,t-5))
                        if err > unusable(t-5) then unusable(t-5) = err
                    next e
                next t
                old = 0
                for t = 1 to 10
                    if unusable(t) < unusable(old) then old = t
                next t
                characters(letteron).s1 = learn(0,old)
                characters(letteron).s2 = chr$(letteron+97)
            endif
            inc goon
            if goon = 3
                goon = 0
                inc letteron
                if letteron = 26 then exit
            endif
            print "Draw a ",chr$(letteron+97),":"
            empty array recog(0)
            linen = 0
        endif
 
        sync
    loop
 
    if file exist("people.ini")
        if fnam$ <> ""
            if file exist(fnam$) then delete file fnam$
            filename$ = fnam$
            gosub save_array
        endif
    else
        open to write 1,"people.ini"
        write string 1,"Default"
        write string 1,"Default.ini"
        close file 1
        filename$ = "Default.ini"
        gosub save_array
    endif
 
return
 
function get_sim(str1$,str2$)
 
    value = 0
    if str1$ = str2$ then value = -1000
    for d = 1 to len(str1$)
        n1 = val(mid$(str1$,d))
        n2 = val(mid$(str2$,d))
        n3 = abs(n1-n2)
        if n3 = 0
            inc value,1
        else
            if n3 < 2 or n3 > 6
                inc value,3
            else
                if n3 > 2 or n3 < 6
                    inc value,6
                else
                    if n3 > 3 and n3 < 5 then inc value,10
                endif
            endif
        endif
    next d
    dec value,len(str1$)*2
 
`    print str1$," ",str2$," ",value
 
endfunction value
 
save_array:
 
    open to write 2,filename$
    for sve = 0 to array count(characters(0))
        write string 2,characters(sve).s1
        write string 2,characters(sve).s2
    next sve
    close file 2
 
return
 
load_array:
 
    open to read 2,filename$
    for sve = 0 to array count(characters(0))
        read string 2,characters(sve).s1
        read string 2,characters(sve).s2
    next sve
    close file 2
 
return