From 1392e189438f0bcfb4f4086a497c88338fb7e236 Mon Sep 17 00:00:00 2001 From: Eetu Rantanen Date: Sat, 17 Apr 2021 19:33:43 +0300 Subject: [PATCH] Clean code & fix screen resize/orientation events There were a few localised functions that were never used. There was also a large chunk of code concerning display.capture() and/or screenshot, but none of the calculated variables were actually used for anything, so I removed them. The larger changes were concerning the touch/tap blocking sensors and the scene transition effects. The current (old) version of Composer creates touch/tap blocking sensors, but one of them would be mispositioned if the app is using graphics v1 compatibility mode. Also, neither of these sensors move or update in size if the screen orientation or display size change. They were also using display.contentWidth and display.contentHeight, so they didn't block touch/tap events on devices with different display aspect ratios than defined in config.lua. The current version of Composer also creates a table for scene transition effects when the library gets required for the first time. This means that if the screen size changes due to orientation or resize event, then future scene transition effects will still play out like the screen dimensions hadn't changed. These effects were also not using actual display width and height values, so they jumped in and out of the screen if the display's aspect ratio differed from config.lua's values. With these changes, Composer library will use the correct values when it is first required. It also adds a new API, `composer.onScreenChange()`, which the user may call at any time during or after orientation and resize events to update Composer's touch sensors and scene transition effects. NB! This does not resize or reposition on-going scene transitions. --- This commit leaves the composer_scene as is, but there is a known problem that requiring Composer in a project will automatically require physics and json libraries as a part of composer_scene. This is a relic of the old Corona SDK Composer GUI Beta product. --- composer.lua | 968 +++++++++++++++++++++++++-------------------------- 1 file changed, 481 insertions(+), 487 deletions(-) diff --git a/composer.lua b/composer.lua index 6a1c1a1..7702207 100644 --- a/composer.lua +++ b/composer.lua @@ -1,10 +1,10 @@ ----------------------------------------------------------------------------------------- -- --- Corona Labs +-- Corona Labs & others -- -- composer.lua -- --- Code is MIT licensed; see https://www.coronalabs.com/links/code/license +-- Code is MIT licensed; see https://github.com/coronalabs/corona/blob/master/LICENSE.md -- ----------------------------------------------------------------------------------------- @@ -58,500 +58,516 @@ lib.debugPrefix = "COMPOSER: " -- localized variables local _tonumber = tonumber -local _pairs = pairs local _toString = tostring -local _stringSub = string.sub -local _stringFind = string.find local _type = type -local _stringFormat = string.format -local _getInfo = system.getInfo -local displayW = display.contentWidth -local displayH = display.contentHeight +local displayW = nil +local displayH = nil local isGraphicsV1 = ( 1 == display.getDefault( "graphicsCompatibility" ) ) ----------------------------------------------------------------------------------------- -- TRANSITION EFFECTS -local effectList = { - ["fade"] = - { - ["from"] = - { - alphaStart = 1.0, - alphaEnd = 0, - }, - - ["to"] = - { - alphaStart = 0, - alphaEnd = 1.0 - } - }, - - ["zoomOutIn"] = - { - ["from"] = - { - xEnd = displayW*0.5, - yEnd = displayH*0.5, - xScaleEnd = 0.001, - yScaleEnd = 0.001 - }, - - ["to"] = - { - xScaleStart = 0.001, - yScaleStart = 0.001, - xScaleEnd = 1.0, - yScaleEnd = 1.0, - xStart = displayW*0.5, - yStart = displayH*0.5, - xEnd = 0, - yEnd = 0 - }, - hideOnOut = true - }, - - ["zoomOutInFade"] = - { - ["from"] = - { - xEnd = displayW*0.5, - yEnd = displayH*0.5, - xScaleEnd = 0.001, - yScaleEnd = 0.001, - alphaStart = 1.0, - alphaEnd = 0 - }, - - ["to"] = - { - xScaleStart = 0.001, - yScaleStart = 0.001, - xScaleEnd = 1.0, - yScaleEnd = 1.0, - xStart = displayW*0.5, - yStart = displayH*0.5, - xEnd = 0, - yEnd = 0, - alphaStart = 0, - alphaEnd = 1.0 - }, - hideOnOut = true - }, - - ["zoomInOut"] = - { - ["from"] = - { - xEnd = -displayW*0.5, - yEnd = -displayH*0.5, - xScaleEnd = 2.0, - yScaleEnd = 2.0 - }, - - ["to"] = - { - xScaleStart = 2.0, - yScaleStart = 2.0, - xScaleEnd = 1.0, - yScaleEnd = 1.0, - xStart = -displayW*0.5, - yStart = -displayH*0.5, - xEnd = 0, - yEnd = 0 - }, - hideOnOut = true - }, - - ["zoomInOutFade"] = - { - ["from"] = - { - xEnd = -displayW*0.5, - yEnd = -displayH*0.5, - xScaleEnd = 2.0, - yScaleEnd = 2.0, - alphaStart = 1.0, - alphaEnd = 0 - }, - - ["to"] = - { - xScaleStart = 2.0, - yScaleStart = 2.0, - xScaleEnd = 1.0, - yScaleEnd = 1.0, - xStart = -displayW*0.5, - yStart = -displayH*0.5, - xEnd = 0, - yEnd = 0, - alphaStart = 0, - alphaEnd = 1.0 - }, - hideOnOut = true - }, - - ["flip"] = - { - ["from"] = - { - xEnd = displayW*0.5, - xScaleEnd = 0.001 - }, - - ["to"] = - { - xScaleStart = 0.001, - xScaleEnd = 1.0, - xStart = displayW*0.5, - xEnd = 0 - } - }, - - ["flipFadeOutIn"] = - { - ["from"] = - { - xEnd = displayW*0.5, - xScaleEnd = 0.001, - alphaStart = 1.0, - alphaEnd = 0 - }, - - ["to"] = - { - xScaleStart = 0.001, - xScaleEnd = 1.0, - xStart = displayW*0.5, - xEnd = 0, - alphaStart = 0, - alphaEnd = 1.0 - } - }, - - ["zoomOutInRotate"] = - { - ["from"] = - { - xEnd = displayW*0.5, - yEnd = displayH*0.5, - xScaleEnd = 0.001, - yScaleEnd = 0.001, - rotationStart = 0, - rotationEnd = -360 - }, - - ["to"] = - { - xScaleStart = 0.001, - yScaleStart = 0.001, - xScaleEnd = 1.0, - yScaleEnd = 1.0, - xStart = displayW*0.5, - yStart = displayH*0.5, - xEnd = 0, - yEnd = 0, - rotationStart = -360, - rotationEnd = 0 - }, - hideOnOut = true - }, - - ["zoomOutInFadeRotate"] = - { - ["from"] = - { - xEnd = displayW*0.5, - yEnd = displayH*0.5, - xScaleEnd = 0.001, - yScaleEnd = 0.001, - rotationStart = 0, - rotationEnd = -360, - alphaStart = 1.0, - alphaEnd = 0 - }, - - ["to"] = - { - xScaleStart = 0.001, - yScaleStart = 0.001, - xScaleEnd = 1.0, - yScaleEnd = 1.0, - xStart = displayW*0.5, - yStart = displayH*0.5, - xEnd = 0, - yEnd = 0, - rotationStart = -360, - rotationEnd = 0, - alphaStart = 0, - alphaEnd = 1.0 - }, - hideOnOut = true - }, +local effectList = nil +local function updateEffects() + local xCenter, yCenter = display.contentCenterX, display.contentCenterY + displayW, displayH = display.actualContentWidth, display.actualContentHeight - ["zoomInOutRotate"] = - { - ["from"] = - { - xEnd = displayW*0.5, - yEnd = displayH*0.5, - xScaleEnd = 2.0, - yScaleEnd = 2.0, - rotationStart = 0, - rotationEnd = -360 - }, - - ["to"] = + local effects = { + ["fade"] = { - xScaleStart = 2.0, - yScaleStart = 2.0, - xScaleEnd = 1.0, - yScaleEnd = 1.0, - xStart = displayW*0.5, - yStart = displayH*0.5, - xEnd = 0, - yEnd = 0, - rotationStart = -360, - rotationEnd = 0 - }, - hideOnOut = true - }, - - ["zoomInOutFadeRotate"] = - { - ["from"] = - { - xEnd = displayW*0.5, - yEnd = displayH*0.5, - xScaleEnd = 2.0, - yScaleEnd = 2.0, - rotationStart = 0, - rotationEnd = -360, - alphaStart = 1.0, - alphaEnd = 0 + ["from"] = + { + alphaStart = 1.0, + alphaEnd = 0, + }, + + ["to"] = + { + alphaStart = 0, + alphaEnd = 1.0 + } }, - - ["to"] = + + ["zoomOutIn"] = { - xScaleStart = 2.0, - yScaleStart = 2.0, - xScaleEnd = 1.0, - yScaleEnd = 1.0, - xStart = displayW*0.5, - yStart = displayH*0.5, - xEnd = 0, - yEnd = 0, - rotationStart = -360, - rotationEnd = 0, - alphaStart = 0, - alphaEnd = 1.0 + ["from"] = + { + xEnd = xCenter, + yEnd = yCenter, + xScaleEnd = 0.001, + yScaleEnd = 0.001 + }, + + ["to"] = + { + xScaleStart = 0.001, + yScaleStart = 0.001, + xScaleEnd = 1.0, + yScaleEnd = 1.0, + xStart = xCenter, + yStart = yCenter, + xEnd = 0, + yEnd = 0 + }, + hideOnOut = true }, - hideOnOut = true - }, - - ["fromRight"] = - { - ["from"] = + + ["zoomOutInFade"] = { - xStart = 0, - yStart = 0, - xEnd = 0, - yEnd = 0, - transition = easing.outQuad + ["from"] = + { + xEnd = xCenter, + yEnd = yCenter, + xScaleEnd = 0.001, + yScaleEnd = 0.001, + alphaStart = 1.0, + alphaEnd = 0 + }, + + ["to"] = + { + xScaleStart = 0.001, + yScaleStart = 0.001, + xScaleEnd = 1.0, + yScaleEnd = 1.0, + xStart = xCenter, + yStart = yCenter, + xEnd = 0, + yEnd = 0, + alphaStart = 0, + alphaEnd = 1.0 + }, + hideOnOut = true }, - - ["to"] = + + ["zoomInOut"] = { - xStart = displayW, - yStart = 0, - xEnd = 0, - yEnd = 0, - transition = easing.outQuad + ["from"] = + { + xEnd = -xCenter, + yEnd = -yCenter, + xScaleEnd = 2.0, + yScaleEnd = 2.0 + }, + + ["to"] = + { + xScaleStart = 2.0, + yScaleStart = 2.0, + xScaleEnd = 1.0, + yScaleEnd = 1.0, + xStart = -xCenter, + yStart = -yCenter, + xEnd = 0, + yEnd = 0 + }, + hideOnOut = true }, - concurrent = true, - sceneAbove = true - }, - - ["fromLeft"] = - { - ["from"] = + + ["zoomInOutFade"] = { - xStart = 0, - yStart = 0, - xEnd = 0, - yEnd = 0, - transition = easing.outQuad + ["from"] = + { + xEnd = -xCenter, + yEnd = -yCenter, + xScaleEnd = 2.0, + yScaleEnd = 2.0, + alphaStart = 1.0, + alphaEnd = 0 + }, + + ["to"] = + { + xScaleStart = 2.0, + yScaleStart = 2.0, + xScaleEnd = 1.0, + yScaleEnd = 1.0, + xStart = -xCenter, + yStart = -yCenter, + xEnd = 0, + yEnd = 0, + alphaStart = 0, + alphaEnd = 1.0 + }, + hideOnOut = true }, - - ["to"] = + + ["flip"] = { - xStart = -displayW, - yStart = 0, - xEnd = 0, - yEnd = 0, - transition = easing.outQuad + ["from"] = + { + xEnd = xCenter, + xScaleEnd = 0.001 + }, + + ["to"] = + { + xScaleStart = 0.001, + xScaleEnd = 1.0, + xStart = xCenter, + xEnd = 0 + } }, - concurrent = true, - sceneAbove = true - }, - - ["fromTop"] = - { - ["from"] = + + ["flipFadeOutIn"] = { - xStart = 0, - yStart = 0, - xEnd = 0, - yEnd = 0, - transition = easing.outQuad + ["from"] = + { + xEnd = xCenter, + xScaleEnd = 0.001, + alphaStart = 1.0, + alphaEnd = 0 + }, + + ["to"] = + { + xScaleStart = 0.001, + xScaleEnd = 1.0, + xStart = xCenter, + xEnd = 0, + alphaStart = 0, + alphaEnd = 1.0 + } }, - - ["to"] = + + ["zoomOutInRotate"] = { - xStart = 0, - yStart = -displayH, - xEnd = 0, - yEnd = 0, - transition = easing.outQuad + ["from"] = + { + xEnd = xCenter, + yEnd = yCenter, + xScaleEnd = 0.001, + yScaleEnd = 0.001, + rotationStart = 0, + rotationEnd = -360 + }, + + ["to"] = + { + xScaleStart = 0.001, + yScaleStart = 0.001, + xScaleEnd = 1.0, + yScaleEnd = 1.0, + xStart = xCenter, + yStart = yCenter, + xEnd = 0, + yEnd = 0, + rotationStart = -360, + rotationEnd = 0 + }, + hideOnOut = true }, - concurrent = true, - sceneAbove = true - }, - - ["fromBottom"] = - { - ["from"] = + + ["zoomOutInFadeRotate"] = { - xStart = 0, - yStart = 0, - xEnd = 0, - yEnd = 0, - transition = easing.outQuad + ["from"] = + { + xEnd = xCenter, + yEnd = yCenter, + xScaleEnd = 0.001, + yScaleEnd = 0.001, + rotationStart = 0, + rotationEnd = -360, + alphaStart = 1.0, + alphaEnd = 0 + }, + + ["to"] = + { + xScaleStart = 0.001, + yScaleStart = 0.001, + xScaleEnd = 1.0, + yScaleEnd = 1.0, + xStart = xCenter, + yStart = yCenter, + xEnd = 0, + yEnd = 0, + rotationStart = -360, + rotationEnd = 0, + alphaStart = 0, + alphaEnd = 1.0 + }, + hideOnOut = true }, - - ["to"] = + + ["zoomInOutRotate"] = { - xStart = 0, - yStart = displayH, - xEnd = 0, - yEnd = 0, - transition = easing.outQuad + ["from"] = + { + xEnd = xCenter, + yEnd = yCenter, + xScaleEnd = 2.0, + yScaleEnd = 2.0, + rotationStart = 0, + rotationEnd = -360 + }, + + ["to"] = + { + xScaleStart = 2.0, + yScaleStart = 2.0, + xScaleEnd = 1.0, + yScaleEnd = 1.0, + xStart = xCenter, + yStart = yCenter, + xEnd = 0, + yEnd = 0, + rotationStart = -360, + rotationEnd = 0 + }, + hideOnOut = true }, - concurrent = true, - sceneAbove = true - }, - - ["slideLeft"] = - { - ["from"] = + + ["zoomInOutFadeRotate"] = { - xStart = 0, - yStart = 0, - xEnd = -displayW, - yEnd = 0, - transition = easing.outQuad + ["from"] = + { + xEnd = xCenter, + yEnd = yCenter, + xScaleEnd = 2.0, + yScaleEnd = 2.0, + rotationStart = 0, + rotationEnd = -360, + alphaStart = 1.0, + alphaEnd = 0 + }, + + ["to"] = + { + xScaleStart = 2.0, + yScaleStart = 2.0, + xScaleEnd = 1.0, + yScaleEnd = 1.0, + xStart = xCenter, + yStart = yCenter, + xEnd = 0, + yEnd = 0, + rotationStart = -360, + rotationEnd = 0, + alphaStart = 0, + alphaEnd = 1.0 + }, + hideOnOut = true }, - - ["to"] = + + ["fromRight"] = { - xStart = displayW, - yStart = 0, - xEnd = 0, - yEnd = 0, - transition = easing.outQuad + ["from"] = + { + xStart = 0, + yStart = 0, + xEnd = 0, + yEnd = 0, + transition = easing.outQuad + }, + + ["to"] = + { + xStart = displayW, + yStart = 0, + xEnd = 0, + yEnd = 0, + transition = easing.outQuad + }, + concurrent = true, + sceneAbove = true }, - concurrent = true, - sceneAbove = true - }, - - ["slideRight"] = - { - ["from"] = + + ["fromLeft"] = { - xStart = 0, - yStart = 0, - xEnd = displayW, - yEnd = 0, - transition = easing.outQuad + ["from"] = + { + xStart = 0, + yStart = 0, + xEnd = 0, + yEnd = 0, + transition = easing.outQuad + }, + + ["to"] = + { + xStart = -displayW, + yStart = 0, + xEnd = 0, + yEnd = 0, + transition = easing.outQuad + }, + concurrent = true, + sceneAbove = true }, - - ["to"] = + + ["fromTop"] = { - xStart = -displayW, - yStart = 0, - xEnd = 0, - yEnd = 0, - transition = easing.outQuad + ["from"] = + { + xStart = 0, + yStart = 0, + xEnd = 0, + yEnd = 0, + transition = easing.outQuad + }, + + ["to"] = + { + xStart = 0, + yStart = -displayH, + xEnd = 0, + yEnd = 0, + transition = easing.outQuad + }, + concurrent = true, + sceneAbove = true }, - concurrent = true, - sceneAbove = true - }, - - ["slideDown"] = - { - ["from"] = + + ["fromBottom"] = { - xStart = 0, - yStart = 0, - xEnd = 0, - yEnd = displayH, - transition = easing.outQuad + ["from"] = + { + xStart = 0, + yStart = 0, + xEnd = 0, + yEnd = 0, + transition = easing.outQuad + }, + + ["to"] = + { + xStart = 0, + yStart = displayH, + xEnd = 0, + yEnd = 0, + transition = easing.outQuad + }, + concurrent = true, + sceneAbove = true }, - - ["to"] = + + ["slideLeft"] = { - xStart = 0, - yStart = -displayH, - xEnd = 0, - yEnd = 0, - transition = easing.outQuad + ["from"] = + { + xStart = 0, + yStart = 0, + xEnd = -displayW, + yEnd = 0, + transition = easing.outQuad + }, + + ["to"] = + { + xStart = displayW, + yStart = 0, + xEnd = 0, + yEnd = 0, + transition = easing.outQuad + }, + concurrent = true, + sceneAbove = true }, - concurrent = true, - sceneAbove = true - }, - - ["slideUp"] = - { - ["from"] = + + ["slideRight"] = { - xStart = 0, - yStart = 0, - xEnd = 0, - yEnd = -displayH, - transition = easing.outQuad + ["from"] = + { + xStart = 0, + yStart = 0, + xEnd = displayW, + yEnd = 0, + transition = easing.outQuad + }, + + ["to"] = + { + xStart = -displayW, + yStart = 0, + xEnd = 0, + yEnd = 0, + transition = easing.outQuad + }, + concurrent = true, + sceneAbove = true }, - - ["to"] = - { - xStart = 0, - yStart = displayH, - xEnd = 0, - yEnd = 0, - transition = easing.outQuad + + ["slideDown"] = + { + ["from"] = + { + xStart = 0, + yStart = 0, + xEnd = 0, + yEnd = displayH, + transition = easing.outQuad + }, + + ["to"] = + { + xStart = 0, + yStart = -displayH, + xEnd = 0, + yEnd = 0, + transition = easing.outQuad + }, + concurrent = true, + sceneAbove = true }, - concurrent = true, - sceneAbove = true - }, - - ["crossFade"] = - { - ["from"] = + + ["slideUp"] = { - alphaStart = 1.0, - alphaEnd = 0, + ["from"] = + { + xStart = 0, + yStart = 0, + xEnd = 0, + yEnd = -displayH, + transition = easing.outQuad + }, + + ["to"] = + { + xStart = 0, + yStart = displayH, + xEnd = 0, + yEnd = 0, + transition = easing.outQuad + }, + concurrent = true, + sceneAbove = true }, - - ["to"] = + + ["crossFade"] = { - alphaStart = 0, - alphaEnd = 1.0 - }, - concurrent = true + ["from"] = + { + alphaStart = 1.0, + alphaEnd = 0, + }, + + ["to"] = + { + alphaStart = 0, + alphaEnd = 1.0 + }, + concurrent = true + } } -} -lib.effectList = effectList + -- Update effects and display objects that are display size dependant. + lib.effectList = effects + effectList = effects + if lib._modalRect then + lib._modalRect.x = display.screenOriginX + lib._modalRect.y = display.screenOriginY + lib._modalRect.width = displayW + lib._modalRect.height = displayH + end + if lib._touchOverlay then + lib._touchOverlay.x = display.screenOriginX + lib._touchOverlay.y = display.screenOriginY + lib._touchOverlay.width = displayW + lib._touchOverlay.height = displayH + end +end +updateEffects() ----------------------------------------------------------------------------------------- @@ -605,40 +621,11 @@ lib._saveSceneAndHide = function( currentScene, newModule, noEffect ) if not currentScene then return; end local screenshot if currentScene and currentScene.numChildren and currentScene.numChildren > 0 and not noEffect then - --screenshot = display.capture( currentScene ) screenshot = currentScene elseif noEffect and currentScene then currentScene.isVisible = false end - -- Since display.capture() only captures the group as far as content width/height, - -- we must make calculations to account for groups that are both less than the total width/height - -- of the screen, as well as groups that are offset have elements that are not on the screen: - local bounds = currentScene.contentBounds - local xMin, xMax = bounds.xMin, bounds.xMax - local yMin, yMax = bounds.yMin, bounds.yMax - - local objectsOutsideLeft = xMin < display.screenOriginX - local objectsOutsideRight = xMax > displayW+(-display.screenOriginX) - local objectsAboveTop = yMin < display.screenOriginY - local objectsBelowBottom = yMax > displayH+(-display.screenOriginY) - - -- Calculate xMin and xMax - if xMin < 0 then xMin = 0; end - if xMax > displayW then - xMax = displayW - end - - -- Caluclate yMin and yMax - if yMin < 0 then yMin = 0; end - if yMax > displayH then - yMax = displayH - end - - -- Calculate actual width/height of screen capture - local width = xMax - xMin - local height = yMax - yMin - -- loop through current scene and remove potential Runtime table listeners for i=currentScene.numChildren,1,-1 do if currentScene[i].enterFrame then Runtime:removeEventListener( "enterFrame", currentScene[i] ); end @@ -670,14 +657,13 @@ end lib._createTouchOverlay = function() - local overlayRect = display.newRect( 0, 0, displayW, displayH ) + local overlayRect = display.newRect( display.screenOriginX, display.screenOriginY, displayW, displayH ) if not isGraphicsV1 then overlayRect.anchorX = 0 overlayRect.anchorY = 0 end - overlayRect:setFillColor( 0 ) overlayRect.isVisible = false - overlayRect.isHitTestable = true -- allow touches when invisible + overlayRect.isHitTestable = true overlayRect:addEventListener( "touch", function() return true end ) overlayRect:addEventListener( "tap", function() return true end ) @@ -1210,14 +1196,13 @@ function lib.showOverlay( sceneName, options, argOffset ) end if isModal then - lib._modalRect = display.newRect( 0, 0, display.actualContentWidth * 1.25, display.actualContentHeight * 1.25 ) - lib._modalRect.x = display.contentCenterX - lib._modalRect.y = display.contentCenterY + lib._modalRect = display.newRect( display.screenOriginX, display.screenOriginY, displayW, displayH ) + if not isGraphicsV1 then + lib._modalRect.anchorX = 0 + lib._modalRect.anchorY = 0 + end lib._modalRect.isVisible = false lib._modalRect.isHitTestable = true - -- prevent touches - lib._modalRect.touch = function() return true; end - lib._modalRect.tap = function() return true; end lib._modalRect:addEventListener( "touch", function() return true end ) lib._modalRect:addEventListener( "tap", function() return true end ) stage:insert( lib._modalRect ) @@ -1595,6 +1580,15 @@ end Runtime:addEventListener( "memoryWarning", purgeLruScene ) +----------------------------------------------------------------------------------------- + +-- Called after orientation change or resize event to update the scene transition effects. +function lib.onScreenChange() + updateEffects() +end + +----------------------------------------------------------------------------------------- + -- TODO: This is deprecated. lib.printMemUsage = function() print("WARNING: composer.printMemUsage() has been removed.") @@ -1632,4 +1626,4 @@ lib._sceneFileExists = function( fileName ) return fileExists end -return lib \ No newline at end of file +return lib