Author Topic: Selecting music files and create a playlist  (Read 1480 times)

MrTAToad

  • Guest
This code (which I'll be using for my current game) allows the user to select tunes to create a playlist.  It can be simply modified to allow the user to select pictures...

Code: (glbasic) [Select]
// --------------------------------- //
// Project: SelectFiles
// Start: Friday, September 11, 2009
// IDE Version: 7.104

TYPE tListOfFiles
selected%
file$
ENDTYPE

TYPE tSelectFiles
listOfFiles[] AS tListOfFiles
playList$[]
extList$[]
title$
fileList$
path$
button_back$
button_select$
button_deSelect$
button_rescan$
button_action$
button_finished$
programDir$
playListFile$
sectionName$
currentLine%
ENDTYPE

GLOBAL selectFiles AS tSelectFiles

FUNCTION initialiseSelectFiles%:ext$,title$,programDir$,dirName$,sectionName$,actionDesc$
LOCAL path$

DIM selectFiles.extList$[0]
DIM selectFiles.playList$[0]

IF SPLITSTR(ext$,selectFiles.extList$[],"|")=0
RETURN FALSE
ENDIF

// Make sure user's directory exists
selectFiles.path$=PLATFORMINFO$("DOCUMENTS")+"/"+programDir$ // Where the INI file will be kept
IF DOESDIREXIST(selectFiles.path$)=FALSE
CREATEDIR(selectFiles.path$)
ENDIF

selectFiles.programDir$=selectFiles.path$+"/"+dirName$ // Where user's media will be kept
IF DOESDIREXIST(selectFiles.programDir$)=FALSE
CREATEDIR(selectFiles.programDir$)
ENDIF

selectFiles.title$=title$
selectFiles.fileList$="fileList"
selectFiles.button_back$="BACK"
selectFiles.button_select$="SELECT"
selectFiles.button_deSelect$="DE-SELECT"
selectFiles.button_rescan$="RESCAN"
selectFiles.button_action$=actionDesc$
selectFiles.button_finished$="FINISHED"
selectFiles.playListFile$=selectFiles.path$+"/PLAYLIST.INI"
selectFiles.sectionName$=sectionName$
selectFiles.currentLine%=-1
ENDFUNCTION

FUNCTION processSelectFiles%:
LOCAL screenWidth%,screenHeight%

GETSCREENSIZE screenWidth%,screenHeight%
DDgui_pushdialog(0,0,screenWidth%,screenHeight%)
DDgui_set("", "TEXT", selectFiles.title$)
DDgui_widget("id1","SEL    File",0,0)
DDgui_spacer(1,8)

// Read in the playlist
readList()

scanForFiles()
rebuildList(screenWidth%,screenHeight%)
DDgui_button(selectFiles.button_back$,selectFiles.button_back$,0,0)
DDgui_button(selectFiles.button_select$,selectFiles.button_select$,0,0)
DDgui_button(selectFiles.button_deSelect$,selectFiles.button_deSelect$,0,0)
DDgui_button(selectFiles.button_rescan$,selectFiles.button_rescan$,0,0)
DDgui_button(selectFiles.button_action$,selectFiles.button_action$,0,0)
DDgui_button(selectFiles.button_finished$,selectFiles.button_finished$,0,0)

WHILE TRUE
DDgui_show(FALSE)
SHOWSCREEN

// Now we process the buttons
// PLAY
IF DDgui_get(selectFiles.button_action$,"CLICKED")
processAction(DDgui_get(selectFiles.fileList$,"SELECT"))
ELSEIF  DDgui_get(selectFiles.button_select$,"CLICKED")
IF selectTune(DDgui_get(selectFiles.fileList$,"SELECT"))=TRUE
// Rebuild list
rebuildList(screenWidth%,screenHeight%)
ENDIF
ELSEIF DDgui_get(selectFiles.button_deSelect$,"CLICKED")
IF deSelectTune(DDgui_get(selectFiles.fileList$,"SELECT"))=TRUE
// Rebuild list
rebuildList(screenWidth%,screenHeight%)
ENDIF
ELSEIF DDgui_get(selectFiles.button_rescan$,"CLICKED")
scanForFiles()
rebuildList(screenWidth%,screenHeight%)
ELSEIF DDgui_get(selectFiles.button_finished$,"CLICKED")
saveList()
RETURN TRUE
ENDIF
WEND
ENDFUNCTION

FUNCTION rebuildList:screenWidth%,screenHeight%
DDgui_list(selectFiles.fileList$,createListFromFiles$(),screenWidth%-11,screenHeight%-70)
ENDFUNCTION

FUNCTION scanForFiles%:
LOCAL currentDir$
LOCAL count%
LOCAL numFiles%
LOCAL ext$,ext2$
LOCAL tempList$[]
LOCAL file AS tListOfFiles

DIM selectFiles.listOfFiles[0]

currentDir$=GETCURRENTDIR$()
SETCURRENTDIR(selectFiles.programDir$)

FOREACH ext$ IN selectFiles.extList$[]
DIM tempList$[0]

count%=GETFILELIST(ext$,tempList$[])
numFiles%=MOD(count%,0x10000)

DEBUG "Files : "+numFiles%+"\n"
IF numFiles%>0
// We have a list of files, so add them to the list
FOREACH ext2$ IN tempList$[]
IF ext2$<>"." AND ext2$<>".."
file.selected%=checkInPlayList(ext2$)
file.file$=ext2$
DIMPUSH selectFiles.listOfFiles[],file
ENDIF
NEXT
ENDIF
NEXT

// Put directory back
SETCURRENTDIR(currentDir$)
SORTARRAY selectFiles.listOfFiles[],ADDRESSOF(sort)
RETURN BOUNDS(selectFiles.listOfFiles[],0)
ENDFUNCTION

FUNCTION sort:a AS tListOfFiles,b AS tListOfFiles
IF b.file$<a.file$
RETURN -1
ELSE
IF b.file$>a.file$
RETURN -1
ELSE
RETURN 0
ENDIF
ENDIF
ENDFUNCTION

FUNCTION createListFromFiles$:
LOCAL loop AS tListOfFiles
LOCAL temp$

temp$=""
FOREACH loop IN selectFiles.listOfFiles[]
IF loop.selected%=TRUE
temp$=temp$+"YES"
ELSE
temp$=temp$+"NO "
ENDIF

temp$=temp$+"   "+loop.file$+"|"
NEXT

RETURN MID$(temp$,0,LEN(temp$)-1)
ENDFUNCTION

FUNCTION processAction:line%
LOCAL file$
LOCAL play%

file$=selectFiles.programDir$+"/"+selectFiles.listOfFiles[line%].file$
IF line%<>selectFiles.currentLine%
// Different line, so play automatically
play%=TRUE
selectFiles.currentLine%=line%
ELSE
IF ISMUSICPLAYING()=FALSE
play%=TRUE
ELSE
play%=FALSE
ENDIF
ENDIF

IF play%=TRUE
IF DOESFILEEXIST(file$)=TRUE
PLAYMUSIC file$,FALSE
ENDIF
ELSE
STOPMUSIC
ENDIF
ENDFUNCTION

FUNCTION selectTune%:line%
selectFiles.listOfFiles[line%].selected%=TRUE
RETURN TRUE
ENDFUNCTION

FUNCTION deSelectTune%:line%
selectFiles.listOfFiles[line%].selected%=FALSE
RETURN TRUE
ENDFUNCTION

FUNCTION saveList%:
LOCAL loop AS tListOfFiles
LOCAL count%

KILLFILE selectFiles.playListFile$
DIM selectFiles.playList$[0]

INIOPEN selectFiles.playListFile$
count%=0
FOREACH loop IN selectFiles.listOfFiles[]
IF loop.selected%=TRUE
INIPUT selectFiles.sectionName$,count%,loop.file$

// And add to the playlist
DIMPUSH selectFiles.playList$[],loop.file$

INC count%
ENDIF
NEXT
INIPUT selectFiles.sectionName$,"COUNT",count%
INIOPEN ""
ENDFUNCTION

FUNCTION readList%:
LOCAL count%
LOCAL loop%
LOCAL temp$

DIM selectFiles.playList$[0]

INIOPEN selectFiles.playListFile$

count%=INIGET$(selectFiles.sectionName$,"COUNT")
IF count%>0
FOR loop%=0 TO count%-1
temp$=INIGET$(selectFiles.sectionName$,loop%)
DIMPUSH selectFiles.playList$[],temp$
NEXT
INIOPEN ""
ENDIF

RETURN BOUNDS(selectFiles.playList$[],0)
ENDFUNCTION

FUNCTION checkInPlayList%:name$
LOCAL loop$

FOREACH loop$ IN selectFiles.playList$[]
IF loop$=name$
RETURN TRUE
ENDIF
NEXT

RETURN FALSE
ENDFUNCTION