diff options
Diffstat (limited to 'js/helper-classes/RDGE/src/core/script/init_state.js')
-rw-r--r-- | js/helper-classes/RDGE/src/core/script/init_state.js | 342 |
1 files changed, 342 insertions, 0 deletions
diff --git a/js/helper-classes/RDGE/src/core/script/init_state.js b/js/helper-classes/RDGE/src/core/script/init_state.js new file mode 100644 index 00000000..8c69d218 --- /dev/null +++ b/js/helper-classes/RDGE/src/core/script/init_state.js | |||
@@ -0,0 +1,342 @@ | |||
1 | /* <copyright> | ||
2 | This file contains proprietary software owned by Motorola Mobility, Inc.<br/> | ||
3 | No rights, expressed or implied, whatsoever to this software are provided by Motorola Mobility, Inc. hereunder.<br/> | ||
4 | (c) Copyright 2011 Motorola Mobility, Inc. All Rights Reserved. | ||
5 | </copyright> */ | ||
6 | |||
7 | // init the view | ||
8 | |||
9 | var loadTag = document.getElementById("loading"); | ||
10 | |||
11 | function LoadState(userRunState, context) | ||
12 | { | ||
13 | this.name = "LoadState"; | ||
14 | this.userRunState = userRunState != undefined ? userRunState : new RDGEState; | ||
15 | this.hasUserState = userRunState != undefined ? true : false; | ||
16 | this.renderer = context.renderer; | ||
17 | this.loadingDone = false; | ||
18 | this.stateManager = context.ctxStateManager; | ||
19 | this.sceneLoadQueue = []; | ||
20 | this.textureLoadQueue = []; | ||
21 | } | ||
22 | |||
23 | LoadState.prototype.loadScene = function(addr, sceneName) | ||
24 | { | ||
25 | var request = new sceneRequestDef(addr, sceneName); | ||
26 | request.doSceneRequest = true; | ||
27 | this.sceneLoadQueue.push( request ); | ||
28 | } | ||
29 | |||
30 | LoadState.prototype.loadTexture = function(textureObject) | ||
31 | { | ||
32 | if(this.stateManager.currentState().name != "LoadState") | ||
33 | { | ||
34 | this.stateManager.PushState( this.stateManager.RDGEInitState, "noInit" ); | ||
35 | } | ||
36 | |||
37 | this.textureLoadQueue.push(textureObject); | ||
38 | } | ||
39 | |||
40 | LoadState.prototype.Init = function() | ||
41 | { | ||
42 | if(this.sceneName) | ||
43 | { | ||
44 | this.loadScene("assets_web/mesh/" + this.sceneName + ".json", this.sceneName); | ||
45 | } | ||
46 | } | ||
47 | |||
48 | LoadState.prototype.ReInit = function() | ||
49 | { | ||
50 | } | ||
51 | |||
52 | LoadState.prototype.Resize = function() | ||
53 | { | ||
54 | if(g_Engine.lastWindowWidth == window.innerWidth && g_Engine.lastWindowHeight == window.innerHeight) | ||
55 | { | ||
56 | this.userRunState.resize(); | ||
57 | g_Engine.lastWindowWidth = window.innerWidth; | ||
58 | g_Engine.lastWindowHeight = window.innerHeight; | ||
59 | } | ||
60 | } | ||
61 | |||
62 | LoadState.prototype.Update = function(dt) | ||
63 | { | ||
64 | // for the current scene go through processing steps | ||
65 | var sceneLoadTop = this.sceneLoadQueue.length - 1; | ||
66 | var texLoadTop = this.textureLoadQueue.length - 1; | ||
67 | |||
68 | if(sceneLoadTop > -1) | ||
69 | { | ||
70 | var curSceneReq = this.sceneLoadQueue[sceneLoadTop]; | ||
71 | |||
72 | // check for completed mesh requests and load the data | ||
73 | g_meshMan.processMeshData(); | ||
74 | |||
75 | if(curSceneReq.doSceneRequest) | ||
76 | { | ||
77 | curSceneReq.requestScene(); | ||
78 | } | ||
79 | else if(curSceneReq.requestComplete) | ||
80 | { | ||
81 | if(curSceneReq.sceneBeginProcessing) | ||
82 | { | ||
83 | // Load meshes attached to the scene | ||
84 | curSceneReq.scene = new SceneGraph(curSceneReq.rawData); | ||
85 | curSceneReq.scene.enableShadows( true ); | ||
86 | g_Engine.AddScene(curSceneReq.name, curSceneReq.scene); | ||
87 | |||
88 | // setup the scene and save a map of mesh names | ||
89 | // that will be check off as the meshes load | ||
90 | curSceneReq.scene.Traverse(curSceneReq.sceneProcessor, false); | ||
91 | |||
92 | // processing is complete | ||
93 | curSceneReq.sceneBeginProcessing = false; | ||
94 | } | ||
95 | // if we are here than the scene is processed but meshes are still loading/processing asynchronously | ||
96 | else if(curSceneReq.processingComplete()) | ||
97 | { | ||
98 | // pop the head node | ||
99 | var sceneReq = this.sceneLoadQueue.shift(); | ||
100 | this.userRunState.onComplete(sceneReq.name, sceneReq.scene); | ||
101 | } | ||
102 | } | ||
103 | |||
104 | } | ||
105 | |||
106 | // load any waiting textures | ||
107 | while(this.textureLoadQueue.length > 0) | ||
108 | { | ||
109 | this.renderer.commitTexture( this.textureLoadQueue.shift() ); | ||
110 | } | ||
111 | |||
112 | // if there is nothing left to load move back to the run state | ||
113 | if( 0 == this.sceneLoadQueue.length && 0 == this.textureLoadQueue.length ) | ||
114 | { | ||
115 | // loaded... remove the state | ||
116 | var stateMan = g_Engine.getContext().ctxStateManager; | ||
117 | stateMan.PopState(); | ||
118 | |||
119 | // Remove loading text | ||
120 | if(loadTag) | ||
121 | { | ||
122 | loadTag.innerHTML="done"; | ||
123 | } | ||
124 | } | ||
125 | |||
126 | if(g_Engine.getContext().getScene() && g_Engine.getContext().getScene() != "not-ready" && this.stateManager.RDGERunState.initialized) | ||
127 | this.userRunState.update(dt); | ||
128 | |||
129 | } | ||
130 | |||
131 | LoadState.prototype.Draw = function() | ||
132 | { | ||
133 | this.renderer._clear(); | ||
134 | |||
135 | if(g_Engine.getContext().getScene() && g_Engine.getContext().getScene() != "not-ready" && this.stateManager.RDGERunState.initialized) | ||
136 | this.userRunState.draw(); | ||
137 | |||
138 | } | ||
139 | |||
140 | LoadState.prototype.Shutdown = function() | ||
141 | { | ||
142 | |||
143 | } | ||
144 | |||
145 | LoadState.prototype.LeaveState = function() | ||
146 | { | ||
147 | if(this.userRunState.onComplete != undefined) | ||
148 | { | ||
149 | this.userRunState.onComplete(); | ||
150 | } | ||
151 | } | ||
152 | |||
153 | LoadState.prototype.MakeSceneRequest = function(addr, name) { | ||
154 | |||
155 | this.hasScene = true; | ||
156 | this.lastSceneName = name; | ||
157 | var request = new XMLHttpRequest(); | ||
158 | request.initState = this; | ||
159 | g_Engine.getContext().sceneGraphMap[name] = "not-ready"; | ||
160 | request.onreadystatechange = function() { | ||
161 | if (request.readyState == 4) { | ||
162 | if (request.status == 200 || window.location.href.indexOf("http") == -1) | ||
163 | { | ||
164 | this.initState.scene = eval("(" + request.responseText + ")"); //retrieve result as an JavaScript object | ||
165 | this.initState.loadingDone = true; | ||
166 | } | ||
167 | else | ||
168 | { | ||
169 | alert("An error has occured making the request"); | ||
170 | } | ||
171 | } | ||
172 | } | ||
173 | |||
174 | request.open("GET", addr, true); | ||
175 | request.send(null); | ||
176 | } | ||
177 | |||
178 | // scene traversal functor to setup the scene | ||
179 | function SetupScene() | ||
180 | { | ||
181 | this.renderer = g_Engine.getContext().renderer; | ||
182 | this.meshLoadingMap = []; | ||
183 | this.onMeshLoaded = function( meshName ) | ||
184 | { | ||
185 | // if the mesh was loading (and is defined) mark it as no longer loading | ||
186 | if(this.meshLoadingMap[meshName]) | ||
187 | this.meshLoadingMap[meshName].stillLoading = false; | ||
188 | } | ||
189 | |||
190 | // set a call back handler to notify us when a mesh is loaded | ||
191 | g_meshMan.addOnLoadedCallback( this ); | ||
192 | } | ||
193 | |||
194 | SetupScene.prototype.process = function(trNode, parent) | ||
195 | { | ||
196 | verifyTransformNode(trNode); | ||
197 | |||
198 | // create and assign parent node | ||
199 | trNode.parent = parent; | ||
200 | |||
201 | if(trNode.local !== undefined) | ||
202 | { | ||
203 | trNode.local = mat4.transpose( trNode.local ); | ||
204 | } | ||
205 | |||
206 | if (((trNode.materialNode || {}).meshNode || {}) != 'undefined') | ||
207 | { | ||
208 | if ( trNode.materialNode !== undefined ) | ||
209 | { | ||
210 | |||
211 | var lookup = g_meshMan.loadMesh(trNode.materialNode.meshNode.mesh); | ||
212 | |||
213 | //~~~~ Hack - the mesh node should be placed in an array of meshes under the transform when exported | ||
214 | trNode.meshes.push(trNode.materialNode.meshNode); | ||
215 | |||
216 | // if the mesh is not loaded add it to our map | ||
217 | if ( lookup == null) | ||