Skip to content

Instantly share code, notes, and snippets.

Created February 12, 2013 10:43
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save anonymous/ca9e3dca1c570c435e36 to your computer and use it in GitHub Desktop.
a_FileBrowse2
from scene import *
import os
def hitBox(loc1,loc2,size2):
if loc1.x>loc2.x and loc1.x<loc2.x+size2.x:
if loc1.y>loc2.y and loc1.y<loc2.y+size2.y:
return True
def filetype(filename,foldername=""):
output=filename.split(".")[-1]
if os.path.isdir(filename) or os.path.isdir(foldername+"/"+filename):
output="Folder"
elif output=="py": output="Python"
elif output==filename: output="Unknown"
return output
#All the work is done in this function
def makeScene(self, touch=False, isTouch=True):
background(1, 1, 1)
rect(778,700,50,50)
#Scroll bar
if isTouch and hitBox(touch.location, Point(700,0), Point(68,768)):
self.currentY=touch.location.y
if isTouch and hitBox(touch.location,Point(778,700),Point(50,50)) and self.currentFolder:
self.currentFolder='/'.join(self.currentFolder.split('/')[:-1])
self.currentY=768
try: self.files=os.listdir(self.currentFolder)
except: self.files=os.listdir(os.getcwd())
self.redraw=True
return
#Draw some lines to seperate the table
stroke(0, 0, 0, 1)
stroke_weight(1)
#This is the bottom of the table. It's often way below the scene
#TODO: This y calculation is confusing. Fix it.
y=768+(0-(len(self.files)+10)*32)-(self.currentY-768)
#Columns.
line(300,y,300,704-(self.currentY-768))
line(500,y,500,704-(self.currentY-768))
line(600,y,600,704-(self.currentY-768))
for i in range(len(self.files)+10):#24):#len(self.files)+24):#(24)
#Don't make lines if they aren't visible
if y>0 and y<self.size.h:
line(0,y,768,y)
y+=32 #Lines are drawn from the bottom up.
y-=32 #Spacer from the top.
#Text
tint(0, 0, 0)
text('Current Folder: '+"\""+self.currentFolder+"\"",
'Futura', 20, 10, y, 3)
y-=32
text('Name', 'Futura', 20, 10, y, 3)
text('Type', 'Futura', 20, 320, y, 3)
text('Size', 'Futura', 20, 520, y, 3)
#Sorting options.
if isTouch:
if hitBox(touch.location,Point(0,y-32),Point(300,32)): self.sort=0
elif hitBox(touch.location,Point(300,y-32),Point(200,32)): self.sort=1
elif hitBox(touch.location,Point(500,y-32),Point(100,32)): self.sort=2
#text(str(self.sort), 'Futura', 20, 620, y, 3)
#Set up the array of information for the table
fileArray=[]
for filename in self.files:
file=self.currentFolder+"/"+filename
if file[0]=="/": file=file[1:]
fileType=filetype(filename,self.currentFolder)
try: fileSize=str(round(os.path.getsize(file)/1024.,1))+"kb"
except:
fileSize=str(round(os.path.getsize(self.currentFolder+"/"+filename)/1024.,1))+"kb"
if os.path.isdir(file) and not self.currentFolder==filename: isPath=True
else: isPath=False
fileArray.append([filename,fileType,fileSize,isPath])
if self.sort==0: #sort by name
fileArray=sorted(fileArray, key=lambda file: str.lower)
elif self.sort==1: #sort by type
fileArray=sorted(fileArray, key=lambda fileType: fileType[1])
elif self.sort==2: #sort by size
fileArray=sorted(fileArray, key=lambda fileSize: float(fileSize[2][:-2]))
else: #shouldn't happen, but this sorts folders from files
fileArray=sorted(fileArray, key=lambda isPath: isPath[3])
#Now that array is set up and sorted, draw it.
for i in range(len(fileArray)):
y-=32
try: hit=hitBox(touch.location,Point(0,y-32),Point(200,32))
except: hit=False
if isTouch and hit: #This means we hit one of the folders.
fill(1,0,0) #Box color = red
currentDir=self.currentFolder+"/"+fileArray[i][0]
if currentDir[0]=="/": currentDir=currentDir[1:] #Set up current filename
if os.path.isdir(currentDir) and not self.currentFolder==currentDir:
self.currentY=768
self.files=os.listdir(currentDir)
self.currentFolder=currentDir
self.redraw=True
return
elif hit: fill(1,0,0) #Hit but not isTouch
else: fill(0,0,0) #Black box
rect(10,y-26,20,20) #Draw rectangle and text..
text(str(fileArray[i][0]), 'Futura', 20, 30, y, 3)
text(str(fileArray[i][1]), 'Futura', 20, 320, y, 3)
text(str(fileArray[i][2]), 'Futura', 20, 520, y, 3)
if fileArray[i][3]==True: #If the file is a directory, draw children
currentDir=self.currentFolder+"/"+fileArray[i][0]
if currentDir[0]=="/": currentDir=currentDir[1:]
for filesInFolder in os.listdir(currentDir):
y-=32
rect(50,y-26,20,20)
text(str(filesInFolder), 'Futura', 20, 70, y, 3)
fill(.9,.9,.9)
rect(700,0,68,1028)
fill(1,0,1)
scrollSize=50#768/len(fileArray)
y=self.currentY-scrollSize/2
rect(700,y,68,scrollSize)
class MyScene (Scene):
def setup(self):
self.sort=2
self.currentY=768 #This is associated with the scrollbar.
self.currentFolder=""
self.files=os.listdir(os.getcwd())
self.redraw=False
makeScene(self,isTouch=False)
def draw(self):
pass
def touch_began(self, touch):
#makeScene(self,touch,True)
pass
def touch_moved(self, touch):
makeScene(self,touch,True)
def touch_ended(self, touch):
makeScene(self,touch,True)
if self.redraw==True:
makeScene(self,touch,False)
run(MyScene())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment