fond1 = Image.load("fond1.png")
fond2 = Image.load("fond2.png")
souris = Image.load("curseur.png")
outil = Image.load("outil.png")
cerclerayon = Image.load("cercle.png")
rectangledim = Image.load("rectangle.png")
lettredim = Image.load("lettre.png")
lignedim = Image.load("ligne.png")
feuille1 = Image.createEmpty(310,255)
feuille2 = Image.createEmpty(310,255)
feuille3 = Image.createEmpty(310,255)
coul = Image.createEmpty(24,24)
font = Font.load("police.ttf")
down = Image.load("downbar.png")
up = Image.load("upbar.png")
batt = Image.load("batt.png")
select = Image.load("select.png")

feuille=feuille1

saveimg = Image.load("save.png")
openimg = Image.load("open.png")
img1 = Image.load("1.png")
img2 = Image.load("2.png")
img3 = Image.load("3.png")
img4 = Image.load("4.png")

dossierimg=img1
doss=1
dossier="ms0:"
number=1

x=419
y=60

fond=fond1
couleur=Color.new(0,0,0)
rayon=30
largeur = 40
longueur = 20
rect=1
lt2=1

long=30
pos=1

h=20

dess=0
t=9
save=0


point=0

lettre="a"
dx=360+1
dy=159+2

cd=1

while true do
System.usbDiskModeActivate()

screen:clear()
screen:blit(0,0,fond)

--pad
pad = Controls.read()

if pad:up() then y=y-1 end
if pad:down() then y=y+1 end
if pad:left() then x=x-1 end
if pad:right() then x=x+1 end

if pad:analogY() < -64 then
y = y-5
end
if pad:analogY() > 64 then
y = y+5
end
if pad:analogX() < -64 then
x = x-5
end
if pad:analogX() > 64 then
x = x+5
end

if x>50 and x<360 and y<=0 then upbar=1 end
if x<50 or x>360 or y>15 then upbar=0 end


--couleur
if save==0 then
if x>=360 and y<=120 and x<480 and y>0 and pad:cross() then
couleur = fond:pixel(x,y) end
if x>=360 and y<=160 and x<480 and y>130 and pad:cross() then
couleur = fond:pixel(x,y) end
end

--épaisseur
if crayon==1 then
if x>=360 and y>=160 and y<=180 and pad:cross() then t=1 end
if x>=360 and y>=180 and y<=198 and pad:cross() then t=3 end
if x>=360 and y>=198 and y<=217 and pad:cross() then t=5 end
if x>=360 and y>=217 and y<=235 and pad:cross() then t=9 end
if x>=360 and y>=235 and y<=254 and pad:cross() then t=13 end
if x>=360 and y>=254 and y<=272 and pad:cross() then t=19 end
end


--outil
screen:blit(0,0,outil)
coul:clear(couleur)
screen:blit(13,242,coul)


if x>=11 and x<=38 and y>=2 and y<=24 and pad:cross() then crayon=1 lt=0 ligne=0 rectangle=0 pxcopie=0 cercle=0
elseif x>=11 and x<=38 and y>=40 and y<=62 and pad:cross() then ligne=1 lt=0 crayon=0 rectangle=0 pxcopie=0 cercle=0
elseif x>=11 and x<=38 and y>=80 and y<=102 and pad:cross() then rectangle=1 lt=0 ligne=0 crayon=0 pxcopie=0 cercle=0
elseif x>=11 and x<=38 and y>=120 and y<=142 and pad:cross() then cercle=1 lt=0 ligne=0 rectangle=0 crayon=0 pxcopie=0
elseif x>=11 and x<=38 and y>=160 and y<=182 and pad:cross() then pxcopie=1 lt=0 ligne=0 rectangle=0 cercle=0 crayon=0
elseif x>=11 and x<=38 and y>=200 and y<=222 and pad:cross() then lt=1 crayon=0 ligne=0 rectangle=0 pxcopie=0 cercle=0 end



--ligne

if ligne == 1 then

screen:blit(360,160,lignedim)


if point==0 and upbar==0 and x>50 and x<360 and y<255 and pad:cross() and not oldpad:cross() then 
px1=x-50
py1=y
point=1
end
if point==1 and upbar==0 and x>50 and x<360 and y<255 and pad:circle() and not oldpad:circle() then 
px2=x-50
py2=y
feuille:drawLine(px1, py1, px2, py2, couleur)
point=0
end
end



--rectangle

function drawRectangle(x,y,longueur,largeur)
for j=x,x+largeur do
feuille:pixel(j, y, couleur)
end

for p=x,x+largeur do
feuille:pixel(p, y+longueur, couleur)
end

for n=y,y+longueur do
feuille:pixel(x,n, couleur)
end

for q=y,y+longueur do
feuille:pixel(x+largeur,q, couleur)
end end


if rectangle == 1  then
screen:blit(360,160,rectangledim)
if x>=385 and x<=395 and y>=235 and y<=245 and pad:cross() and not oldpad:cross() then longueur=longueur+1 end
if x>=445 and x<=465 and y>=235 and y<=245 and pad:cross() and not oldpad:cross() then largeur=largeur+1 end
if x>=385 and x<=395 and y>=262 and y<=282 and pad:cross() and not oldpad:cross() then longueur=longueur-1 end
if x>=445 and x<=465 and y>=262 and y<=282 and pad:cross() and not oldpad:cross() then largeur=largeur-1 end
if longueur>=136 then longueur=136 end
if largeur>=155 then largeur=155 end
if longueur<=0 then longueur=0 end
if largeur<=0 then largeur=0 end
screen:print(383,252,longueur)
screen:print(443,252,largeur)

if x>=390 and x<=410 and y>=190 and y<=200 and pad:cross() then rect=1 end
if x>=430 and x<=449 and y>=190 and y<=200 and pad:cross() then rect=2 end

if rect==1 and upbar==0 then
if x>50 and x<360-largeur and y>0 and y<255-longueur and pad:cross() and not oldpad:cross() then

feuille:fillRect(x-50,y,largeur,longueur,couleur)

end end 

if rect==2 and upbar==0 then
if x>50 and x<360-largeur and y>0 and y<255-longueur and pad:cross() and not oldpad:cross() then
drawRectangle(x-50,y,longueur,largeur)
end end

end



--cercle
function drawCircle(x, y, rayon, color)
pi = math.pi
step = 2/(rayon*rayon*0.5)
for i=0,2, step do
a = x+rayon*math.cos(pi*i)
c = y+rayon*math.sin(pi*i)
if a>=0 and a<360 and c>=0 and c<272 then
feuille:pixel(a, c, couleur)
end
end
end

if cercle==1 then
screen:blit(360,160,lignedim)
if cd==1 then
if x>=50 and x<360 and y<255 and y>14 and pad:cross() and not oldpad:cross() then
p1=x-50
p2=y
cd=2
end
end

if cd==2
then
if x>=50 and x<360 and pad:circle() and not oldpad:circle() then

rayon=((x-50-p1)*(x-50-p1)+(y-p2)*(y-p2))^(1/2)

drawCircle(p1, p2, rayon, couleur)

cd=1 
end
end
end

--pxcopie
if pxcopie==1 then
 if x>50 and x<360 and y>20 and y<252 and pad:cross() then
couleur = feuille:pixel(x-50,y) end
if x>360 and y<255 and x<480 and y>130 and pad:cross() then
couleur = fond:pixel(x,y) end
fond=fond2
else fond=fond1
end


--lettre
if lt==1 then


if x>=360 and x<=376 and y>=159 and y<=174 and pad:cross() then 
lettre="a"
dx=360+1
dy=159+2
 end
if x>=377 and x<=393 and y>=159 and y<=174 and pad:cross() then 
lettre="b"
dx=377+1
dy=159+2
 end
if x>=394 and x<=410 and y>=159 and y<=174 and pad:cross() then 
 lettre="c"
dx=394+1
dy=159+2
 end
if x>=411 and x<=427 and y>=159 and y<=174 and pad:cross() then 
 lettre="d"
dx=411+1
dy=159+2
 end
if x>=428 and x<=444 and y>=159 and y<=174 and pad:cross() then 
 lettre="e"
dx=428+1
dy=159+2
 end
if x>=445 and x<=461 and y>=159 and y<=174 and pad:cross() then 
 lettre="f"
dx=445+1
dy=159+2
 end
if x>=462 and x<=479 and y>=159 and y<=174 and pad:cross() then 
 lettre="g"
dx=462+1
dy=159+2
 end
if x>=360 and x<=376 and y>=175 and y<=190 and pad:cross() then 
 lettre="h"
dx=360+1
dy=175+2
 end
if x>=377 and x<=393 and y>=175 and y<=190 and pad:cross() then 
 lettre="i"
dx=377+1
dy=175+2
 end
if x>=394 and x<=410 and y>=175 and y<=190 and pad:cross() then 
 lettre="j"
dx=394+1
dy=175+2
 end
if x>=411 and x<=427 and y>=175 and y<=190 and pad:cross() then 
 lettre="k"
dx=411+1
dy=175+2
 end
if x>=428 and x<=444 and y>=175 and y<=190 and pad:cross() then 
 lettre="l"
dx=428+1
dy=175+2
 end
if x>=445 and x<=461 and y>=175 and y<=190 and pad:cross() then 
 lettre="m"
dx=445+1
dy=175+2
 end
if x>=462 and x<=479 and y>=175 and y<=190 and pad:cross() then 
 lettre="n"
dx=462+1
dy=175+2
 end
if x>=360 and x<=376 and y>=191 and y<=206 and pad:cross() then 
 lettre="o"
dx=360+1
dy=191+2
 end
if x>=377 and x<=393 and y>=191 and y<=206 and pad:cross() then 
 lettre="p"
dx=377+1
dy=191+2
 end
if x>=394 and x<=410 and y>=191 and y<=206 and pad:cross() then 
 lettre="q"
dx=394+1
dy=191+2
 end
if x>=411 and x<=427 and y>=191 and y<=206 and pad:cross() then 
 lettre="r"
dx=411+1
dy=191+2
 end
if x>=428 and x<=444 and y>=191 and y<=206 and pad:cross() then 
 lettre="s"
dx=428+1
dy=191+2
 end
if x>=445 and x<=461 and y>=191 and y<=206 and pad:cross() then 
 lettre="t"
dx=445+1
dy=191+2
 end
if x>=462 and x<=479 and y>=191 and y<=206 and pad:cross() then 
 lettre="u"
dx=462+1
dy=191+2
 end
if x>=360 and x<=376 and y>=207 and y<=222 and pad:cross() then 
 lettre="v"
dx=360+1
dy=207+2
 end
if x>=377 and x<=393 and y>=207 and y<=222 and pad:cross() then 
 lettre="w"
dx=377+1
dy=207+2
 end
if x>=394 and x<=410 and y>=207 and y<=222 and pad:cross() then 
 lettre="x"
dx=394+1
dy=207+2
 end
if x>=411 and x<=427 and y>=207 and y<=222 and pad:cross() then 
 lettre="y"
dx=411+1
dy=207+2
 end
if x>=428 and x<=444 and y>=207 and y<=222 and pad:cross() then 
 lettre="z"
dx=428+1
dy=207+2
 end

screen:blit(360,160,lettredim)
screen:blit(dx,dy,select)
font:setPixelSizes(20,20)

if x<=360 and x>=50 and y<255 and upbar==0 and pad:cross() and not oldpad:cross() then
feuille:fontPrint(font,x-50,y,lettre,couleur) end
end

--dessin (crayon)
dessin=Image.createEmpty(t,t)
dessin:clear(couleur)

screen:blit(50,0,feuille)
if crayon==1 and upbar==0 and x<=360 and x>=50 and y<255 and pad:cross() then
feuille:blit(x-50+1-t/2,y+1-t/2,dessin)
end


--new
if x>=210 and x<=250 and y>=257 and y<=270 and pad:cross() then feuille:clear() end


--autre



--upbar
if System.powerGetBatteryLifePercent()==0 then g=0 end
if System.powerGetBatteryLifePercent()>0 then g=5 end
if System.powerGetBatteryLifePercent()>=10 then g=10 end
if System.powerGetBatteryLifePercent()>=20 then g=15 end
if System.powerGetBatteryLifePercent()>=30 then g=20 end
if System.powerGetBatteryLifePercent()>=40 then g=25 end
if System.powerGetBatteryLifePercent()>=50 then g=30 end
if System.powerGetBatteryLifePercent()>=60 then g=35 end
if System.powerGetBatteryLifePercent()>=70 then g=40 end
if System.powerGetBatteryLifePercent()>=80 then g=45 end
if System.powerGetBatteryLifePercent()>=90 then g=50 end

powerbar = Image.createEmpty(g,8)
powerbar:clear(couleur)

screen:blit(50,0,up)
screen:blit(190,3,powerbar)
screen:blit(189,2,batt)
screen:print(150,5,System.powerGetBatteryLifePercent().."%")
screen:print(60,5,"x"..x)
screen:print(100,5,"y"..y)

if upbar==1 then
if x>=320 and x<=350 and y<=12 and pad:cross() then System.Quit() end

end

--calque
screen:blit(50,255,down)
if x>=55 and x<=100 and y>=257 and pad:cross() then 
feuille=feuille1 end
if x>=101 and x<=146 and y>=257 and pad:cross() then 
feuille=feuille2 end
if x>=147 and x<=192 and y>=257 and pad:cross() then 
feuille=feuille3 end


--save
if x>=310 and x<=350 and y>=257 and pad:cross() then 
save=1
end

if save==1 then
crayon=0 lt=0 ligne=0 rectangle=0 pxcopie=0 cercle=0
screen:blit(0,0,saveimg)
screen:blit(135,98,dossierimg)
if x>=348 and x<=369 and y>=50+48 and y<=50+69 and pad:cross() and not oldpad:cross() then doss=doss+1 end
if doss>4 then doss=1 end

if doss==1 then 
dossierimg=img1
dossier="ms0:"
elseif doss==2 then 
dossierimg=img2
dossier="ms0:/PSP"
elseif doss==3 then 
dossierimg=img3 
dossier="ms0:/PICTURE"
elseif doss==4 then 
dossierimg=img4 
dossier="ms0:/PSP/PHOTO"
end

if x>=357 and x<=377 and y>=66 and y<=84 and pad:cross() and not oldpad:cross() then number=number+1 end

screen:print(140,74,"image"..number)

if x>=400 and x<=460 and y>=184 and y<=202 and pad:cross() then
feuille:save(dossier.."/image"..number..".png") 
save=0
end
if x>=330 and x<=390 and y>=184 and y<=202 and pad:cross() then
save=0
end

end

--open
if x>=260 and x<=300 and y>=257 and pad:cross() then 
open=1
end

if open==1 then
crayon=0 lt=0 ligne=0 rectangle=0 pxcopie=0 cercle=0
screen:blit(0,0,openimg)
screen:blit(135,98,dossierimg)
if x>=348 and x<=369 and y>=50+48 and y<=50+69 and pad:cross() and not oldpad:cross() then doss=doss+1 end
if doss>4 then doss=1 end

if doss==1 then 
dossierimg=img1
dossier="ms0:"
elseif doss==2 then 
dossierimg=img2
dossier="ms0:/PSP"
elseif doss==3 then 
dossierimg=img3 
dossier="ms0:/PICTURE"
elseif doss==4 then 
dossierimg=img4 
dossier="ms0:/PSP/PHOTO"
end

if x>=357 and x<=377 and y>=66 and y<=84 and pad:cross() and not oldpad:cross() then number=number+1 end

screen:print(140,74,"image"..number)

if x>=400 and x<=460 and y>=184 and y<=202 and pad:cross() then
feuille=Image.load(dossier.."/image"..number..".png") 
open=0
end
if x>=330 and x<=390 and y>=184 and y<=202 and pad:cross() then
open=0
end

end


--reste
if x>=480 then x=480 end
if x<=0 then x=0 end
if y>=272 then y=272 end
if y<=0 then y=0 end

screen:blit(x-7,y-7,souris)

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