This also includes some bug fixes and enhancements:
Enchancements
* enterScene - this can now be attached to the local group to execute a function after the scene is loaded or being redisplayed in the case of books
* now that this is a class instead of module, multiple director classes can be used, however please note that it is necessary to pass a director reference to any scene you need to load!
Bug Fixes
* mask issue fix - when loading scenes a strange problem was occurring for objects using Masks, this is now fixed by changing when each scene has its coordinates set
special thanks some some peeps in the UK!
[code]
–====================================================================–
– DIRECTOR CLASS
–====================================================================–
–[[
- Version: 1.4
- Made by Ricardo Rauber Pereira @ 2010
- Blog: http://rauberlabs.blogspot.com/
- Mail: ricardorauber@gmail.com
- Twitter: @rauberlabs
******************
-
INFORMATION
****************** -
This class is free to use, feel free to change but please send new versions
or new features like new effects to me and help us to make it better! -
Please take a look on the template.lua file and don’t forget to always
insert your display objects into the localGroup. -
If you like Director Class, please help us donating at my blog, so I could
keep doing it for free. http://rauberlabs.blogspot.com/
******************
-
HISTORY
****************** -
06-OCT-2010 - Ricardo Rauber - Created;
-
07-OCT-2010 - Ricardo Rauber - Functions loadScene and fxEnded were
taken off from the changeScene function;
Added function cleanGroups for best
memory clean up;
Added directorView and effectView groups
for better and easier control;
Please see INFORMATION to know how to use it; -
14-NOV-2010 - Ricardo Rauber - Bux fixes and new getScene function to get
the name of the active scene(lua file); -
14-FEB-2011 - Ricardo Rauber - General Bug Fixes;
-
26-APR-2011 - Ricardo Rauber - cleanGroups() changed; Added Pop Up;
-
21-JUN-2011 - Ricardo Rauber - Added error control; cleanGroups() removed;
Added touch protection; loadScene() changed;
Effects improved; Send Parameters; Bug fixes; -
28-JUN-2011 - Ricardo Rauber - Added Books;
-
05-JUL-2011 - Ricardo Rauber - Added rebuildGroup(), initVars() and Toggle Degug;
-
21-JUL-2011 - Ricardo Rauber - Search for missed objects to insert into the
right group and prevent keeping on the screen; -
25-AUG-2011 - Ricardo Rauber - Bug fixes;
– ]]
LCS = require(“core_lcs”)
Director = LCS.class {
}
function Director:init()
–====================================================================–
– TOGGLE DEBUG
–====================================================================–
self.showDebug = true
–showDebug = false
–====================================================================–
– CONTENT INFO
–====================================================================–
self._W = display.contentWidth
self._H = display.contentHeight
–====================================================================–
– DISPLAY GROUPS
–====================================================================–
self.directorView = display.newGroup()
self.currView = display.newGroup()
self.prevView = display.newGroup()
self.nextView = display.newGroup()
self.protectionView = display.newGroup()
self.popupView = display.newGroup()
self.effectView = display.newGroup()
–====================================================================–
– VARIABLES
–====================================================================–
self.prevScreen, self.currScreen, self.nextScreen, self.popupScreen = nil, nil, nil, nil
self.prevScene, self.currScene, self.nextScene, self.popupScene = “main”, “main”, “main”, “main”
self.newScene = nil
self.fxTime = 200
self.fxEnded = function(event) self:effectEnded(event) end
self.safeDelay = 50
self.isChangingScene = false
self.popUpOnClose = nil
self.isBook = false
self.bookPages = {}
self.currBookPage = 1
self.moveBookPage = function(event) self:movingBookPage(event) end
self.prevView.x = -self._W
self.prevView.y = 0
self.currView.x = 0
self.currView.y = 0
self.nextView.x = self._W
self.nextView.y = 0
self.popupView.x = 0
self.popupView.y = 0
–====================================================================–
– PROTECTION
–====================================================================–
– Rectangle
self.protection = display.newRect( -self._W, -self._H, self._W * 3, self._H * 3 )
self.protection:setFillColor( 255, 255, 255 )
self.protection.alpha = 0.01
self.protection.isVisible = false
self.protectionView:insert( self.protection )
self.fncProtection = function(event) self:touchProtection(event) end
self.protection:addEventListener( “touch”, self.fncProtection )
self.protection:addEventListener( “tap”, self.fncProtection )
end
–====================================================================–
– SHOW ERRORS
–====================================================================–
function Director:showError( errorMessage, debugMessage )
local message = "Director ERROR: " … tostring( errorMessage )
local function onComplete( event )
print()
print( “-----------------------” )
print( message )
print( “-----------------------” )
print( debugMessage )
print( “-----------------------” )
error()
end
if showDebug then
local alert = native.showAlert( “Director Class - ERROR”, message, { “OK” }, onComplete )
else
onComplete()
end
end
–====================================================================–
– GARBAGE COLLECTOR
–====================================================================–
function Director:garbageCollect( event )
collectgarbage( “collect” )
end
–====================================================================–
– IS DISPLAY OBJECT ?
–====================================================================–
function Director:isDisplayObject( aDisplayObject )
local coronaMetaTable = getmetatable( display.getCurrentStage() )
return( type( aDisplayObject ) == “table” and getmetatable( aDisplayObject ) == coronaMetaTable )
end
– Listener
function Director:touchProtection( event )
return false
end
–====================================================================–
– GET COLOR
–====================================================================–
function Director:getColor( strValue1, strValue2, strValue3 )
– Variables
local r, g, b
– Test Parameters
if type( strValue1 ) == “nil” then
strValue1 = “black”
end
– Find Color
if string.lower( tostring( strValue1 ) ) == “red” then
r = 255
g = 0
b = 0
elseif string.lower( tostring( strValue1 ) ) == “green” then
r = 0
g = 255
b = 0
elseif string.lower( tostring( strValue1 ) ) == “blue” then
r = 0
g = 0
b = 255
elseif string.lower( tostring( strValue1 ) ) == “yellow” then
r = 255
g = 255
b = 0
elseif string.lower( tostring( strValue1 ) ) == “pink” then
r = 255
g = 0
b = 255
elseif string.lower( tostring( strValue1 ) ) == “white” then
r = 255
g = 255
b = 255
elseif type( strValue1 ) == “number”
and type( strValue2 ) == “number”
and type( strValue3 ) == “number” then
r = strValue1
g = strValue2
b = strValue3
else
r = 0
g = 0
b = 0
end
– Return
return r, g, b
end
function Director:initViews()
self.directorView:insert( self.currView )
self.directorView:insert( self.prevView )
self.directorView:insert( self.nextView )
self.directorView:insert( self.protectionView )
self.directorView:insert( self.popupView )
self.directorView:insert( self.effectView )
end
–====================================================================–
– CHANGE CONTROLS
–====================================================================–
– Effects Time
function Director:changeFxTime( newFxTime )
if type( newFxTime ) == “number” then
self.fxTime = newFxTime
end
end
– Safe Delay
function Director:changeSafeDelay( newSafeDelay )
if type( newSafeDelay ) == “number” then
self.safeDelay = newSafeDelay
end
end
–====================================================================–
– GET SCENES
–====================================================================–
– Previous
function Director:getPrevScene()
return self.prevScene
end
– Current
function Director:getCurrScene()
return self.currScene
end
– Next
function Director:getNextScene()
return self.nextScene
end
–====================================================================–
– REBUILD GROUP
–====================================================================–
function Director:rebuildGroup( target )
– Verify which group
– Prev
if target == “prev” then
– Clean Group
self.prevView:removeSelf()
– Search for the localGroup.clean() function
if self.prevScreen then
if self.prevScreen.clean then
– Clean Object
local handler, message = pcall( self.prevScreen.clean )
if not handler then
self:showError( “Failed to clean object '” … self.prevScene … “’ - Please verify the localGroup.clean() function.”, message )
return false
end
end
end
– Create Group
self.prevView = display.newGroup()
– Curr
elseif target == “curr” then
– Clean Group
self.currView:removeSelf()
– Search for the localGroup.clean() function
if self.currScreen then
if self.currScreen.clean then
– Clean Object
local handler, message = pcall( self.currScreen.clean )
if not handler then
self:showError( “Failed to clean object '” … self.currScene … “’ - Please verify the localGroup.clean() function.”, message )
return false
end
end
end
– Create Group
self.currView = display.newGroup()
– Next
elseif target == “next” then
– Clean Group
self.nextView:removeSelf()
– Search for the localGroup.clean() function
if self.nextScreen then
if self.nextScreen.clean then
– Clean Object
local handler, message = pcall( self.nextScreen.clean )
if not handler then
self:showError( “Failed to clean object '” … self.nextScene … “’ - Please verify the localGroup.clean() function.”, message )
return false
end
end
end
– Create Group
self.nextView = display.newGroup()
– Popup
elseif target == “popup” then
– Clean Group
self.popupView:removeSelf()
– Search for the localGroup.clean() function
if self.popupScreen then
if self.popupScreen.clean then
– Clean Object
local handler, message = pcall( self.popupScreen.clean )
if not handler then
self:showError( “Failed to clean object '” … self.popupScene … “’ - Please verify the localGroup.clean() function.”, message )
return false
end
end
end
– Create Group
self.popupView = display.newGroup()
end
– Init
self:initViews()
end
–====================================================================–
– INITIATE VARIABLES
–====================================================================–
function Director:initVars( target )
– Verify which group
– Prev
if target == “prev” then
– Search for the localGroup.initVars() function
if self.prevScreen then
if self.prevScreen.initVars then
– Init Vars
local handler, message = pcall( prevScreen.initVars )
if not handler then
self:showError( “Failed to initiate variables of object '” … self.prevScene … “’ - Please verify the localGroup.initVars() function.”, message )
return false
end
end
end
– Curr
elseif target == “curr” then
– Search for the localGroup.initVars() function
if self.currScreen then
if self.currScreen.initVars then
– Init Vars
local handler, message = pcall( currScreen.initVars )
if not handler then
self:showError( “Failed to initiate variables of object '” … self.currScene … “’ - Please verify the localGroup.initVars() function.”, message )
return false
end
end
end
– Next
elseif target == “next” then
– Search for the localGroup.initVars() function
if self.nextScreen then
if self.nextScreen.initVars then
– Init Vars
local handler, message = pcall( nextScreen.initVars )
if not handler then
self:showError( “Failed to initiate variables of object '” … self.nextScene … “’ - Please verify the localGroup.initVars() function.”, message )
return false
end
end
end
– Popup
elseif target == “popup” then
– Search for the localGroup.initVars() function
if self.popupScreen then
if self.popupScreen.initVars then
– Init Vars
local handler, message = pcall( popupScreen.initVars )
if not handler then
self:showError( “Failed to initiate variables of object '” … self.popupScene … “’ - Please verify the localGroup.initVars() function.”, message )
return false
end
end
end
end
end
–====================================================================–
– UNLOAD SCENE
–====================================================================–
function Director:unloadScene( moduleName )
– Test parameters
if moduleName ~= “main” then
– Verify if it was loaded
if type( package.loaded[moduleName] ) ~= “nil” then
– Search for the clean() function
if package.loaded[moduleName].clean then
– Execute
local handler, message = pcall( package.loaded[moduleName].clean )
if not handler then
self:showError( “Failed to clean module '” … moduleName … “’ - Please verify the clean() function.”, message )
return false
end
end
– Try to free memory
package.loaded[moduleName] = nil
local function garbage( event )
self:garbageCollect()
end
timer.performWithDelay( self.fxTime, garbage )
end
end
end
–====================================================================–
– LOAD SCENE
–====================================================================–
function Director:loadScene( moduleName, target, params )
– Test parameters
if type( moduleName ) ~= “string” then
self:showError( "Module name must be a string. moduleName = " … tostring( moduleName ) )
return false
end
– Load Module
if not package.loaded[moduleName] then
local handler, message = pcall( require, moduleName )
if not handler then
self:showError( “Failed to load module '” … moduleName … “’ - Please check if the file exists and it is correct.”, message )
return false
end
end
– Serach new() Function
if not package.loaded[moduleName].new then
self:showError( “Module '” … tostring( moduleName ) … “’ must have a new() function.” )
return false
end
local functionName = package.loaded[moduleName].new
– Variables
local handler
– Load choosed scene
– Prev
if string.lower( target ) == “prev” then
– Rebuild Group
self:rebuildGroup( “prev” )
– Unload Scene
if self.prevScene ~= self.currScene and self.prevScene ~= self.nextScene then
self:unloadScene( moduleName )
end
– Start Scene
handler, self.prevScreen = pcall( functionName, params )
if not handler then
self:showError( “Failed to execute new( params ) function on '” … tostring( moduleName ) … “’.”, self.prevScreen )
return false
end
– Check if it returned an object
if not self:isDisplayObject( self.prevScreen ) then
self:showError( “Module " … moduleName … " must return a display.newGroup().” )
return false
end
– Books Touch Area
local bookBackground = display.newRect( 0, 0, self._W, self._H )
bookBackground.alpha = 0.01
bookBackground:addEventListener( “touch”, self.moveBookPage )
self.prevView:insert( bookBackground )
– Finish
self.prevView:insert( self.prevScreen )
self.prevScene = moduleName
– Initiate Variables
self:initVars( “prev” )
– Curr
elseif string.lower( target ) == “curr” then
– Rebuild Group
self:rebuildGroup( “curr” )
– Unload Scene
if self.prevScene ~= self.currScene and self.currScene ~= self.nextScene then
self:unloadScene( moduleName )
end
– Start Scene
handler, self.currScreen = pcall( functionName, params )
if not handler then
self:showError( “Failed to execute new( params ) function on '” … tostring( moduleName ) … “’.”, self.currScreen )
return false
end
– Check if it returned an object
if not self:isDisplayObject( self.currScreen ) then
self:showError( “Module " … moduleName … " must return a display.newGroup().” )
return false
end
– Books Touch Area
local bookBackground = display.newRect( 0, 0, self._W, self._H )
bookBackground.alpha = 0.01
bookBackground:addEventListener( “touch”, self.moveBookPage )
self.currView:insert( bookBackground )
– Finish
self.currView:insert( self.currScreen )
self.currScene = moduleName
– Initiate Variables
self:initVars( “curr” )
– Next
else
– Rebuild Group
self:rebuildGroup( “next” )
– Unload Scene
if self.prevScene ~= self.nextScene and self.currScene ~= self.nextScene then
self:unloadScene( moduleName )
end
– Start Scene
handler, self.nextScreen = pcall( functionName, params )
if not handler then
self:showError( “Failed to execute new( params ) function on '” … tostring( moduleName ) … “’.”, self.nextScreen )
return false
end
– Check if it returned an object
if not self:isDisplayObject( self.nextScreen ) then
self:showError( “Module " … moduleName … " must return a display.newGroup().” )
return false
end
– Books Touch Area
local bookBackground = display.newRect( 0, 0, self._W, self._H )
bookBackground.alpha = 0.01
bookBackground:addEventListener( “touch”, self.moveBookPage )
self.nextView:insert( bookBackground )
– Finish
self.nextView:insert( self.nextScreen )
self.nextScene = moduleName
– Initiate Variables
self:initVars( “next” )
end
– Return
return true
end
– Load prev screen
function Director:loadPrevScene( moduleName, params )
self.prevView.x = -self._W
self:loadScene( moduleName, “prev”, params )
if self.prevScreen.enterScene then
handler, result = pcall( self.prevScreen.enterScene, nil )
if not handler then
self:showError( “Failed to execute enterScene function on prev”)
end
end
end
– Load curr screen
function Director:loadCurrScene( moduleName, params )
self.currView.x = 0
self:loadScene( moduleName, “curr”, params )
if self.currScreen.enterScene then
handler, result = pcall( self.currScreen.enterScene, nil )
if not handler then
self:showError( "Failed to execute enterScene function on curr " … moduleName)
end
end
end
– Load next screen
function Director:loadNextScene( moduleName, params )
self.nextView.x = self._W
self:loadScene( moduleName, “next”, params )
if self.nextScreen.enterScene then
handler, result = pcall( self.nextScreen.enterScene, nil )
if not handler then
self:showError( “Failed to execute enterScene function on next”)
end
end
end
–====================================================================–
– EFFECT ENDED
–====================================================================–
function Director:effectEnded( event )
– Reset current view
self.currView.x = 0
self.currView.y = 0
self.currView.xScale = 1
self.currView.yScale = 1
– Rebuild Group
self:rebuildGroup( “curr” )
– Unload scene
if self.currScene ~= self.nextScene then
self:unloadScene( self.currScene )
end
– Next -> Current
self.currScreen = self.nextScreen
self.currScene = self.newScene
self.currView:insert( self.currScreen )
– Reset next view
self.nextView.x = self._W
self.nextView.y = 0
self.nextView.xScale = 1
self.nextView.yScale = 1
self.nextScene = “main”
self.nextScreen = nil
– Finish
self.isChangingScene = false
self.protection.isVisible = false
– Return
return true
end
–====================================================================–
– CHANGE SCENE
–====================================================================–
function Director:changeScene( params, nextLoadScene, effect, arg1, arg2, arg3 )
– If is changing scene, return without do anything
if self.isChangingScene then
return true
else
self.isChangingScene = true
end
– If is book, return without do anything
if self.isBook then
return true
end
– Test parameters
if type( params ) ~= “table” then
arg3 = arg2
arg2 = arg1
arg1 = effect
effect = nextLoadScene
nextLoadScene = params
params = nil
end
if type( nextLoadScene ) ~= “string” then
self:showError( "The scene name must be a string. scene = " … tostring( nextLoadScene ) )
return false
end
– If is popup, don’t change
if self.popupScene ~= “main” then
self:showError( “Could not change scene inside a popup.” )
return false
end
– Verify objects on current stage
local currentStage = display.getCurrentStage()
for i = currentStage.numChildren, 1, -1 do
– Verify directorId
if type( currentStage[i].directorId ) == “nil” then
currentStage[i].directorId = self.currScene
end
– Insert into the CURR group if it’s needed
if currentStage[i].directorId == self.currScene
and currentStage[i].directorId ~= “main” then
self.currScreen:insert( currentStage[i] )
end
end
– Prevent change to main
if nextLoadScene == “main” then
return true
end
– Protection
self.protection.isVisible = true
– Variables
self.newScene = nextLoadScene
local showFx
– Load Scene
self:loadNextScene( self.newScene, params )
– Verify objects on current stage
for i = currentStage.numChildren, 1, -1 do
– Verify directorId
if type( currentStage[i].directorId ) == “nil” then
currentStage[i].directorId = self.newScene
end
– Insert into the NEXT group if it’s needed
if currentStage[i].directorId == self.newScene
and currentStage[i].directorId ~= “main” then
self.nextScreen:insert( currentStage[i] )
end
end
– EFFECT: Move From Right
if effect == “moveFromRight” then
self.nextView.x = self._W
self.nextView.y = 0
showFx = transition.to( self.nextView, { x = 0, time = self.fxTime } )
showFx = transition.to( self.currView, { x = -self._W, time = self.fxTime, onComplete = self.fxEnded } )
– EFFECT: Over From Right
elseif effect == “overFromRight” then
self.nextView.x = self._W
self.nextView.y = 0
showFx = transition.to( self.nextView, { x = 0, time = self.fxTime, onComplete = self.fxEnded } )
– EFFECT: Move From Left
elseif effect == “moveFromLeft” then
self.nextView.x = -self._W
self.nextView.y = 0
showFx = transition.to( self.nextView, { x = 0, time = self.fxTime } )
showFx = transition.to( self.currView, { x = self._W, time = self.fxTime, onComplete = self.fxEnded } )
– EFFECT: Over From Left
elseif effect == “overFromLeft” then
self.nextView.x = -self._W
self.nextView.y = 0
showFx = transition.to( self.nextView, { x = 0, time = self.fxTime, onComplete = self.fxEnded } )
– EFFECT: Move From Top
elseif effect == “moveFromTop” then
self.nextView.x = 0
self.nextView.y = -self._H
showFx = transition.to( self.nextView, { y = 0, time = self.fxTime } )
showFx = transition.to( self.currView, { y = self._H, time = self.fxTime, onComplete = self.fxEnded } )
– EFFECT: Over From Top
elseif effect == “overFromTop” then
self.nextView.x = 0
self.nextView.y = -self._H
showFx = transition.to( self.nextView, { y = 0, time = self.fxTime, onComplete = self.fxEnded } )
– EFFECT: Move From Bottom
elseif effect == “moveFromBottom” then
self.nextView.x = 0
self.nextView.y = self._H
showFx = transition.to( self.nextView, { y = 0, time = self.fxTime } )
showFx = transition.to( self.currView, { y = -self._H, time = self.fxTime, onComplete = self.fxEnded } )
– EFFECT: Over From Bottom
elseif effect == “overFromBottom” then
self.nextView.x = 0
self.nextView.y = self._H
showFx = transition.to( self.nextView, { y = 0, time = self.fxTime, onComplete = self.fxEnded } )
– EFFECT: Crossfade
elseif effect == “crossfade” then
self.nextView.x = 0
self.nextView.y = 0
self.nextView.alpha = 0
showFx = transition.to( self.currView, { alpha = 0, time = self.fxTime * 2 } )
showFx = transition.to( self.nextView, { alpha = 1, time = self.fxTime * 2, onComplete = self.fxEnded } )
– EFFECT: Fade
– ARG1 = color [string]
– ARG1 = red [number]
– ARG2 = green [number]
– ARG3 = blue [number]
elseif effect == “fade” then
self.nextView.x = self._W
self.nextView.y = 0
local fade = display.newRect( -self._W, -self._H, self._W * 3, self._H * 3 )
fade.alpha = 0
fade:setFillColor( self:getColor( arg1, arg2, arg3 ) )
self.effectView:insert( fade )
local function returnFade( event )
self.currView.x = self._W
self.nextView.x = 0
local function removeFade( event )
fade:removeSelf()
self:fxEnded()
end
showFx = transition.to( fade, { alpha = 0, time = self.fxTime, onComplete = removeFade } )
end
showFx = transition.to( fade, { alpha = 1.0, time = self.fxTime, onComplete = returnFade } )
– EFFECT: Flip
elseif effect == “flip” then
self.nextView.xScale = 0.001
self.nextView.x = self._W / 2
local phase1, phase2
showFx = transition.to( self.currView, { xScale = 0.001, time = self.fxTime } )
showFx = transition.to( self.currView, { x = self._W / 2, time = self.fxTime } )
phase1 = function( e )
showFx = transition.to( self.nextView, { xScale = 0.001, x = self._W / 2, time = self.fxTime, onComplete = phase2 } )
end
phase2 = function( e )
showFx = transition.to( self.nextView, { xScale = 1, x = 0, time = self.fxTime, onComplete = self.fxEnded } )
end
showFx = transition.to( self.nextView, { time = 0, onComplete = phase1 } )
– EFFECT: Down Flip
elseif effect == “downFlip” then
self.nextView.x = self._W / 2
self.nextView.y = self._H * 0.15
self.nextView.xScale = 0.001
self.nextView.yScale = 0.7
local phase1, phase2, phase3, phase4
phase1 = function( e )
showFx = transition.to( self.currView, { xScale = 0.7, yScale = 0.7, x = self._W * 0.15, y = self._H * 0.15, time = self.fxTime, onComplete = phase2 } )
end
phase2 = function( e )
showFx = transition.to( self.currView, { xScale = 0.001, x = self._W / 2, time = self.fxTime, onComplete = phase3 } )
end
phase3 = function( e )
showFx = transition.to( self.nextView, { x = self._W * 0.15, xScale = 0.7, time = self.fxTime, onComplete = phase4 } )
end
phase4 = function( e )
showFx = transition.to( self.nextView, { xScale = 1, yScale = 1, x = 0, y = 0, time = self.fxTime, onComplete = self.fxEnded } )
end
showFx = transition.to( self.currView, { time = 0, onComplete = phase1 } )
– EFFECT: None
else
timer.performWithDelay( self.safeDelay, self.fxEnded )
end
– Return
return true
end
–====================================================================–
– OPEN POPUP
–====================================================================–
function Director:openPopUp( params, newPopUpScene, onClose )
– Test parameters
if type( params ) ~= “table” then
onClose = newPopUpScene
newPopUpScene = params
params = nil
end
if type( newPopUpScene ) ~= “string” then
self:showError( "Module name must be a string. moduleName = " … tostring( newPopUpScene ) )
return false
end
if type( onClose ) == “function” then
self.popUpOnClose = onClose
else
self.popUpOnClose = nil
end
– Test scene name
if newPopUpScene == self.currScene
or newPopUpScene == self.nextScene
or newPopUpScene == “main”
then
return false
end
– If is inside a popup, don’t load
if self.popupScene ~= “main” then
self:showError( “Could not load more then 1 popup.” )
return false
end
– Rebuild Group
self:rebuildGroup( “popup” )
– Unload Scene
self:unloadScene( newPopUpScene )
self.popupScene = “main”
self.popupScreen = nil
– Load scene
local handler, message = pcall( require, newPopUpScene )
if not handler then
self:showError( “Failed to load module '” … newPopUpScene … “’ - Please check if the file exists and it is correct.”, message )
return false
end
– Serach for new() function
if not package.loaded[newPopUpScene].new then
self:showError( “Module '” … tostring( newPopUpScene ) … “’ must have a new() function.” )
return false
end
– Execute new() function
local functionName = package.loaded[newPopUpScene].new
handler, self.popupScreen = pcall( functionName, params )
if not handler then
self:showError( “Failed to execute news( params ) function on '” … tostring( self.moduleName ) … “’.”, self.popupScreen )
return false
end
– Test if a group was returned
if not self:isDisplayObject( self.currScreen ) then
self:showError( “Module " … tostring( self.moduleName ) … " must return a display.newGroup().” )
return false
end
self.popupView:insert( self.popupScreen )
self.popupScene = newPopUpScene
– Initiate Variables
self:initVars( “popup” )
– Protection
self.protection.isVisible = true
– Return
return true
end
–====================================================================–
– CLOSE POPUP
–====================================================================–
function Director:closePopUp(params)
– If no popup is loaded, don’t do anything
if self.popupScene == “main” then
return true
end
– Rebuild Group
self:rebuildGroup( “popup” )
– Unload Scene
self:unloadScene( self.popupScene )
self.popupScene = “main”
self.popupScreen = nil
– Protection
self.protection.isVisible = false
– Call function
if type( self.popUpOnClose ) == “function” then
self.popUpOnClose(params)
end
– Return
return true
end
–====================================================================–
– VERIFY IF IS BOOK
–====================================================================–
function Director:isBook()
return self.isBook
end
–====================================================================–
– GET CURRENT PAGE NAME
–====================================================================–
function Director:getCurrBookPage()
if self.bookPages[self.currBookPage] then
return self.bookPages[self.currBookPage]
end
end
–====================================================================–
– GET CURRENT PAGE NUMBER
–====================================================================–
function Director:getCurrBookPageNum ()
return self.currBookPage
end
–====================================================================–
– GET PAGE COUNT
–====================================================================–
function Director:getBookPageCount ()
return table.maxn( self.bookPages )
end
–====================================================================–
– INSERT PAGES
–====================================================================–
function Director:newBookPages( pageList, page, fade )
– If is not book, return without do anything
if not self.isBook then
return true
end
– Test parameters
if type( pageList ) ~= “table” then
return true
end
– Clean it
while self:getBookPageCount() > 0 do
table.remove( self.bookPages )
end
– Mount
local i = 1
while pageList[i] do
if type( pageList[i] ) == “table” then
self.bookPages[i] = pageList[i]
i = i + 1
end
end
– Fade
local fadeFx = display.newRect( 0, 0, self._W, self._H )
fadeFx:setFillColor( 0, 0, 0 )
fadeFx.alpha = 0
fadeFx.isVisible = false
self.effectView:insert( fadeFx )
local fxEnded = function( e )
– Remove Fade
fadeFx:removeSelf()
fadeFx = nil
end
– Go to page
local change = function( e )
– Rebuild Group
self:rebuildGroup( “prev” )
– Unload Scene
if self.prevScene ~= self.bookPages[page] and self.prevScene ~= self.bookPages[page-1] then
self:unloadScene( self.prevScene )
end
self.prevScene = “main”
– Rebuild Group
self:rebuildGroup( “next” )
– Unload Scene
if self.nextScene ~= self.bookPages[page] and self.nextScene ~= self.bookPages[page+1] then
self:unloadScene( self.nextScene )
end
self.nextScene = “main”
– Load pages 1 and 2
if self.bookPages[page] then
self:loadCurrScene( self.bookPages[page].page, self.bookPages[page].params )
self.currBookPage = page
end
if self.bookPages[page+1] then
self:loadNextScene( self.bookPages[page+1].page, self.bookPages[page+1].params )
end
if self.bookPages[page-1] then
self:loadPrevScene( self.bookPages[page-1].page, self.bookPages[page-1].params )
end
– Search for the localGroup.start() function
if self.currScreen then
if self.currScreen.start then
– Start Page
local handler, message = pcall( self.currScreen.start )
if not handler then
self:showError( “Failed to start page of object '” … self.currScene … “’ - Please verify the localGroup.start() function.”, message )
return false
end
end
end
– Complete Fade
local showFx = transition.to( fadeFx, { alpha = 0, time = self.fxTime, onComplete = fxEnded } )
end
– Execute Fade
if fade then
fadeFx.isVisible = true
local showFx = transition.to( fadeFx, { alpha = 1, time = self.fxTime, onComplete = change } )
else
change()
end
end
–====================================================================–
– CHANGE BOOK PAGE
–====================================================================–
function Director:changeBookPage( target )
– If is not book, return without do anything
if not self.isBook then
return true
end
– If is changing scene, return without do anything
if self.isChangingScene then
return true
else
self.isChangingScene = true
end
– Test parameters
if type( target ) == nil then
return true
end
if string.lower( target ) == “next” then
if self:getCurrBookPageNum() + 1 < self:getBookPageCount() then
target = self:getCurrBookPageNum() + 1
else
target = self:getBookPageCount()
end
elseif string.lower( target ) == “prev” then
if self:getCurrBookPageNum() - 1 > 1 then
target = self:getCurrBookPageNum() - 1
else
target = 1
end
end
if not type( target ) == “number” then
return true
end
local showFx
– Prevent page -1
if target < 1 or
target > self:getBookPageCount() or
target == self:getCurrBookPageNum() then
local bookFxEnded = function( event )
self.isChangingScene = false
end
showFx = transition.to( self.prevView, { x = -self._W, time = self.fxTime, transition = easing.outQuad } )
showFx = transition.to( self.currView, { x = 0, time = self.fxTime, transition = easing.outQuad } )
showFx = transition.to( self.nextView, { x = self._W, time = self.fxTime, transition = easing.outQuad, onComplete = bookFxEnded } )
else
– Animate to the correct side
– Moved left
if target > self:getCurrBookPageNum() then
local bookFxEnded = function( event )
– Rebuild Group
self:rebuildGroup( “prev” )
– Unload Scene
if self.prevScene ~= self.currScene and self.prevScene ~= self.nextScene then
self:unloadScene( prevScene )
end
– Curr -> Prev
self.prevScreen = self.currScreen
self.prevScene = self.currScene
self.prevView:insert( self.prevScreen )
self.prevView.x = -self._W
– Initiate Variables
self:initVars( “prev” )
– Next -> Curr
self.currScreen = self.nextScreen
self.currScene = self.nextScene
self.currView:insert( self.currScreen )
self.currView.x = 0
self.nextScreen = nil
– Load Scene
if self.bookPages[target + 1] then
self:loadNextScene( self.bookPages[target + 1].page, self.bookPages[target + 1].params )
end
self.nextView.x = self._W
– Finish
self.currBookPage = target
self.isChangingScene = false
– Search for the localGroup.start() function
if self.currScreen then
if self.currScreen.start then
– Start Page
local handler, message = pcall( self.currScreen.start )
if not handler then
self:showError( “Failed to start page of object '” … self.currScene … “’ - Please verify the localGroup.start() function.”, message )
return false
end
end
end
end
showFx = transition.to( self.prevView, { x = -self._W, time = self.fxTime, transition = easing.outQuad } )
showFx = transition.to( self.currView, { x = -self._W, time = self.fxTime, transition = easing.outQuad } )
showFx = transition.to( self.nextView, { x = 0, time = self.fxTime, transition = easing.outQuad, onComplete = bookFxEnded } )
– Moved right
else
local bookFxEnded = function( event )
– Rebuild Group
self:rebuildGroup( “next” )
– Unload Scene
if self.prevScene ~= self.nextScene and self.currScene ~= self.nextScene then
self:unloadScene( self.nextScene )
end
– Curr -> Next
self.nextScreen = self.currScreen
self.nextScene = self.currScene
self.nextView:insert( self.nextScreen )
self.nextView.x = self._W
– Initiate Variables
self:initVars( “next” )
– Prev -> Curr
self.currScreen = self.prevScreen
self.currScene = self.prevScene
self.currView:insert( self.currScreen )
self.currView.x = 0
self.prevScreen = nil
– Load Scene
if self.bookPages[target - 1] then
self:loadPrevScene( self.bookPages[target - 1].page, self.bookPages[target - 1].params )
end
self.prevView.x = -self._W
– Finish
self.currBookPage = target
self.isChangingScene = false
– Search for the localGroup.start() function
if self.currScreen then
if self.currScreen.start then
– Start Page
local handler, message = pcall( self.currScreen.start )
if not handler then
self:showError( “Failed to start page of object '” … self.currScene … “’ - Please verify the localGroup.start() function.”, message )
return false
end
end
end
end
showFx = transition.to( self.prevView, { x = 0, time = self.fxTime, transition = easing.outQuad } )
showFx = transition.to( self.currView, { x = self._W, time = self.fxTime, transition = easing.outQuad } )
showFx = transition.to( self.nextView, { x = self._W, time = self.fxTime, transition = easing.outQuad, onComplete = bookFxEnded } )
end
end
end
–====================================================================–
– MOVE TO CHANGE BOOK PAGE
–====================================================================–
function Director:movingBookPage( event )
local result = false
– If is not book, return without do anything
if not self.isBook then
return true
end
– If is changing scene, return without do anything
if self.isChangingScene then
return true
end
– Verify event
if event.phase == “moved” then
display.getCurrentStage():setFocus(event.target)
– Move pages while touching
if event.xStart > event.x then
self.prevView.x = -self._W - ( event.xStart - event.x )
self.currView.x = 0 - ( event.xStart - event.x )
self.nextView.x = self._W - ( event.xStart - event.x )
else
self.prevView.x = -self._W + ( event.x - event.xStart )
self.currView.x = 0 + ( event.x - event.xStart )
self.nextView.x = self._W + ( event.x - event.xStart )
end
elseif event.phase == “ended” then
– If page reach limit then change
local limit = 0.2
if self.currView.x < -self._W * limit then
self:changeBookPage( “next” )
elseif self.currView.x > self._W * limit then
self:changeBookPage( “prev” )
else
self:changeBookPage( self:getCurrBookPageNum() )
end
display.getCurrentStage():setFocus( event.target, nil )
event.target.isFocus = false
end
return result
end
–====================================================================–
– GO TO BOOK PAGE
–====================================================================–
function Director:goToPage( params, target, fade )
– Test parameters
if type( params ) ~= “table” then
fade = target
target = params
params = nil
end
if type( target ) ~= “number” then
self:showError( "The page name must be a number. page = " … tostring( target ) )
return false
end
if target < 1 then
self:showError( "Cannot change to page lower then 1. page = " … tostring( target ) )
return false
end
– Fade
local fadeFx = display.newRect( -self._W, -self._H, self._W * 3, self._H * 3 )
fadeFx:setFillColor( 0, 0, 0 )
fadeFx.alpha = 0
fadeFx.isVisible = false
self.effectView:insert( fadeFx )
local fxEnded = function( e )
– Remove Fade
fadeFx:removeSelf()
fadeFx = nil
end
– Go to page
local change = function( e )
– First page
if target == 1 then
– Rebuild Group
self:rebuildGroup( “prev” )
– Unload Scene
if self.prevScene ~= self.currScene and self.prevScene ~= self.nextScene then
self:unloadScene( prevScene )
end
– Load Scenes
if self.bookPages[1] then
self:loadCurrScene( self.bookPages[1].page, params or self.bookPages[1].params )
self.currBookPage = 1
end
if self.bookPages[2] then
self:loadNextScene( self.bookPages[2].page, self.bookPages[2].params )
end
– Last page
elseif target == self:getBookPageCount() then
– Load Scenes
if self.bookPages[target] then
self:loadCurrScene( self.bookPages[target].page, params or self.bookPages[target].params )
self.currBookPage = target
end
if self.bookPages[target - 1] then
self:loadPrevScene( self.ookPages[target - 1].page, self.bookPages[target - 1].params )
end
– Rebuild Group
self:rebuildGroup( “next” )
– Unload Scene
if self.prevScene ~= self.nextScene and self.currScene ~= self.nextScene then
self:unloadScene( self.nextScene )
end
– Somewhere in the middle
else
– Load Scenes
if self.bookPages[target-1] then
self:loadPrevScene( self.bookPages[target - 1].page, self.bookPages[target - 1].params )
end
if self.bookPages[target] then
self:loadCurrScene( self.bookPages[target].page, params or self.bookPages[target].params )
self.currBookPage = target
end
if self.bookPages[target + 1] then
self:loadNextScene( self.bookPages[target + 1].page, self.bookPages[target + 1].params )
end
end
– Search for the localGroup.start() function
if self.currScreen then
if self.currScreen.start then
– Start Page
local handler, message = pcall( self.currScreen.start )
if not handler then
self:showError( “Failed to start page of object '” … self.currScene … “’ - Please verify the localGroup.start() function.”, message )
return false
end
end
end
– Complete Fade
local showFx = transition.to( fadeFx, { alpha = 0, time = self.fxTime, onComplete = fxEnded } )
end
– Execute Fade
if fade then
fadeFx.isVisible = true
local showFx = transition.to( fadeFx, { alpha = 1, time = self.fxTime, onComplete = change } )
else
change()
end
end
–====================================================================–
– TURN TO BOOK OR SCENES
–====================================================================–
function Director:turnToBook()
self.isBook = true
end
function Director:turnToScenes()
– Toggle Books
self.isBook = false
– Rebuild Group
self:rebuildGroup( “prev” )
– Unload Scene
if self.prevScene ~= self.currScene and self.prevScene ~= self.nextScene then
self:unloadScene( self.prevScene )
end
– Rebuild Group
self:rebuildGroup( “next” )
– Unload Scene
if self.prevScene ~= self.nextScene and self.currScene ~= self.nextScene then
self:unloadScene( self.nextScene )
end
– Finish
self.prevScreen = nil
self.nextScreen = nil
self.prevScene = “main”
self.nextScene = “main”
end
[/code] [import]uid: 152006 topic_id: 27996 reply_id: 327996[/import]