@dave1707 Thanks, I’ve learned something new “readProjectTab” that’s what I was looking for, where could I find more commands like that to learn? Sorry if I’m asking too much but do you think you could make this code run a new tab running the AR example? I tried to implement the readProjectTab function but every method I tried I got errors.
--# AR
function setup()
-- Create a new craft scene
scene = craft.scene()
scene.sun:get(craft.light).intensity = 0.7
if craft.ar.isSupported then
-- Enable AR session
scene.ar:run()
-- Keep a list of detected planes
planes = {}
-- Option to turn plane detection on and off
parameter.boolean("PlaneDetection", true, function(b)
scene.ar.planeDetection = b
end)
-- Option to draw any detected planes using camera rendering mask
parameter.boolean("DrawPlanes", true, function(b)
local c = scene.camera:get(craft.camera)
if b then
c.mask = ~0
else
c.mask = 1
end
end)
parameter.boolean("DrawPointCloud", true)
local grid = readImage("Project:GridWhite")
scene.ar.didAddAnchors = function(anchors)
for k,v in pairs(anchors) do
local p = scene:entity():add(Plane, v, grid)
planes[v.identifier] = p
end
end
scene.ar.didUpdateAnchors = function(anchors)
for k,v in pairs(anchors) do
local p = planes[v.identifier]
p:updateWithAnchor(v)
end
end
scene.ar.didRemoveAnchors = function(anchors)
for k,v in pairs(anchors) do
local p = planes[v.identifier]
p.entity:destroy()
planes[v.identifier] = nil
end
end
trackingState =
{
[AR_NOT_AVAILABLE] = "Not Available",
[AR_LIMITED] = "Limited",
[AR_NORMAL] = "Normal"
}
cross = image(16,16)
setContext(cross)
pushStyle()
fill(255, 198, 0, 255)
noStroke()
rectMode(CENTER)
rect(cross.width/2, cross.height/2, 3, cross.height)
rect(cross.width/2, cross.height/2, cross.width, 3)
popStyle()
setContext()
end
end
function update(dt)
scene:update(dt)
end
-- Called automatically by codea
function draw()
update(DeltaTime)
-- Draw the scene
scene:draw()
local status = nil
if craft.ar.isSupported then
status = trackingState[scene.ar.trackingState]
if DrawPointCloud then
local c = scene.camera:get(craft.camera)
for k,v in pairs(scene.ar.points) do
local p = c:worldToScreen(v)
sprite(cross, p.x, p.y)
end
end
else
status = "AR Not Supported"
end
fill(255, 255, 255, 255)
text(status, WIDTH/2, HEIGHT - 50)
end
function touched(touch)
if craft.ar.isSupported and touch.state == BEGAN then
local results = scene.ar:hitTest(
vec2(touch.x, touch.y),
AR_EXISTING_PLANE_CLIPPED)
for k,v in pairs(results) do
local e = scene:entity()
local cube = e:add(Cube, v.position + vec3(0,0.5,0), 0.1)
break
end
end
end
--# Cube
Cube = class()
function Cube:init(entity,position, size)
self.entity = scene:entity()
self.entity.model = craft.model()
self.entity.position = position
self.entity.scale=vec3(.5,.5,.5)*size
self.entity:add(craft.rigidbody, STATIC, 1)
end
--# Plane
Plane = class()
function Plane:init(entity, anchor, map)
self.entity = entity
self.entity.model = craft.model.plane(vec2(1,1))
self.entity:get(craft.renderer).mask = 1<<2
local mat = craft.material("Materials:Basic")
mat.map = map
mat.blendMode = NORMAL
mat.diffuse = color(120, 200, 255)
self.entity.material = mat
-- Collisions
self.entity:add(craft.rigidbody, STATIC)
self.entity:add(craft.shape.box,
vec3(1,0.1,1), vec3(0,-0.05,0))
self:updateWithAnchor(anchor, true)
end
function Plane:updateWithAnchor(anchor, s)
self.entity.position = anchor.position
self.entity.scale = anchor.extent + vec3(0,1,0)
self.entity.rotation = anchor.rotation
self.entity.material.offsetRepeat =
vec4(0,0,anchor.extent.x, anchor.extent.z)
end
--# Main
function setup()
func=menu -- start screen
buttonTab={} -- buttons table
-- buttons that show on the menu screen
table.insert(buttonTab,button(350,600,100,50,"Screen 1",menu,screen1))
table.insert(buttonTab,button(350,500,100,50,"Screen 2",menu,screen2))
table.insert(buttonTab,button(350,400,100,50,"Screen 3",menu,screen3))
table.insert(buttonTab,button(700,50,100,50,"EXIT",menu,close))
-- buttons that show on screen1
table.insert(buttonTab,button(350,600,150,50,"Google",screen1,url1))
table.insert(buttonTab,button(350,500,150,50,"twolivesleft",screen1,url2))
table.insert(buttonTab,button(350,100,150,50,"Menu",screen1,menu))
-- buttons that show on screen2
table.insert(buttonTab,button(350,600,150,50,"map quest",screen2,url3))
table.insert(buttonTab,button(350,100,150,50,"Menu",screen2,menu))
-- buttons that show on screen3
table.insert(buttonTab,button(350,600,150,50,"amazon",screen3,url4))
table.insert(buttonTab,button(350,100,150,50,"Menu",screen3,menu))
end
function draw()
background(40, 40, 50)
func() -- call the function set by the button
end
function touched(t) -- check if a button was pressed
if t.state==BEGAN then
for a,b in ipairs(buttonTab) do
if b:touched(t) then
break
end
end
end
end
function menu()
background(58, 97, 136, 255)
fontSize(30)
text("Menu Screen",350,700)
drawButtonTab()
end
function screen1()
background(101, 43, 43, 255)
fontSize(30)
text("Screen 1",350,700)
drawButtonTab()
end
function screen2()
background(110, 165, 74, 255)
fontSize(30)
text("Screen 2",350,700)
drawButtonTab()
end
function screen3()
background(150, 144, 44, 255)
fontSize(30)
text("Screen 3",350,700)
drawButtonTab()
end
function url1()
openURL('http://www.google.com',true)
func=screen1 -- return to screen1
end
function url2()
openURL('http://twolivesleft.com',true)
func=screen1 -- return to screen2
end
function url3()
openURL('http://www.mapquest.com',true)
func=screen2 -- return to screen3
end
function url4()
readProjectTab("AR")
func=screen3 -- return to screen4
end
function drawButtonTab() -- draw selected buttons from button table
fontSize(20)
for a,b in ipairs(buttonTab) do
b:draw()
end
end
button=class()
function button:init(x,y,w,h,name,screen,func)
self.x=x -- x position
self.y=y -- y position
self.w=w -- width
self.h=h -- height
self.name=name -- name to put on the button
self.screen=screen -- screen to draw the button on
self.func=func -- function to call when the button is pressed
end
function button:draw()
if func==self.screen then -- draw the button
sprite("Cargo Bot:Dialogue Button",self.x,self.y,120)
fill(255, 0, 0, 255)
text(self.name,self.x,self.y)
end
end
function button:touched(t)
if func==self.screen then -- only check current screen button
if t.x>self.x-self.w/2 and t.x<self.x+self.w/2 and
t.y>self.y-self.h/2 and t.y<self.y+self.h/2 then
func=self.func else for function url4 do readProjectTab"AR"
return true
end
return false
end
end