1 /*
  2   Copyright (c) 2008 Seneca College
  3   Licenced under the MIT License (http://www.c3dl.org/index.php/mit-license/)
  4 */
  5 
  6 // functions for initialization
  7 
  8 c3dl.mainCallBacks = [];
  9 c3dl.preloadModels = [];
 10 
 11 /**
 12 	@private
 13 	Add a progress bar for each canvas on the page.  Place them in the
 14 	middle of each canvas.  The progress bar is an animated gif placed 
 15 	in the center of the screen.  
 16 	
 17 	The primary purpose of using a progress bar is to notify users
 18 	that collada model files are being parsed.   When the collada
 19 	Queue becomes empty, it will automatically call removeProgressBars().
 20 */
 21 c3dl.addProgressBars = function()
 22 {
 23 	// get all the canvases in the DOM.
 24 	var canvases = document.getElementsByTagName('canvas');
 25 		
 26 	// add a progress bar for each of the canvases.
 27 	for (var i = 0; i < canvases.length; i++)
 28 	{
 29 		// to place the loading gif in the center of the canvas, we'll need to
 30 		// get the absolute position of the canvas.
 31 		var pos = c3dl.getObjectPosition(canvases[i]);
 32 		var xOffset = pos[0];
 33 		var yOffset = pos[1];
 34 
 35 		var progressBar = document.createElement("img");
 36 		progressBar.src = basePath + "loading.gif";
 37 		progressBar.style.position = 'absolute';
 38 
 39 		// place the center of the gif in the center of the canvas
 40 		// First get the middle of the canvas then add the offset from the 
 41 		// top or left corner of the screen, then move the image left 
 42 		// (by subtracting) since positioning the image is relative to 
 43 		// the top left corner of the image.
 44 		progressBar.style.left = (canvases[i].width/2) + xOffset - (50);//progressBar.width/2); progressbar is not yet loaded
 45 		progressBar.style.top =  (canvases[i].height/2) + yOffset - (50);// so its width and height is zero.
 46 
 47 		// make it translucent as to not annoy the user too much.
 48 		progressBar.style.opacity = 0.5;
 49 
 50 		// try to force this image above all others.
 51 		progressBar.style.zIndex = 100;
 52 		
 53 		// make sure the id of each progress bar is unique so we 
 54 		// can remove them all later.
 55 		progressBar.id = 'c3dl_progress_bar_' + i;
 56 		document.body.appendChild(progressBar);
 57 	}
 58 }
 59 
 60 /**
 61 	@private
 62 	Remove all the progress bars from the page.  Each canvas will have
 63 	a progress bar if the user of the library used addModel() to add
 64 	a model to be parsed. 
 65 */
 66 c3dl.removeProgressBars = function()
 67 {
 68 	// To remove all the loading bars, we just need to get the number of
 69 	// canvases on the page.  Since every canvas will have their own loading
 70 	// bar, we know how many bars there will be.  This will allow us to 
 71 	// create the id's of each individual loading bar.
 72 	var numProgressBars = document.getElementsByTagName('canvas').length;
 73 	for (var i = 0; i < numProgressBars; i++)
 74 	{
 75 		// generate the id's of the progress bars
 76 		var progressBarID = 'c3dl_progress_bar_' + i;
 77 
 78 		var progressBar = document.getElementById(progressBarID);
 79 		document.body.removeChild(progressBar);
 80 	}
 81 }
 82 
 83 /**
 84 	@private
 85 	This is a function which the browser will call once the loading of the 
 86 	page is done.
 87 	
 88 	Once the page is done loading, this function will place all the models 
 89 	the user will use in the course of the script into the ColladaQueue.  
 90 	Once the Queue detects it is empty, it will call all the 'main' 
 91 	functions the user wants to start automatically.
 92 
 93 	If the user did not provide the library with main functions, it will be
 94 	up to the user to call those main functions.  If the user did not 
 95 	provide the models they will use in their script, references to models 
 96 	may not exit yet when the main funciton is executed.
 97 */
 98 c3dl.init = function()
 99 {
100 	// if the user does not want to parse any collada models,
101 	// we don't put anything in the queue and go right ahead and 
102 	// call the main methods.
103 	if( c3dl.preloadModels.length == 0)
104 	{
105 		for (var i = 0; i < c3dl.mainCallBacks.length; i++)
106 		{
107 			// Each element is an object which holds a function 
108 			// and a tag.  They were both placed in a wrapper
109 			// object so we can stick to using arrays for simplicity.
110 			var func = c3dl.mainCallBacks[i].f;
111 			var tag =  c3dl.mainCallBacks[i].t;
112 			func(tag);
113 		}
114 	}
115 	// otherwise we will let the collada queue call the main methods
116 	// once all the models have been parsed.
117 	// By creating collada objects, they will be placed in the queue.
118 	// Once the queue is empty, the main methods will be called by
119 	// the Queue.
120 	else
121 	{
122 		// This will add an animated gif to the DOM, letting
123 		// the user know that there is loading occuring.
124 		c3dl.addProgressBars();
125 	
126 		for (var i=0; i < c3dl.preloadModels.length; i++)
127 		{
128 			var preloadColadda = new c3dl.Collada();
129 			preloadColadda.init(c3dl.preloadModels[i]);
130 		}
131 	}
132 }
133 
134 /**
135 	Add a model to the collada queue to be parsed
136 	before the main funciton is run. Call this function
137 	once for each collada file your script will use.
138 
139 	@param {string} model - path to a .dae file.
140 */
141 c3dl.addModel = function(model)
142 {
143 	c3dl.preloadModels.push(model);
144 }
145 
146 /**
147 	Add a function to a list of functions to call once the
148 	page is finished loading.
149 
150 	@param {Function} func - the function to call once the web page
151 	is finished loading.
152 
153 	@param {String} tagName - the tag name of the canvas associated 
154 	with the function.
155 */
156 c3dl.addMainCallBack = function(func, tagName)
157 {
158 	// put both objects into a wrapper object so later
159 	// we can access the couple as an array access.
160 	var obj = {
161 		f:func,
162 		t:tagName
163 	};
164 
165 	c3dl.mainCallBacks.push(obj);
166 }
167 
168 // This will make sure the c3dl.init() funciton is called once the web page
169 // is done loading.
170 if (document.addEventListener) {
171 	document.addEventListener("DOMContentLoaded", c3dl.init, false);
172 }
173