Page 1 of 1

"Quick & dirty" file selector

Posted: Mon Mar 04, 2019 4:29 pm
by Henko
In some cases (prototyping, for instance), a simple, small file selector may be handy.
Here is one with the following properties/shortcoming:
- only files from the current directory may be listed
- a filter on file extension may be used (empty filter "" lists all files)
- background under the selector window is untouched (using "page" mechanism)
- filenames are displayed in a scrollable list

Button are used to add "lines" to the selector page, as normal lines always are drawn on the base page.
An alternative would be to use the "sprite scan" statement to temporarely save the background under the selector window and put is back using the "sprite stamp" statement upon termination of the file selector. In that case, normal lines could be used, but all objects like buttons should have to be hidden or deleted explicitely.

B4BE5EE6-3E6F-467D-8F5C-147AA815E5AC.jpeg
B4BE5EE6-3E6F-467D-8F5C-147AA815E5AC.jpeg (979.47 KiB) Viewed 6661 times

Code: Select all

graphics ! graphics clear ! draw color 0,0,0
f$ = file_select$("sB files","sb",100,100,200,300,.8,.9,.6,1)
pause 1
text ! print f$
end

' paged filesector
' list files with extension ext$ from current directory
' background remains unchanged after fileselector closes
' title$ = title of the fileselector window
' ext$ = file extension filter.
'        If ext$="" then all files will be selected
'
def file_select$(title$,ext$,xs,ys,ww,hh,R,G,B,alpha)
name$="dh%ur/sp"
page name$ set 
page name$ frame xs,ys,ww,hh
page name$ color R,G,B,alpha
button name$&"bottom" text "" at -6,hh-3 size ww+12,3
button name$&"left" text "" at 0,-6 size 3,hh+12
button name$&"right" text "" at ww-3,-6 size 3,hh+12
button name$&"upper1" text "" at -6,0 size ww+12,3
button name$&"upper2" text "" at -6,30 size ww+12,3
button name$&"title" text title$ at 40,3 size 120,27
button name$&"cancel" text " Cancel " at 10,hh-50 size 80,40
button name$&"ok" text " OK " at ww-90,hh-50 size 80,40
dir "" list files fil$,nfiles
dim ff$(nfiles) ! nf=0
for i=0 to nfiles-1 ! f$=fil$(i) ! pos=instr(f$,".")
  if pos>-1 then
    e$=right$(f$,len(f$)-pos-1)
    if e$=ext$ then ! nf+=1 ! ff$(nf-1)=f$ ! end if
    end if
  next i
if nf then
  dim fil$(nf) ! for i=0 to nf-1 ! fil$(i)=ff$(i) ! next i
  end if
set lists custom ! fill color R,G,B
list name$ text fil$ at 3,33 size ww-7,hh-96
page name$ show
nr=-1 ! fsel$="" 
do slowdown
  if bp(name$&"cancel") then ! fsel$="" ! break ! end if
  nr=list_selected(name$) ! if nr=-1 then continue
  if bp(name$&"ok") then ! fsel$=fil$(nr) ! break ! end if
  until forever
page name$ hide ! page "" set ! return fsel$
end def

def bp(a$) = button_pressed(a$)

Re: "Quick & dirty" file selector

Posted: Mon Mar 04, 2019 5:33 pm
by Henko
Finally, the window and title bar may be "drawn" using two buttons, thus eliminating the "tric" with the button "lines". This seems the most simple solution. Also, the alpha parameter is removed, as it turned out not to be used in the function.

9C06EE4B-9E3D-4EC9-84B4-AE028F701888.jpeg
9C06EE4B-9E3D-4EC9-84B4-AE028F701888.jpeg (719.77 KiB) Viewed 6656 times

Code: Select all

graphics ! graphics clear ! draw color 0,0,0
f$=file_select$("sB files","sb",100,100,200,300,.8,.9,.6)
pause 1
text ! print f$
end

' paged filesector
' list files with extension ext$ from current directory
' background remains unchanged after fileselector closes
' title$ = title of the fileselector window
' ext$ = file extension filter.
'        If ext$="" then all files will be selected
'
def file_select$(title$,ext$,xs,ys,ww,hh,R,G,B)
name$= "xf%cg/te"
page name$ set 
page name$ frame xs,ys,ww,hh
page name$ color R,G,B,0
fill color R,G,B ! set buttons custom
button name$&"win" text "" at 0,0 size ww,hh ! set buttons default
button name$&"title" text title$ at 1,1 size ww-2,27
button name$&"cancel" text " Cancel " at 10,hh-50 size 80,40
button name$&"ok" text " OK " at ww-90,hh-50 size 80,40
dir "" list files fil$,nfiles
dim ff$(nfiles) ! nf=0
for i=0 to nfiles-1 ! f$=fil$(i) ! pos=instr(f$,".")
  if pos>-1 then
    e$=right$(f$,len(f$)-pos-1)
    if e$=ext$ then ! nf+=1 ! ff$(nf-1)=f$ ! end if
    end if
  next i
if nf then
  dim fil$(nf) ! for i=0 to nf-1 ! fil$(i)=ff$(i) ! next i
  end if
set lists custom ! fill color R,G,B
list name$ text fil$ at 3,33 size ww-7,hh-96
page name$ show
nr=-1 ! fsel$="" 
do slowdown
  if bp(name$&"cancel") then ! fsel$="" ! break ! end if
  nr=list_selected(name$) ! if nr=-1 then continue
  if bp(name$&"ok") then ! fsel$=fil$(nr) ! break ! end if
  until forever
page name$ hide ! page "" set ! return fsel$
end def

def bp(a$) = button_pressed(a$)

Re: "Quick & dirty" file selector

Posted: Mon Mar 04, 2019 6:08 pm
by matt7
Thanks, Henko. This is helpful.

I'm wondering why you need to do a graphics clear though? Why not create a fullscreen page (maybe solid white or black, and maybe with a transparency of 50% or something) and then create the list and buttons for selecting a file on that new page? Then when "Ok" is pressed, delete the page before returning the selected filename. No scanning the graphics screen to a sprite for restoring later would be required.

The background color and alpha can be used to block or obscure what is already on the screen, whether that be TEXT or GRAPHICS, and it also would block the user from interacting with any other objects underneath the new page, forcing them to select a file or cancel the operation.

This would be similar to how my MSGBOX library works.

Re: "Quick & dirty" file selector

Posted: Mon Mar 04, 2019 6:22 pm
by matt7
Disregard my earlier question. I just realized that the reason you switched from drawing lines to using a button to create the window border was to prevent overwriting anything on the graphics layer. I only glanced at the code and didn't realize the graphics clear at the top is only for the demo code showing how to use the file_select$ function.

Re: "Quick & dirty" file selector

Posted: Mon Mar 04, 2019 6:43 pm
by Henko
:D :D
I was just answering your question in a post, when your post came up
Ok

Re: "Quick & dirty" file selector

Posted: Mon Mar 04, 2019 8:58 pm
by rbytes
This is a very simple and handy selector. I will look forward to using it.

Thanks.

Re: "Quick & dirty" file selector

Posted: Tue Mar 05, 2019 9:57 am
by Dutchman
Thanks Henko,
My own solution to find and select files is far too complex for easy use. viewtopic.php?f=20&t=1530&p=9321
This solution is very elegant and useful in most cases. :!:

Re: "Quick & dirty" file selector

Posted: Tue Mar 05, 2019 1:09 pm
by Henko
Dutchman wrote:
Tue Mar 05, 2019 9:57 am
Thanks Henko,
My own solution to find and select files is far too complex for easy use. viewtopic.php?f=20&t=1530&p=9321
This solution is very elegant and useful in most cases. :!:
Thank you for the kind words ;)
The next step is a very simple program launcher:
Put the function and a calling statement " run file_selector$(......,"sb",....) " in a new program, name it like "RUN" and make a icon for it on the home screen
(Maybe something must be done to adress the right directory?)