├── README.markdown ├── Scene Manager.gproj ├── button.lua ├── easing.lua ├── gfx ├── crossfade-down.png ├── crossfade-up.png ├── fade-down.png ├── fade-up.png ├── flip with fade-down.png ├── flip with fade-up.png ├── flip with shade-down.png ├── flip with shade-up.png ├── flip-down.png ├── flip-up.png ├── move with fade-down.png ├── move with fade-up.png ├── move-down.png ├── move-up.png ├── over with fade-down.png ├── over with fade-up.png ├── over-down.png ├── over-up.png ├── scene1.jpg ├── scene2.jpg ├── scene3.jpg └── scene4.jpg ├── main.lua ├── scene1.lua ├── scene2.lua ├── scene3.lua ├── scene4.lua └── scenemanager.lua /README.markdown: -------------------------------------------------------------------------------- 1 | SceneManager for Gideros -------------------------------------------------------------------------------- /Scene Manager.gproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | -------------------------------------------------------------------------------- /button.lua: -------------------------------------------------------------------------------- 1 | --[[ 2 | A generic button class 3 | 4 | This code is MIT licensed, see http://www.opensource.org/licenses/mit-license.php 5 | (C) 2010 - 2011 Gideros Mobile 6 | ]] 7 | 8 | Button = gideros.class(Sprite) 9 | 10 | function Button:init(upState, downState) 11 | self.upState = upState 12 | self.downState = downState 13 | 14 | self.focus = false 15 | 16 | self:updateVisualState(false) 17 | 18 | self:addEventListener(Event.MOUSE_DOWN, self.onMouseDown, self) 19 | self:addEventListener(Event.MOUSE_MOVE, self.onMouseMove, self) 20 | self:addEventListener(Event.MOUSE_UP, self.onMouseUp, self) 21 | 22 | self:addEventListener(Event.TOUCHES_BEGIN, self.onTouchesBegin, self) 23 | self:addEventListener(Event.TOUCHES_MOVE, self.onTouchesMove, self) 24 | self:addEventListener(Event.TOUCHES_END, self.onTouchesEnd, self) 25 | self:addEventListener(Event.TOUCHES_CANCEL, self.onTouchesCancel, self) 26 | end 27 | 28 | function Button:onMouseDown(event) 29 | if self:hitTestPoint(event.x, event.y) then 30 | self.focus = true 31 | self:updateVisualState(true) 32 | event:stopPropagation() 33 | end 34 | end 35 | 36 | function Button:onMouseMove(event) 37 | if self.focus then 38 | if not self:hitTestPoint(event.x, event.y) then 39 | self.focus = false; 40 | self:updateVisualState(false) 41 | end 42 | event:stopPropagation() 43 | end 44 | end 45 | 46 | function Button:onMouseUp(event) 47 | if self.focus then 48 | self.focus = false; 49 | self:updateVisualState(false) 50 | self:dispatchEvent(Event.new("click")) 51 | event:stopPropagation() 52 | end 53 | end 54 | 55 | -- if button is on focus, stop propagation of touch events 56 | function Button:onTouchesBegin(event) 57 | if self.focus then 58 | event:stopPropagation() 59 | end 60 | end 61 | 62 | -- if button is on focus, stop propagation of touch events 63 | function Button:onTouchesMove(event) 64 | if self.focus then 65 | event:stopPropagation() 66 | end 67 | end 68 | 69 | -- if button is on focus, stop propagation of touch events 70 | function Button:onTouchesEnd(event) 71 | if self.focus then 72 | event:stopPropagation() 73 | end 74 | end 75 | 76 | -- if touches are cancelled, reset the state of the button 77 | function Button:onTouchesCancel(event) 78 | if self.focus then 79 | self.focus = false; 80 | self:updateVisualState(false) 81 | event:stopPropagation() 82 | end 83 | end 84 | 85 | -- if state is true show downState else show upState 86 | function Button:updateVisualState(state) 87 | if state then 88 | if self:contains(self.upState) then 89 | self:removeChild(self.upState) 90 | end 91 | 92 | if not self:contains(self.downState) then 93 | self:addChild(self.downState) 94 | end 95 | else 96 | if self:contains(self.downState) then 97 | self:removeChild(self.downState) 98 | end 99 | 100 | if not self:contains(self.upState) then 101 | self:addChild(self.upState) 102 | end 103 | end 104 | end 105 | -------------------------------------------------------------------------------- /easing.lua: -------------------------------------------------------------------------------- 1 | --[[ 2 | Author of this easing functions for Lua is Josh Tynjala 3 | https://github.com/joshtynjala/gtween.lua 4 | Licensed under the MIT license. 5 | 6 | Easing functions adapted from Robert Penner's AS3 tweening equations. 7 | ]] 8 | 9 | local backS = 1.70158 10 | easing = {}; 11 | easing.inBack = function(ratio) 12 | return ratio*ratio*((backS+1)*ratio-backS) 13 | end 14 | easing.outBack = function(ratio) 15 | ratio = ratio - 1 16 | return ratio*ratio*((backS+1)*ratio+backS)+1 17 | end 18 | easing.inOutBack = function(ratio) 19 | ratio = ratio * 2 20 | if ratio < 1 then 21 | return 0.5*(ratio*ratio*((backS*1.525+1)*ratio-backS*1.525)) 22 | else 23 | ratio = ratio - 2 24 | return 0.5*(ratio*ratio*((backS*1.525+1)*ratio+backS*1.525)+2) 25 | end 26 | end 27 | easing.inBounce = function(ratio) 28 | return 1-easing.outBounce(1-ratio,0,0,0) 29 | end 30 | easing.outBounce = function(ratio) 31 | if ratio < 1/2.75 then 32 | return 7.5625*ratio*ratio 33 | elseif ratio < 2/2.75 then 34 | ratio = ratio - 1.5/2.75 35 | return 7.5625*ratio*ratio+0.75 36 | elseif ratio < 2.5/2.75 then 37 | ratio= ratio - 2.25/2.75 38 | return 7.5625*ratio*ratio+0.9375 39 | else 40 | ratio = ratio - 2.625/2.75 41 | return 7.5625*ratio*ratio+0.984375 42 | end 43 | end 44 | easing.inOutBounce = function(ratio) 45 | ratio = ratio * 2 46 | if ratio < 1 then 47 | return 0.5*easing.inBounce(ratio,0,0,0) 48 | else 49 | return 0.5*easing.outBounce(ratio-1,0,0,0)+0.5 50 | end 51 | end 52 | easing.inCircular = function(ratio) 53 | return -(math.sqrt(1-ratio*ratio)-1) 54 | end 55 | easing.outCircular = function(ratio) 56 | return math.sqrt(1-(ratio-1)*(ratio-1)) 57 | end 58 | easing.inOutCircular = function(ratio) 59 | ratio = ratio * 2 60 | if ratio < 1 then 61 | return -0.5*(math.sqrt(1-ratio*ratio)-1) 62 | else 63 | ratio = ratio - 2 64 | return 0.5*(math.sqrt(1-ratio*ratio)+1) 65 | end 66 | end 67 | easing.inCubic = function(ratio) 68 | return ratio*ratio*ratio 69 | end 70 | easing.outCubic = function(ratio) 71 | ratio = ratio - 1 72 | return ratio*ratio*ratio+1 73 | end 74 | easing.inOutCubic = function(ratio) 75 | if ratio < 0.5 then 76 | return 4*ratio*ratio*ratio 77 | else 78 | ratio = ratio - 1 79 | return 4*ratio*ratio*ratio+1 80 | end 81 | end 82 | local elasticA = 1; 83 | local elasticP = 0.3; 84 | local elasticS = elasticP/4; 85 | easing.inElastic = function(ratio) 86 | if ratio == 0 or ratio == 1 then 87 | return ratio 88 | end 89 | ratio = ratio - 1 90 | return -(elasticA * math.pow(2, 10 * ratio) * math.sin((ratio - elasticS) * (2 * math.pi) / elasticP)); 91 | end 92 | easing.outElastic = function(ratio) 93 | if ratio == 0 or ratio == 1 then 94 | return ratio 95 | end 96 | return elasticA * math.pow(2, -10 * ratio) * math.sin((ratio - elasticS) * (2 * math.pi) / elasticP) + 1; 97 | end 98 | easing.inOutElastic = function(ratio) 99 | if ratio == 0 or ratio == 1 then 100 | return ratio 101 | end 102 | ratio = ratio*2-1 103 | if ratio < 0 then 104 | return -0.5 * (elasticA * math.pow(2, 10 * ratio) * math.sin((ratio - elasticS*1.5) * (2 * math.pi) /(elasticP*1.5))); 105 | end 106 | return 0.5 * elasticA * math.pow(2, -10 * ratio) * math.sin((ratio - elasticS*1.5) * (2 * math.pi) / (elasticP*1.5)) + 1; 107 | end 108 | easing.inExponential = function(ratio) 109 | if ratio == 0 then 110 | return 0 111 | end 112 | return math.pow(2, 10 * (ratio - 1)) 113 | end 114 | easing.outExponential = function(ratio) 115 | if ratio == 1 then 116 | return 1 117 | end 118 | return 1-math.pow(2, -10 * ratio) 119 | end 120 | easing.inOutExponential = function(ratio) 121 | if ratio == 0 or ratio == 1 then 122 | return ratio 123 | end 124 | ratio = ratio*2-1 125 | if 0 > ratio then 126 | return 0.5*math.pow(2, 10*ratio) 127 | end 128 | return 1-0.5*math.pow(2, -10*ratio) 129 | end 130 | easing.linear = function(ratio) 131 | return ratio 132 | end 133 | easing.inQuadratic = function(ratio) 134 | return ratio*ratio 135 | end 136 | easing.outQuadratic = function(ratio) 137 | return -ratio*(ratio-2) 138 | end 139 | easing.inOutQuadratic = function(ratio) 140 | if ratio < 0.5 then 141 | return 2*ratio*ratio 142 | end 143 | return -2*ratio*(ratio-2)-1 144 | end 145 | easing.inQuartic = function(ratio) 146 | return ratio*ratio*ratio*ratio 147 | end 148 | easing.outQuartic = function(ratio) 149 | ratio = ratio - 1 150 | return 1-ratio*ratio*ratio*ratio 151 | end 152 | easing.inOutQuartic = function(ratio) 153 | if ratio < 0.5 then 154 | return 8*ratio*ratio*ratio*ratio 155 | end 156 | ratio = ratio - 1 157 | return -8*ratio*ratio*ratio*ratio+1 158 | end 159 | easing.inQuintic = function(ratio) 160 | return ratio*ratio*ratio*ratio*ratio 161 | end 162 | easing.outQuintic = function(ratio) 163 | ratio = ratio - 1 164 | return 1+ratio*ratio*ratio*ratio*ratio 165 | end 166 | easing.inOutQuintic = function(ratio) 167 | if ratio < 0.5 then 168 | return 16*ratio*ratio*ratio*ratio*ratio 169 | end 170 | ratio = ratio - 1 171 | return 16*ratio*ratio*ratio*ratio*ratio+1 172 | end 173 | easing.inSine = function(ratio) 174 | return 1-math.cos(ratio * (math.pi / 2)) 175 | end 176 | easing.outSine = function(ratio) 177 | return math.sin(ratio * (math.pi / 2)) 178 | end 179 | easing.inOutSine = function(ratio) 180 | return -0.5*(math.cos(ratio*math.pi)-1) 181 | end 182 | -------------------------------------------------------------------------------- /gfx/crossfade-down.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gideros/Scene-Manager/HEAD/gfx/crossfade-down.png -------------------------------------------------------------------------------- /gfx/crossfade-up.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gideros/Scene-Manager/HEAD/gfx/crossfade-up.png -------------------------------------------------------------------------------- /gfx/fade-down.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gideros/Scene-Manager/HEAD/gfx/fade-down.png -------------------------------------------------------------------------------- /gfx/fade-up.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gideros/Scene-Manager/HEAD/gfx/fade-up.png -------------------------------------------------------------------------------- /gfx/flip with fade-down.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gideros/Scene-Manager/HEAD/gfx/flip with fade-down.png -------------------------------------------------------------------------------- /gfx/flip with fade-up.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gideros/Scene-Manager/HEAD/gfx/flip with fade-up.png -------------------------------------------------------------------------------- /gfx/flip with shade-down.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gideros/Scene-Manager/HEAD/gfx/flip with shade-down.png -------------------------------------------------------------------------------- /gfx/flip with shade-up.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gideros/Scene-Manager/HEAD/gfx/flip with shade-up.png -------------------------------------------------------------------------------- /gfx/flip-down.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gideros/Scene-Manager/HEAD/gfx/flip-down.png -------------------------------------------------------------------------------- /gfx/flip-up.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gideros/Scene-Manager/HEAD/gfx/flip-up.png -------------------------------------------------------------------------------- /gfx/move with fade-down.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gideros/Scene-Manager/HEAD/gfx/move with fade-down.png -------------------------------------------------------------------------------- /gfx/move with fade-up.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gideros/Scene-Manager/HEAD/gfx/move with fade-up.png -------------------------------------------------------------------------------- /gfx/move-down.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gideros/Scene-Manager/HEAD/gfx/move-down.png -------------------------------------------------------------------------------- /gfx/move-up.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gideros/Scene-Manager/HEAD/gfx/move-up.png -------------------------------------------------------------------------------- /gfx/over with fade-down.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gideros/Scene-Manager/HEAD/gfx/over with fade-down.png -------------------------------------------------------------------------------- /gfx/over with fade-up.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gideros/Scene-Manager/HEAD/gfx/over with fade-up.png -------------------------------------------------------------------------------- /gfx/over-down.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gideros/Scene-Manager/HEAD/gfx/over-down.png -------------------------------------------------------------------------------- /gfx/over-up.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gideros/Scene-Manager/HEAD/gfx/over-up.png -------------------------------------------------------------------------------- /gfx/scene1.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gideros/Scene-Manager/HEAD/gfx/scene1.jpg -------------------------------------------------------------------------------- /gfx/scene2.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gideros/Scene-Manager/HEAD/gfx/scene2.jpg -------------------------------------------------------------------------------- /gfx/scene3.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gideros/Scene-Manager/HEAD/gfx/scene3.jpg -------------------------------------------------------------------------------- /gfx/scene4.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gideros/Scene-Manager/HEAD/gfx/scene4.jpg -------------------------------------------------------------------------------- /main.lua: -------------------------------------------------------------------------------- 1 | --[[ 2 | This code is MIT licensed, see http://www.opensource.org/licenses/mit-license.php 3 | (C) 2010 - 2011 Gideros Mobile 4 | ]] 5 | 6 | local sceneManager = SceneManager.new({ 7 | ["scene1"] = Scene1, 8 | ["scene2"] = Scene2, 9 | ["scene3"] = Scene3, 10 | ["scene4"] = Scene4, 11 | }) 12 | 13 | sceneManager:addEventListener("transitionBegin", function() print("manager - transition begin") end) 14 | sceneManager:addEventListener("transitionEnd", function() print("manager - transition end") end) 15 | 16 | stage:addChild(sceneManager) 17 | 18 | local transitions = { 19 | SceneManager.moveFromLeft, 20 | SceneManager.moveFromRight, 21 | SceneManager.moveFromBottom, 22 | SceneManager.moveFromTop, 23 | SceneManager.moveFromLeftWithFade, 24 | SceneManager.moveFromRightWithFade, 25 | SceneManager.moveFromBottomWithFade, 26 | SceneManager.moveFromTopWithFade, 27 | SceneManager.overFromLeft, 28 | SceneManager.overFromRight, 29 | SceneManager.overFromBottom, 30 | SceneManager.overFromTop, 31 | SceneManager.overFromLeftWithFade, 32 | SceneManager.overFromRightWithFade, 33 | SceneManager.overFromBottomWithFade, 34 | SceneManager.overFromTopWithFade, 35 | SceneManager.fade, 36 | SceneManager.crossFade, 37 | SceneManager.flip, 38 | SceneManager.flipWithFade, 39 | SceneManager.flipWithShade, 40 | } 41 | 42 | local scenes = {"scene1", "scene2", "scene3", "scene4"} 43 | 44 | local currentScene = 1 45 | local function nextScene() 46 | local next = scenes[currentScene] 47 | 48 | currentScene = currentScene + 1 49 | if currentScene > #scenes then 50 | currentScene = 1 51 | end 52 | 53 | return next 54 | end 55 | 56 | local moveButton = Button.new(Bitmap.new(Texture.new("gfx/move-up.png")), Bitmap.new(Texture.new("gfx/move-down.png"))) 57 | moveButton:setPosition(20, 10) 58 | stage:addChild(moveButton) 59 | 60 | local moveWithFadeButton = Button.new(Bitmap.new(Texture.new("gfx/move with fade-up.png")), Bitmap.new(Texture.new("gfx/move with fade-down.png"))) 61 | moveWithFadeButton:setPosition(160, 10) 62 | stage:addChild(moveWithFadeButton) 63 | 64 | local overButton = Button.new(Bitmap.new(Texture.new("gfx/over-up.png")), Bitmap.new(Texture.new("gfx/over-down.png"))) 65 | overButton:setPosition(20, 50) 66 | stage:addChild(overButton) 67 | 68 | local overWithFadeButton = Button.new(Bitmap.new(Texture.new("gfx/over with fade-up.png")), Bitmap.new(Texture.new("gfx/over with fade-down.png"))) 69 | overWithFadeButton:setPosition(160, 50) 70 | stage:addChild(overWithFadeButton) 71 | 72 | local fadeButton = Button.new(Bitmap.new(Texture.new("gfx/fade-up.png")), Bitmap.new(Texture.new("gfx/fade-down.png"))) 73 | fadeButton:setPosition(20, 90) 74 | stage:addChild(fadeButton) 75 | 76 | local crossfadeButton = Button.new(Bitmap.new(Texture.new("gfx/crossfade-up.png")), Bitmap.new(Texture.new("gfx/crossfade-down.png"))) 77 | crossfadeButton:setPosition(160, 90) 78 | stage:addChild(crossfadeButton) 79 | 80 | local flipButton = Button.new(Bitmap.new(Texture.new("gfx/flip-up.png")), Bitmap.new(Texture.new("gfx/flip-down.png"))) 81 | flipButton:setPosition(20, 130) 82 | stage:addChild(flipButton) 83 | 84 | local flipWithFadeButton = Button.new(Bitmap.new(Texture.new("gfx/flip with fade-up.png")), Bitmap.new(Texture.new("gfx/flip with fade-down.png"))) 85 | flipWithFadeButton:setPosition(160, 130) 86 | stage:addChild(flipWithFadeButton) 87 | 88 | local flipWithShadeButton = Button.new(Bitmap.new(Texture.new("gfx/flip with shade-up.png")), Bitmap.new(Texture.new("gfx/flip with shade-down.png"))) 89 | flipWithShadeButton:setPosition(20, 170) 90 | stage:addChild(flipWithShadeButton) 91 | 92 | moveButton:addEventListener("click", 93 | function() 94 | local transition = transitions[math.random(1, 4)] 95 | sceneManager:changeScene(nextScene(), 1, transition, easing.outBack, { eventFilter={Event.MOUSE_DOWN} } ) 96 | end) 97 | 98 | moveWithFadeButton:addEventListener("click", 99 | function() 100 | local transition = transitions[math.random(5, 8)] 101 | sceneManager:changeScene(nextScene(), 1, transition, easing.outQuadratic, { eventFilter={Event.MOUSE_DOWN} } ) 102 | end) 103 | 104 | overButton:addEventListener("click", 105 | function() 106 | local transition = transitions[math.random(9, 12)] 107 | sceneManager:changeScene(nextScene(), 1, transition, easing.outBounce, { userData = "outBounce" } ) 108 | end) 109 | 110 | overWithFadeButton:addEventListener("click", 111 | function() 112 | local transition = transitions[math.random(13, 16)] 113 | sceneManager:changeScene(nextScene(), 1, transition, easing.outQuadratic, { userData = "hello", eventFilter={Event.MOUSE_DOWN} } ) 114 | end) 115 | 116 | fadeButton:addEventListener("click", 117 | function() 118 | sceneManager:changeScene(nextScene(), 1, SceneManager.fade, easing.linear) 119 | end) 120 | 121 | crossfadeButton:addEventListener("click", 122 | function() 123 | sceneManager:changeScene(nextScene(), 1, SceneManager.crossfade, easing.linear) 124 | end) 125 | 126 | flipButton:addEventListener("click", 127 | function() 128 | sceneManager:changeScene(nextScene(), 1, SceneManager.flip, easing.inOutQuadratic) 129 | end) 130 | 131 | flipWithFadeButton:addEventListener("click", 132 | function() 133 | sceneManager:changeScene(nextScene(), 1, SceneManager.flipWithFade, easing.inOutQuadratic) 134 | end) 135 | 136 | flipWithShadeButton:addEventListener("click", 137 | function() 138 | sceneManager:changeScene(nextScene(), 1, SceneManager.flipWithShade, easing.inOutQuadratic) 139 | end) 140 | 141 | sceneManager:changeScene(nextScene()) 142 | -------------------------------------------------------------------------------- /scene1.lua: -------------------------------------------------------------------------------- 1 | Scene1 = gideros.class(Sprite) 2 | 3 | function Scene1:init(t) 4 | if t then 5 | print("Scene1: ", t) 6 | end 7 | 8 | self:addChild(Bitmap.new(Texture.new("gfx/scene1.jpg"))) 9 | 10 | self:addEventListener("enterBegin", self.onTransitionInBegin, self) 11 | self:addEventListener("enterEnd", self.onTransitionInEnd, self) 12 | self:addEventListener("exitBegin", self.onTransitionOutBegin, self) 13 | self:addEventListener("exitEnd", self.onTransitionOutEnd, self) 14 | end 15 | 16 | function Scene1:onTransitionInBegin() 17 | print("scene1 - enter begin") 18 | end 19 | 20 | function Scene1:onTransitionInEnd() 21 | print("scene1 - enter end") 22 | end 23 | 24 | function Scene1:onTransitionOutBegin() 25 | print("scene1 - exit begin") 26 | end 27 | 28 | function Scene1:onTransitionOutEnd() 29 | print("scene1 - exit end") 30 | end 31 | -------------------------------------------------------------------------------- /scene2.lua: -------------------------------------------------------------------------------- 1 | Scene2 = gideros.class(Sprite) 2 | 3 | function Scene2:init(t) 4 | if t then 5 | print("Scene2: ", t) 6 | end 7 | 8 | self:addChild(Bitmap.new(Texture.new("gfx/scene2.jpg"))) 9 | 10 | self:addEventListener("enterBegin", self.onTransitionInBegin, self) 11 | self:addEventListener("enterEnd", self.onTransitionInEnd, self) 12 | self:addEventListener("exitBegin", self.onTransitionOutBegin, self) 13 | self:addEventListener("exitEnd", self.onTransitionOutEnd, self) 14 | end 15 | 16 | function Scene2:onTransitionInBegin() 17 | print("scene2 - enter begin") 18 | end 19 | 20 | function Scene2:onTransitionInEnd() 21 | print("scene2 - enter end") 22 | end 23 | 24 | function Scene2:onTransitionOutBegin() 25 | print("scene2 - exit begin") 26 | end 27 | 28 | function Scene2:onTransitionOutEnd() 29 | print("scene2 - exit end") 30 | end 31 | -------------------------------------------------------------------------------- /scene3.lua: -------------------------------------------------------------------------------- 1 | Scene3 = gideros.class(Sprite) 2 | 3 | function Scene3:init(t) 4 | if t then 5 | print("Scene3: ", t) 6 | end 7 | 8 | self:addChild(Bitmap.new(Texture.new("gfx/scene3.jpg"))) 9 | 10 | self:addEventListener("enterBegin", self.onTransitionInBegin, self) 11 | self:addEventListener("enterEnd", self.onTransitionInEnd, self) 12 | self:addEventListener("exitBegin", self.onTransitionOutBegin, self) 13 | self:addEventListener("exitEnd", self.onTransitionOutEnd, self) 14 | end 15 | 16 | function Scene3:onTransitionInBegin() 17 | print("scene3 - enter begin") 18 | end 19 | 20 | function Scene3:onTransitionInEnd() 21 | print("scene3 - enter end") 22 | end 23 | 24 | function Scene3:onTransitionOutBegin() 25 | print("scene3 - exit begin") 26 | end 27 | 28 | function Scene3:onTransitionOutEnd() 29 | print("scene3 - exit end") 30 | end 31 | -------------------------------------------------------------------------------- /scene4.lua: -------------------------------------------------------------------------------- 1 | Scene4 = gideros.class(Sprite) 2 | 3 | function Scene4:init(t) 4 | if t then 5 | print("Scene4: ", t) 6 | end 7 | 8 | self:addChild(Bitmap.new(Texture.new("gfx/scene4.jpg"))) 9 | 10 | self:addEventListener("enterBegin", self.onTransitionInBegin, self) 11 | self:addEventListener("enterEnd", self.onTransitionInEnd, self) 12 | self:addEventListener("exitBegin", self.onTransitionOutBegin, self) 13 | self:addEventListener("exitEnd", self.onTransitionOutEnd, self) 14 | end 15 | 16 | function Scene4:onTransitionInBegin() 17 | print("scene4 - enter begin") 18 | end 19 | 20 | function Scene4:onTransitionInEnd() 21 | print("scene4 - enter end") 22 | end 23 | 24 | function Scene4:onTransitionOutBegin() 25 | print("scene4 - exit begin") 26 | end 27 | 28 | function Scene4:onTransitionOutEnd() 29 | print("scene4 - exit end") 30 | end 31 | -------------------------------------------------------------------------------- /scenemanager.lua: -------------------------------------------------------------------------------- 1 | --[[ 2 | SceneManager v1.0.4 3 | 4 | changelog: 5 | ---------- 6 | 7 | v1.0.4 - 08.04.2012 8 | Added option to filter a list of events during transitions 9 | Moved increment of time to end of onEnterFrame so that time goes from 0 to 1 10 | Added additional "real time" argument to dispatched transitions 11 | Added option to pass user data to a scene when it gets created 12 | 13 | v1.0.3 - 19.11.2011 14 | Fixed incorrect calculation of width/height in landscape modes 15 | 16 | v1.0.2 - 17.11.2011 17 | Change event names 18 | 19 | v1.0.1 - 06.11.2011 20 | Add collectgarbage() to the end of transition 21 | 22 | v1.0 - 06.11.2011 23 | Initial release 24 | 25 | 26 | This code is MIT licensed, see http://www.opensource.org/licenses/mit-license.php 27 | (C) 2010 - 2011 Gideros Mobile 28 | ]] 29 | 30 | 31 | SceneManager = Core.class(Sprite) 32 | 33 | --[[ 34 | The following two deltas will correct the screen offsets for different screen sizes and 35 | resolutions depending on a projects scale mode setting. Without them transitions between 36 | scenes would take the wrong dimensions and end with ugly flickering or sudden disappearance 37 | of objects. 38 | ]] 39 | local dx = 2 * math.ceil(application:getLogicalTranslateX() / application:getLogicalScaleX()) 40 | local dy = 2 * math.ceil(application:getLogicalTranslateY() / application:getLogicalScaleY()) 41 | 42 | 43 | function SceneManager.moveFromRight(scene1, scene2, t) 44 | local width = application:getContentWidth() + dx 45 | 46 | scene1:setX(-t * width) 47 | scene2:setX((1 - t) * width) 48 | end 49 | 50 | function SceneManager.moveFromLeft(scene1, scene2, t) 51 | local width = application:getContentWidth() + dx 52 | 53 | scene1:setX(t * width) 54 | scene2:setX((t - 1) * width) 55 | end 56 | 57 | function SceneManager.overFromRight(scene1, scene2, t) 58 | local width = application:getContentWidth() + dx 59 | 60 | scene2:setX((1 - t) * width) 61 | end 62 | 63 | function SceneManager.overFromLeft(scene1, scene2, t) 64 | local width = application:getContentWidth() + dx 65 | 66 | scene2:setX((t - 1) * width) 67 | end 68 | 69 | function SceneManager.moveFromRightWithFade(scene1, scene2, t) 70 | local width = application:getContentWidth() + dx 71 | 72 | scene1:setAlpha(1 - t) 73 | scene1:setX(-t * width) 74 | scene2:setX((1 - t) * width) 75 | end 76 | 77 | function SceneManager.moveFromLeftWithFade(scene1, scene2, t) 78 | local width = application:getContentWidth() + dx 79 | 80 | scene1:setAlpha(1 - t) 81 | scene1:setX(t * width) 82 | scene2:setX((t - 1) * width) 83 | end 84 | 85 | function SceneManager.overFromRightWithFade(scene1, scene2, t) 86 | local width = application:getContentWidth() + dx 87 | 88 | scene1:setAlpha(1 - t) 89 | scene2:setX((1 - t) * width) 90 | end 91 | 92 | function SceneManager.overFromLeftWithFade(scene1, scene2, t) 93 | local width = application:getContentWidth() + dx 94 | 95 | scene1:setAlpha(1 - t) 96 | scene2:setX((t - 1) * width) 97 | end 98 | 99 | function SceneManager.moveFromBottom(scene1, scene2, t) 100 | local height = application:getContentHeight() + dy 101 | 102 | scene1:setY(-t * height) 103 | scene2:setY((1 - t) * height) 104 | end 105 | 106 | function SceneManager.moveFromTop(scene1, scene2, t) 107 | local height = application:getContentHeight() + dy 108 | 109 | scene1:setY(t * height) 110 | scene2:setY((t - 1) * height) 111 | end 112 | 113 | function SceneManager.overFromBottom(scene1, scene2, t) 114 | local height = application:getContentHeight() + dy 115 | 116 | scene2:setY((1 - t) * height) 117 | end 118 | 119 | function SceneManager.overFromTop(scene1, scene2, t) 120 | local height = application:getContentHeight() + dy 121 | 122 | scene2:setY((t - 1) * height) 123 | end 124 | 125 | function SceneManager.moveFromBottomWithFade(scene1, scene2, t) 126 | local height = application:getContentHeight() + dy 127 | 128 | scene1:setAlpha(1 - t) 129 | scene1:setY(-t * height) 130 | scene2:setY((1 - t) * height) 131 | end 132 | 133 | function SceneManager.moveFromTopWithFade(scene1, scene2, t) 134 | local height = application:getContentHeight() + dy 135 | 136 | scene1:setAlpha(1 - t) 137 | scene1:setY(t * height) 138 | scene2:setY((t - 1) * height) 139 | end 140 | 141 | 142 | function SceneManager.overFromBottomWithFade(scene1, scene2, t) 143 | local height = application:getContentHeight() + dy 144 | 145 | scene1:setAlpha(1 - t) 146 | scene2:setY((1 - t) * height) 147 | end 148 | 149 | function SceneManager.overFromTopWithFade(scene1, scene2, t) 150 | local height = application:getContentHeight() + dy 151 | 152 | scene1:setAlpha(1 - t) 153 | scene2:setY((t - 1) * height) 154 | end 155 | 156 | function SceneManager.fade(scene1, scene2, t) 157 | if t < 0.5 then 158 | scene1:setAlpha((0.5 - t) * 2) 159 | else 160 | scene1:setAlpha(0) 161 | end 162 | 163 | if t < 0.5 then 164 | scene2:setAlpha(0) 165 | else 166 | scene2:setAlpha((t - 0.5) * 2) 167 | end 168 | end 169 | 170 | function SceneManager.crossfade(scene1, scene2, t) 171 | scene1:setAlpha(1 - t) 172 | scene2:setAlpha(t) 173 | end 174 | 175 | function SceneManager.flip(scene1, scene2, t) 176 | local width = application:getContentWidth() + dx 177 | 178 | if t < 0.5 then 179 | local s = (0.5 - t) * 2 180 | scene1:setScaleX(s) 181 | scene1:setX((1 - s) * width * 0.5) 182 | else 183 | scene1:setScaleX(0) 184 | scene1:setX(width * 0.5) 185 | end 186 | 187 | if t < 0.5 then 188 | scene2:setScaleX(0) 189 | scene2:setX(width * 0.5) 190 | else 191 | local s = (t - 0.5) * 2 192 | scene2:setScaleX(s) 193 | scene2:setX((1 - s) * width * 0.5) 194 | end 195 | end 196 | 197 | function SceneManager.flipWithFade(scene1, scene2, t) 198 | local width = application:getContentWidth() + dx 199 | 200 | if t < 0.5 then 201 | local s = (0.5 - t) * 2 202 | scene1:setScaleX(s) 203 | scene1:setX((1 - s) * width * 0.5) 204 | scene1:setAlpha(s) 205 | else 206 | scene1:setScaleX(0) 207 | scene1:setX(width * 0.5) 208 | scene1:setAlpha(0) 209 | end 210 | 211 | if t < 0.5 then 212 | scene2:setScaleX(0) 213 | scene2:setX(width * 0.5) 214 | scene2:setAlpha(0) 215 | else 216 | local s = (t - 0.5) * 2 217 | scene2:setScaleX(s) 218 | scene2:setX((1 - s) * width * 0.5) 219 | scene2:setAlpha(s) 220 | end 221 | end 222 | 223 | function SceneManager.flipWithShade(scene1, scene2, t) 224 | local width = application:getContentWidth() + dx 225 | 226 | if t < 0.5 then 227 | local s = (0.5 - t) * 2 228 | scene1:setScaleX(s) 229 | scene1:setX((1 - s) * width * 0.5) 230 | scene1:setColorTransform(1 - t, 1 - t, 1 - t, 1) 231 | else 232 | scene1:setScaleX(0) 233 | scene1:setX(width * 0.5) 234 | scene1:setColorTransform(0.5, 0.5, 0.5, 1) 235 | end 236 | 237 | if t < 0.5 then 238 | scene2:setScaleX(0) 239 | scene2:setX(width * 0.5) 240 | scene2:setColorTransform(0.5, 0.5, 0.5, 1) 241 | else 242 | local s = (t - 0.5) * 2 243 | scene2:setScaleX(s) 244 | scene2:setX((1 - s) * width * 0.5) 245 | scene2:setColorTransform(t, t, t, 1) 246 | end 247 | end 248 | 249 | local function dispatchEvent(dispatcher, name) 250 | if dispatcher:hasEventListener(name) then 251 | dispatcher:dispatchEvent(Event.new(name)) 252 | end 253 | end 254 | 255 | local function defaultEase(ratio) 256 | return ratio 257 | end 258 | 259 | function SceneManager:init(scenes) 260 | self.scenes = scenes 261 | self.tweening = false 262 | self.transitionEventCatcher = Sprite.new() 263 | self:addEventListener(Event.ENTER_FRAME, self.onEnterFrame, self) 264 | end 265 | 266 | function SceneManager:changeScene(scene, duration, transition, ease, options) 267 | self.eventFilter = options and options.eventFilter 268 | 269 | if self.tweening then 270 | return 271 | end 272 | 273 | if self.scene1 == nil then 274 | self.scene1 = self.scenes[scene].new(options and options.userData) 275 | self:addChild(self.scene1) 276 | dispatchEvent(self, "transitionBegin") 277 | dispatchEvent(self.scene1, "enterBegin") 278 | dispatchEvent(self, "transitionEnd") 279 | dispatchEvent(self.scene1, "enterEnd") 280 | return 281 | end 282 | 283 | self.duration = duration 284 | self.transition = transition 285 | self.ease = ease or defaultEase 286 | 287 | self.scene2 = self.scenes[scene].new(options and options.userData) 288 | self.scene2:setVisible(false) 289 | self:addChild(self.scene2) 290 | 291 | self.time = 0 292 | self.currentTimer = os.timer() 293 | self.tweening = true 294 | end 295 | 296 | function SceneManager:filterTransitionEvents(event) 297 | event:stopPropagation() 298 | end 299 | 300 | function SceneManager:onTransitionBegin() 301 | if self.eventFilter then 302 | stage:addChild(self.transitionEventCatcher) 303 | for i,event in ipairs(self.eventFilter) do 304 | self.transitionEventCatcher:addEventListener(event, self.filterTransitionEvents, self) 305 | end 306 | end 307 | end 308 | 309 | function SceneManager:onTransitionEnd() 310 | if self.eventFilter then 311 | for i,event in ipairs(self.eventFilter) do 312 | self.transitionEventCatcher:removeEventListener(event, self.filterTransitionEvents, self) 313 | end 314 | self.transitionEventCatcher:removeFromParent() 315 | end 316 | end 317 | 318 | function SceneManager:onEnterFrame(event) 319 | if not self.tweening then 320 | return 321 | end 322 | 323 | if self.time == 0 then 324 | self:onTransitionBegin() 325 | self.scene2:setVisible(true) 326 | dispatchEvent(self, "transitionBegin") 327 | dispatchEvent(self.scene1, "exitBegin") 328 | dispatchEvent(self.scene2, "enterBegin") 329 | end 330 | 331 | local timer = os.timer() 332 | local deltaTime = timer - self.currentTimer 333 | self.currentTimer = timer 334 | 335 | local t = (self.duration == 0) and 1 or (self.time / self.duration) 336 | 337 | self.transition(self.scene1, self.scene2, self.ease(t), t) 338 | 339 | if self.time == self.duration then 340 | dispatchEvent(self, "transitionEnd") 341 | dispatchEvent(self.scene1, "exitEnd") 342 | dispatchEvent(self.scene2, "enterEnd") 343 | self:onTransitionEnd() 344 | 345 | self:removeChild(self.scene1) 346 | self.scene1 = self.scene2 347 | self.scene2 = nil 348 | self.tweening = false 349 | 350 | collectgarbage() 351 | end 352 | 353 | self.time = self.time + deltaTime 354 | 355 | if self.time > self.duration then 356 | self.time = self.duration 357 | end 358 | 359 | end 360 | --------------------------------------------------------------------------------