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
8// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html8// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
9//9//
10 10
11/* global CreatePalette: true, App, Feedback, History, UIT */11/* global CreatePalette:true, App, Feedback, History, Preload, UIT */
12 12
13CreatePalette = function (side, leftInputs, rightInputs, uiCommandCallback) {13CreatePalette = function (side, leftInputs, rightInputs, uiCommandCallback) {
14 // Tool menu displayed on top of forearm.14 // Tool menu displayed on top of forearm.
@@ -332,6 +332,11 @@ CreatePalette = function (side, leftInputs, rightInputs, uiCommandCallback) { · 317 unmodified lines
332 }332 }
333 333
334 334
335 function getAssetURLs() {
336 return Preload.findURLs([PALETTE_HEADER_HEADING_PROPERTIES, PALETTE_HEADER_BAR_PROPERTIES, PALETTE_TITLE_PROPERTIES,
337 PALETTE_ITEMS]);
338 }
339
335 function setHand(hand) {340 function setHand(hand) {
336 // Assumes UI is not displaying.341 // Assumes UI is not displaying.
337 var NUMBER_OF_HANDS = 2;342 var NUMBER_OF_HANDS = 2;
@@ -533,6 +538,7 @@ CreatePalette = function (side, leftInputs, rightInputs, uiCommandCallback) { · 195 unmodified lines
533 }538 }
534 539
535 return {540 return {
541 assetURLs: getAssetURLs,
536 setHand: setHand,542 setHand: setHand,
537 overlayIDs: getOverlayIDs,543 overlayIDs: getOverlayIDs,
538 setVisible: setVisible,544 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
13Preload = (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
72 return new ToolIcon();72 return new ToolIcon();
73 }73 }
74 74
75 function getAssetURLs() {
76 return [MODEL_PROPERTIES.url];
77 }
78
75 function setHand(side) {79 function setHand(side) {
76 // Assumes UI is not displaying.80 // Assumes UI is not displaying.
77 if (side === LEFT_HAND) {81 if (side === LEFT_HAND) {
@@ -154,6 +158,7 @@ ToolIcon = function (side) { · 76 unmodified lines
154 }158 }
155 159
156 return {160 return {
161 assetURLs: getAssetURLs,
157 setHand: setHand,162 setHand: setHand,
158 display: display,163 display: display,
159 clear: clear,164 clear: clear,
unpublishedScripts/marketplace/shapes/modules/toolsMenu.js+8−1
@@ -8,7 +8,7 @@ · 7 unmodified lines
8// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html8// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
9//9//
10 10
11/* global ToolsMenu: true, App, Feedback, UIT */11/* global ToolsMenu:true, App, Feedback, Preload, UIT */
12 12
13ToolsMenu = function (side, leftInputs, rightInputs, uiCommandCallback) {13ToolsMenu = function (side, leftInputs, rightInputs, uiCommandCallback) {
14 // Tool menu displayed on top of forearm.14 // Tool menu displayed on top of forearm.
@@ -2042,6 +2042,12 @@ ToolsMenu = function (side, leftInputs, rightInputs, uiCommandCallback) { · 2027 unmodified lines
2042 return new ToolsMenu();2042 return new ToolsMenu();
2043 }2043 }
2044 2044
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
2045 controlHand = side === LEFT_HAND ? rightInputs.hand() : leftInputs.hand();2051 controlHand = side === LEFT_HAND ? rightInputs.hand() : leftInputs.hand();
2046 2052
2047 function setHand(hand) {2053 function setHand(hand) {
@@ -3672,6 +3678,7 @@ ToolsMenu = function (side, leftInputs, rightInputs, uiCommandCallback) { · 1624 unmodified lines
3672 }3678 }
3673 3679
3674 return {3680 return {
3681 assetURLs: getAssetURLs,
3675 COLOR_TOOL: COLOR_TOOL,3682 COLOR_TOOL: COLOR_TOOL,
3676 SCALE_TOOL: SCALE_TOOL,3683 SCALE_TOOL: SCALE_TOOL,
3677 CLONE_TOOL: CLONE_TOOL,3684 CLONE_TOOL: CLONE_TOOL,
unpublishedScripts/marketplace/shapes/shapes.js+7−2
@@ -8,7 +8,7 @@ · 7 unmodified lines
8// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html8// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
9//9//
10 10
11/* global Feedback, History */11/* global Feedback, History, Preload */
12 12
13(function () {13(function () {
14 14
@@ -87,6 +87,7 @@ · 72 unmodified lines
87 Script.include("./modules/highlights.js");87 Script.include("./modules/highlights.js");
88 Script.include("./modules/history.js");88 Script.include("./modules/history.js");
89 Script.include("./modules/laser.js");89 Script.include("./modules/laser.js");
90 Script.include("./modules/preload.js");
90 Script.include("./modules/selection.js");91 Script.include("./modules/selection.js");
91 Script.include("./modules/toolIcon.js");92 Script.include("./modules/toolIcon.js");
92 Script.include("./modules/toolsMenu.js");93 Script.include("./modules/toolsMenu.js");
@@ -235,8 +236,12 @@ · 142 unmodified lines
235 }236 }
236 237
237 toolIcon = new ToolIcon(otherHand(side));238 toolIcon = new ToolIcon(otherHand(side));
238 toolsMenu = new ToolsMenu(side, leftInputs, rightInputs, uiCommandCallback);
239 createPalette = new CreatePalette(side, leftInputs, rightInputs, uiCommandCallback);239 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());
240 245
241 getIntersection = side === LEFT_HAND ? rightInputs.intersection : leftInputs.intersection;246 getIntersection = side === LEFT_HAND ? rightInputs.intersection : leftInputs.intersection;
242 247