Skip to content

Instantly share code, notes, and snippets.

@dermotbalson
Created September 13, 2013 04:09
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 dermotbalson/6546692 to your computer and use it in GitHub Desktop.
Save dermotbalson/6546692 to your computer and use it in GitHub Desktop.
Step by step 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.
--]]
--HOW TO USE THIS PROJECT
--There are a number of tabs at the top. Press on a tab to see its code.
--Work through the tabs from left to right, to see the project develop
--You can run the code in any of the project tabs, by
--1. running the program, and
--2. selecting the tab number using the controls at the upper left of the screen
--The program will remember your selection after that.
--This enables you to work with one tab at a time, make changes and see the effects by running the program
--# 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
local tabs = {}
local fnames = {"setup","draw","touched","collide","orientationChanged","close","restart","keyboard","cleanup"}
local fns = {}
local tabDesc={}
function setup()
for k,v in ipairs(fnames) do --store addresses of key event functions
fns[v] = _G[v]
end
LastCode=readProjectData("Code") or 1 --load stored tab number
parameter.integer("Choose_a_tab",1,#tabs,LastCode,ShowList) --tab selector
parameter.action("Run selected tab", RunCode)
RunCode()
end
function ShowList()
output.clear()
for i=1,#tabs do
print(i,tabDesc[i])
end
end
--these two functions do all the tab switching magic (thanks to Andrew_Stacey)
function localise(n,d)
if d then tabDesc[n]=d end
local t= {}
setmetatable(t,{__index = _G})
setfenv(2,t)
tabs[n] = t
end
--change tabs
function RunCode()
output.clear()
saveProjectData("Code",Choose_a_tab)
cleanup()
local t = tabs[Choose_a_tab]
for k,v in ipairs(fnames) do
if t[v] then _G[v] = t[v] else _G[v] = fns[v] end -- overwrite with the new code
end
setup()
end
--default empty function to avoid errors for tabs that don't have it
function cleanup()
end
--# Step_1
localise(1,"tab1") --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
localise(2,"tab2") --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
localise(3,"tab3") --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
localise(4,"tab4") --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
--# Step_5
localise(5,"tab5") --DELETE this line if you copy this code to another project (it is used to manage tab changes)
--# Step_6
localise(6,"tab6") --DELETE this line if you copy this code to another project (it is used to manage tab changes)
--# Step_7
localise(7,"tab7") --DELETE this line if you copy this code to another project (it is used to manage tab changes)
--# Step_8
localise(8,"tab8") --DELETE this line if you copy this code to another project (it is used to manage tab changes)
--# Step_9
localise(9,"tab9") --DELETE this line if you copy this code to another project (it is used to manage tab changes)
--# Step_10
localise(10,"tab10") --DELETE this line if you copy this code to another project (it is used to manage tab changes)
--# Step_11
localise(11,"tab11") --DELETE this line if you copy this code to another project (it is used to manage tab changes)
--# Step_12
localise(12,"tab12") --DELETE this line if you copy this code to another project (it is used to manage tab changes)
--# Step_13
localise(13,"tab13") --DELETE this line if you copy this code to another project (it is used to manage tab changes)
--# Step_14
localise(14,"tab14") --DELETE this line if you copy this code to another project (it is used to manage tab changes)
--# Step_15
localise(15,"tab15") --DELETE this line if you copy this code to another project (it is used to manage tab changes)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment