Skip to content

Instantly share code, notes, and snippets.

@monkeyman32123
Last active August 29, 2015 14:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save monkeyman32123/374a4779bf394c3f55ce to your computer and use it in GitHub Desktop.
Save monkeyman32123/374a4779bf394c3f55ce to your computer and use it in GitHub Desktop.
Codea Explorer
--# Main
function setup()
h = Explorer(true)
parameter.action("remove",function() h:remove() end)
parameter.action("re-add",function() h:remove(); h=Explorer() end)
parameter.color("bgcol")
end
function draw()
background(bgcol)
end
function touched(t)
end
--# Explorer
Explorer = class()
function Explorer:init(ignoreCodeaVals)
self.wid = 275
self.tb = Tbox(WIDTH+10,10,self.wid-20,3*HEIGHT/4)
self.tb:chngtbl(_G)
self.stationary = true
self.x = WIDTH-self.wid
self.shift = self.wid
self.isin = false
self.twtime = .25
self.orientation = CurrentOrientation
self.WIDTH,self.HEIGHT = WIDTH,HEIGHT
self.types = {"other","number","table","userdata","function"}
self.tb.types = self.types
self.tb.type = self.types[3]
self.odraw = draw
self.otouch = touched or (function(t) end)
if ignoreCodeaVals then self.tb.ignore = true else self.tb.ignore=false end
draw = function() self.odraw(); self:draw() end
touched = function(t) self:touched(t); self.otouch(t) end
self:setvals()
end
function Explorer:setvals()
self.b1 = vec4(self.x+self.shift,0,self.wid,HEIGHT)
self.b2 = vec4(self.tb.x-1,self.tb.y-1,self.tb.w+2,self.tb.h+2)
self.b3 = vec4(self.tb.x-1,self.tb.y+self.tb.h+30,self.tb.w+2,HEIGHT/5-30)
self.x1= self.x+self.shift-10+10*(self.shift/self.wid)-5
self.x2= self.x+self.shift-10*(self.shift/self.wid)-5
self.tabs = {}
for i=1,#self.types do
self.tabs[i] = {vec4(self.tb.x-1+(i-1)*((self.tb.w+2)/#self.types),self.tb.y+self.tb.h,(self.tb.w+2)/#self.types,31),
self.types[i]}
end
end
function Explorer:draw()
if self.orientation~=CurrentOrientation or self.WIDTH~=WIDTH or self.HEIGHT~=HEIGHT then
local typ,parents,numparents,pos = self.tb.type,self.tb.parents,self.tb.numparents,self.tb.pos
local key,texts,isin,ignore = self.tb.key,self.tb.txts,self.isin,self.tb.ignore
self:remove()
self:init()
self.tb.type,self.tb.parents,self.tb.numparents,self.tb.pos = typ,parents,numparents,pos
self.tb.key,self.tb.txts,self.tb.ignore = key,texts,ignore
if isin then self.shift = 0;self.tb.x = self.tb.x - self.wid; self.isin = true;self:setvals() end
end
if not self.stationary then
self:setvals()
end
pushStyle()
blendMode(ONE_MINUS_DST_COLOR,ONE_MINUS_SRC_COLOR)
textMode(CENTER)
strokeWidth(2)
fill(0, 0, 0, 0)
stroke(255)
if self.isin then
rect(self.b1.x,self.b1.y,self.b1.z,self.b1.w)
rect(self.b2.x,self.b2.y,self.b2.z,self.b2.w)
rect(self.b3.x,self.b3.y,self.b3.z,self.b3.w)
for k,v in ipairs(self.tabs) do
rect(v[1].x,v[1].y,v[1].z,v[1].w)
end
fill(255)
fontSize(11)
for k,v in ipairs(self.tabs) do
if self.tb.type == v[2] then
rect(v[1].x,v[1].y,v[1].z,v[1].w)
end
text(v[2],v[1].x+v[1].z/2,v[1].y+v[1].w/2)
end
textMode(CORNER)
text("Codea Explorer - by monkeyman32123",self.tb.x+2,self.tb.y+self.tb.h+HEIGHT/5+2)
textWrapWidth(self.tb.w-10)
clip(self.b3.x,self.b3.y+1,self.b3.z-2,self.b3.w-2)
fontSize(22)
text(self.tb:concatkeys().." >> "..self.tb:getkey(),self.b3.x+4,self.b3.y+2)
clip()
self.tb:draw()
end
line(self.x1,HEIGHT-20,self.x2,HEIGHT-10)
line(self.x1,HEIGHT,self.x2,HEIGHT-10)
popStyle()
end
function Explorer:touched(t)
if self.stationary then
if self.isin then
if t.state == BEGAN then
if not self.t1 then
if t.y > HEIGHT-30 and t.x >self.x-30 and t.x < self.x then
self.t1 = t.id
return
end
end
if not self.t2 then
if t.x > self.b3.x and t.x < self.b3.x+self.b3.z and t.y > self.b3.y and t.y < self.b3.y+self.b3.w then
self.t2,self.ot = t.id,ElapsedTime
return
end
end
for k,v in pairs(self.tabs) do
if not v[3] then
if t.x> v[1].x and t.y>v[1].y then
if t.x< (v[1].x+v[1].z) and t.y< (v[1].y+v[1].w) then
v[3] = t.id
return
end
end
end
end
elseif t.state == ENDED then
if t.id == self.t1 then
self.t1 = nil
if t.y > HEIGHT-30 and t.x >self.x-30 and t.x < self.x then
self:tweenout()
return
end
elseif t.id == self.t2 then
self.t2 = nil
if t.x > self.b3.x and t.x < self.b3.x+self.b3.z and t.y > self.b3.y and t.y < self.b3.y+self.b3.w then
if ElapsedTime-self.ot > 1 then
self.tb:top()
else
self.tb:resurface()
end
return
end
end
for k,v in pairs(self.tabs) do
if v[3] == t.id then
v[3] = nil
if t.x> v[1].x and t.y>v[1].y then
if t.x< (v[1].x+v[1].z) and t.y< (v[1].y+v[1].w) then
self.tb.type = self.types[k]
self.tb.pos = 0
end
end
return
end
end
end
self.tb:touched(t)
elseif not self.isin then
if t.x>WIDTH-30 and t.y > HEIGHT-30 then
self:tweenin()
end
end
end
end
function Explorer:tweenin()
tween(self.twtime,self.tb,{x=self.tb.x-self.wid},nil,function() self:isstationary(true); self:setvals() end)
tween(self.twtime,self,{shift=0})
self:isstationary(false)
self.isin=true
end
function Explorer:tweenout()
tween(self.twtime,self.tb,{x=self.tb.x+self.wid},nil,function() self:isstationary(true); self.isin = false; self:setvals() end)
tween(self.twtime,self,{shift=self.wid})
self:isstationary(false)
end
function Explorer:isstationary(setting)
if setting ~= nil then
self.stationary = setting
end
return self.stationary
end
function Explorer:remove()
draw,touched = self.odraw,self.otouch
end
------------
Tbox = class()
function Tbox:init(x,y,width,height)
self.x = x or 0
self.y = y or 0
self.w = width or 100
self.h = height or 100
self.txts = {}
self.edge = 10
self.pos = 0
self.heights = {}
self.key = "_G"
self.fs = 12
end
function Tbox:draw()
self.pos = math.max(self.pos,0)
pushStyle()
clip(self.x,self.y,self.w,self.h)
translate(self.x,self.y)
self:style()
local tot,xshift,str,y,i,d,q = 2,self.edge/2,"",0,1,0,self.h+self.pos
for key,val in pairs(self.txts) do
if self:test(val) and not(self.ignore and Tbox.nope[key]) then
self.heights[key] = self.heights[key] or select(2,textSize(i.." : "..tostring(key).." : "..tostring(val)))+self.fs
y = self.heights[key]
d = q-tot
if d <= 0 then
break
elseif d-y-2>=self.h then
tot = tot + y + 4
else
if self.selected == key then
rect(1,d-y-2,self.w-2,y+4)
end
str = i.." : "..tostring(key).." : "..tostring(val)
text(str,xshift,d-y+self.fs)
tot = tot + y + 4
d = d-y-2
line(0,d,self.w,d)
end
i = i + 1
end
end
if not self.tid and self.pos > tot-self.h then
self.pos = math.max(.9*self.pos+.1*(tot-self.h),0)
end
self.pos = self.pos + (self.vel or 0)
self.vel = (self.vel or 0)*.75
translate(-self.x,-self.y)
clip()
popStyle()
end
function Tbox:test(t)
if self.type == "other" then
for k,v in pairs(self.types) do
if v == type(t) then
return false
end
end
return true
else
if self.type == type(t) then
return true
else
return false
end
end
end
function Tbox:style()
textMode(CORNER)
fill(255)
strokeWidth(1)
stroke(255)
fontSize(self.fs)
font("Inconsolata")
textWrapWidth(self.w-self.edge)
end
function Tbox:touched(t)
if t.state == BEGAN and not self.tid then
if t.x < self.x+self.w and t.x > self.x then
if t.y < self.y+self.h and t.y > self.y then
self.tid = t.id
self.op = t.y
self.ot = ElapsedTime
self.vel = 0
self.lastdelta = 0
self:chosen(t.y)
end
end
elseif t.state == MOVING and t.id == (self.tid or nil) then
if self.op then
if math.abs(t.y-self.op) > 10 then
self.op = nil
self.selected = nil
end
end
self.lastdelta = t.deltaY
self.pos = self.pos + t.deltaY
self.vel = 0
elseif (t.state == ENDED or t.state == CANCELLED) and (t.id == (self.tid or nil)) then
if self.op then
self:chosen(t.y,true)
end
self.tid,self.op = nil,nil
local r = (t.deltaY+self.lastdelta)/2
if math.abs(r)>5 then self.vel = r end
end
end
function Tbox:chosen(y,ended)
pushStyle()
self:style()
local tot,str = 0,""
local i,delta,d,q=1,0,0,self.h+self.pos+2
for k,v in pairs(self.txts) do
if self:test(v) and not(self.ignore and Tbox.nope[k]) then
self.heights[k] = self.heights[k] or select(2,textSize(i.." : "..tostring(k).." : "..tostring(v)))+12
delta = self.heights[k]
d = q-tot
if y-self.y < d then
if y-self.y > d-delta-4 then
if type(v) == "table" then
if ended then
if k == self.selected then
self:dive(v,k)
end
self.selected = nil
else
self.selected = k
end
end
break
end
end
tot = tot + delta + 4
i = i + 1
end
end
popStyle()
end
function Tbox:chngtbl(tbl)
self.txts = tbl
self.pos = 0
end
function Tbox:dive(newtab,key)
self.numparents = (self.numparents or 0) + 1
if not self.parents then self.parents = {} end
self.parents[self.numparents] = {self.txts,self.key,self.pos}
self.key = tostring(key)
self:chngtbl(newtab)
end
function Tbox:resurface()
if self.parents and self.numparents > 0 then
self:chngtbl(self.parents[self.numparents][1])
self.key = self.parents[self.numparents][2]
self.pos = self.parents[self.numparents][3]
self.parents[self.numparents] = nil
self.numparents = self.numparents - 1
end
end
function Tbox:top()
self.numparents, self.parents = nil
self.key = "_G"
self:chngtbl(_G)
end
function Tbox:getparentkey()
if self.parents and self.parents[1] then
return self.parents[self.numparents][2]
else
return ""
end
end
function Tbox:concatkeys()
if self.parents then
local str = ""
for i=1,#self.parents do
str = str.." >> "..self.parents[i][2]
end
return str
else
return ""
end
end
function Tbox:getkey()
return self.key
end
Tbox.nope ={MOVING=true,listProjectData=true,shader=true,ContentScaleFactor=true,loadfile=true,
applyMatrix=true,point=true,DeltaTime=true,listProjectTabs=true,MULTIPLY=true,deviceMetrics=true,
RIGHT=true,SRC_ALPHA=true,close=true,clearLocalData=true,SRC_COLOR=true,xpcall=true,ortho=true,
soundbuffer=true,RETAINED=true,STANDARD=true,spriteBatching=true,collectgarbage=true,FRONT=true,
CurrentOrientation=true,SOUNDS=true,POLYGON=true,stopRecording=true,noStroke=true,buffer=true,
keyboardBuffer=true,parameter=true,zLevel=true,saveGlobalData=true,popStyle=true,background=true,
DST_ALPHA=true,SHADERS=true,BEGAN=true,ROPE=true,music=true,saveProjectData=true,STATIONARY=true,
SOUND_SQUAREWAVE=true,print=true,textSize=true,LEFT=true,COMPOUND=true,noise=true,LANDSCAPE_LEFT=true,
pointSize=true,LANDSCAPE_ANY=true,readProjectTab=true,ENDED=true,SOUND_JUMP=true,textAlign=true,
resetMatrix=true,clip=true,font=true,ONE=true,resetStyle=true,RETURN=true,DISABLED=true,rawlen=true,
CHAIN=true,readLocalData=true,showKeyboard=true,os=true,type=true,DECODE=true,textMode=true,TEXT=true,
output=true,DATA=true,clearParameters=true,spriteList=true,assetList=true,saveProjectTab=true,
spriteSize=true,CAMERA_BACK=true,Tbox=true,textWrapWidth=true,package=true,clearOutput=true,
startRecording=true,SOUND_SINEWAVE=true,modelMatrix=true,SOUND_PICKUP=true,ONE_MINUS_SRC_COLOR=true,
CANCELLED=true,tint=true,noTint=true,spriteMode=true,ONE_MINUS_SRC_ALPHA=true,SOUND_HIT=true,
NORMAL=true,WIDTH=true,rawset=true,FPS=true,CIRCLE=true,setup=true,BACK=true,debug=true,rawget=true,
STATIC=true,utf8=true,readProjectData=true,dofile=true,unpack=true,DYNAMIC=true,cameraSource=true,
_G=true,fontSize=true,ONE_MINUS_DST_ALPHA=true,strokeWidth=true,PORTRAIT_UPSIDE_DOWN=true,
clearProjectData=true,rawequal=true,supportedOrientations=true,vec4=true,SOUND_RANDOM=true,line=true,
select=true,BACKSPACE=true,ANY=true,loadstring=true,isKeyboardShowing=true,scale=true,io=true,
ZERO=true,translate=true,camera=true,restart=true,listGlobalData=true,vec3=true,rotate=true,
openURL=true,tostring=true,_VERSION=true,error=true,setContext=true,CurrentTouch=true,image=true,
pcall=true,PRISMATIC=true,CAMERA=true,tonumber=true,pasteboard=true,SOUND_BLIT=true,HEIGHT=true,
FORMAT_STEREO8=true,string=true,readProjectInfo=true,watch=true,popMatrix=true,getmetatable=true,
setmetatable=true,class=true,noClip=true,require=true,fill=true,sound=true,LANDSCAPE_RIGHT=true,
viewMatrix=true,pairs=true,rectMode=true,physics=true,soundBufferSize=true,isRecording=true,
pushMatrix=true,backingMode=true,alert=true,KINEMATIC=true,RADIUS=true,noFill=true,FORMAT_MONO16=true,
ROUND=true,Gravity=true,ADDITIVE=true,text=true,load=true,readText=true,pushStyle=true,vec2=true,
projectionMatrix=true,PORTRAIT_ANY=true,math=true,MUSIC=true,lineCapMode=true,fontMetrics=true,
readGlobalData=true,draw=true,blendMode=true,http=true,iparameter=true,touched=true,tween=true,
json=true,CORNERS=true,SOUND_SHOOT=true,ONE_MINUS_DST_COLOR=true,WELD=true,ellipseMode=true,
REVOLUTE=true,readImage=true,assert=true,SOUND_NOISE=true,speech=true,perspective=true,EDGE=true,
FULLSCREEN=true,matrix=true,ENCODE=true,color=true,DISTANCE=true,coroutine=true,sprite=true,
OVERLAY=true,FORMAT_STEREO16=true,SRC_ALPHA_SATURATE=true,ipairs=true,rsqrt=true,displayMode=true,
SOUND_EXPLODE=true,FULLSCREEN_NO_BUTTONS=true,ellipse=true,mesh=true,CENTER=true,SPRITES=true,
saveLocalData=true,Explorer=true,CORNER=true,location=true,RotationRate=true,DST_COLOR=true,
ElapsedTime=true,SOUND_SAWTOOTH=true,rect=true,stroke=true,saveText=true,triangulate=true,
UserAcceleration=true,saveImage=true,listLocalData=true,CAMERA_FRONT=true,smooth=true,
saveProjectInfo=true,SOUND_POWERUP=true,PROJECT=true,SQUARE=true,PORTRAIT=true,hideKeyboard=true,
next=true,setInstructionLimit=true,noSmooth=true,FORMAT_MONO8=true,table=true}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment