複製代碼
-
------
-
-- updateScene for update package.
-
-- This is a object, not a class.
-
-- In this scene, it will show download progress bar
-
-- and state for uncompress.
-
-- @author zrong(zengrong.net)
-
-- Creation: 2014-07-03
-
-
local updater = require("update.updater")
-
local sharedDirector = CCDirector:sharedDirector()
-
-
-- check device screen size
-
local glview = sharedDirector:getOpenGLView()
-
local size = glview:getFrameSize()
-
local display = {}
-
display.sizeInPixels = {width = size.width, height = size.height}
-
-
local w = display.sizeInPixels.width
-
local h = display.sizeInPixels.height
-
-
CONFIG_SCREEN_WIDTH = 1280
-
CONFIG_SCREEN_HEIGHT = 800
-
CONFIG_SCREEN_AUTOSCALE = "FIXED_HEIGHT"
-
-
local scale, scaleX, scaleY
-
-
scaleX, scaleY = w / CONFIG_SCREEN_WIDTH, h / CONFIG_SCREEN_HEIGHT
-
scale = scaleY
-
CONFIG_SCREEN_WIDTH = w / scale
-
-
glview:setDesignResolutionSize(CONFIG_SCREEN_WIDTH, CONFIG_SCREEN_HEIGHT, kResolutionNoBorder)
-
-
local winSize = sharedDirector:getWinSize()
-
display.contentScaleFactor = scale
-
display.size = {width = winSize.width, height = winSize.height}
-
display.width = display.size.width
-
display.height = display.size.height
-
display.cx = display.width / 2
-
display.cy = display.height / 2
-
display.c_left = -display.width / 2
-
display.c_right = display.width / 2
-
display.c_top = display.height / 2
-
display.c_bottom = -display.height / 2
-
display.left = 0
-
display.right = display.width
-
display.top = display.height
-
display.bottom = 0
-
display.widthInPixels = display.sizeInPixels.width
-
display.heightInPixels = display.sizeInPixels.height
-
-
print("# display in updateScene start")
-
print(string.format("# us.CONFIG_SCREEN_AUTOSCALE = %s", CONFIG_SCREEN_AUTOSCALE))
-
print(string.format("# us.CONFIG_SCREEN_WIDTH = %0.2f", CONFIG_SCREEN_WIDTH))
-
print(string.format("# us.CONFIG_SCREEN_HEIGHT = %0.2f", CONFIG_SCREEN_HEIGHT))
-
print(string.format("# us.display.widthInPixels = %0.2f", display.widthInPixels))
-
print(string.format("# us.display.heightInPixels = %0.2f", display.heightInPixels))
-
print(string.format("# us.display.contentScaleFactor = %0.2f", display.contentScaleFactor))
-
print(string.format("# us.display.width = %0.2f", display.width))
-
print(string.format("# us.display.height = %0.2f", display.height))
-
print(string.format("# us.display.cx = %0.2f", display.cx))
-
print(string.format("# us.display.cy = %0.2f", display.cy))
-
print(string.format("# us.display.left = %0.2f", display.left))
-
print(string.format("# us.display.right = %0.2f", display.right))
-
print(string.format("# us.display.top = %0.2f", display.top))
-
print(string.format("# us.display.bottom = %0.2f", display.bottom))
-
print(string.format("# us.display.c_left = %0.2f", display.c_left))
-
print(string.format("# us.display.c_right = %0.2f", display.c_right))
-
print(string.format("# us.display.c_top = %0.2f", display.c_top))
-
print(string.format("# us.display.c_bottom = %0.2f", display.c_bottom))
-
print("# display in updateScene done")
-
-
display.ANCHOR_POINTS = {
-
CCPoint(0.5, 0.5), -- CENTER
-
CCPoint(0, 1), -- TOP_LEFT
-
CCPoint(0.5, 1), -- TOP_CENTER
-
CCPoint(1, 1), -- TOP_RIGHT
-
CCPoint(0, 0.5), -- CENTER_LEFT
-
CCPoint(1, 0.5), -- CENTER_RIGHT
-
CCPoint(0, 0), -- BOTTOM_LEFT
-
CCPoint(1, 0), -- BOTTOM_RIGHT
-
CCPoint(0.5, 0), -- BOTTOM_CENTER
-
}
-
-
display.CENTER = 1
-
display.LEFT_TOP = 2; display.TOP_LEFT = 2
-
display.CENTER_TOP = 3; display.TOP_CENTER = 3
-
display.RIGHT_TOP = 4; display.TOP_RIGHT = 4
-
display.CENTER_LEFT = 5; display.LEFT_CENTER = 5
-
display.CENTER_RIGHT = 6; display.RIGHT_CENTER = 6
-
display.BOTTOM_LEFT = 7; display.LEFT_BOTTOM = 7
-
display.BOTTOM_RIGHT = 8; display.RIGHT_BOTTOM = 8
-
display.BOTTOM_CENTER = 9; display.CENTER_BOTTOM = 9
-
-
function display.align(target, anchorPoint, x, y)
-
target:setAnchorPoint(display.ANCHOR_POINTS[anchorPoint])
-
if x and y then target:setPosition(x, y) end
-
end
-
-
local us = CCScene:create()
-
us.name = "updateScene"
-
-
local localResInfo = nil
-
-
function us._addUI()
-
-- Get the newest resinfo in ures.
-
local localResInfo = updater.getResCopy()
-
-
local __bg = CCSprite:create(us._getres("res/pic/init_bg.png"))
-
display.align(__bg, display.CENTER, display.cx, display.cy)
-
us:addChild(__bg, 0)
-
-
local __label = CCLabelTTF:create("Loading...", "Arial", 24)
-
__label:setColor(ccc3(255, 0, 0))
-
us._label = __label
-
display.align(__label, display.CENTER, display.cx, display.bottom+30)
-
us:addChild(__label, 10)
-
end
-
-
function us._getres(path)
-
if not localResInfo then
-
localResInfo = updater.getResCopy()
-
end
-
for key, value in pairs(localResInfo.oth) do
-
print("us._getres:", key, value)
-
local pathInIndex = string.find(key, path)
-
if pathInIndex and pathInIndex >= 1 then
-
print("us._getres getvalue:", path)
-
res[path] = value
-
return value
-
end
-
end
-
return path
-
end
-
-
function us._sceneHandler(event)
-
if event == "enter" then
-
print(string.format("updateScene \"%s:onEnter()\"", us.name))
-
us.onEnter()
-
elseif event == "cleanup" then
-
print(string.format("updateScene \"%s:onCleanup()\"", us.name))
-
us.onCleanup()
-
elseif event == "exit" then
-
print(string.format("updateScene \"%s:onExit()\"", us.name))
-
us.onExit()
-
-
if DEBUG_MEM then
-
print("----------------------------------------")
-
print(string.format("LUA VM MEMORY USED: %0.2f KB", collectgarbage("count")))
-
CCTextureCache:sharedTextureCache():dumpCachedTextureInfo()
-
print("----------------------------------------")
-
end
-
end
-
end
-
-
function us._updateHandler(event, value)
-
updater.state = event
-
if event == "success" then
-
updater.stateValue = value:getCString()
-
updater.updateFinalResInfo()
-
if us._succHandler then
-
us._succHandler()
-
end
-
elseif event == "error" then
-
updater.stateValue = value:getCString()
-
elseif event == "progress" then
-
updater.stateValue = tostring(value:getValue())
-
elseif event == "state" then
-
updater.stateValue = value:getCString()
-
end
-
us._label:setString(updater.stateValue)
-
assert(event ~= "error",
-
string.format("Update error: %s !", updater.stateValue))
-
end
-
-
function us.addListener(handler)
-
us._succHandler = handler
-
return us
-
end
-
-
function us.onEnter()
-
updater.update(us._updateHandler)
-
end
-
-
function us.onExit()
-
updater.clean()
-
us:unregisterScriptHandler()
-
end
-
-
function us.onCleanup()
-
end
-
-
us:registerScriptHandler(us._sceneHandler)
-
us._addUI()
-
return us
-
|