ohm.js 9.5 KB

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