Skip to content

Instantly share code, notes, and snippets.

@dermotbalson
Last active December 22, 2015 17:49
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dermotbalson/6508514 to your computer and use it in GitHub Desktop.
Save dermotbalson/6508514 to your computer and use it in GitHub Desktop.
Template
--# Notes
--[[
This is a template for creating multi-tab projects
That is, projects where each tab can be run independently
This can be used for step by step tutorials, with users able to choose which tab runs
It can also be used for multi-stage projects, eg games where there are several stages like
* start menu
* play (maybe with multiple levels)
* win
* lose
Each of these can have their own tab, with its own draw function, touch function, etc
The way it works is that each tab is completely self contained. It needs one line at the top to create an identifier for the tab, that will enable it to be run when selected. The easiest identifier is a number, which has been used here, so the Steps 1-4 have been numbered as 1, 2, 3 and 4.
If you are going to switch between tabs, it may be necessary to do some housekeeping first, and you can do this by including a cleanup() function in any tabs that need it. This function (if it exists) will always be called before tabs are switched.
--]]
--# Main
--Main
--This code manages which Code tab is run
--it remembers your last choice, and if you select a different one, it runs that instead
tabs = {}
function setup()
LastCode=readProjectData("Code") or 1 --load stored tab number
parameter.integer("Choose_a_tab",1,#tabs,LastCode,RunCode) --tab selector
parameter.action("Previous tab", RunPrevious)
parameter.action("Next tab",RunNext)
end
--change tabs
function RunCode()
output.clear()
saveProjectData("Code",Choose_a_tab)
cleanup()
invoke(Choose_a_tab)
end
function RunPrevious()
if Choose_a_tab>1 then Choose_a_tab=Choose_a_tab-1 RunCode() end
end
function RunNext()
if Choose_a_tab<#tabs then Choose_a_tab=Choose_a_tab+1 RunCode() end
end
--default empty function to avoid errors for tabs that don't have it
function cleanup()
end
--these two functions do all the tab switching magic (thanks to Andrew_Stacey)
function localise(n)
local t= {}
setmetatable(t,{__index = _G})
--setfenv(2,t)
tabs[n] = t
return t
end
function invoke(n)
local t = tabs[n]
setup = t.setup
if t.draw then draw = t.draw end
if t.touched then touched=t.touched end
if t.collide then collide=t.collide end
if t.cleanup then cleanup=t.cleanup end
setup()
end
--# Step_1
_ENV = localise(1) --DELETE this line if you copy this code to another project (it is used to manage tab changes)
function setup()
print("Step 1")
end
function draw()
background(50)
fontSize(48)
text("Step 1",400,400)
end
--# Step_2
_ENV = localise(2) --DELETE this line if you copy this code to another project (it is used to manage tab changes)
function setup()
print("Step 2")
end
function draw()
background(50)
fontSize(48)
text("Step 2",400,400)
end
--# Step_3
_ENV = localise(3) --DELETE this line if you copy this code to another project (it is used to manage tab changes)
function setup()
print("Step 3")
end
function draw()
background(50)
fontSize(48)
text("Step 3",400,400)
end
--# Step_4
_ENV = localise(4) --DELETE this line if you copy this code to another project (it is used to manage tab changes)
function setup()
print("Step 4 - touch the screen")
x,y=400,400
end
function draw()
background(50)
fontSize(48)
text("Step 4 - touch the screen",x,y)
end
function touched(touch)
x,y=touch.x,touch.y
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment