History

Merge pull request #11574 from ctrlaltdavid/shapes-preload

Preload Shapes app asset files so that they're in disk cache ready for use

Brad Hefta-Gaub committed 9 years ago

Commit 704cb1f2 parents 9f3331e 74899e45 files · +202 −4Browse files

Unified · Split

5 files
  1. unpublishedScripts/marketplace/shapes/modules/createPalette.js +7 −1
  2. unpublishedScripts/marketplace/shapes/modules/preload.js +175 −0
  3. unpublishedScripts/marketplace/shapes/modules/toolIcon.js +5 −0
  4. unpublishedScripts/marketplace/shapes/modules/toolsMenu.js +8 −1
  5. unpublishedScripts/marketplace/shapes/shapes.js +7 −2
unpublishedScripts/marketplace/shapes/modules/createPalette.js+7−1
@@ -8,7 +8,7 @@ · 7 unmodified lines
88 // See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
99 //
1010
11−/* global CreatePalette: true, App, Feedback, History, UIT */
11+/* global CreatePalette:true, App, Feedback, History, Preload, UIT */
1212
1313 CreatePalette = function (side, leftInputs, rightInputs, uiCommandCallback) {
1414 // Tool menu displayed on top of forearm.
@@ -332,6 +332,11 @@ CreatePalette = function (side, leftInputs, rightInputs, uiCommandCallback) { · 317 unmodified lines
332332 }
333333
334334
335+ function getAssetURLs() {
336+ return Preload.findURLs([PALETTE_HEADER_HEADING_PROPERTIES, PALETTE_HEADER_BAR_PROPERTIES, PALETTE_TITLE_PROPERTIES,
337+ PALETTE_ITEMS]);
338+ }
339+
335340 function setHand(hand) {
336341 // Assumes UI is not displaying.
337342 var NUMBER_OF_HANDS = 2;
@@ -533,6 +538,7 @@ CreatePalette = function (side, leftInputs, rightInputs, uiCommandCallback) { · 195 unmodified lines
533538 }
534539
535540 return {
541+ assetURLs: getAssetURLs,
536542 setHand: setHand,
537543 overlayIDs: getOverlayIDs,
538544 setVisible: setVisible,
unpublishedScripts/marketplace/shapes/modules/preload.js+175−0
@@ -0,0 +1,175 @@
1+//
2+// preload.js
3+//
4+// Created by David Rowe on 11 Oct 2017.
5+// Copyright 2017 High Fidelity, Inc.
6+//
7+// Distributed under the Apache License, Version 2.0.
8+// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
9+//
10+
11+/* global Preload:true, App */
12+
13+Preload = (function () {
14+ // Provide facility to preload asset files so that they are in disk cache.
15+ // Global object.
16+
17+ "use strict";
18+
19+ var loadTimer = null,
20+ urlsToLoad = [],
21+ nextURLToLoad = 0,
22+ LOAD_INTERVAL = 50, // ms
23+ overlays = [],
24+ deleteTimer = null,
25+ DELETE_INTERVAL = LOAD_INTERVAL; // ms
26+
27+ function findURLs(items) {
28+ var urls = [],
29+ i,
30+ length;
31+
32+ function findURLsInObject(item) {
33+ var property;
34+
35+ for (property in item) {
36+ if (item.hasOwnProperty(property)) {
37+ if (property === "url" || property === "imageURL" || property === "imageOverlayURL") {
38+ if (item[property]) {
39+ urls.push(item[property]);
40+ }
41+ } else if (typeof item[property] === "object") {
42+ findURLsInObject(item[property]);
43+ }
44+ }
45+ }
46+ }
47+
48+ for (i = 0, length = items.length; i < length; i++) {
49+ switch (typeof items[i]) {
50+ case "string":
51+ urls.push(items[i]);
52+ break;
53+ case "object":
54+ findURLsInObject(items[i]);
55+ break;
56+ default:
57+ App.log("ERROR: Cannot find URL in item type " + (typeof items[i]));
58+ }
59+ }
60+
61+ return urls;
62+ }
63+
64+ function deleteOverlay() {
65+ if (overlays.length === 0) { // Just in case.
66+ deleteTimer = null;
67+ return;
68+ }
69+
70+ Overlays.deleteOverlay(overlays[0]);
71+ overlays.shift();
72+
73+ if (overlays.length > 0) {
74+ deleteTimer = Script.setTimeout(deleteOverlay, DELETE_INTERVAL);
75+ } else {
76+ deleteTimer = null;
77+ }
78+ }
79+
80+ function loadNextURL() {
81+
82+ function loadURL(url) {
83+ var DOMAIN_CORNER = { x: -16382, y: -16382, z: -16382 }, // Near but not right on domain corner.
84+ DUMMY_OVERLAY_PROPERTIES = {
85+ fbx: {
86+ overlay: "model",
87+ dimensions: { x: 0.001, y: 0.001, z: 0.001 },
88+ position: DOMAIN_CORNER,
89+ ignoreRayIntersection: false,
90+ alpha: 0.0,
91+ visible: false
92+ },
93+ svg: {
94+ overlay: "image3d",
95+ scale: 0.001,
96+ position: DOMAIN_CORNER,
97+ ignoreRayIntersection: true,
98+ alpha: 0.0,
99+ visible: false
100+ },
101+ png: {
102+ overlay: "image3d",
103+ scale: 0.001,
104+ position: DOMAIN_CORNER,
105+ ignoreRayIntersection: true,
106+ alpha: 0.0,
107+ visible: false
108+ }
109+ },
110+ fileType,
111+ properties;
112+
113+ fileType = url.slice(-3);
114+ if (DUMMY_OVERLAY_PROPERTIES.hasOwnProperty(fileType)) {
115+ properties = Object.clone(DUMMY_OVERLAY_PROPERTIES[fileType]);
116+ properties.url = url;
117+ overlays.push(Overlays.addOverlay(properties.overlay, properties));
118+ if (deleteTimer === null) {
119+ // Can't delete overlay straight away otherwise asset load is abandoned.
120+ deleteTimer = Script.setTimeout(deleteOverlay, DELETE_INTERVAL);
121+ }
122+ } else {
123+ App.log("ERROR: Cannot preload asset " + url);
124+ }
125+ }
126+
127+ // Find next URL that hasn't already been loaded;
128+ while (nextURLToLoad < urlsToLoad.length && urlsToLoad.indexOf(urlsToLoad[nextURLToLoad]) < nextURLToLoad) {
129+ nextURLToLoad += 1;
130+ }
131+
132+ // Load the URL if there's one to load.
133+ if (nextURLToLoad < urlsToLoad.length) {
134+ // Load the URL.
135+ loadURL(urlsToLoad[nextURLToLoad]);
136+ nextURLToLoad += 1;
137+
138+ // Load the next, if any, after a short delay.
139+ loadTimer = Script.setTimeout(loadNextURL, LOAD_INTERVAL);
140+ } else {
141+ loadTimer = null;
142+ }
143+ }
144+
145+ function load(urls) {
146+ urlsToLoad = urlsToLoad.concat(urls);
147+ if (loadTimer === null) {
148+ loadNextURL();
149+ }
150+ }
151+
152+ function tearDown() {
153+ var i,
154+ length;
155+
156+ if (loadTimer) {
157+ Script.clearTimeout(loadTimer);
158+ }
159+
160+ if (deleteTimer) {
161+ Script.clearTimeout(deleteTimer);
162+ for (i = 0, length = overlays.length; i < length; i++) {
163+ Overlays.deleteOverlay(overlays[i]);
164+ }
165+ }
166+ }
167+
168+ Script.scriptEnding.connect(tearDown);
169+
170+ return {
171+ findURLs: findURLs,
172+ load: load
173+ };
174+
175+}());
unpublishedScripts/marketplace/shapes/modules/toolIcon.js+5−0
@@ -72,6 +72,10 @@ ToolIcon = function (side) { · 71 unmodified lines
7272 return new ToolIcon();
7373 }
7474
75+ function getAssetURLs() {
76+ return [MODEL_PROPERTIES.url];
77+ }
78+
7579 function setHand(side) {
7680 // Assumes UI is not displaying.
7781 if (side === LEFT_HAND) {
@@ -154,6 +158,7 @@ ToolIcon = function (side) { · 76 unmodified lines
154158 }
155159
156160 return {
161+ assetURLs: getAssetURLs,
157162 setHand: setHand,
158163 display: display,
159164 clear: clear,
unpublishedScripts/marketplace/shapes/modules/toolsMenu.js+8−1
@@ -8,7 +8,7 @@ · 7 unmodified lines
88 // See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
99 //
1010
11−/* global ToolsMenu: true, App, Feedback, UIT */
11+/* global ToolsMenu:true, App, Feedback, Preload, UIT */
1212
1313 ToolsMenu = function (side, leftInputs, rightInputs, uiCommandCallback) {
1414 // Tool menu displayed on top of forearm.
@@ -2042,6 +2042,12 @@ ToolsMenu = function (side, leftInputs, rightInputs, uiCommandCallback) { · 2027 unmodified lines
20422042 return new ToolsMenu();
20432043 }
20442044
2045+ function getAssetURLs() {
2046+ return Preload.findURLs([MENU_HEADER_HEADING_PROPERTIES, MENU_HEADER_BAR_PROPERTIES, MENU_HEADER_BACK_PROPERTIES,
2047+ MENU_HEADER_TITLE_PROPERTIES, MENU_HEADER_TITLE_BACK_URL, MENU_HEADER_ICON_PROPERTIES, UI_ELEMENTS,
2048+ PICKLIST_UP_ARROW, PICKLIST_DOWN_ARROW, OPTONS_PANELS, MENU_ITEMS, FOOTER_ITEMS]);
2049+ }
2050+
20452051 controlHand = side === LEFT_HAND ? rightInputs.hand() : leftInputs.hand();
20462052
20472053 function setHand(hand) {
@@ -3672,6 +3678,7 @@ ToolsMenu = function (side, leftInputs, rightInputs, uiCommandCallback) { · 1624 unmodified lines
36723678 }
36733679
36743680 return {
3681+ assetURLs: getAssetURLs,
36753682 COLOR_TOOL: COLOR_TOOL,
36763683 SCALE_TOOL: SCALE_TOOL,
36773684 CLONE_TOOL: CLONE_TOOL,
unpublishedScripts/marketplace/shapes/shapes.js+7−2
@@ -8,7 +8,7 @@ · 7 unmodified lines
88 // See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
99 //
1010
11−/* global Feedback, History */
11+/* global Feedback, History, Preload */
1212
1313 (function () {
1414
@@ -87,6 +87,7 @@ · 72 unmodified lines
8787 Script.include("./modules/highlights.js");
8888 Script.include("./modules/history.js");
8989 Script.include("./modules/laser.js");
90+ Script.include("./modules/preload.js");
9091 Script.include("./modules/selection.js");
9192 Script.include("./modules/toolIcon.js");
9293 Script.include("./modules/toolsMenu.js");
@@ -235,8 +236,12 @@ · 142 unmodified lines
235236 }
236237
237238 toolIcon = new ToolIcon(otherHand(side));
238− toolsMenu = new ToolsMenu(side, leftInputs, rightInputs, uiCommandCallback);
239239 createPalette = new CreatePalette(side, leftInputs, rightInputs, uiCommandCallback);
240+ toolsMenu = new ToolsMenu(side, leftInputs, rightInputs, uiCommandCallback);
241+
242+ Preload.load(toolIcon.assetURLs());
243+ Preload.load(createPalette.assetURLs());
244+ Preload.load(toolsMenu.assetURLs());
240245
241246 getIntersection = side === LEFT_HAND ? rightInputs.intersection : leftInputs.intersection;
242247