Skip to content

Instantly share code, notes, and snippets.

@simsaens
Created June 3, 2013 17:35
Show Gist options
  • Save simsaens/5699814 to your computer and use it in GitHub Desktop.
Save simsaens/5699814 to your computer and use it in GitHub Desktop.
Simple Scene Switching - Put the tabs in the following order: Main, Scene, BoxScene, CircleScene, Button
BoxScene = class(Scene)
function BoxScene:init()
-- We have to initialize our
-- super class
Scene.init(self)
end
function BoxScene:draw()
background(78, 99, 51, 255)
fill(195, 154, 154, 255)
rectMode(CENTER)
rect(WIDTH/2,HEIGHT/2,300,300)
end
Button = class()
function Button:init(name, position, action)
-- Name to appear on the button
self.name = name
-- Location of the button (vec2 type)
self.position = position
-- Function to run when button is pressed
self.action = action
-- Default button size
self.size = vec2(150,75)
end
function Button:draw()
-- This lets us alter the style
-- just for this button
pushStyle()
-- Tell Codea we want to draw from the center
rectMode(CENTER)
textMode(CENTER)
-- Choose a font
font("Inconsolata")
fontSize(24)
-- Pick a button color
fill(153, 57, 57, 255)
-- Draw a reddish rectangle at
-- self.position with self.size
rect( self.position.x, self.position.y, self.size.x, self.size.y )
-- Use a white color for text
fill(255)
text( self.name, self.position.x, self.position.y )
-- Old style is restored here
popStyle()
end
function Button:containsPoint(x, y)
-- Check if point x, y lies within the rectangle
-- defined by self.position with self.size
-- Get half the size for computing the bounds
local halfSize = self.size * 0.5
-- Figure out the lower left and upper right
-- corners of our button
local lowerLeft = self.position - halfSize
local upperRight = self.position + halfSize
-- Check if the point is inside them
if x > lowerLeft.x and
x < upperRight.x and
y > lowerLeft.y and
y < upperRight.y then
-- Button contains point
return true
end
-- Button does not contain point
return false
end
function Button:touched(touch)
-- Check three conditions necessary for a button tap:
-- Touch has ended
-- Touch is a "tap"
-- The button actually contains the touch
if touch.state == ENDED and
touch.tapCount == 1 then
if self.action then
-- If we have an action attached, run it
self.action()
end
end
end
CircleScene = class(Scene)
function CircleScene:init()
-- We have to initialize our
-- super class
Scene.init(self)
end
function CircleScene:draw()
background(51, 56, 99, 255)
fill(154, 181, 195, 255)
ellipse(WIDTH/2,HEIGHT/2,300,300)
end
-- SimpleScenes
-- Use this function to perform your initial setup
function setup()
-- Create two different scenes, these are subclasses
-- of the Scene class
boxScene = BoxScene()
circleScene = CircleScene()
-- Add a button to box scene to change to circle scene
circleButton = Button("Circle Scene",
vec2(WIDTH/2, 50),
function() currentScene = circleScene end)
boxScene:addButton(circleButton)
-- Add a button to circle scene to change to box scene
boxButton = Button("Box Scene",
vec2(WIDTH/2, 50),
function() currentScene = boxScene end)
circleScene:addButton(boxButton)
-- currentScene represents the scene we are currently
-- drawing. We can switch scenes by assigning a new value
-- to this variable
currentScene = boxScene
end
-- This function gets called once every frame
function draw()
currentScene:drawScene()
end
function touched(touch)
currentScene:touched(touch)
end
Scene = class()
function Scene:init()
self.buttons = {}
end
function Scene:addButton(button)
self.buttons[button] = button
end
function Scene:removeButton(button)
self.buttons[button] = nil
end
function Scene:drawScene()
pushStyle()
-- Draw our scene content
self:draw()
-- Now draw all our buttons
for k,btn in pairs(self.buttons) do
btn:draw()
end
popStyle()
end
function Scene:draw()
end
function Scene:touched(touch)
-- Check if any buttons are touched
for k,btn in pairs(self.buttons) do
if btn:containsPoint(touch.x, touch.y) then
btn:touched(touch)
return
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment