Skip to content

Instantly share code, notes, and snippets.

@Shchvova
Last active December 14, 2016 16:26
Show Gist options
  • Save Shchvova/c4884457e5b71b24349a3247e6bcc78e to your computer and use it in GitHub Desktop.
Save Shchvova/c4884457e5b71b24349a3247e6bcc78e to your computer and use it in GitHub Desktop.
On Demand Resources demo

This demo adds On Demand Resource capabilities to Corona Cannon project. It would download all images used in project displaying loading screen with progress bar.

# Clone some CoronaCannon
git clone git@github.com:coronalabs-samples/CoronaCannon.git

cd CoronaCannon

# Apply On-Demand Resources patch
curl https://gist.githubusercontent.com/Shchvova/c4884457e5b71b24349a3247e6bcc78e/raw/patch.diff | git apply -
From e1ba9cc9e544e3376645033134882de82a91488d Mon Sep 17 00:00:00 2001
From: Vlad Shcherban <vlad@coronalabs.com>
Date: Wed, 14 Dec 2016 11:19:52 -0500
Subject: [PATCH] On Demand Resource loading
---
build.settings | 7 +++++-
main.lua | 9 +++++--
scenes/odr.lua | 75 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
3 files changed, 88 insertions(+), 3 deletions(-)
create mode 100644 scenes/odr.lua
diff --git a/build.settings b/build.settings
index 868da40..e73bb58 100755
--- a/build.settings
+++ b/build.settings
@@ -45,6 +45,10 @@ settings = {
},
tvos = {
iCloud = true,
+ onDemandResources =
+ {
+ { tag="resources", resource="images" },
+ },
icon = {
small = { -- A collection of 400x240 images, in order from top to bottom
'Icon-tvOS-Small-4.png',
@@ -79,7 +83,8 @@ settings = {
},
plugins = {
['plugin.iCloud'] = {publisherId = 'com.coronalabs', supportedPlatforms = {iphone = true, appletvos = true, ['iphone-sim'] = true, osx = true}},
- ['plugin.bit'] = {publisherId = 'com.coronalabs'} -- Needed for Tiled loader
+ ['plugin.bit'] = {publisherId = 'com.coronalabs'}, -- Needed for Tiled loader
+ ["plugin.onDemandResources"] = { publisherId = "com.coronalabs", supportedPlatforms = { appletvos = true } },
},
excludeFiles = {
all = {
diff --git a/main.lua b/main.lua
index 3d5d236..570ff06 100644
--- a/main.lua
+++ b/main.lua
@@ -93,5 +93,10 @@ require('libs.relayout')
local overscan = require('libs.overscan')
overscan.compensate(databox.overscanValue)
--- Show menu scene
-composer.gotoScene('scenes.menu')
+if system.getInfo('platform') == 'tvos' then
+ -- download OnDemandResources
+ composer.gotoScene('scenes.odr')
+else
+ -- Show menu scene
+ composer.gotoScene('scenes.menu')
+end
diff --git a/scenes/odr.lua b/scenes/odr.lua
new file mode 100644
index 0000000..3287429
--- /dev/null
+++ b/scenes/odr.lua
@@ -0,0 +1,75 @@
+
+local composer = require('composer')
+local widget = require('widget')
+local relayout = require("libs.relayout")
+local odr = require("plugin.onDemandResources")
+local json = require("json")
+
+local scene = composer.newScene()
+
+local tag = "resources"
+
+function scene:create()
+
+end
+
+local progress = nil
+
+local function downloadODResources()
+ local _W, _H, _CX, _CY = relayout._W, relayout._H, relayout._CX, relayout._CY
+ local group = scene.view
+ progress = widget.newProgressView( {
+ x = _CX,
+ y = _CY,
+ width = _W*0.7,
+ isAnimated = false,
+ } )
+
+ progress:setProgress( 0 )
+
+
+ local function progressUpdater ( event )
+ progress:setProgress( odr.progress(tag) or 0 )
+ end
+ Runtime:addEventListener( "enterFrame", progressUpdater )
+
+ odr.request(tag, true, function( event2 )
+ Runtime:removeEventListener( "enterFrame", progressUpdater )
+ progress:removeSelf( )
+ progress = nil
+ if event2.isError then
+ display.newText( "Error downloading ODR", _CX, _CY, nil, _CY/12 )
+ else
+ composer.gotoScene('scenes.menu')
+ end
+ end)
+ odr.setDownloadPriority(tag, "urgent")
+
+end
+
+function scene:show(event)
+ if event.phase == 'did' then
+ odr.request(tag, false, function( event )
+ if event.isError then
+ downloadODResources()
+ else
+ composer.gotoScene('scenes.menu')
+ end
+ end)
+ end
+end
+
+function scene:hide(event)
+ if event.phase == 'did' then
+ if progress then
+ progress:removeSelf( )
+ progress = nil
+ end
+ end
+end
+
+scene:addEventListener('create')
+scene:addEventListener('show')
+scene:addEventListener('hide')
+
+return scene
--
2.8.1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment