使用Lua对Corona SDK的Flipboard效应

使用Lua对Corona SDK的Flipboard效应,lua,coronasdk,flipboard,Lua,Coronasdk,Flipboard,大家好, 这不是一个问题,只是为了显示代码,如果有人对它感兴趣 我使用了Corona Share Your Code站点中介绍的动画书项目,并对其进行了一些修改。() main.lua文件: ----------------------------------------------------------------------------------------- -- -- main.lua -- ----------------------------------------------

大家好,

这不是一个问题,只是为了显示代码,如果有人对它感兴趣

我使用了Corona Share Your Code站点中介绍的动画书项目,并对其进行了一些修改。()

main.lua文件:

-----------------------------------------------------------------------------------------
--
-- main.lua
--
-----------------------------------------------------------------------------------------

local Flipbook = require 'flipbook'

local nrPages = 6

local myFirstFlipbook = Flipbook:newFlipbook(nrPages, "mainshadow2.png" )

for x=1,nrPages do

    local grp1 = display.newGroup()
    --grp1:setReferencePoint( display.TopLeftReferencePoint )
    local rct1 = display.newRect( 0, 0, display.contentWidth, display.contentHeight )
    --grp1.isVisible = false
    local txt1 = display.newText("Meu texto "..x.." ...", 2, display.contentHeight - 20, native.systemFont, 16)
    txt1:setTextColor(0, 0, 0)
    local img1 = display.newImageRect( "background"..x..".png",display.contentWidth/5*4,display.contentHeight/5*4 )
    img1.x = display.contentWidth/2
    img1.y = display.contentHeight/2
    grp1:insert(rct1)
    grp1:insert(img1)
    grp1:insert(txt1)

    local grp2 = display.newGroup()
    --grp4:setReferencePoint( display.TopLeftReferencePoint )
    local rct2 = display.newRect( 0, 0, display.contentWidth, display.contentHeight )
    --grp1.isVisible = false
    local txt2 = display.newText("Meu texto "..x.." ...", 2, display.contentHeight - 20, native.systemFont, 16)
    txt2:setTextColor(0, 0, 0)
    local img2 = display.newImageRect( "background"..x..".png",display.contentWidth/5*4,display.contentHeight/5*4 )
    img2.x = display.contentWidth/2
    img2.y = display.contentHeight/2
    grp2:insert(rct2)
    grp2:insert(img2)
    grp2:insert(txt2)

    myFirstFlipbook:addPage( grp1 )
    myFirstFlipbook:addMirror( nrPages, grp2 )

end
-----------------------------------------------------------------------------------------
--
-- flipbook.lua
--
-----------------------------------------------------------------------------------------


local Flipbook = {
    mask = graphics.newMask( "mask1.png" ),
    mask2 = graphics.newMask( "mask2.png" ),
    currentPage = 1,
    pages = {},
    backPages = {},
    displayGroup = display.newGroup(),
    control = 2
}
Flipbook.__index = Flipbook

function Flipbook:newFlipbook(nrPages, newBackShadow, newBasicShadow, newCurlShadow) --Create a new flipbook
    local tempFlipbook = setmetatable({},self)

    -- Adds a shadow for where the curled page overlaps itself
    tempFlipbook.backShadow = display.newImage( newBackShadow )
    tempFlipbook.backShadow.isHitTestMasked = false
    tempFlipbook.backShadow.y = display.contentHeight / 2
    tempFlipbook.backShadow.isVisible = false
    tempFlipbook.displayGroup:insert( nrPages + 1, tempFlipbook.backShadow )
    -- Adds event listener
    tempFlipbook.displayGroup:addEventListener( "touch", tempFlipbook )
    ----
    return tempFlipbook
end

function Flipbook:addPage( newPageGroup ) -- Add a new page to your flipbook

    local tempPageImage = newPageGroup
    tempPageImage:setMask( self.mask )
    tempPageImage.maskX = display.contentWidth
    tempPageImage.isHitTestMasked = false
    tempPageImage:setReferencePoint( display.CenterReferencePoint )
    table.insert( self.pages, tempPageImage ) -- Adds new page to pages index
    self.displayGroup:insert( 1, tempPageImage ) -- Adds new page to display group

end

function Flipbook:addMirror( nrPages, newPageGroup )

    local tempPageImageMirror = newPageGroup
    tempPageImageMirror.isVisible = false
    tempPageImageMirror:setMask( self.mask2 )
    tempPageImageMirror:setReferencePoint( display.CenterReferencePoint )
    table.insert( self.backPages, tempPageImageMirror ) -- Adds new page to pages index
    self.displayGroup:insert( nrPages + 1, tempPageImageMirror )

end

function Flipbook:updateCurlEffect( handleX, handleY, originY ) -- Updates the curl effect assets

    local hX = 0
    if (handleX > display.contentWidth) then
        hX = display.contentWidth
    elseif (handleX < 0) then
        hX = 0
    else
        hX = handleX
    end

    local controlX = 0
    if (hX > display.contentWidth / 2) then
        controlX = (( hX - display.contentWidth / 2) / (display.contentWidth / 2))
    else
        controlX = (( display.contentWidth / 2 - hX ) / (display.contentWidth / 2))
    end
    if controlX < 0.0001 then
        controlX = 0.0001
    end
    if controlX > 0.9999 then
        controlX = 0.9999
    end
    --print(alphaX)

    self.pages[self.currentPage].maskX = hX



    if (hX > display.contentWidth / 2) then
        self.backPages[self.currentPage+1].isVisible = false
        self.backPages[self.currentPage]:setMask(self.mask2)
        self.backPages[self.currentPage].isVisible = true
        self.backPages[self.currentPage].xReference = 0
        self.backPages[self.currentPage].x = (display.contentWidth - hX)
        self.backPages[self.currentPage].maskX = display.contentWidth / 2
        self.backPages[self.currentPage].xScale = controlX

        if (self.control > 1) then
            self.backShadow:scale(-1,1)
            self.control = 1
            --self.backShadow.xReference = display.contentWidth / 2
            self.backShadow.x = display.contentWidth + 21
            self.backShadow.maskX = 0
        end
    else
        self.backPages[self.currentPage].isVisible = false
        self.backPages[self.currentPage+1].isVisible = true
        self.backPages[self.currentPage+1]:setMask(self.mask)
        self.backPages[self.currentPage+1]:toFront()
        self.backPages[self.currentPage+1].xReference = 0
        self.backPages[self.currentPage+1].x = hX
        self.backPages[self.currentPage+1].maskX = display.contentWidth / 2
        self.backPages[self.currentPage+1].xScale = controlX

        if (self.control < 2) then
            self.backShadow:scale(-1,1)
            self.control = 2
            --self.backShadow.xReference = display.contentWidth / 2
            self.backShadow.x = -21
            self.backShadow.maskX = 0
        end
    end
    self.backShadow.alpha = controlX
    --self.backShadow.sizeX = display.contentWidth

end

function Flipbook:setAssetsVisible( isVisibleBoolean ) -- Curl effect assets visibility switch box
    if isVisibleBoolean then
        self.backPages[self.currentPage].isVisible = isVisibleBoolean
        self.backShadow.isVisible = isVisibleBoolean
        self.backShadow:toFront()
        self.backPages[self.currentPage]:toFront()
    else
        --for k,v in pairs(self.backPages) do
        for k=1, #self.backPages do
            self.backPages[k].isVisible = isVisibleBoolean
        end
        self.backShadow.isVisible = isVisibleBoolean
    end
end

function Flipbook:touch( event )
    if event.phase == "began" then
        if  event.xStart > 5 * display.contentWidth / 6 and self.currentPage < # self.pages 
            or event.xStart < display.contentWidth / 6 and self.currentPage > 1 then
            if event.xStart < display.contentWidth / 6 then
                self.currentPage = self.currentPage - 1
                self.pages[self.currentPage].isVisible = true
            end
            display.getCurrentStage():setFocus( self.displayGroup )
            self.isFocus = true
            self:setAssetsVisible( true )
        else
            return false
        end
    end
    if self.isFocus then
        self:updateCurlEffect( event.x, event.y, event.yStart )
        if event.phase == "ended" or event.phase == "cancelled" then
            self:updateCurlEffect( display.contentWidth, 0, 0 )
            if math.abs(event.x - event.xStart) > display.contentWidth / 6 then
                if event.xStart > 5 * display.contentWidth / 6 and self.currentPage < # self.pages then
                    self.pages[self.currentPage].isVisible = false
                    self.currentPage = self.currentPage + 1
                end
                self:setAssetsVisible( false )
                display.getCurrentStage():setFocus( nil )
                self.isFocus = nil
            else
                if event.xStart < display.contentWidth / 6 and self.currentPage > 1 then
                    self.pages[self.currentPage].isVisible = false
                    self.currentPage = self.currentPage + 1
                end
                self:setAssetsVisible( false )
                display.getCurrentStage():setFocus( nil )
                self.isFocus = nil
            end
        end
        return true
    end
end

return Flipbook
和flipbook.lua文件:

-----------------------------------------------------------------------------------------
--
-- main.lua
--
-----------------------------------------------------------------------------------------

local Flipbook = require 'flipbook'

local nrPages = 6

local myFirstFlipbook = Flipbook:newFlipbook(nrPages, "mainshadow2.png" )

for x=1,nrPages do

    local grp1 = display.newGroup()
    --grp1:setReferencePoint( display.TopLeftReferencePoint )
    local rct1 = display.newRect( 0, 0, display.contentWidth, display.contentHeight )
    --grp1.isVisible = false
    local txt1 = display.newText("Meu texto "..x.." ...", 2, display.contentHeight - 20, native.systemFont, 16)
    txt1:setTextColor(0, 0, 0)
    local img1 = display.newImageRect( "background"..x..".png",display.contentWidth/5*4,display.contentHeight/5*4 )
    img1.x = display.contentWidth/2
    img1.y = display.contentHeight/2
    grp1:insert(rct1)
    grp1:insert(img1)
    grp1:insert(txt1)

    local grp2 = display.newGroup()
    --grp4:setReferencePoint( display.TopLeftReferencePoint )
    local rct2 = display.newRect( 0, 0, display.contentWidth, display.contentHeight )
    --grp1.isVisible = false
    local txt2 = display.newText("Meu texto "..x.." ...", 2, display.contentHeight - 20, native.systemFont, 16)
    txt2:setTextColor(0, 0, 0)
    local img2 = display.newImageRect( "background"..x..".png",display.contentWidth/5*4,display.contentHeight/5*4 )
    img2.x = display.contentWidth/2
    img2.y = display.contentHeight/2
    grp2:insert(rct2)
    grp2:insert(img2)
    grp2:insert(txt2)

    myFirstFlipbook:addPage( grp1 )
    myFirstFlipbook:addMirror( nrPages, grp2 )

end
-----------------------------------------------------------------------------------------
--
-- flipbook.lua
--
-----------------------------------------------------------------------------------------


local Flipbook = {
    mask = graphics.newMask( "mask1.png" ),
    mask2 = graphics.newMask( "mask2.png" ),
    currentPage = 1,
    pages = {},
    backPages = {},
    displayGroup = display.newGroup(),
    control = 2
}
Flipbook.__index = Flipbook

function Flipbook:newFlipbook(nrPages, newBackShadow, newBasicShadow, newCurlShadow) --Create a new flipbook
    local tempFlipbook = setmetatable({},self)

    -- Adds a shadow for where the curled page overlaps itself
    tempFlipbook.backShadow = display.newImage( newBackShadow )
    tempFlipbook.backShadow.isHitTestMasked = false
    tempFlipbook.backShadow.y = display.contentHeight / 2
    tempFlipbook.backShadow.isVisible = false
    tempFlipbook.displayGroup:insert( nrPages + 1, tempFlipbook.backShadow )
    -- Adds event listener
    tempFlipbook.displayGroup:addEventListener( "touch", tempFlipbook )
    ----
    return tempFlipbook
end

function Flipbook:addPage( newPageGroup ) -- Add a new page to your flipbook

    local tempPageImage = newPageGroup
    tempPageImage:setMask( self.mask )
    tempPageImage.maskX = display.contentWidth
    tempPageImage.isHitTestMasked = false
    tempPageImage:setReferencePoint( display.CenterReferencePoint )
    table.insert( self.pages, tempPageImage ) -- Adds new page to pages index
    self.displayGroup:insert( 1, tempPageImage ) -- Adds new page to display group

end

function Flipbook:addMirror( nrPages, newPageGroup )

    local tempPageImageMirror = newPageGroup
    tempPageImageMirror.isVisible = false
    tempPageImageMirror:setMask( self.mask2 )
    tempPageImageMirror:setReferencePoint( display.CenterReferencePoint )
    table.insert( self.backPages, tempPageImageMirror ) -- Adds new page to pages index
    self.displayGroup:insert( nrPages + 1, tempPageImageMirror )

end

function Flipbook:updateCurlEffect( handleX, handleY, originY ) -- Updates the curl effect assets

    local hX = 0
    if (handleX > display.contentWidth) then
        hX = display.contentWidth
    elseif (handleX < 0) then
        hX = 0
    else
        hX = handleX
    end

    local controlX = 0
    if (hX > display.contentWidth / 2) then
        controlX = (( hX - display.contentWidth / 2) / (display.contentWidth / 2))
    else
        controlX = (( display.contentWidth / 2 - hX ) / (display.contentWidth / 2))
    end
    if controlX < 0.0001 then
        controlX = 0.0001
    end
    if controlX > 0.9999 then
        controlX = 0.9999
    end
    --print(alphaX)

    self.pages[self.currentPage].maskX = hX



    if (hX > display.contentWidth / 2) then
        self.backPages[self.currentPage+1].isVisible = false
        self.backPages[self.currentPage]:setMask(self.mask2)
        self.backPages[self.currentPage].isVisible = true
        self.backPages[self.currentPage].xReference = 0
        self.backPages[self.currentPage].x = (display.contentWidth - hX)
        self.backPages[self.currentPage].maskX = display.contentWidth / 2
        self.backPages[self.currentPage].xScale = controlX

        if (self.control > 1) then
            self.backShadow:scale(-1,1)
            self.control = 1
            --self.backShadow.xReference = display.contentWidth / 2
            self.backShadow.x = display.contentWidth + 21
            self.backShadow.maskX = 0
        end
    else
        self.backPages[self.currentPage].isVisible = false
        self.backPages[self.currentPage+1].isVisible = true
        self.backPages[self.currentPage+1]:setMask(self.mask)
        self.backPages[self.currentPage+1]:toFront()
        self.backPages[self.currentPage+1].xReference = 0
        self.backPages[self.currentPage+1].x = hX
        self.backPages[self.currentPage+1].maskX = display.contentWidth / 2
        self.backPages[self.currentPage+1].xScale = controlX

        if (self.control < 2) then
            self.backShadow:scale(-1,1)
            self.control = 2
            --self.backShadow.xReference = display.contentWidth / 2
            self.backShadow.x = -21
            self.backShadow.maskX = 0
        end
    end
    self.backShadow.alpha = controlX
    --self.backShadow.sizeX = display.contentWidth

end

function Flipbook:setAssetsVisible( isVisibleBoolean ) -- Curl effect assets visibility switch box
    if isVisibleBoolean then
        self.backPages[self.currentPage].isVisible = isVisibleBoolean
        self.backShadow.isVisible = isVisibleBoolean
        self.backShadow:toFront()
        self.backPages[self.currentPage]:toFront()
    else
        --for k,v in pairs(self.backPages) do
        for k=1, #self.backPages do
            self.backPages[k].isVisible = isVisibleBoolean
        end
        self.backShadow.isVisible = isVisibleBoolean
    end
end

function Flipbook:touch( event )
    if event.phase == "began" then
        if  event.xStart > 5 * display.contentWidth / 6 and self.currentPage < # self.pages 
            or event.xStart < display.contentWidth / 6 and self.currentPage > 1 then
            if event.xStart < display.contentWidth / 6 then
                self.currentPage = self.currentPage - 1
                self.pages[self.currentPage].isVisible = true
            end
            display.getCurrentStage():setFocus( self.displayGroup )
            self.isFocus = true
            self:setAssetsVisible( true )
        else
            return false
        end
    end
    if self.isFocus then
        self:updateCurlEffect( event.x, event.y, event.yStart )
        if event.phase == "ended" or event.phase == "cancelled" then
            self:updateCurlEffect( display.contentWidth, 0, 0 )
            if math.abs(event.x - event.xStart) > display.contentWidth / 6 then
                if event.xStart > 5 * display.contentWidth / 6 and self.currentPage < # self.pages then
                    self.pages[self.currentPage].isVisible = false
                    self.currentPage = self.currentPage + 1
                end
                self:setAssetsVisible( false )
                display.getCurrentStage():setFocus( nil )
                self.isFocus = nil
            else
                if event.xStart < display.contentWidth / 6 and self.currentPage > 1 then
                    self.pages[self.currentPage].isVisible = false
                    self.currentPage = self.currentPage + 1
                end
                self:setAssetsVisible( false )
                display.getCurrentStage():setFocus( nil )
                self.isFocus = nil
            end
        end
        return true
    end
end

return Flipbook
-----------------------------------------------------------------------------------------
--
--动画书
--
-----------------------------------------------------------------------------------------
本地动画书={
mask=graphics.newMask(“mask1.png”),
mask2=graphics.newMask(“mask2.png”),
currentPage=1,
页码={},
backPages={},
displayGroup=display.newGroup(),
控制=2
}
动画书。\索引=动画书
功能动画书:新建动画书(nrPages、newBackShadow、newBasicShadow、newCurlShadow)——创建新的动画书
本地tempFlipbook=setmetatable({},self)
--为卷曲页面自身重叠的位置添加阴影
tempFlipbook.backShadow=display.newImage(newBackShadow)
tempFlipbook.backShadow.isHitTestMasked=false
tempFlipbook.backShadow.y=display.contentHeight/2
tempFlipbook.backShadow.isVisible=false
tempFlipbook.displayGroup:插入(nrPages+1,tempFlipbook.backShadow)
--添加事件侦听器
tempFlipbook.displayGroup:addEventListener(“触摸”,tempFlipbook)
----
返回动画书
结束
功能动画书:添加页面(newPageGroup)——向动画书添加新页面
本地tempPageImage=newPageGroup
tempPageImage:setMask(self.mask)
tempPageImage.maskX=display.contentWidth
tempPageImage.isHitTestMasked=false
tempPageImage:setReferencePoint(display.CenterReferencePoint)
table.insert(self.pages,tempPageImage)--将新页面添加到页面索引
displayGroup:insert(1,tempPageImage)--将新页面添加到显示组
结束
功能动画书:添加镜像(nrPages,newPageGroup)
本地tempPageImageMirror=newPageGroup
tempPageImageMirror.isVisible=false
tempPageImageMirror:setMask(self.mask2)
tempPageImageMirror:setReferencePoint(display.CenterReferencePoint)
表.insert(self.backPages,tempPageImageMirror)——将新页面添加到页面索引
self.displayGroup:insert(nrPages+1,tempPageImageMirror)
结束
函数动画书:UpdateCurleEffect(handleX、handleY、originY)——更新卷曲效果资源
局部hX=0
如果(handleX>display.contentWidth),则
hX=display.contentWidth
elseif(handleX<0)则
hX=0
其他的
hX=手柄
结束
本地控制x=0
如果(hX>display.contentWidth/2),则
controlX=((hX-display.contentWidth/2)/(display.contentWidth/2))
其他的
controlX=((display.contentWidth/2-hX)/(display.contentWidth/2))
结束
如果controlX<0.0001,则
controlX=0.0001
结束
如果controlX>0.9999,则
controlX=0.9999
结束
--打印(字母)
self.pages[self.currentPage].maskX=hX
如果(hX>display.contentWidth/2),则
self.backPages[self.currentPage+1].isVisible=false
self.backPages[self.currentPage]:设置掩码(self.mask2)
self.backPages[self.currentPage].isVisible=true
self.backPages[self.currentPage].xReference=0
self.backPages[self.currentPage].x=(display.contentWidth-hX)
self.backPages[self.currentPage].maskX=display.contentWidth/2
self.backPages[self.currentPage].xScale=controlX
如果(自我控制>1),则
自阴影:比例(-1,1)
自我控制=1
--self.backShadow.xReference=display.contentWidth/2
self.backShadow.x=display.contentWidth+21
self.backShadow.maskX=0
结束
其他的
self.backPages[self.currentPage].isVisible=false
self.backPages[self.currentPage+1].isVisible=true
self.backPages[self.currentPage+1]:设置掩码(self.mask)
self.backPages[self.currentPage+1]:toFront()
self.backPages[self.currentPage+1].xReference=0
self.backPages[self.currentPage+1].x=hX
self.backPages[self.currentPage+1].maskX=display.contentWidth/2
self.backPages[self.currentPage+1].xScale=controlX
如果(自我控制<2),则
自阴影:比例(-1,1)
自我控制=2
--self.backShadow.xReference=display.contentWidth/2
self.backShadow.x=-21
self.backShadow.maskX=0
结束
结束
self.backShadow.alpha=controlX
--self.backShadow.sizeX=display.contentWidth
结束
功能动画书:SetAssetVisible(isVisibleBoolean)--卷曲效果资源可见性开关框
如果是VisibleBoolean,那么
self.backPages[self.currentPage].isVisible=isVisibleBoolean
self.backShadow.isVisible=isVisibleBoolean
self.backShadow:toFront()
self.backPages[self.currentPage]:toFront()
其他的
--对于k,v成对(self.backPages)做
对于k=1,#self.backPages可以
self.backPages[k].isVisible=isVisibleBoolean
结束
self.backShadow.isVisible=isVisibleBoolean
结束
结束
功能动画书:触摸(事件)
如果event.phase==“开始”,则
如果event.xStart>5*display.contentWidth/6和self.currentPage<#self.pages
或者event.xStart1,然后
如果event.xStart