aframe.js 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919
  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 "transparent":
  216. aframeObject.setAttribute('material','transparent', propertyValue);
  217. break;
  218. case "opacity":
  219. aframeObject.setAttribute('material','opacity', propertyValue);
  220. break;
  221. case "fog":
  222. aframeObject.setAttribute('material','fog', propertyValue);
  223. break;
  224. case "wireframe":
  225. aframeObject.setAttribute('wireframe', propertyValue);
  226. break;
  227. case "wireframe-linewidth":
  228. aframeObject.setAttribute('wireframeLinewidth', propertyValue);
  229. break;
  230. // case "clickable":
  231. // console.log("set clickable!");
  232. // value = propertyValue;
  233. // break;
  234. // case "clickable":
  235. // if (propertyValue) {
  236. // aframeObject.addEventListener('click', function (evt) {
  237. // console.log("click!");
  238. // vwf_view.kernel.fireEvent(node.ID, "clickEvent",evt.detail.cursorEl.id);
  239. // });
  240. // }
  241. // break;
  242. case "src":
  243. aframeObject.setAttribute('src', propertyValue);
  244. break;
  245. case "repeat":
  246. aframeObject.setAttribute('repeat', propertyValue);
  247. break;
  248. case "look-controls-enabled":
  249. aframeObject.setAttribute('look-controls', 'enabled', propertyValue);
  250. break;
  251. case "wasd-controls":
  252. aframeObject.setAttribute('wasd-controls', 'enabled', propertyValue);
  253. break;
  254. default:
  255. value = undefined;
  256. break;
  257. }
  258. }
  259. if ( value === undefined && aframeObject.nodeName == "A-TEXT" ) {
  260. value = propertyValue;
  261. switch ( propertyName ) {
  262. case "value":
  263. aframeObject.setAttribute('value', propertyValue);
  264. break;
  265. case "color":
  266. aframeObject.setAttribute('color', propertyValue);
  267. break;
  268. case "side":
  269. aframeObject.setAttribute('side', propertyValue);
  270. break;
  271. default:
  272. value = undefined;
  273. break;
  274. }
  275. }
  276. if ( value === undefined && aframeObject.nodeName == "A-SCENE" ) {
  277. value = propertyValue;
  278. switch ( propertyName ) {
  279. case "fog":
  280. aframeObject.setAttribute('fog', propertyValue);
  281. break;
  282. case "assets":
  283. var assetsElement = document.createElement('a-assets');
  284. aframeObject.appendChild(assetsElement);
  285. if (propertyValue) {
  286. httpGetJson(propertyValue).then(function (response) {
  287. console.log(JSON.parse(response));
  288. let assets = JSON.parse(response);
  289. for (var prop in assets) {
  290. var elm = document.createElement(assets[prop].tag);
  291. elm.setAttribute('id', prop);
  292. elm.setAttribute('src', assets[prop].src);
  293. assetsElement.appendChild(elm);
  294. }
  295. }).catch(function (error) {
  296. console.log(error);
  297. });
  298. }
  299. break;
  300. default:
  301. value = undefined;
  302. break;
  303. }
  304. }
  305. if ( value === undefined && aframeObject.nodeName == "A-BOX") {
  306. value = propertyValue;
  307. switch ( propertyName ) {
  308. case "depth":
  309. aframeObject.setAttribute('depth', propertyValue);
  310. break;
  311. case "height":
  312. aframeObject.setAttribute('height', propertyValue);
  313. break;
  314. case "width":
  315. aframeObject.setAttribute('width', propertyValue);
  316. break;
  317. default:
  318. value = undefined;
  319. break;
  320. }
  321. }
  322. if ( value === undefined && aframeObject.nodeName == "A-LIGHT" ) {
  323. value = propertyValue;
  324. switch ( propertyName ) {
  325. //"angle", "color", "decay", "distance", "ground-color", "intensity", "penumbra", "type", "target"
  326. case "color":
  327. aframeObject.setAttribute('color', propertyValue);
  328. break;
  329. case "type":
  330. aframeObject.setAttribute('type', propertyValue);
  331. break;
  332. case "intensity":
  333. aframeObject.setAttribute('intensity', propertyValue);
  334. break;
  335. case "distance":
  336. aframeObject.setAttribute('distance', propertyValue);
  337. break;
  338. default:
  339. value = undefined;
  340. break;
  341. }
  342. }
  343. if ( value === undefined && aframeObject.nodeName == "A-COLLADA-MODEL") {
  344. value = propertyValue;
  345. switch ( propertyName ) {
  346. case "src":
  347. aframeObject.setAttribute('src', propertyValue);
  348. break;
  349. default:
  350. value = undefined;
  351. break;
  352. }
  353. }
  354. if ( value === undefined && aframeObject.nodeName == "A-PLANE") {
  355. value = propertyValue;
  356. switch ( propertyName ) {
  357. case "height":
  358. aframeObject.setAttribute('height', propertyValue);
  359. break;
  360. case "width":
  361. aframeObject.setAttribute('width', propertyValue);
  362. break;
  363. default:
  364. value = undefined;
  365. break;
  366. }
  367. }
  368. if (value === undefined && aframeObject.nodeName == "A-SPHERE") {
  369. value = propertyValue;
  370. switch (propertyName) {
  371. case "radius":
  372. aframeObject.setAttribute('radius', propertyValue);
  373. break;
  374. default:
  375. value = undefined;
  376. break;
  377. }
  378. }
  379. if (value === undefined && aframeObject.nodeName == "A-CAMERA") {
  380. value = propertyValue;
  381. switch (propertyName) {
  382. case "active":
  383. aframeObject.setAttribute('active', propertyValue);
  384. break;
  385. default:
  386. value = undefined;
  387. break;
  388. }
  389. }
  390. }
  391. return value;
  392. },
  393. // -- gettingProperty ----------------------------------------------------------------------
  394. gettingProperty: function (nodeID, propertyName, propertyValue) {
  395. var node = this.state.nodes[nodeID];
  396. var value = undefined;
  397. if (node && node.aframeObj) {
  398. var aframeObject = node.aframeObj;
  399. if (isNodeDefinition(node.prototypes)) {
  400. switch ( propertyName ) {
  401. }
  402. }
  403. if ( value === undefined && aframeObject.attributes.hasOwnProperty("wasd-controls")) {
  404. value = propertyValue;
  405. switch ( propertyName ) {
  406. case "enabled":
  407. aframeObject.setAttribute('wasd-controls', 'enabled', propertyValue);
  408. break;
  409. }
  410. }
  411. if ( value === undefined && isAEntityDefinition( node.prototypes ) ) {
  412. switch ( propertyName ) {
  413. case "interpolation":
  414. var interpolation = aframeObject.getAttribute('interpolation');
  415. if (interpolation !== null && interpolation !== undefined) {
  416. value = interpolation.duration;
  417. }
  418. break;
  419. case "position":
  420. var pos = aframeObject.getAttribute('position');
  421. if ( pos !== undefined ){
  422. value = [pos.x, pos.y, pos.z];
  423. }
  424. break;
  425. case "scale":
  426. var scale = aframeObject.getAttribute('scale');
  427. if ( scale !== undefined ){
  428. value = [scale.x, scale.y, scale.z];
  429. }
  430. break;
  431. case "rotation":
  432. var rot = aframeObject.getAttribute('rotation');
  433. if ( rot !== undefined ){
  434. value = [rot.x, rot.y, rot.z];
  435. }
  436. break;
  437. case "color":
  438. value = aframeObject.getAttribute('color');
  439. break;
  440. case "fog":
  441. if (aframeObject.getAttribute('material')){
  442. value = aframeObject.getAttribute('material').fog;
  443. }
  444. break;
  445. case "opacity":
  446. if (aframeObject.getAttribute('material')){
  447. value = aframeObject.getAttribute('material').opacity;
  448. }
  449. break;
  450. case "transparent":
  451. if (aframeObject.getAttribute('material')){
  452. value = aframeObject.getAttribute('material').transparent;
  453. }
  454. break;
  455. case "wireframe":
  456. value = aframeObject.getAttribute('wireframe');
  457. break;
  458. case "wireframe-linewidth":
  459. value = aframeObject.getAttribute('wireframeLinewidth');
  460. break;
  461. // case "clickable":
  462. // value = propertyValue;
  463. // break;
  464. case "src":
  465. value = aframeObject.getAttribute('src');
  466. break;
  467. case "repeat":
  468. value = aframeObject.getAttribute('repeat');
  469. case "look-controls-enabled":
  470. var look = aframeObject.getAttribute('look-controls-enabled');
  471. if (look !== null && look !== undefined) {
  472. value = aframeObject.getAttribute('look-controls').enabled;
  473. }
  474. break;
  475. case "wasd-controls":
  476. var wasd = aframeObject.getAttribute('wasd-controls');
  477. if (wasd !== null && wasd !== undefined) {
  478. value = aframeObject.getAttribute('wasd-controls').enabled;
  479. }
  480. break;
  481. }
  482. }
  483. if ( value === undefined && aframeObject.nodeName == "A-SCENE" ) {
  484. switch ( propertyName ) {
  485. case "fog":
  486. value = aframeObject.getAttribute('fog');
  487. break;
  488. }
  489. }
  490. if ( value === undefined && aframeObject.nodeName == "A-BOX" ) {
  491. switch ( propertyName ) {
  492. case "depth":
  493. value = aframeObject.getAttribute('depth');
  494. break;
  495. case "height":
  496. value = aframeObject.getAttribute('height');
  497. break;
  498. case "width":
  499. value = aframeObject.getAttribute('width');
  500. break;
  501. }
  502. }
  503. if ( value === undefined && aframeObject.nodeName == "A-LIGHT" ) {
  504. //"angle", "color", "decay", "distance", "ground-color", "intensity", "penumbra", "type", "target"
  505. switch (propertyName) {
  506. case "color":
  507. value = aframeObject.getAttribute('color');
  508. break;
  509. case "type":
  510. value = aframeObject.getAttribute('type');
  511. break;
  512. case "distance":
  513. value = aframeObject.getAttribute('distance');
  514. break;
  515. case "intensity":
  516. value = aframeObject.getAttribute('intensity');
  517. break;
  518. }
  519. }
  520. if ( value === undefined && aframeObject.nodeName == "A-PLANE" ) {
  521. switch (propertyName) {
  522. case "height":
  523. value = aframeObject.getAttribute('height');
  524. break;
  525. case "width":
  526. value = aframeObject.getAttribute('width');
  527. break;
  528. }
  529. }
  530. if ( value === undefined && aframeObject.nodeName == "A-SPHERE" ) {
  531. switch (propertyName) {
  532. case "radius":
  533. value = aframeObject.getAttribute('radius');
  534. break;
  535. }
  536. }
  537. if ( value === undefined && aframeObject.nodeName == "A-TEXT" ) {
  538. switch (propertyName) {
  539. case "value":
  540. value = aframeObject.getAttribute('value');
  541. break;
  542. case "color":
  543. value = aframeObject.getAttribute('color');
  544. break;
  545. case "side":
  546. value = aframeObject.getAttribute('side');
  547. break;
  548. }
  549. }
  550. if ( value === undefined && aframeObject.nodeName == "A-CAMERA" ) {
  551. switch (propertyName) {
  552. case "active":
  553. value = aframeObject.getAttribute('active');
  554. break;
  555. }
  556. }
  557. if ( value === undefined && aframeObject.nodeName == "A-COLLADA-MODEL" ) {
  558. switch (propertyName) {
  559. case "src":
  560. value = aframeObject.getAttribute('src');
  561. break;
  562. }
  563. }
  564. }
  565. if ( value !== undefined ) {
  566. propertyValue = value;
  567. }
  568. return value;
  569. }
  570. });
  571. function setAFrameObjectComponents(node, config) {
  572. let protos = node.prototypes;
  573. let parentNode = self.state.nodes[node.parentID];
  574. if (self.state.isAFrameClass(protos, "http://vwf.example.com/aframe/wasd-controls.vwf")) {
  575. parentNode.aframeObj.setAttribute('wasd-controls', {});
  576. }
  577. node.aframeObj = parentNode.aframeObj;
  578. }
  579. function createAFrameObject(node, config) {
  580. var protos = node.prototypes;
  581. var aframeObj = undefined;
  582. if (self.state.isAFrameClass(protos, "http://vwf.example.com/aframe/ascene.vwf")) {
  583. aframeObj = document.createElement('a-scene');
  584. self.state.scenes[node.ID] = aframeObj;
  585. } else if (self.state.isAFrameClass(protos, "http://vwf.example.com/aframe/acamera.vwf")) {
  586. aframeObj = document.createElement('a-camera');
  587. } else if (self.state.isAFrameClass(protos, "http://vwf.example.com/aframe/alight.vwf")) {
  588. aframeObj = document.createElement('a-light');
  589. } else if (self.state.isAFrameClass(protos, "http://vwf.example.com/aframe/acursor.vwf")) {
  590. aframeObj = document.createElement('a-cursor');
  591. } else if (self.state.isAFrameClass(protos, "http://vwf.example.com/aframe/asky.vwf")) {
  592. aframeObj = document.createElement('a-sky');
  593. } else if (self.state.isAFrameClass(protos, "http://vwf.example.com/aframe/abox.vwf")) {
  594. aframeObj = document.createElement('a-box');
  595. } else if (self.state.isAFrameClass(protos, "http://vwf.example.com/aframe/aplane.vwf")) {
  596. aframeObj = document.createElement('a-plane');
  597. } else if (self.state.isAFrameClass(protos, "http://vwf.example.com/aframe/atext.vwf")) {
  598. aframeObj = document.createElement('a-text');
  599. } else if (self.state.isAFrameClass(protos, "http://vwf.example.com/aframe/acolladamodel.vwf")) {
  600. aframeObj = document.createElement('a-collada-model');
  601. } else if (self.state.isAFrameClass(protos, "http://vwf.example.com/aframe/asphere.vwf")) {
  602. aframeObj = document.createElement('a-sphere');
  603. } else if (self.state.isAFrameClass(protos, "http://vwf.example.com/aframe/aentity.vwf")) {
  604. aframeObj = document.createElement('a-entity');
  605. }
  606. aframeObj.setAttribute('id', node.ID);
  607. return aframeObj;
  608. }
  609. function addNodeToHierarchy(node) {
  610. if (node.aframeObj) {
  611. if (self.state.nodes[node.parentID] !== undefined) {
  612. var parent = self.state.nodes[node.parentID];
  613. if (parent.aframeObj) {
  614. if (parent.children === undefined) {
  615. parent.children = [];
  616. }
  617. parent.children.push(node.ID);
  618. //console.info( "Adding child: " + childID + " to " + nodeID );
  619. parent.aframeObj.appendChild(node.aframeObj);
  620. }
  621. }
  622. if (node.aframeObj.nodeName !== "A-SCENE") {
  623. node.scene = self.state.scenes[self.kernel.application()];
  624. }
  625. }
  626. }
  627. function getPrototypes(kernel, extendsID) {
  628. var prototypes = [];
  629. var id = extendsID;
  630. while (id !== undefined) {
  631. prototypes.push(id);
  632. id = kernel.prototype(id);
  633. }
  634. return prototypes;
  635. }
  636. function isNodeDefinition(prototypes) {
  637. var found = false;
  638. if (prototypes) {
  639. for (var i = 0; i < prototypes.length && !found; i++) {
  640. found = (prototypes[i] == "http://vwf.example.com/aframe/node.vwf");
  641. }
  642. }
  643. return found;
  644. }
  645. function isAEntityDefinition( prototypes ) {
  646. var found = false;
  647. if ( prototypes ) {
  648. for ( var i = 0; i < prototypes.length && !found; i++ ) {
  649. found = ( prototypes[i] == "http://vwf.example.com/aframe/aentity.vwf" );
  650. }
  651. }
  652. return found;
  653. }
  654. // Changing this function significantly from the GLGE code
  655. // Will search hierarchy down until encountering a matching child
  656. // Will look into nodes that don't match.... this might not be desirable
  657. function FindChildByName(obj, childName, childType, recursive) {
  658. var child = undefined;
  659. if (recursive) {
  660. // TODO: If the obj itself has the child name, the object will be returned by this function
  661. // I don't think this this desirable.
  662. if (nameTest.call(this, obj, childName)) {
  663. child = obj;
  664. } else if (obj.children && obj.children.length > 0) {
  665. for (var i = 0; i < obj.children.length && child === undefined; i++) {
  666. child = FindChildByName(obj.children[i], childName, childType, true);
  667. }
  668. }
  669. } else {
  670. if (obj.children) {
  671. for (var i = 0; i < obj.children.length && child === undefined; i++) {
  672. if (nameTest.call(this, obj.children[i], childName)) {
  673. child = obj.children[i];
  674. }
  675. }
  676. }
  677. }
  678. return child;
  679. }
  680. function nameTest(obj, name) {
  681. if (obj.name == "") {
  682. return (obj.parent.name + "Child" == name);
  683. } else {
  684. return (obj.name == name || obj.id == name || obj.vwfID == name);
  685. }
  686. }
  687. function httpGet(url) {
  688. return new Promise(function (resolve, reject) {
  689. // do the usual Http request
  690. let request = new XMLHttpRequest();
  691. request.open('GET', url);
  692. request.onload = function () {
  693. if (request.status == 200) {
  694. resolve(request.response);
  695. } else {
  696. reject(Error(request.statusText));
  697. }
  698. };
  699. request.onerror = function () {
  700. reject(Error('Network Error'));
  701. };
  702. request.send();
  703. });
  704. }
  705. async function httpGetJson(url) {
  706. // check if the URL looks like a JSON file and call httpGet.
  707. let regex = /\.(json)$/i;
  708. if (regex.test(url)) {
  709. // call the async function, wait for the result
  710. return await httpGet(url);
  711. } else {
  712. throw Error('Bad Url Format');
  713. }
  714. }
  715. });