1 /* 2 Copyright (c) 2008 Seneca College 3 Licenced under the MIT License (http://www.c3dl.org/index.php/mit-license/) 4 */ 5 6 /** 7 @private 8 @class ColladaManager prevents the same collada file from being loaded into 9 memory more than once. 10 */ 11 c3dl.ColladaManager = {}; 12 13 // parallel arrays. keys have the filePaths, values have the c3dl.SceneNode root 14 // nodes. 15 c3dl.ColladaManager.keys = []; 16 c3dl.ColladaManager.values = []; 17 18 /** 19 @private 20 Get the scenegraph's root for the filePath. 21 22 @param {String} filePath 23 24 @returns c3dl.SceneNode or null if the file has not finished loading. 25 26 c3dl.ColladaManager.getSceneGraphRoot = function(filePath) 27 { 28 var index = c3dl.ColladaManager.getIndex(filePath); 29 30 // if it's in the table 31 if(index != -1) 32 { 33 // The loader will set the root once it has finished parsing. 34 // Initially when we create the sceneGraph, the root is null 35 // indicating the graph hasn't been created. 36 return c3dl.ColladaManager.values[index]; 37 } 38 else 39 { 40 return null; 41 } 42 }*/ 43 44 45 /** 46 @private 47 Load a collada file at 'filePath'. This method will check if 48 the model is already loaded, thus preventing the file being 49 loaded twice. 50 */ 51 c3dl.ColladaManager.loadFile = function(filePath) 52 { 53 // prevent loading the file twice 54 if(c3dl.ColladaManager.isFileLoaded(filePath) == false) 55 { 56 // create a node which the loader will assign other nodes. 57 var rootNode = new c3dl.SceneNode(); 58 59 // give the loader a sceneGraph which it will populate with nodes. 60 // We know it has finished once it has set the scenegraph's root. 61 var colladaLoader = new c3dl.ColladaLoader(); 62 colladaLoader.load(filePath, rootNode); 63 64 c3dl.ColladaManager.keys.push(filePath); 65 c3dl.ColladaManager.values.push(rootNode); 66 } 67 } 68 69 /** 70 @private 71 Make a copy of the sceneGraph 72 73 @param {String} filePath 74 */ 75 c3dl.ColladaManager.getSceneGraphCopy = function(filePath) 76 { 77 if(c3dl.ColladaManager.isFileLoaded(filePath)) 78 { 79 var i = c3dl.ColladaManager.getIndex(filePath); 80 81 // get a copy of the scenegraph 82 var sg = c3dl.ColladaManager.values[i].getCopy(); 83 84 //return ColladaManager.values[i]; 85 return sg; 86 } 87 88 // return null? 89 } 90 91 /** 92 @private 93 Has the file already been loaded? 94 95 @param filePath {string} 96 97 @return true if the file has already been loaded, otherwise false. 98 */ 99 c3dl.ColladaManager.isFileLoaded = function(filePath) 100 { 101 // if its in the 'table', it will return non-negative one. 102 return c3dl.ColladaManager.getIndex(filePath) != -1 ? true : false; 103 } 104 105 /** 106 @private 107 Get the 0-based index of the filePath. If we have 108 that index, we can reference the values array since 109 they are parallel. 110 */ 111 c3dl.ColladaManager.getIndex = function(filePath) 112 { 113 var index = -1; 114 115 for (var i = 0; i < c3dl.ColladaManager.values.length; i++) 116 { 117 if(filePath == c3dl.ColladaManager.keys[i]) 118 { 119 index = i; 120 break; 121 } 122 } 123 return index; 124 } 125