REM ***************************************
REM Author: Phaelax
REM Challenge: Furious File Scanning
REM ***************************************
 
set display mode 1024,768,32
 
dim viewPointer(2)   : `0=viewport offset, 1=scroll amount, 2=timestamp
dim WIN_X(1)         : `File browser window location, 0=location,1=mouse offset
dim WIN_Y(1)         : `File browser window location, 0=location,1=mouse offset
dim WIN_WIDTH(1)     : `Width of file window, 0=width,1=mouse offset
dim WIN_HEIGHT(1)    : `height of file window, excluding title and status bars,0=width,1=mouse offset
dim FLAGS(4)         : `1=mouse, 2=titleMove, 3=scrollbarMove, 4=resize
dim THUMB(1)         : `scrollbar thumb position (local position)
dim MOUSE(3)         : `0=mouse click count, 1=timestamp of last click, 2=delay allowed for clicks
dim _FData(2)        : `0=file count, 1=file view offset, 2=current selected file, 3=click flag
dim TEMP#(0)         : `whatever temp value I need to hold globally
dim files$(0,3)      : `1=file name, 2=file size, 3=folder flag
dim IMAGE(1)         : `0=original width, 1=original height
dim imgExt$(3)       : `image extensions
dim sfxExt$(3)       : `sound extensions
dim musicExt$(3)     : `music extensions
dim MZFix(1)         : `fix for the mousemovez command
dim ICON_FOLDER(0)   : `image number for folder icon
dim ICON_SFX(2)      : `image number for play/pause button icons,0=play,1=pause,2=current
dim FILE_TYPE(0)     : `1=image, 2=sfx, 3=music, 4=video
dim volSlider(1)     : `volume slider, 0=drag flag, 1=value %
dim bars(10)         : `1-10 spectrum bar values, 0=timestamp for update
dim mouseHover(2)    : `0=timestamp, 1=hover delay before showing tooltips, 2=file index to show
_FData(0) = 0        : `file count
_FData(1) = 0        : `file view offset
_FData(2) = -1       : `currently selected file
THUMB(0) = 0         : `scrollbar thumb position (local position)
MOUSE(2) = 300       : `delay allowed for double clicks (milliseconds)
WIN_X(0,0) = 200     : `File browser window location
WIN_Y(0,0) = 100     : `File browser window location
WIN_WIDTH(0) = 400   : `Width of file window
WIN_HEIGHT(0) = 160  : `height of file window, excluding title and status bars
ICON_FOLDER(0)=1     : `image number for folder icon
ICON_SFX(0)=2        : `image number for play button icon
ICON_SFX(1)=3        : `image number for pause button icon
mouseHover(1)=1000   : `delay(ms) before tooltips appear
 
rem supported image extensions
imgExt$(1) = ".jpg" : imgExt$(2) = ".png" : imgExt$(3) = ".bmp"
rem supported sound extensions
sfxExt$(1) = ".wav" : sfxExt$(2) = ".mp3" : sfxExt$(3) = ".wma"
rem supported music extensions
musicExt$(1) = ".xm" : musicExt$(2) = ".mid" : musicExt$(3) = ".mp3"
 
sprite 1,1,1,99      : `create default sprite for image preview
 
gosub getImages
 
 
rem default directory
setDir("C:\Documents and Settings\Phaelax\My Documents")
`setDir("G:\")
 
lastFile$ = ""
 
sync on
 
set text font "Arial", 1
set text size 14
 
DO
   cls
   selectedFileIndex = drawFileList(WIN_X(0),WIN_Y(0),WIN_WIDTH(0),WIN_HEIGHT(0),_FData(1))
   if selectedFileIndex > -1 then lastFile$ = files$(selectedFileIndex,1)
 
   ink rgb(255,255,255),0
   set cursor 1,1
   print mousemovey()
 
   sync
LOOP
 
 
REM **************************************************
REM Set the current directory
REM **************************************************
function setDir(path$)
   rem clear current file list
   r = _FData(0)
   undim files$(r,3)
 
   rem set current path and get files
   set dir path$
   perform checklist for files
 
   _FData(0) = checklist quantity()+1 : r = _FData(0)
   _FData(1) = 0
   dim files$(r,3)
 
   files$(1,1) = "My Computer"
   files$(1,2) = ""
   files$(1,3) = "2"
 
   rem fill array with file names and dates
   find first
   for i = 2 to _FData(0)
      files$(i,1) = get file name$()
      files$(i,2) = get file date$()
      files$(i,3) = str$(get file type())
      find next
   next i
   _FData(2) = -1
 
endfunction
 
REM **************************************************
REM Set the current directory to display drive listing
REM **************************************************
function setDirDrives()
   rem clear current file list
   r = _FData(0)
   undim files$(r,3)
 
   rem get list of available drives
   perform checklist for drives
 
   _FData(0) = checklist quantity() : r = _FData(0)
   _FData(1) = 0
   dim files$(r,3)
 
   rem fill array with drive names
   find first
   for i = 1 to _FData(0)
      files$(i,1) = get file name$()
      files$(i,2) = get file date$()
      files$(i,3) = str$(get file type())
      find next
   next i
   _FData(2) = -1
endfunction
 
REM **************************************************
REM Displays and handles file list and preview window
REM Returns the index of the currently selected file
REM or -1 if no selection is made
REM **************************************************
function drawFileList(x,y,width,height,offset)
   MIN_WIDTH = 400                                          : `minimum width for window
   MIN_HEIGHT = 160                                         : `minimum height for window
   if width < MIN_WIDTH then width = MIN_WIDTH
   if height < MIN_HEIGHT then height = MIN_HEIGHT
   _titleX = x                                              : `title bar X-coord (also starting window location)
   _titleY = y                                              : `title bar Y-coord (also starting window location)
   _titleHeight = 16                                        : `title bar height
   _listX = _titleX                                         : `X location to display list at
   _listY = _titleY + _titleHeight+1                        : `Y location to display list at
   _lineHeight = 16                                         : `display height of each row
   _listWidth = width/2                                     : `display width of each row
   _listHeight = height                                     : `total display height for list view
   _listOffset = offset                                     : `file number in list to start display from
   _thumbWidth = 16                                         : `scrollbar thumb width
   _thumbHeight = (_showCount * height) / _FData(0)         : `scrollbar thumb height
   if _thumbHeight > height then _thumbHeight = height
   _thumbX = _listX + _listWidth                            : `scrollbar thumb X position
   _thumbY = _listY + THUMB(0)                              : `scrollbar thumb Y position
   _previewX = _thumbX + _thumbWidth                        : `preview window X position
   _previewY = _listY                                       : `preview window Y position
   _previewWidth = width/2 - _thumbWidth                    : `preview window width
   _previewHeight = _listHeight                             : `preview window height
   _previewSize = getMin(_previewWidth,_previewHeight)-48   : `dimension of image preview
   _showCount = height/_lineHeight                          : `number of rows to display
   _titleWidth = width                                      : `width of title bar (also width of entire window)
   _statusX = _titleX                                       : `X location of status bar
   _statusY = _listY+_listHeight                            : `Y location of status bar
   _statusWidth = _titleWidth                               : `width of status bar
   _statusHeight = 18                                       : `height of status bar
   _scrollDelay = 25                                        : `delay between scrolling steps
   _scrollStartStep = 12                                    : `initial step amount in scrolling
   _backgroundColor = rgb(177,194,207)                      : `background color for list and preview windows
   _foregroundColor = rgb(255,255,255)                      : `foreground color for list and preview windows
   _highlightBgColor1 = rgb(185,199,212)                    : `background color of highlighted row
   _highlightBgColor2 = rgb(144,165,187)                    : `background color of highlighted row
   _titleFontColor = rgb(22,69,110)                         : `text color for title bar
 
   SELECTED_FILE_INDEX = -1
 
   if FLAGS(4) = 13
      H# = _listHeight - _thumbHeight
      V# = _FData(0)*_lineHeight - _listHeight
      THUMB(0) = (viewPointer(0) * H#) / V#
   endif
 
   rem reset mouse flag
   if mouseclick() = 0 then FLAGS(1) = 0 : MOUSE(3) = 0
 
   if MOUSE(1) + MOUSE(2) < timer() then MOUSE(0) = 0
   if mouseclick() = 1 and MOUSE(3) = 0
      MOUSE(3) = 1
      MOUSE(0) = MOUSE(0)+1
      MOUSE(1) = timer()
   endif
 
   if mousemovex() or mousemovey() then mouseHover(0) = timer()
 
   rem display list
   ink _backgroundColor,0
   box _listX, _listY, (_listX+_listWidth), (_listY+_listHeight)
   ink _foregroundColor,0
 
   rem loop through files currently displayed in view
   _listOffset = viewPointer(0)/_lineHeight
   _pixelOffset = viewPointer(0) - _listOffset*_lineHeight
   max = _listOffset + _showCount + 1
   if max > _FData(0) then max = _FData(0)
 
   for i = 1+_listOffset to max
      ly = _listY + (i-_listOffset-1)*_lineHeight - _pixelOffset
      rem if currently selected file
      if i = _FData(2)
         ink _highlightBackColor,0
         gradientVerticalBox(_listX, ly, _listX+_listWidth, ly+_lineHeight,_highlightBgColor1,_highlightBgColor2)
         ink _foregroundColor,0
      endif
      if files$(i,3) = "1" then paste image ICON_FOLDER(0),_listX+2,ly+3, 1
      if files$(i,3) = "2" then box _listX+2,ly+3,_listX+12,ly+13
      text 16+_listX, ly,  capLength$(files$(i,1),_listWidth)
 
      rem file selection
      if FLAGS(1)=0 AND mouseWithin(_listX,ly,_listX+_listWidth,ly+_lineHeight)
         rem handle tooltip trigger
         if mouseHover(0)+mouseHover(1) <= timer() and text width(files$(i,1)) > _listWidth
            mouseHover(2) = i
         else
            mouseHover(2) = -1
         endif
         if mouseclick() = 1
            FLAGS(1) = 1
            SELECTED_FILE_INDEX = i
            rem new selection so delete any existing image preview or loaded sound files
            if SELECTED_FILE_INDEX <> _FData(2)
               FILE_TYPE(0) = -1
               if sound exist(1) = 1 then delete sound 1
               if music exist(1) = 1 then delete music 1
               if image exist(99) = 1 then delete image 99
               hide sprite 1
 
               rem set global currently selected file
               _FData(2) = SELECTED_FILE_INDEX
 
               rem check if this selected file is an image
               if isImage(files$(i,1))
                  FILE_TYPE(0) = 1
                  load image get dir$()+"\"+files$(i,1), 99
                  sprite 1, _previewX+4, _previewY+10, 99
                  set sprite 1,1,0
                  show sprite 1
                  IMAGE(0) = sprite width(1)
                  IMAGE(1) = sprite height(1)
                  scaleSprite(1, _previewSize)
               endif
               rem check if this selected file is a sound file
               if isSound(files$(i,1))
                  FILE_TYPE(0) = 2
                  sprite 1,_previewX+_previewWidth/2-5, _previewY+_previewSize/2-5, ICON_SFX(0)
                  size sprite 1, 10, 10
                  set sprite 1,1,1
                  show sprite 1
                  if sound exist(1) then delete sound 1
                  ICON_SFX(2) = ICON_SFX(0)
                  resetSpectrum()
                  pauseSpectrum()
               endif
               rem check if this selected file is a music file
               if isMusic(files$(i,1))
                  FILE_TYPE(0) = 3
                  sprite 1, _previewX+_previewWidth/2-5, _previewY+_previewSize/2-5, ICON_SFX(0)
                  size sprite 1, 10, 10
                  set sprite 1,1,1
                  show sprite 1
                  if music exist(1) then delete music 1
                  ICON_SFX(2) = ICON_SFX(0)
                  resetSpectrum()
                  pauseSpectrum()
               endif
            endif
         endif
         rem double click to change directories
         if MOUSE(0) > 1
            MOUSE(0) = 0
            if files$(i,3) = "1"
               setDir(get dir$()+"\"+files$(i,1))
               THUMB(0) = 1
               viewPointer(0) = 0
               exitfunction -1
            endif
            if files$(i,3) = "2"
               setDirDrives()
               THUMB(0) = 1
               viewPointer(0) = 0
               exitfunction -1
            endif
         endif
      endif
   next i
 
   rem draw scrollbar track
   gradientHorizontalBox(_thumbX,_listY,_thumbX+_thumbWidth-8,_listY+_listHeight, rgb(82,112,133), _backgroundColor)
   gradientHorizontalBox(_thumbX+8,_listY,_thumbX+_thumbWidth,_listY+_listHeight, _backgroundColor,rgb(82,112,133))
 
   rem draw scrollbar thumb
   gradientHorizontalBox(_thumbX, _thumbY, (_thumbX+_thumbWidth)-8, _thumbY+_thumbHeight, rgb(51,63,72), rgb(89,96,101))
   gradientHorizontalBox(_thumbX+8, _thumbY, (_thumbX+_thumbWidth), _thumbY+_thumbHeight, rgb(89,96,101),rgb(51,63,72))
 
   rem draw preview window
   ink _backgroundColor,0
   box _previewX+1, _previewY, (_previewX+_previewWidth), (_previewY+_previewHeight)
   ink 0,0
   rem if a file is selected, display preview information
   if _FData(2) > -1
      rem if display a preview for an image
      if FILE_TYPE(0) = 1
         sx = (_previewX + _previewWidth/2) - (sprite width(1)/2)
         sy = _previewY + (_previewSize-sprite height(1))/2
         sprite 1,sx,sy,99
      endif
      rem if previewing a sound file
      if FILE_TYPE(0) = 2
 
         sx = _previewX+_previewWidth/2
         sy = _previewY+_previewSize/2
 
         _handleSpectrum(sx-62,sy-49)
         gradientVerticalBox(sx-71,sy-9,sx+54,sy+8, rgb(215,229,237), rgb(164,165,171))
 
         sprite 1,sx-67,sy-5,ICON_SFX(2)
         _handleVolumeSlider(1,sx-55,sy-5)
 
         if mouseclick() = 1 AND mouseWithin(sprite x(1),sprite y(1),sprite x(1)+sprite width(1),sprite y(1)+sprite height(1)) AND FLAGS(1) = 0
            FLAGS(1) = 1
            if sound exist(1)
               if sound playing(1)
                  if sound paused(1)
                     resume sound 1
                     ICON_SFX(2) = ICON_SFX(1)
                     resumeSpectrum()
                  else
                     pause sound 1
                     ICON_SFX(2) = ICON_SFX(0)
                     pauseSpectrum()
                  endif
               endif
            else
               file$ =  get dir$()+"\"+files$(_FData(2),1)
               load sound file$,1
               play sound 1
               ICON_SFX(2) = ICON_SFX(1)
               resumeSpectrum()
            endif
         endif
      endif
      rem if previewing a music file
      if FILE_TYPE(0) = 3
 
         sx = _previewX+_previewWidth/2
         sy = _previewY+_previewSize/2
 
         _handleSpectrum(sx-62,sy-49)
         gradientVerticalBox(sx-62,sy-9,sx+63,sy+8, rgb(215,229,237), rgb(164,165,171))
 
         sprite 1,sx-58,sy-5,ICON_SFX(2)
         _handleVolumeSlider(1,sx-46,sy-5)
 
         if mouseclick() = 1 AND mouseWithin(sprite x(1),sprite y(1),sprite x(1)+sprite width(1),sprite y(1)+sprite height(1)) AND FLAGS(1) = 0
            FLAGS(1) = 1
            if music exist(1)
               if music playing(1)
                  if music paused(1)
                     resume music 1
                     ICON_SFX(2) = ICON_SFX(1)
                     resumeSpectrum()
                  else
                     pause music 1
                     ICON_SFX(2) = ICON_SFX(0)
                     pauseSpectrum()
                  endif
               endif
            else
               file$ =  get dir$()+"\"+files$(_FData(2),1)
               load music file$,1
               play music 1
               ICON_SFX(2) = ICON_SFX(1)
               resumeSpectrum()
            endif
         endif
      endif
      rem if previewing a video file
      if FILE_TYPE(0) = 4
 
      endif
      rem display selected file info (name, creation date, size)
      ink 0,0
      text _previewX+4, _previewY+_previewSize+2, capLength$(files$(_FData(2),1),_previewWidth)
      text _previewX+4, _previewY+_previewSize+14, "Date: "+files$(_FData(2),2)
      fs$ =  bytesToKB$(file size(get dir$()+"\"+files$(_FData(2),1)))
      text _previewX+4, _previewY+_previewSize+26, "Size: "+fs$+"KB"
   endif
 
 
   rem draw title bar
   gradientVerticalBox(_titleX, _titleY, (_titleX+_titleWidth), (_titleY+_titleHeight),rgb(132,195,248),rgb(44,103,153))
   title$ = get dir$()+"\"+files$(_FData(2),1)
   ink _titleFontColor,0
   text _titleX+2, _titleY, capLength$(title$,_titleWidth)
 
   rem draw status bar
   gradientVerticalBox(_statusX, _statusY, (_statusX+_statusWidth), (_statusY+_statusHeight),rgb(76,94,107),_backgroundColor)
   ink 0,0
   text _statusX+8, _statusY+2, str$(_FData(0))+" objects"
 
   rem draw window outline
   ink rgb(34,51,66),0
   box _titleX-1,_titleY-1,_titleX+_titleWidth+1,_titleY-1
   box (_titleX-1), (_statusY+_statusHeight+1), (_titleX+_titleWidth+1), (_statusY+_statusHeight+1)
   box x-1,_titleY,x-1,_statusY+_statusHeight
   box x+_titleWidth+1,_titleY,x+_titleWidth+1,_statusY+_statusHeight
 
   rem draw tooltip
   if mouseHover(2) > -1
      ly = _listY + (mouseHover(2)-_listOffset-1)*_lineHeight - _pixelOffset
      ink rgb(66,148,203),0
      box 14+_listX,ly,16+_listX+text width( files$(mouseHover(2),1)),ly+_lineHeight
      ink rgb(255,255,255),0
      text 16+_listX, ly,  files$(mouseHover(2),1)
   endif
 
   rem ----------------------------
   rem   drag window by title bar
   rem ----------------------------
   if mouseclick() = 1 AND mouseWithin(_titleX, _titleY, (_titleX+_titleWidth), (_titleY+_titleHeight)) AND FLAGS(1) = 0
      FLAGS(1) = 1
      FLAGS(2) = 1
      WIN_X(1) = WIN_X(0) - mousex()
      WIN_Y(1) = WIN_Y(0) - mousey()
   endif
   if FLAGS(2) = 1
      if mouseclick() = 0 then FLAGS(2) = 0
      WIN_X(0) = mousex() +  WIN_X(1)
      WIN_Y(0) = mousey() +  WIN_Y(1)
      if WIN_X(0) < 0 then WIN_X(0) = 0 : WIN_X(1) = WIN_X(0) - mousex()
      if WIN_Y(0) < 0 then WIN_Y(0) = 0 : WIN_Y(1) = WIN_Y(0) - mousey()
      if WIN_X(0)+_titleWidth > screen width()-1 then WIN_X(0) = screen width()-1 - _titleWidth  : WIN_X(1) = WIN_X(0) - mousex()
      h = (_statusY+_statusHeight) - _titleY
      if WIN_Y(0)+h > screen height()-1 then WIN_Y(0) = screen height()-1 - h  : WIN_Y(1) = WIN_Y(0) - mousey()
   endif
 
   rem ----------------------------
   rem      scrollbar control
   rem ----------------------------
   direction = getMouseScrollDirection()
   if direction > 0 then viewPointer(1) = _scrollStartStep
   if direction < 0 then viewPointer(1) = 0-_scrollStartStep
 
   if viewPointer(1) <> 0
      if viewPointer(2) + _scrollDelay <= timer()
         viewPointer(2) = timer()
         viewPointer(0) = viewPointer(0) + viewPointer(1)
         if viewPointer(1) > 0
            viewPointer(1) = viewPointer(1) - 1
            if viewPointer(1) < 0 then viewPointer(1) = 0
         else
            if viewPointer(1) < 0
               viewPointer(1) = viewPointer(1) + 1
               if viewPointer(1) > 0 then viewPointer(1) = 0
            endif
         endif
         if viewPointer(0) < 0 then viewPointer(0) = 0
         if viewPointer(0) > _FData(0)*_lineHeight - _listHeight then viewPointer(0) = _FData(0)*_lineHeight - _listHeight
         rem position thumb to match view scroll
         H# = _listHeight - _thumbHeight
         V# = _FData(0)*_lineHeight - _listHeight
         if V# <= 0
            THUMB(0) = 0
         else
            THUMB(0) = (viewPointer(0) * H#) / V#
         endif
         THUMB(0) = (viewPointer(0) * H#) / V#
      endif
   endif
   if viewPointer(0) > _FData(0)*_lineHeight - _listHeight then viewPointer(0) = _FData(0)*_lineHeight - _listHeight
   if viewPointer(0) < 0 then viewPointer(0) = 0
 
   if mouseclick() = 1 AND mouseWithin(_thumbX, _thumbY, _thumbX+_thumbWidth,_thumbY+_thumbHeight) AND FLAGS(1) = 0
      FLAGS(1) = 1
      FLAGS(3) = 1
      THUMB(1) = _thumbY - mousey()
   endif
   if FLAGS(3) = 1
      if mouseclick() = 0 then FLAGS(3) = 0
      setThumbPos((mousey() - _listY) + THUMB(1),_listHeight, _thumbHeight)
   endif
 
   rem ----------------------------
   rem      resize window
   rem ----------------------------
   rem draw resize box
   a = _titleX + _titleWidth
   b = _statusY + _statusHeight
   ink 0,0
   line a-3,b+1,a+1,b-3
   line a-7,b+1,a+1,b-7
   line a-11,b+1,a+1,b-11
 
   if mouseclick() = 1 AND mouseWithin(a-10, b-10, a, b) AND FLAGS(1) = 0
      FLAGS(1) = 1
      FLAGS(4) = 1
      WIN_WIDTH(1) = _titleX + _titleWidth - mousex()
      WIN_HEIGHT(1) = _titleY + _listHeight - mousey()
      TEMP#(0) = (0.0 + THUMB(0))/_listHeight
   endif
   if FLAGS(4) = 1
      if mouseclick() = 0 then FLAGS(4) = 0
      rem window dimensions
      WIN_WIDTH(0) = (mousex() - _titleX) + WIN_WIDTH(1)
      WIN_HEIGHT(0) = (mousey() - _titleY) + WIN_HEIGHT(1)
      rem position thumb
      H# = _listHeight - _thumbHeight
      V# = _FData(0)*_lineHeight - _listHeight
      if V# <= 0
         THUMB(0) = 0
      else
         THUMB(0) = (viewPointer(0) * H#) / V#
      endif
      rem scale image preview, if available
      if _FData(2) > -1 and FILE_TYPE(0) = 1
         scaleSprite(1, _previewSize)
      endif
   endif
endfunction SELECTED_FILE_INDEX
 
 
 
REM **************************************************
REM handles rendering and events of the volume slider
REM **************************************************
function _handleVolumeSlider(sliderNo, x, y)
   gradientVerticalBox(x,y,x+104,y+9,rgb(164,165,171),rgb(215,229,237))
   ink rgb(82,82,82),0
   box x+volSlider(1),y,x+volSlider(1)+4,y+9
 
   if mouseclick() = 1 AND mouseWithin(x+volSlider(1),y,x+volSlider(1)+4,y+9) AND FLAGS(1) = 0
      FLAGS(1) = 1
      volSlider(0) = mousex() - (x+volSlider(1))
   endif
   if volSlider(0) > -1
      volSlider(1) = (mousex() - volSlider(0)) - x
      if volSlider(1) < 0 then volSlider(1) = 0
      if volSlider(1) > 100 then volSlider(1) = 100
      if mouseclick() = 0 then volSlider(0) = -1
   endif
endfunction
 
REM **************************************************
REM Get direction of mouse wheel scroll
REM **************************************************
function getMouseScrollDirection()
   z = mousemovez()
   if z < MZFix(1) then MZFix(1) = z : exitfunction 1
   if z > MZFix(1) then MZFix(1) = z : exitfunction -1
endfunction 0
 
REM **************************************************
REM Safely sets the scrollbar thumb position
REM **************************************************
function setThumbPos(pos, _listHeight, _thumbHeight)
   if pos < 0 then pos = 0
   H# = _listHeight - _thumbHeight
   if H# = 0 then exitfunction
   if pos > H# then pos = H#
 
   THUMB(0) = pos
   V# = _FData(0)*16 - _listHeight
   viewPointer(0) = (pos*V#) / H#
endfunction
 
REM **************************************************
REM Scales a sprite so the largest dimension matches
REM 'size' while maintaining the correct aspect ratio
REM **************************************************
function scaleSprite(sprNo, size)
   if IMAGE(0) = 0 then exitfunction
   ratio# = IMAGE(1) / (0.0+IMAGE(0))
   rem if width is greater than height
   if IMAGE(0) > IMAGE(1)
      size sprite sprNo, size, size*ratio#
   else
      size sprite sprNo, size/ratio#, size
   endif
endfunction
 
REM **************************************************
REM Returns true if file name has an image extension
REM **************************************************
function isImage(file$)
   for j = 1 to 3
      if endsWith(file$, imgExt$(j)) = 1 then exitfunction 1
   next j
endfunction 0
 
REM **************************************************
REM Returns true if file name has a sound extension
REM **************************************************
function isSound(file$)
   for j = 1 to 3
      if endsWith(file$, sfxExt$(j)) = 1 then exitfunction 1
   next j
endfunction
 
REM **************************************************
REM Returns true if file name has a music extension
REM **************************************************
function isMusic(file$)
   for j = 1 to 3
      if endsWith(file$, musicExt$(j)) = 1 then exitfunction 1
   next j
endfunction
 
REM **************************************************
REM Returns true if string$ ends with suffix$
REM **************************************************
function endsWith(string$, suffix$)
   if len(suffix$) > len(string$) then exitfunction 0
   L = len(string$)
   for k = len(suffix$) to 1 step -1
      if mid$(suffix$,k) <> mid$(string$,L) then exitfunction 0
      dec L
   next k
endfunction 1
 
REM **************************************************
REM Returns the string truncated to a specified width
REM in pixels.
REM **************************************************
function capLength$(string$, width)
   L = len(string$)
   min = width / text width("A")
   for j = min to L
      if text width(left$(string$,j)) > width then exitfunction left$(string$,j-1)
   next j
endfunction string$
 
REM **************************************************
REM Converts bytes into KB limited to 2 decimals
REM **************************************************
function bytesToKB$(b)
   k$ = str$( b/1024.0)
   for j = len(k$) to 1 step -1
      if mid$(k$,j) = "."
         k$ = left$(k$,j+2)
         exitfunction k$
      endif
   next j
endfunction k$
 
REM **************************************************
REM Draws a spectrum meter from (X,Y) to (X+125,Y+40)
REM **************************************************
function _handleSpectrum(x,y)
   gradientVerticalBox(x,y,x+125,y+40,rgb(240,242,245),rgb(194,204,216))
 
   for j = 0 to 9
      drawMeter(x+3+(j*12),y+37,bars(j+1))
   next j
   if bars(0) + 100 <= timer()
      for j = 1 to 10
         bars(j) = rnd(17)
      next j
      bars(0) = timer()
   endif
endfunction
 
REM **************************************************
REM Draws a spectrum bar from (X,Y) to (X+10,Y-40)
REM **************************************************
function drawMeter(x,y,value)
   for k = 0 to 17
      if k <= value : ink rgb(17,21,26),0 : else : ink rgb(152,164,178),0 : endif
      box x,y-k*2,x+10,y-k*2
   next k
endfunction
 
REM **************************************************
REM Resets all spectrum values
REM **************************************************
function resetSpectrum()
   for j = 1 to 10
      bars(j) = -1
   next j
endfunction
 
REM **************************************************
REM Pauses the spectrum
REM **************************************************
function pauseSpectrum()
   bars(0) = timer()+65535
endfunction
 
REM **************************************************
REM Resumes the spectrum
REM **************************************************
function resumeSpectrum()
   bars(0) = 0
endfunction
 
REM **************************************************
REM Draws a box with a horizontal gradient
REM **************************************************
function gradientHorizontalBox(left,top,right,bottom,color1,color2)
   width#=right-left
   for x=left to right-1
      t# = (x-left) / width#
      ink getTransitionalColor(color1,color2,t#),0
      box x,top,x+1,bottom
   next x
endfunction
 
REM **************************************************
REM Draws a box with a vertical gradient
REM **************************************************
function gradientVerticalBox(left,top,right,bottom,color1,color2)
   height#=bottom-top
   for y=top to bottom-1
      t# = (y-top) / height#
      ink getTransitionalColor(color1,color2,t#),0
      box left,y,right,y+1
   next y
endfunction
 
REM **************************************************
REM Returns a linear interpolated color between base
REM color and target color. Percent ranges from 0 to 1
REM **************************************************
function getTransitionalColor(base, target, percent#)
   br = rgbr(base)
   bg = rgbg(base)
   bb = rgbb(base)
   tr = rgbr(target)
   tg = rgbg(target)
   tb = rgbb(target)
   tr = br + (tr-br)*percent#
   tg = bg + (tg-bg)*percent#
   tb = bb + (tb-bb)*percent#
   color = rgb(tr,tg,tb)
endfunction color
 
REM **************************************************
REM Checks to see if mouse is within the specified
REM coordinates
REM **************************************************
function mouseWithin(x1,y1,x2,y2)
   if mousex() > x1 and mousex() < x2 and mousey() > y1 and mousey() < y2 then exitfunction 1
endfunction 0
 
REM **************************************************
REM Get largest of two values
REM **************************************************
function getMax(x,y)
   if x > y then exitfunction x
endfunction y
 
REM **************************************************
REM Get smallest of two values
REM **************************************************
function getMin(x,y)
   if x > y then exitfunction y
endfunction x
 
 
getImages:
   rem -------- draw folder icon --------
   ink rgb(175,171,104),0
   restore Folder
   for y = 1 to 10
      for x = 1 to 12
         read z
         if z = 1 then dot x,y
      next x
   next y
   get image ICON_FOLDER(0),1,1,12,10,1
   rem -------- draw play button icon --------
   cls
   ink rgb(9,92,137),0
   restore PlayButton
   for y = 1 to 11
      for x = 1 to 12
         read z
         if z = 1 then dot x,y
      next x
   next y
   get image ICON_SFX(0),1,1,12,12,1
   rem -------- pause button icon --------
   cls
   ink rgb(9,92,137),0
   box 1,1,3,12
   box 8,1,10,12
   get image ICON_SFX(1),1,1,12,12
RETURN
 
 
Folder:
data 0,1,1,1,0,0,0,0,0,0,0,0
data 1,1,1,1,1,0,0,0,0,0,0,0
data 1,1,1,1,1,1,1,1,1,1,1,0
data 1,1,1,1,1,1,1,1,1,1,1,1
data 1,1,1,1,1,1,1,1,1,1,1,1
data 1,1,1,1,1,1,1,1,1,1,1,1
data 1,1,1,1,1,1,1,1,1,1,1,1
data 1,1,1,1,1,1,1,1,1,1,1,1
data 1,1,1,1,1,1,1,1,1,1,1,1
data 0,1,1,1,1,1,1,1,1,1,1,0
 
PlayButton:
data 1,1,0,0,0,0,0,0,0,0,0,0
data 1,1,1,1,0,0,0,0,0,0,0,0
data 1,1,1,1,1,1,0,0,0,0,0,0
data 1,1,1,1,1,1,1,1,0,0,0,0
data 1,1,1,1,1,1,1,1,1,1,0,0
data 1,1,1,1,1,1,1,1,1,1,1,1
data 1,1,1,1,1,1,1,1,1,1,0,0
data 1,1,1,1,1,1,1,1,0,0,0,0
data 1,1,1,1,1,1,0,0,0,0,0,0
data 1,1,1,1,0,0,0,0,0,0,0,0
data 1,1,0,0,0,0,0,0,0,0,0,0