aframe.js 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920
  1. "use strict";
  2. // VWF & A-Frame model driver
  3. // Copyright 2017 Krestianstvo.org project
  4. //
  5. // Copyright 2012 United States Government, as represented by the Secretary of Defense, Under
  6. // Secretary of Defense (Personnel & Readiness).
  7. //
  8. // Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
  9. // in compliance with the License. You may obtain a copy of the License at
  10. //
  11. // http://www.apache.org/licenses/LICENSE-2.0
  12. //
  13. // Unless required by applicable law or agreed to in writing, software distributed under the License
  14. // is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
  15. // or implied. See the License for the specific language governing permissions and limitations under
  16. // the License.
  17. /// vwf/model/scenejs.js is a placeholder for a 3-D scene manager.
  18. ///
  19. /// @module vwf/model/aframe
  20. /// @requires vwf/model
  21. define(["module", "vwf/model", "vwf/utility"], function (module, model, utility) {
  22. return model.load(module, {
  23. // == Module Definition ====================================================================
  24. // -- initialize ---------------------------------------------------------------------------
  25. initialize: function () {
  26. self = this;
  27. this.state = {
  28. nodes: {},
  29. scenes: {},
  30. prototypes: {},
  31. createLocalNode: function (nodeID, childID, childExtendsID, childImplementsIDs,
  32. childSource, childType, childIndex, childName, callback) {
  33. return {
  34. "parentID": nodeID,
  35. "ID": childID,
  36. "extendsID": childExtendsID,
  37. "implementsIDs": childImplementsIDs,
  38. "source": childSource,
  39. "type": childType,
  40. "name": childName,
  41. "prototypes": undefined,
  42. "aframeObj": undefined,
  43. "scene": undefined
  44. };
  45. },
  46. isAFrameClass: function (prototypes, classID) {
  47. if (prototypes) {
  48. for (var i = 0; i < prototypes.length; i++) {
  49. if (prototypes[i] === classID) {
  50. //console.info( "prototypes[ i ]: " + prototypes[ i ] );
  51. return true;
  52. }
  53. }
  54. }
  55. return false;
  56. },
  57. isAFrameComponent: function (prototypes) {
  58. var found = false;
  59. if (prototypes) {
  60. for (var i = 0; i < prototypes.length && !found; i++) {
  61. found = (prototypes[i] === "http://vwf.example.com/aframe/node.vwf");
  62. }
  63. }
  64. return found;
  65. },
  66. isAEntityComponent: function (prototypes) {
  67. var found = false;
  68. if (prototypes) {
  69. for (var i = 0; i < prototypes.length && !found; i++) {
  70. found = (prototypes[i] === "http://vwf.example.com/aframe/aentityComponent.vwf");
  71. }
  72. }
  73. return found;
  74. },
  75. isAFrameEntityComponent: function (prototypes) {
  76. var found = false;
  77. if (prototypes) {
  78. for (var i = 0; i < prototypes.length && !found; i++) {
  79. found = (prototypes[i] === "http://vwf.example.com/aframe/aentityComponent.vwf");
  80. }
  81. }
  82. return found;
  83. }
  84. };
  85. this.state.kernel = this.kernel.kernel.kernel;
  86. },
  87. // == Model API ============================================================================
  88. // -- creatingNode -------------------------------------------------------------------------
  89. creatingNode: function (nodeID, childID, childExtendsID, childImplementsIDs,
  90. childSource, childType, childIndex, childName, callback /* ( ready ) */) {
  91. // If the parent nodeID is 0, this node is attached directly to the root and is therefore either
  92. // the scene or a prototype. In either of those cases, save the uri of the new node
  93. var childURI = (nodeID === 0 ? childIndex : undefined);
  94. var appID = this.kernel.application();
  95. // If the node being created is a prototype, construct it and add it to the array of prototypes,
  96. // and then return
  97. var prototypeID = utility.ifPrototypeGetId(appID, this.state.prototypes, nodeID, childID);
  98. if (prototypeID !== undefined) {
  99. this.state.prototypes[prototypeID] = {
  100. parentID: nodeID,
  101. ID: childID,
  102. extendsID: childExtendsID,
  103. implementsID: childImplementsIDs,
  104. source: childSource,
  105. type: childType,
  106. name: childName
  107. };
  108. return;
  109. }
  110. var protos = getPrototypes(this.kernel, childExtendsID);
  111. //var kernel = this.kernel.kernel.kernel;
  112. var node;
  113. if (this.state.isAFrameComponent(protos) || this.state.isAEntityComponent(protos)) {
  114. // Create the local copy of the node properties
  115. if (this.state.nodes[childID] === undefined) {
  116. this.state.nodes[childID] = this.state.createLocalNode(nodeID, childID, childExtendsID, childImplementsIDs,
  117. childSource, childType, childIndex, childName, callback);
  118. }
  119. node = this.state.nodes[childID];
  120. node.prototypes = protos;
  121. if ( childType == "component" ){
  122. if ( nodeID !== undefined ) {
  123. setAFrameObjectComponents(node);
  124. }
  125. } else {
  126. node.aframeObj = createAFrameObject(node);
  127. addNodeToHierarchy(node);
  128. //notifyDriverOfPrototypeAndBehaviorProps();
  129. }
  130. }
  131. },
  132. // -- initializingProperty -----------------------------------------------------------------
  133. initializingProperty: function (nodeID, propertyName, propertyValue) {
  134. var value = undefined;
  135. var node = this.state.nodes[nodeID];
  136. if (node !== undefined) {
  137. value = this.settingProperty(nodeID, propertyName, propertyValue);
  138. }
  139. return value;
  140. },
  141. // -- creatingProperty ---------------------------------------------------------------------
  142. creatingProperty: function (nodeID, propertyName, propertyValue) {
  143. return this.initializingProperty(nodeID, propertyName, propertyValue);
  144. },
  145. // -- deletingNode -------------------------------------------------------------------------
  146. //deletingNode: function( nodeID ) {
  147. //},
  148. // -- deletingNode -------------------------------------------------------------------------
  149. deletingNode: function( nodeID ) {
  150. if ( this.state.nodes[ nodeID ] !== undefined ) {
  151. var node = this.state.nodes[ nodeID ];
  152. if ( node.aframeObj !== undefined ) {
  153. // removes and destroys object
  154. node.aframeObj.parentNode.removeChild(node.aframeObj);
  155. node.aframeObj = undefined;
  156. }
  157. delete this.state.nodes[ nodeID ];
  158. }
  159. },
  160. // -- settingProperty ----------------------------------------------------------------------
  161. settingProperty: function (nodeID, propertyName, propertyValue) {
  162. var node = this.state.nodes[nodeID];
  163. var value = undefined;
  164. if (node && node.aframeObj && utility.validObject(propertyValue)) {
  165. var aframeObject = node.aframeObj;
  166. if (isNodeDefinition(node.prototypes)) {
  167. // 'id' will be set to the nodeID
  168. value = propertyValue;
  169. switch (propertyName) {
  170. default:
  171. value = undefined;
  172. break;
  173. }
  174. }
  175. var componentName = "wasd-controls";
  176. if ( value === undefined && aframeObject.attributes.hasOwnProperty(componentName)) {
  177. value = propertyValue;
  178. switch ( propertyName ) {
  179. case "enabled":
  180. aframeObject.setAttribute(componentName, 'enabled', propertyValue);
  181. if (propertyValue){
  182. // aframeObject.addEventListener('componentchanged', function (evt) {
  183. // if (evt.detail.name === 'position') {
  184. // self.kernel.fireEvent(node.parentID, "setPosition", evt.detail.newData);
  185. // }
  186. // if (evt.detail.name === 'rotation') {
  187. // self.kernel.fireEvent(node.ID, "setRotation", evt.detail.newData);
  188. // }
  189. // });
  190. }
  191. break;
  192. default:
  193. value = undefined;
  194. break;
  195. }
  196. }
  197. if ( value === undefined && isAEntityDefinition( node.prototypes ) ) {
  198. value = propertyValue;
  199. switch ( propertyName ) {
  200. case "interpolation":
  201. aframeObject.setAttribute('interpolation', { duration: propertyValue});
  202. break;
  203. case "position":
  204. aframeObject.setAttribute('position', { x: propertyValue[0], y: propertyValue[1], z: propertyValue[2] });
  205. break;
  206. case "rotation":
  207. aframeObject.setAttribute('rotation', { x: propertyValue[0], y: propertyValue[1], z: propertyValue[2] });
  208. break;
  209. case "scale":
  210. aframeObject.setAttribute('scale', { x: propertyValue[0], y: propertyValue[1], z: propertyValue[2] });
  211. break;
  212. case "color":
  213. aframeObject.setAttribute('color', propertyValue);
  214. break;
  215. case "fog":
  216. aframeObject.setAttribute('material','fog', propertyValue);
  217. break;
  218. case "wireframe":
  219. aframeObject.setAttribute('wireframe', propertyValue);
  220. break;
  221. case "wireframe-linewidth":
  222. aframeObject.setAttribute('wireframeLinewidth', propertyValue);
  223. break;
  224. // case "clickable":
  225. // value = propertyValue;
  226. // break;
  227. // case "clickable":
  228. // if (propertyValue) {
  229. // aframeObject.addEventListener('click', function (evt) {
  230. // vwf_view.kernel.fireEvent(node.ID, "clickEvent");
  231. // });
  232. // }
  233. // break;
  234. case "src":
  235. aframeObject.setAttribute('src', propertyValue);
  236. break;
  237. case "repeat":
  238. aframeObject.setAttribute('repeat', propertyValue);
  239. break;
  240. case "look-controls-enabled":
  241. aframeObject.setAttribute('look-controls', 'enabled', propertyValue);
  242. break;
  243. case "wasd-controls":
  244. aframeObject.setAttribute('wasd-controls', 'enabled', propertyValue);
  245. break;
  246. default:
  247. value = undefined;
  248. break;
  249. }
  250. }
  251. if ( value === undefined && aframeObject.nodeName == "A-TEXT" ) {
  252. value = propertyValue;
  253. switch ( propertyName ) {
  254. case "value":
  255. aframeObject.setAttribute('value', propertyValue);
  256. break;
  257. case "color":
  258. aframeObject.setAttribute('color', propertyValue);
  259. break;
  260. case "side":
  261. aframeObject.setAttribute('side', propertyValue);
  262. break;
  263. default:
  264. value = undefined;
  265. break;
  266. }
  267. }
  268. if ( value === undefined && aframeObject.nodeName == "A-SCENE" ) {
  269. value = propertyValue;
  270. switch ( propertyName ) {
  271. case "fog":
  272. aframeObject.setAttribute('fog', propertyValue);
  273. break;
  274. case "assets":
  275. var assetsElement = document.createElement('a-assets');
  276. aframeObject.appendChild(assetsElement);
  277. if (propertyValue) {
  278. httpGetJson(propertyValue).then(function (response) {
  279. console.log(JSON.parse(response));
  280. let assets = JSON.parse(response);
  281. for (var prop in assets) {
  282. var elm = document.createElement(assets[prop].tag);
  283. elm.setAttribute('id', prop);
  284. elm.setAttribute('src', assets[prop].src);
  285. assetsElement.appendChild(elm);
  286. }
  287. }).catch(function (error) {
  288. console.log(error);
  289. });
  290. }
  291. break;
  292. default:
  293. value = undefined;
  294. break;
  295. }
  296. }
  297. if ( value === undefined && aframeObject.nodeName == "A-BOX") {
  298. value = propertyValue;
  299. switch ( propertyName ) {
  300. case "depth":
  301. aframeObject.setAttribute('depth', propertyValue);
  302. break;
  303. case "height":
  304. aframeObject.setAttribute('height', propertyValue);
  305. break;
  306. case "width":
  307. aframeObject.setAttribute('width', propertyValue);
  308. break;
  309. default:
  310. value = undefined;
  311. break;
  312. }
  313. }
  314. if ( value === undefined && aframeObject.nodeName == "A-LIGHT" ) {
  315. value = propertyValue;
  316. switch ( propertyName ) {
  317. //"angle", "color", "decay", "distance", "ground-color", "intensity", "penumbra", "type", "target"
  318. case "color":
  319. aframeObject.setAttribute('color', propertyValue);
  320. break;
  321. case "type":
  322. aframeObject.setAttribute('type', propertyValue);
  323. break;
  324. case "intensity":
  325. aframeObject.setAttribute('intensity', propertyValue);
  326. break;
  327. case "distance":
  328. aframeObject.setAttribute('distance', propertyValue);
  329. break;
  330. default:
  331. value = undefined;
  332. break;
  333. }
  334. }
  335. if ( value === undefined && aframeObject.nodeName == "A-COLLADA-MODEL") {
  336. value = propertyValue;
  337. switch ( propertyName ) {
  338. case "src":
  339. aframeObject.setAttribute('src', propertyValue);
  340. break;
  341. default:
  342. value = undefined;
  343. break;
  344. }
  345. }
  346. if ( value === undefined && aframeObject.nodeName == "A-PLANE") {
  347. value = propertyValue;
  348. switch ( propertyName ) {
  349. case "height":
  350. aframeObject.setAttribute('height', propertyValue);
  351. break;
  352. case "width":
  353. aframeObject.setAttribute('width', propertyValue);
  354. break;
  355. default:
  356. value = undefined;
  357. break;
  358. }
  359. }
  360. if (value === undefined && aframeObject.nodeName == "A-SPHERE") {
  361. value = propertyValue;
  362. switch (propertyName) {
  363. case "radius":
  364. aframeObject.setAttribute('radius', propertyValue);
  365. break;
  366. default:
  367. value = undefined;
  368. break;
  369. }
  370. }
  371. if (value === undefined && aframeObject.nodeName == "A-CAMERA") {
  372. value = propertyValue;
  373. switch (propertyName) {
  374. case "activeForAvatar":
  375. console.log("set active camera to view");
  376. break;
  377. case "forAvatar":
  378. if (propertyValue) {
  379. // aframeObject.addEventListener('componentchanged', function (evt) {
  380. // if (evt.detail.name === 'position') {
  381. // self.kernel.fireEvent(node.ID, "setAvatarPosition", evt.detail.newData);
  382. // }
  383. // if (evt.detail.name === 'rotation') {
  384. // self.kernel.fireEvent(node.ID, "setAvatarRotation", evt.detail.newData);
  385. // }
  386. // });
  387. }
  388. break;
  389. default:
  390. value = undefined;
  391. break;
  392. }
  393. }
  394. //if (!aframeObject) return value;
  395. //if (propertyValue !== undefined) {
  396. //self = this;
  397. }
  398. return value;
  399. },
  400. // -- gettingProperty ----------------------------------------------------------------------
  401. gettingProperty: function (nodeID, propertyName, propertyValue) {
  402. var node = this.state.nodes[nodeID];
  403. var value = undefined;
  404. if (node && node.aframeObj) {
  405. var aframeObject = node.aframeObj;
  406. if (isNodeDefinition(node.prototypes)) {
  407. switch ( propertyName ) {
  408. }
  409. }
  410. if ( value === undefined && aframeObject.attributes.hasOwnProperty("wasd-controls")) {
  411. value = propertyValue;
  412. switch ( propertyName ) {
  413. case "enabled":
  414. aframeObject.setAttribute('wasd-controls', 'enabled', propertyValue);
  415. break;
  416. }
  417. }
  418. if ( value === undefined && isAEntityDefinition( node.prototypes ) ) {
  419. switch ( propertyName ) {
  420. case "interpolation":
  421. var interpolation = aframeObject.getAttribute('interpolation');
  422. if (interpolation !== null && interpolation !== undefined) {
  423. value = interpolation.duration;
  424. }
  425. break;
  426. case "position":
  427. var pos = aframeObject.getAttribute('position');
  428. if ( pos !== undefined ){
  429. value = [pos.x, pos.y, pos.z];
  430. }
  431. break;
  432. case "scale":
  433. var scale = aframeObject.getAttribute('scale');
  434. if ( scale !== undefined ){
  435. value = [scale.x, scale.y, scale.z];
  436. }
  437. break;
  438. case "rotation":
  439. var rot = aframeObject.getAttribute('rotation');
  440. if ( rot !== undefined ){
  441. value = [rot.x, rot.y, rot.z];
  442. }
  443. break;
  444. case "color":
  445. value = aframeObject.getAttribute('color');
  446. break;
  447. case "fog":
  448. if (aframeObject.getAttribute('material')){
  449. value = aframeObject.getAttribute('material').fog;
  450. }
  451. break;
  452. case "wireframe":
  453. value = aframeObject.getAttribute('wireframe');
  454. break;
  455. case "wireframe-linewidth":
  456. value = aframeObject.getAttribute('wireframeLinewidth');
  457. break;
  458. // case "clickable":
  459. // value = propertyValue;
  460. // break;
  461. case "src":
  462. value = aframeObject.getAttribute('src');
  463. break;
  464. case "repeat":
  465. value = aframeObject.getAttribute('repeat');
  466. case "look-controls-enabled":
  467. var look = aframeObject.getAttribute('look-controls-enabled');
  468. if (look !== null && look !== undefined) {
  469. value = aframeObject.getAttribute('look-controls').enabled;
  470. }
  471. break;
  472. case "wasd-controls":
  473. var wasd = aframeObject.getAttribute('wasd-controls');
  474. if (wasd !== null && wasd !== undefined) {
  475. value = aframeObject.getAttribute('wasd-controls').enabled;
  476. }
  477. break;
  478. }
  479. }
  480. if ( value === undefined && aframeObject.nodeName == "A-SCENE" ) {
  481. switch ( propertyName ) {
  482. case "fog":
  483. value = aframeObject.getAttribute('fog');
  484. break;
  485. }
  486. }
  487. if ( value === undefined && aframeObject.nodeName == "A-BOX" ) {
  488. switch ( propertyName ) {
  489. case "depth":
  490. value = aframeObject.getAttribute('depth');
  491. break;
  492. case "height":
  493. value = aframeObject.getAttribute('height');
  494. break;
  495. case "width":
  496. value = aframeObject.getAttribute('width');
  497. break;
  498. }
  499. }
  500. if ( value === undefined && aframeObject.nodeName == "A-LIGHT" ) {
  501. //"angle", "color", "decay", "distance", "ground-color", "intensity", "penumbra", "type", "target"
  502. switch (propertyName) {
  503. case "color":
  504. value = aframeObject.getAttribute('color');
  505. break;
  506. case "type":
  507. value = aframeObject.getAttribute('type');
  508. break;
  509. case "distance":
  510. value = aframeObject.getAttribute('distance');
  511. break;
  512. case "intensity":
  513. value = aframeObject.getAttribute('intensity');
  514. break;
  515. }
  516. }
  517. if ( value === undefined && aframeObject.nodeName == "A-PLANE" ) {
  518. switch (propertyName) {
  519. case "height":
  520. value = aframeObject.getAttribute('height');
  521. break;
  522. case "width":
  523. value = aframeObject.getAttribute('width');
  524. break;
  525. }
  526. }
  527. if ( value === undefined && aframeObject.nodeName == "A-SPHERE" ) {
  528. switch (propertyName) {
  529. case "radius":
  530. value = aframeObject.getAttribute('radius');
  531. break;
  532. }
  533. }
  534. if ( value === undefined && aframeObject.nodeName == "A-TEXT" ) {
  535. switch (propertyName) {
  536. case "value":
  537. value = aframeObject.getAttribute('value');
  538. break;
  539. case "color":
  540. value = aframeObject.getAttribute('color');
  541. break;
  542. case "side":
  543. value = aframeObject.getAttribute('side');
  544. break;
  545. }
  546. }
  547. if ( value === undefined && aframeObject.nodeName == "A-CAMERA" ) {
  548. switch (propertyName) {
  549. }
  550. }
  551. if ( value === undefined && aframeObject.nodeName == "A-COLLADA-MODEL" ) {
  552. switch (propertyName) {
  553. case "src":
  554. value = aframeObject.getAttribute('src');
  555. break;
  556. }
  557. }
  558. }
  559. if ( value !== undefined ) {
  560. propertyValue = value;
  561. }
  562. return value;
  563. }
  564. });
  565. function setAFrameObjectComponents(node, config) {
  566. let protos = node.prototypes;
  567. let parentNode = self.state.nodes[node.parentID];
  568. if (self.state.isAFrameClass(protos, "http://vwf.example.com/aframe/wasd-controls.vwf")) {
  569. parentNode.aframeObj.setAttribute('wasd-controls', {});
  570. }
  571. node.aframeObj = parentNode.aframeObj;
  572. }
  573. function createAFrameObject(node, config) {
  574. var protos = node.prototypes;
  575. var aframeObj = undefined;
  576. if (self.state.isAFrameClass(protos, "http://vwf.example.com/aframe/ascene.vwf")) {
  577. aframeObj = document.createElement('a-scene');
  578. self.state.scenes[node.ID] = aframeObj;
  579. } else if (self.state.isAFrameClass(protos, "http://vwf.example.com/aframe/acamera.vwf")) {
  580. aframeObj = document.createElement('a-camera');
  581. } else if (self.state.isAFrameClass(protos, "http://vwf.example.com/aframe/alight.vwf")) {
  582. aframeObj = document.createElement('a-light');
  583. } else if (self.state.isAFrameClass(protos, "http://vwf.example.com/aframe/acursor.vwf")) {
  584. aframeObj = document.createElement('a-cursor');
  585. } else if (self.state.isAFrameClass(protos, "http://vwf.example.com/aframe/asky.vwf")) {
  586. aframeObj = document.createElement('a-sky');
  587. } else if (self.state.isAFrameClass(protos, "http://vwf.example.com/aframe/abox.vwf")) {
  588. aframeObj = document.createElement('a-box');
  589. } else if (self.state.isAFrameClass(protos, "http://vwf.example.com/aframe/aplane.vwf")) {
  590. aframeObj = document.createElement('a-plane');
  591. } else if (self.state.isAFrameClass(protos, "http://vwf.example.com/aframe/atext.vwf")) {
  592. aframeObj = document.createElement('a-text');
  593. } else if (self.state.isAFrameClass(protos, "http://vwf.example.com/aframe/acolladamodel.vwf")) {
  594. aframeObj = document.createElement('a-collada-model');
  595. } else if (self.state.isAFrameClass(protos, "http://vwf.example.com/aframe/asphere.vwf")) {
  596. aframeObj = document.createElement('a-sphere');
  597. } else if (self.state.isAFrameClass(protos, "http://vwf.example.com/aframe/aentity.vwf")) {
  598. aframeObj = document.createElement('a-entity');
  599. }
  600. aframeObj.setAttribute('id', node.ID);
  601. return aframeObj;
  602. }
  603. function addNodeToHierarchy(node) {
  604. if (node.aframeObj) {
  605. if (self.state.nodes[node.parentID] !== undefined) {
  606. var parent = self.state.nodes[node.parentID];
  607. if (parent.aframeObj) {
  608. if (parent.children === undefined) {
  609. parent.children = [];
  610. }
  611. parent.children.push(node.ID);
  612. //console.info( "Adding child: " + childID + " to " + nodeID );
  613. parent.aframeObj.appendChild(node.aframeObj);
  614. }
  615. }
  616. if (node.aframeObj.nodeName !== "A-SCENE") {
  617. node.scene = self.state.scenes[self.kernel.application()];
  618. }
  619. }
  620. }
  621. function getPrototypes(kernel, extendsID) {
  622. var prototypes = [];
  623. var id = extendsID;
  624. while (id !== undefined) {
  625. prototypes.push(id);
  626. id = kernel.prototype(id);
  627. }
  628. return prototypes;
  629. }
  630. function isNodeDefinition(prototypes) {
  631. var found = false;
  632. if (prototypes) {
  633. for (var i = 0; i < prototypes.length && !found; i++) {
  634. found = (prototypes[i] == "http://vwf.example.com/aframe/node.vwf");
  635. }
  636. }
  637. return found;
  638. }
  639. function isAEntityDefinition( prototypes ) {
  640. var found = false;
  641. if ( prototypes ) {
  642. for ( var i = 0; i < prototypes.length && !found; i++ ) {
  643. found = ( prototypes[i] == "http://vwf.example.com/aframe/aentity.vwf" );
  644. }
  645. }
  646. return found;
  647. }
  648. // Changing this function significantly from the GLGE code
  649. // Will search hierarchy down until encountering a matching child
  650. // Will look into nodes that don't match.... this might not be desirable
  651. function FindChildByName(obj, childName, childType, recursive) {
  652. var child = undefined;
  653. if (recursive) {
  654. // TODO: If the obj itself has the child name, the object will be returned by this function
  655. // I don't think this this desirable.
  656. if (nameTest.call(this, obj, childName)) {
  657. child = obj;
  658. } else if (obj.children && obj.children.length > 0) {
  659. for (var i = 0; i < obj.children.length && child === undefined; i++) {
  660. child = FindChildByName(obj.children[i], childName, childType, true);
  661. }
  662. }
  663. } else {
  664. if (obj.children) {
  665. for (var i = 0; i < obj.children.length && child === undefined; i++) {
  666. if (nameTest.call(this, obj.children[i], childName)) {
  667. child = obj.children[i];
  668. }
  669. }
  670. }
  671. }
  672. return child;
  673. }
  674. function nameTest(obj, name) {
  675. if (obj.name == "") {
  676. return (obj.parent.name + "Child" == name);
  677. } else {
  678. return (obj.name == name || obj.id == name || obj.vwfID == name);
  679. }
  680. }
  681. function httpGet(url) {
  682. return new Promise(function (resolve, reject) {
  683. // do the usual Http request
  684. let request = new XMLHttpRequest();
  685. request.open('GET', url);
  686. request.onload = function () {
  687. if (request.status == 200) {
  688. resolve(request.response);
  689. } else {
  690. reject(Error(request.statusText));
  691. }
  692. };
  693. request.onerror = function () {
  694. reject(Error('Network Error'));
  695. };
  696. request.send();
  697. });
  698. }
  699. async function httpGetJson(url) {
  700. // check if the URL looks like a JSON file and call httpGet.
  701. let regex = /\.(json)$/i;
  702. if (regex.test(url)) {
  703. // call the async function, wait for the result
  704. return await httpGet(url);
  705. } else {
  706. throw Error('Bad Url Format');
  707. }
  708. }
  709. });