Skip to content

Instantly share code, notes, and snippets.

@dermotbalson
Last active December 22, 2015 17:18
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 dermotbalson/6504690 to your computer and use it in GitHub Desktop.
Save dermotbalson/6504690 to your computer and use it in GitHub Desktop.
template2
--# 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
The idea is to provide very clean code which can be picked up and pasted into a user's own project, so
each tab has its own setup and draw function etc.
The problem of course is that when you have multiple copies of the same function, Codea compiles from
left to right, so the right hand copy will be run. We could ask users to drag the tab they want to run, to
the right hand side, but this is not really for newbies.
This project does it for the user, by providing Next and Previous parameters to move between tabs, and copying the selected code to the Code tab on the right. The user then needs to press the restart icon at bottom of the screen to activate the new code.
It is clean, requiring no special lines of code in each of the step by step tabs.
--]]
--# Basics
--T1
function setup()
--Setup
print("Basics")
end
function draw()
--Draw
background(50)
fontSize(48)
text("Basics",400,400)
end
--# Spaceship
--T2
function setup()
--Setup
print("Spaceship")
end
function draw()
--Draw
background(50)
fontSize(48)
text("Spaceship",400,400)
end
--# Asteroids
--T3
function setup()
--Setup
print("Asteroids")
end
function draw()
--Draw
background(50)
fontSize(48)
text("Asteroids",400,400)
end
--# Scoring
--T4
function setup()
--Setup
print("Scoring")
x,y=400,400
end
function draw()
--Draw
background(50)
fontSize(48)
text("Scoring - touch the screen",x,y)
end
function touched(touch)
x,y=touch.x,touch.y
end
--# 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
Step={} --put everything in a table to avoid name clashes
function Step.SetupTabs()
Step.Tabs={"Basics","Spaceship","Asteroids","Scoring"} --***** Modify if you need more tabs
Step.LastCode=readProjectData("Code") or 1
parameter.action("Previous tab", Step.RunPrevious)
parameter.action("Next tab",Step.RunNext)
Step.Ready=true --prevents parameter events firing until we've finished setting up
end
--change tabs
function Step.RunCode()
--output.clear()
saveProjectData("Code",Step.LastCode) --save current selection
cleanup() --clean up existing tab
local c=readProjectTab(Step.Tabs[Step.LastCode]) --save selected code to right hand tab
--insert special lines to run the setup and draw functions from this tab, replace dummy comments
c=string.gsub(c,"--Setup","Step.SetupTabs()")
c=string.gsub(c,"--Draw","Step.draw()")
c="--DON'T COPY THIS CODE TO YOUR OWN PROJECT. USE THE ORIGINAL CODE TAB AT LEFT\n\n"..c
saveProjectTab("Code",c)
Step.Message="Press restart (the circular icon below) to run the new code"
end
function Step.RunPrevious()
if Step.Ready then --only runs code if we've finished setting up
if Step.LastCode>1 then
Step.LastCode=Step.LastCode-1
Step.RunCode()
end
end
end
function Step.RunNext()
if Step.Ready then --only runs code if we've finished setting up
if Step.LastCode<#Step.Tabs then
Step.LastCode=Step.LastCode+1
Step.RunCode()
end
end
end
function Step.draw()
output.clear()
local e=""
for i=1,#Step.Tabs do
if i==Step.LastCode then e=" -Selected" else e="" end
print(i,Step.Tabs[i],e)
end
if Step.Message then print("******************************\n"
..Step.Message.."\n******************************") end
end
--these are just here in case the selected code doesn't contain them,
--in which case, if they exist in any of the other code tabs Codea will use
--those instead, which we don't want. So these empty functions just ensure
--none of the functions (with the same names) on the left of this tab get used.
--They don't have the Step prefix because they aren't really part of this tab
function draw()
end
function collide(contact)
end
function touched(touch)
end
function cleanup()
end
--# Code
--DON'T COPY THIS CODE TO YOUR OWN PROJECT. USE THE ORIGINAL CODE TAB AT LEFT
--T2
function setup()
Step.SetupTabs()
print("Spaceship")
end
function draw()
Step.draw()
background(50)
fontSize(48)
text("Spaceship",400,400)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment