Partie = 1
Saut = 20
selector = 1
PosY = 60
Espace = 15
PosYDep = 60
PosX = 10
x = 0
y = 0
System.currentDirectory("ms0:/")
Fichiers = System.listDirectory()
endroit = "explorateur"
bleu = Color.new(255,255,255)

while true do
 pad = Controls.read()
 screen:clear()

 if endroit == "explorateur" then
  Android:Barre()
  for i = Partie, Partie + Saut - 1 do
   screen:fontPrint(font,PosX,PosY,Fichiers[i].name,blanc)
   PosY = PosY + Espace
   if table.getn(Fichiers) == i then break end
  end

  PosY = PosYDep

  screen:fontPrint(font,2,(selector-Partie)*15+PosYDep,">",blanc)
  

  if pad:down() and not oldpad:down() and selector < table.getn(Fichiers) then
   selector = selector + 1
  end
  if pad:up() and not oldpad:up() and selector > 1 then
   selector = selector - 1
  end

  if selector > Partie + Saut - 1 and Partie + Saut - 1 ~= table.getn(Fichiers) then
   Partie = Partie + Saut
  elseif selector < Partie then
   Partie = Partie - Saut
  end

  if pad:triangle() and oldpad:triangle() ~= pad:triangle() then
   oldpad = pad
   selector = 1
   Partie = 1
   if System.currentDirectory() ~= "ms0:/" then
    selector = 1
    System.currentDirectory("..")
    Fichiers = System.listDirectory()
   end
  end

  if pad:cross() and oldpad:cross() then
   if Fichiers[selector].directory then
    System.currentDirectory(Fichiers[selector].name)
    Fichiers = System.listDirectory()
    Partie = 1
    select = 1
   else
    extension = string.upper(string.sub(Fichiers[selector].name,-3))
    if extension == "MP3" then
     Mp3me.load(Fichiers[selector].name)
     Mp3me.play()
     endroit = "Music"
    end
    if extension == "OGG" then
     Ogg.load(Fichiers[selector].name)
     Ogg.play()
     endroit = "Music"
    end
    if extension == "JPG" or extension == "PNG" then
     pic = Image.load(Fichiers[selector].name)
     endroit = "image"
    end
    if extension == "LUA" then
     if question("Lancer le script lua?") == 1 then
      dofile(Fichiers[selector].name)
     end
    end
    if extension == "PBP" then
     if question("Lancer le PBP?") == 1 then
      System.runeboot(Fichiers[selector].name)
     end
    end
   end
  end
 end

 if endroit == "Music" then
  Android:Barre()
  screen:fontPrint(font,10,185,"Lecture en cours :"..Fichiers[selector].name, Color.new(255,0,0))
  if extension == "MP3" then
   screen:fontPrint(font,30,40,"Titre :"..Mp3me.title(), blanc)
   screen:fontPrint(font,30,55,"Artiste : "..Mp3me.artist(), blanc)
   screen:fontPrint(font,30,70,"Annee : "..Mp3me.year(), blanc)
   screen:fontPrint(font,30,85,"Album : "..Mp3me.album(), blanc)
   screen:fontPrint(font,30,100,"Piste : "..Mp3me.trackNumber(), blanc)
   screen:fontPrint(font,30,115,"Duree musique : "..Mp3me.songTime(), blanc)
   screen:drawLine(0,270,Mp3me.percent()/100*480,270,Color.new(255,0,0))
   screen:drawLine(0,271,Mp3me.percent()/100*480,271,Color.new(255,150,150))
  end
  if extension == "OGG" then
   screen:fontPrint(font,30,40,"Titre :"..Ogg.title(), blanc)
   screen:fontPrint(font,30,55,"Artiste : "..Ogg.artist(), blanc)
   screen:fontPrint(font,30,70,"Annee : "..Ogg.year(), blanc)
   screen:fontPrint(font,30,85,"Album : "..Ogg.album(), blanc)
   screen:fontPrint(font,30,100,"Piste : "..Ogg.trackNumber(), blanc)
   screen:fontPrint(font,30,115,"Duree musique : "..Ogg.songTime(), blanc)
   screen:drawLine(0,270,Ogg.percent()/100*480,270,Color.new(255,0,0))
   screen:drawLine(0,271,Ogg.percent()/100*480,271,Color.new(255,150,150))
  end
  screen:fontPrint(font,30,140,"X : Pause", bleu)
  screen:fontPrint(font,30,155,"O : Retourner a l'explorateur", bleu)
  if Ogg.percent()==100 or Mp3me.percent()==100 then
   endroit = "explorateur"
  elseif pad:cross() and not oldpad:cross() then
   Mp3me.pause()
   Ogg.pause()
  elseif pad:circle() and not oldpad:circle() then
   Mp3me.stop()
   Ogg.stop()
   endroit = "explorateur"
  end
 end

 if endroit == "image" then
  screen:fillRect(0,0,480,272,Color.new(100,100,100))
  if math.abs(pad:analogY()) > 15 then
   y = y + pad:analogY() / 15
  end
  if math.abs(pad:analogX()) > 15 then
   x = x + pad:analogX() / 15
  end

  if x > 470 then
   x = 470
  end
  if y > 262 then
   y = 262
  end
  if x < 480 - pic:width() then
   x = 480 - pic:width()
  end
  if y < 272 - pic:height() then
   y = 272 - pic:height()
  end
  screen:blit(x,y,pic)
  screen:fontPrint(font,10,242,pic:width().."pix*"..pic:height().."pix",bleu)
  screen:fontPrint(font,10,257,"O : Retourner a l'explorateur", bleu)
  if pad:circle() and not oldpad:circle() then
   endroit = "explorateur"
  end
 end

 if pad:square() and not oldpad:square() then
  selector = 1
  System.currentDirectory("ms0:/PSP/GAME/ANDROID/")
  Android:Quitter()
 end

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