ohm.js 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. "use strict";
  2. /*
  3. The MIT License (MIT)
  4. Copyright (c) 2014-2018 Nikolai Suslov and the Krestianstvo.org project contributors. (https://github.com/NikolaySuslov/livecodingspace/blob/master/LICENSE.md)
  5. Virtual World Framework Apache 2.0 license (https://github.com/NikolaySuslov/livecodingspace/blob/master/licenses/LICENSE_VWF.md)
  6. */
  7. // VWF & Ohm model driver
  8. define( [ "module", "vwf/model", "vwf/utility", "vwf/model/ohm/ohm.min"], function( module, model, utility, ohm) {
  9. // vwf/model/example/scene.js is a demonstration of a scene manager.
  10. return model.load( module, {
  11. // == Module Definition ====================================================================
  12. // -- pipeline -----------------------------------------------------------------------------
  13. // pipeline: [ log ], // vwf <=> log <=> scene
  14. // -- initialize ---------------------------------------------------------------------------
  15. initialize: function() {
  16. var self = this;
  17. this.state = {
  18. nodes: {},
  19. scenes: {},
  20. prototypes: {},
  21. createLocalNode: function (nodeID, childID, childExtendsID, childImplementsIDs,
  22. childSource, childType, childIndex, childName, callback) {
  23. return {
  24. "parentID": nodeID,
  25. "ID": childID,
  26. "extendsID": childExtendsID,
  27. "implementsIDs": childImplementsIDs,
  28. "source": childSource,
  29. "type": childType,
  30. "name": childName,
  31. "prototypes": undefined,
  32. "lang": {
  33. "grammar": undefined,
  34. "semantics": undefined,
  35. "source": undefined
  36. }
  37. };
  38. },
  39. isOhmNodeComponent: function (prototypes) {
  40. var found = false;
  41. if (prototypes) {
  42. for (var i = 0; i < prototypes.length && !found; i++) {
  43. found = (prototypes[i] === "http://vwf.example.com/ohm/node.vwf");
  44. }
  45. }
  46. return found;
  47. }
  48. };
  49. this.state.kernel = this.kernel.kernel.kernel;
  50. this.ohm = ohm;
  51. window._ohm = this.ohm;
  52. //this.state.kernel = this.kernel.kernel.kernel;
  53. },
  54. // == Model API ============================================================================
  55. // -- creatingNode -------------------------------------------------------------------------
  56. creatingNode: function( nodeID, childID, childExtendsID, childImplementsIDs,
  57. childSource, childType, childIndex, childName, callback /* ( ready ) */ ) {
  58. // If the parent nodeID is 0, this node is attached directly to the root and is therefore either
  59. // the scene or a prototype. In either of those cases, save the uri of the new node
  60. var childURI = (nodeID === 0 ? childIndex : undefined);
  61. var appID = this.kernel.application();
  62. // If the node being created is a prototype, construct it and add it to the array of prototypes,
  63. // and then return
  64. var prototypeID = utility.ifPrototypeGetId(appID, this.state.prototypes, nodeID, childID);
  65. if (prototypeID !== undefined) {
  66. this.state.prototypes[prototypeID] = {
  67. parentID: nodeID,
  68. ID: childID,
  69. extendsID: childExtendsID,
  70. implementsID: childImplementsIDs,
  71. source: childSource,
  72. type: childType,
  73. name: childName
  74. };
  75. return;
  76. }
  77. var protos = getPrototypes(this.kernel, childExtendsID);
  78. //var kernel = this.kernel.kernel.kernel;
  79. var node;
  80. if (this.state.isOhmNodeComponent(protos)) {
  81. // Create the local copy of the node properties
  82. if (this.state.nodes[childID] === undefined) {
  83. this.state.nodes[childID] = this.state.createLocalNode(nodeID, childID, childExtendsID, childImplementsIDs,
  84. childSource, childType, childIndex, childName, callback);
  85. }
  86. node = this.state.nodes[childID];
  87. node.prototypes = protos;
  88. //node.aframeObj = createAFrameObject(node);
  89. //addNodeToHierarchy(node);
  90. //notifyDriverOfPrototypeAndBehaviorProps();
  91. }
  92. },
  93. // -- initializingNode -------------------------------------------------------------------------
  94. // initializingNode: function( nodeID, childID, childExtendsID, childImplementsIDs,
  95. // childSource, childType, childIndex, childName ) {
  96. // },
  97. // -- deletingNode -------------------------------------------------------------------------
  98. //deletingNode: function( nodeID ) {
  99. //},
  100. // -- initializingProperty -----------------------------------------------------------------
  101. initializingProperty: function( nodeID, propertyName, propertyValue ) {
  102. var value = undefined;
  103. var node = this.state.nodes[nodeID];
  104. if (node !== undefined) {
  105. value = this.settingProperty(nodeID, propertyName, propertyValue);
  106. }
  107. return value;
  108. },
  109. // -- creatingProperty ---------------------------------------------------------------------
  110. creatingProperty: function (nodeID, propertyName, propertyValue) {
  111. return this.initializingProperty(nodeID, propertyName, propertyValue);
  112. },
  113. // -- settingProperty ----------------------------------------------------------------------
  114. settingProperty: function( nodeID, propertyName, propertyValue ) {
  115. var node = this.state.nodes[nodeID];
  116. var value = undefined;
  117. if (node && utility.validObject(propertyValue)) {
  118. switch ( propertyName ) {
  119. case "ohmLang":
  120. node.lang.source = propertyValue;
  121. node.lang.grammar = ohm.grammar(propertyValue);
  122. node.lang.semantics = node.lang.grammar.createSemantics();
  123. break;
  124. default:
  125. value = undefined;
  126. break;
  127. }
  128. }
  129. return value;
  130. },
  131. // -- gettingProperty ----------------------------------------------------------------------
  132. gettingProperty: function( nodeID, propertyName, propertyValue ) {
  133. var node = this.state.nodes[nodeID];
  134. var value = undefined;
  135. if (node) {
  136. switch ( propertyName ) {
  137. case "grammar":
  138. value = node.lang.grammar;
  139. break;
  140. case "semantics":
  141. value = node.lang.semantics;
  142. break;
  143. }
  144. }
  145. if ( value !== undefined ) {
  146. propertyValue = value;
  147. }
  148. return value;
  149. }
  150. } );
  151. function getPrototypes(kernel, extendsID) {
  152. var prototypes = [];
  153. var id = extendsID;
  154. while (id !== undefined) {
  155. prototypes.push(id);
  156. id = kernel.prototype(id);
  157. }
  158. return prototypes;
  159. }
  160. } );