ohm.js 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. "use strict";
  2. // Copyright 2012 United States Government, as represented by the Secretary of Defense, Under
  3. // Secretary of Defense (Personnel & Readiness).
  4. //
  5. // Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
  6. // in compliance with the License. You may obtain a copy of the License at
  7. //
  8. // http://www.apache.org/licenses/LICENSE-2.0
  9. //
  10. // Unless required by applicable law or agreed to in writing, software distributed under the License
  11. // is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
  12. // or implied. See the License for the specific language governing permissions and limitations under
  13. // the License.
  14. /// vwf/model/scenejs.js is a placeholder for a 3-D scene manager.
  15. ///
  16. /// @module vwf/model/scenejs
  17. /// @requires vwf/model
  18. define( [ "module", "vwf/model", "vwf/utility", "vwf/model/ohm/ohm.min"], function( module, model, utility, ohm) {
  19. // vwf/model/example/scene.js is a demonstration of a scene manager.
  20. return model.load( module, {
  21. // == Module Definition ====================================================================
  22. // -- pipeline -----------------------------------------------------------------------------
  23. // pipeline: [ log ], // vwf <=> log <=> scene
  24. // -- initialize ---------------------------------------------------------------------------
  25. initialize: function() {
  26. var 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. "lang": {
  43. "grammar": undefined,
  44. "semantics": undefined,
  45. "source": undefined
  46. }
  47. };
  48. },
  49. isOhmNodeComponent: function (prototypes) {
  50. var found = false;
  51. if (prototypes) {
  52. for (var i = 0; i < prototypes.length && !found; i++) {
  53. found = (prototypes[i] === "http://vwf.example.com/ohm/node.vwf");
  54. }
  55. }
  56. return found;
  57. }
  58. };
  59. this.state.kernel = this.kernel.kernel.kernel;
  60. this.ohm = ohm;
  61. window._ohm = this.ohm;
  62. //this.state.kernel = this.kernel.kernel.kernel;
  63. },
  64. // == Model API ============================================================================
  65. // -- creatingNode -------------------------------------------------------------------------
  66. creatingNode: function( nodeID, childID, childExtendsID, childImplementsIDs,
  67. childSource, childType, childIndex, childName, callback /* ( ready ) */ ) {
  68. // If the parent nodeID is 0, this node is attached directly to the root and is therefore either
  69. // the scene or a prototype. In either of those cases, save the uri of the new node
  70. var childURI = (nodeID === 0 ? childIndex : undefined);
  71. var appID = this.kernel.application();
  72. // If the node being created is a prototype, construct it and add it to the array of prototypes,
  73. // and then return
  74. var prototypeID = utility.ifPrototypeGetId(appID, this.state.prototypes, nodeID, childID);
  75. if (prototypeID !== undefined) {
  76. this.state.prototypes[prototypeID] = {
  77. parentID: nodeID,
  78. ID: childID,
  79. extendsID: childExtendsID,
  80. implementsID: childImplementsIDs,
  81. source: childSource,
  82. type: childType,
  83. name: childName
  84. };
  85. return;
  86. }
  87. var protos = getPrototypes(this.kernel, childExtendsID);
  88. //var kernel = this.kernel.kernel.kernel;
  89. var node;
  90. if (this.state.isOhmNodeComponent(protos)) {
  91. // Create the local copy of the node properties
  92. if (this.state.nodes[childID] === undefined) {
  93. this.state.nodes[childID] = this.state.createLocalNode(nodeID, childID, childExtendsID, childImplementsIDs,
  94. childSource, childType, childIndex, childName, callback);
  95. }
  96. node = this.state.nodes[childID];
  97. node.prototypes = protos;
  98. //node.aframeObj = createAFrameObject(node);
  99. //addNodeToHierarchy(node);
  100. //notifyDriverOfPrototypeAndBehaviorProps();
  101. }
  102. },
  103. // -- initializingNode -------------------------------------------------------------------------
  104. // initializingNode: function( nodeID, childID, childExtendsID, childImplementsIDs,
  105. // childSource, childType, childIndex, childName ) {
  106. // },
  107. // -- deletingNode -------------------------------------------------------------------------
  108. //deletingNode: function( nodeID ) {
  109. //},
  110. // -- initializingProperty -----------------------------------------------------------------
  111. initializingProperty: function( nodeID, propertyName, propertyValue ) {
  112. var value = undefined;
  113. var node = this.state.nodes[nodeID];
  114. if (node !== undefined) {
  115. value = this.settingProperty(nodeID, propertyName, propertyValue);
  116. }
  117. return value;
  118. },
  119. // -- creatingProperty ---------------------------------------------------------------------
  120. creatingProperty: function (nodeID, propertyName, propertyValue) {
  121. return this.initializingProperty(nodeID, propertyName, propertyValue);
  122. },
  123. // -- settingProperty ----------------------------------------------------------------------
  124. settingProperty: function( nodeID, propertyName, propertyValue ) {
  125. var node = this.state.nodes[nodeID];
  126. var value = undefined;
  127. if (node && utility.validObject(propertyValue)) {
  128. switch ( propertyName ) {
  129. case "ohmLang":
  130. node.lang.source = propertyValue;
  131. node.lang.grammar = ohm.grammar(propertyValue);
  132. node.lang.semantics = node.lang.grammar.createSemantics();
  133. break;
  134. default:
  135. value = undefined;
  136. break;
  137. }
  138. }
  139. return value;
  140. },
  141. // -- gettingProperty ----------------------------------------------------------------------
  142. gettingProperty: function( nodeID, propertyName, propertyValue ) {
  143. var node = this.state.nodes[nodeID];
  144. var value = undefined;
  145. if (node) {
  146. switch ( propertyName ) {
  147. case "grammar":
  148. value = node.lang.grammar;
  149. break;
  150. case "semantics":
  151. value = node.lang.semantics;
  152. break;
  153. }
  154. }
  155. if ( value !== undefined ) {
  156. propertyValue = value;
  157. }
  158. return value;
  159. }
  160. } );
  161. function getPrototypes(kernel, extendsID) {
  162. var prototypes = [];
  163. var id = extendsID;
  164. while (id !== undefined) {
  165. prototypes.push(id);
  166. id = kernel.prototype(id);
  167. }
  168. return prototypes;
  169. }
  170. } );