Skip to content

Instantly share code, notes, and snippets.

/a_FileBrowse.py Secret

Created February 11, 2013 12:45
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/3508459568dbdfa45c3b to your computer and use it in GitHub Desktop.
Save anonymous/3508459568dbdfa45c3b to your computer and use it in GitHub Desktop.
a_FileBrowse
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):
output=filename.split(".")[-1]
if os.path.isdir(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)
#Draw some lines to seperate the table
stroke(0, 0, 0, 1)
stroke_weight(1)
#Columns.
line(300,0,300,704-(self.currentY-768))
line(500,0,500,704-(self.currentY-768))
line(600,0,600,704-(self.currentY-768))
#This is the bottom row. 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)
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,1028,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)
for filename in self.files:
y-=32 #Text is drawn from the top down.
if isTouch and hitBox(touch.location,Point(0,y-32),Point(200,32)):
fill(1,0,0)
if os.path.isdir(filename) and not self.currentFolder==filename:
self.currentFolder=self.currentFolder+filename
self.files=os.listdir(self.currentFolder)
self.redraw=True
return
else: fill(0,0,0)
rect(10,y-26,20,20)
text(str(filename), 'Futura', 20, 30, y, 3)
text(filetype(filename), 'Futura', 20, 320, y, 3)
try: txt=str(round(os.path.getsize(filename)/1024.,1))+"kb"
except: txt=str(round(os.path.getsize(self.currentFolder+"/"+filename)/1024.,1))+"kb"
text(txt, 'Futura', 20, 520, y, 3)
if os.path.isdir(filename):
for filesInFolder in os.listdir(filename):
rect(50,y-58,20,20)
y-=32
text(str(filesInFolder), 'Futura', 20, 70, y, 3)
#Scroll bar
if isTouch:
scrollSize=768/len(self.files)+24
if hitBox(touch.location,
Point(700,self.currentY-scrollSize),
Point(68,768-scrollSize)):
self.currentY=touch.location.y
fill(.9,.9,.9)
rect(700,0,68,1028)
fill(1,0,1)
scrollSize=768/(len(self.files)/3)
y=self.currentY-scrollSize/2
if y>768-scrollSize: y=768-scrollSize
elif y<0: y=0
rect(700,y,68,scrollSize)
self.redraw==True
class MyScene (Scene):
def setup(self):
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):
pass
def touch_moved(self, touch):
makeScene(self,touch,True)
def touch_ended(self, touch):
makeScene(self,touch,True)
if self.redraw==True:
self.currentY=768
makeScene(self,touch,True)
self.redraw==False
run(MyScene())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment