pts.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  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. */
  5. // VWF & PtsJS model driver
  6. import { Fabric } from '/core/vwf/fabric.js';
  7. class PTSModel extends Fabric {
  8. constructor(module) {
  9. console.log("PTSModel constructor");
  10. super(module, "Model");
  11. }
  12. factory() {
  13. let _self_ = this;
  14. return this.load( this.module,
  15. {
  16. // == Module Definition ====================================================================
  17. // -- pipeline -----------------------------------------------------------------------------
  18. // pipeline: [ log ], // vwf <=> log <=> scene
  19. // -- initialize ---------------------------------------------------------------------------
  20. initialize: function() {
  21. var self = this;
  22. this.state = {
  23. nodes: {},
  24. scenes: {},
  25. prototypes: {},
  26. createLocalNode: function (nodeID, childID, childExtendsID, childImplementsIDs,
  27. childSource, childType, childIndex, childName, callback) {
  28. return {
  29. "parentID": nodeID,
  30. "ID": childID,
  31. "extendsID": childExtendsID,
  32. "implementsIDs": childImplementsIDs,
  33. "source": childSource,
  34. "type": childType,
  35. "name": childName,
  36. "prototypes": undefined
  37. };
  38. },
  39. isNodeComponent: function (prototypes) {
  40. var found = false;
  41. if (prototypes) {
  42. for (var i = 0; i < prototypes.length && !found; i++) {
  43. found = (prototypes[i] === "proxy/pts/node.vwf");
  44. }
  45. }
  46. return found;
  47. },
  48. isClass: function (prototypes, classID) {
  49. if (prototypes) {
  50. for (var i = 0; i < prototypes.length; i++) {
  51. if (prototypes[i] === classID) {
  52. //console.info( "prototypes[ i ]: " + prototypes[ i ] );
  53. return true;
  54. }
  55. }
  56. }
  57. return false;
  58. },
  59. isPTDefinition: function (prototypes) {
  60. var found = false;
  61. if (prototypes) {
  62. for (var i = 0; i < prototypes.length && !found; i++) {
  63. found = (prototypes[i] == "proxy/pts/pt.vwf");
  64. }
  65. }
  66. return found;
  67. },
  68. createObject: function (node, config) {
  69. var protos = node.prototypes;
  70. var obj = undefined;
  71. if (this.isClass(protos, "proxy/pts/scene.vwf")) {
  72. let el = document.createElement("space");
  73. el.setAttribute("id", "space");
  74. el.style.height = "600px";
  75. el.style.width = "800px";
  76. document.querySelector("body").appendChild(el);
  77. Pts.namespace( window );
  78. obj = new CanvasSpace("#space").setup(
  79. { bgcolor: "#99eeff", retina: true, resize: false });
  80. obj.nodeName = "space";
  81. obj.nodeID = node.ID;
  82. this.scenes[node.ID] = node;
  83. node.form = obj.getForm();
  84. }
  85. // if (this.isClass(protos, "proxy/pts/player.vwf")) {
  86. // toneObj = new Tone.PolySynth(Tone.MembraneSynth);
  87. // }
  88. if(this.isPTDefinition(protos)) {
  89. obj = new Pt(2);
  90. obj.nodeName = "pt";
  91. obj.nodeID = node.ID;
  92. }
  93. return obj
  94. },
  95. addNodeToHierarchy: function (node) {
  96. if (node.obj) {
  97. if (node.obj.nodeName !== "space") {
  98. node.scene = this.scenes[self.kernel.application()];
  99. }
  100. }
  101. }
  102. };
  103. this.state.kernel = this.kernel.kernel.kernel;
  104. //this.Tone = Tone;
  105. //this.state.kernel = this.kernel.kernel.kernel;
  106. },
  107. // == Model API ============================================================================
  108. // -- creatingNode -------------------------------------------------------------------------
  109. creatingNode: function( nodeID, childID, childExtendsID, childImplementsIDs,
  110. childSource, childType, childIndex, childName, callback /* ( ready ) */ ) {
  111. // If the parent nodeID is 0, this node is attached directly to the root and is therefore either
  112. // the scene or a prototype. In either of those cases, save the uri of the new node
  113. var childURI = (nodeID === 0 ? childIndex : undefined);
  114. var appID = this.kernel.application();
  115. // If the node being created is a prototype, construct it and add it to the array of prototypes,
  116. // and then return
  117. var prototypeID = _self_.utility.ifPrototypeGetId(appID, this.state.prototypes, nodeID, childID);
  118. if (prototypeID !== undefined) {
  119. this.state.prototypes[prototypeID] = {
  120. parentID: nodeID,
  121. ID: childID,
  122. extendsID: childExtendsID,
  123. implementsID: childImplementsIDs,
  124. source: childSource,
  125. type: childType,
  126. name: childName
  127. };
  128. return;
  129. }
  130. var protos = _self_.getPrototypes(this.kernel, childExtendsID);
  131. //var kernel = this.kernel.kernel.kernel;
  132. var node;
  133. if (this.state.isNodeComponent(protos)) {
  134. // Create the local copy of the node properties
  135. if (this.state.nodes[childID] === undefined) {
  136. this.state.nodes[childID] = this.state.createLocalNode(nodeID, childID, childExtendsID, childImplementsIDs,
  137. childSource, childType, childIndex, childName, callback);
  138. }
  139. node = this.state.nodes[childID];
  140. node.prototypes = protos;
  141. node.obj = this.state.createObject(node);
  142. this.state.addNodeToHierarchy(node);
  143. //let aframeDriver = vwf.views["/drivers/view/aframe"];
  144. //notifyDriverOfPrototypeAndBehaviorProps();
  145. }
  146. },
  147. // -- initializingNode -------------------------------------------------------------------------
  148. // initializingNode: function( nodeID, childID, childExtendsID, childImplementsIDs,
  149. // childSource, childType, childIndex, childName ) {
  150. // },
  151. // -- deletingNode -------------------------------------------------------------------------
  152. deletingNode: function( nodeID ) {
  153. if (nodeID) {
  154. var childNode = this.state.nodes[nodeID];
  155. if (!childNode) return;
  156. if (childNode !== undefined) {
  157. if (childNode.children) {
  158. for (var i = 0; i < childNode.children.length; i++) {
  159. this.deletingNode(childNode.children[i]);
  160. }
  161. }
  162. if (childNode.obj !== undefined) {
  163. // removes and destroys object
  164. let space = childNode.scene.obj;
  165. let player = Object.values(space.players).filter(el=> el.myID == nodeID)[0];
  166. space.remove(player.animateID);
  167. childNode.obj = undefined;
  168. }
  169. delete this.state.nodes[nodeID];
  170. }
  171. }
  172. },
  173. // -- initializingProperty -----------------------------------------------------------------
  174. initializingProperty: function( nodeID, propertyName, propertyValue ) {
  175. var value = undefined;
  176. var node = this.state.nodes[nodeID];
  177. if (node !== undefined) {
  178. value = this.settingProperty(nodeID, propertyName, propertyValue);
  179. }
  180. return value;
  181. },
  182. // -- creatingProperty ---------------------------------------------------------------------
  183. creatingProperty: function (nodeID, propertyName, propertyValue) {
  184. return this.initializingProperty(nodeID, propertyName, propertyValue);
  185. },
  186. // -- settingProperty ----------------------------------------------------------------------
  187. settingProperty: function( nodeID, propertyName, propertyValue ) {
  188. let self = this;
  189. let node = this.state.nodes[nodeID];
  190. var value = undefined;
  191. if (node && node.obj && _self_.utility.validObject(propertyValue)) {
  192. let object = node.obj;
  193. // if (self.state.isComponentNodeDefinition(node.prototypes)) {
  194. // value = propertyValue;
  195. // switch (propertyName) {
  196. // default:
  197. // value = undefined;
  198. // break;
  199. // }
  200. // }
  201. if (value === undefined && self.state.isPTDefinition(node.prototypes)) {
  202. value = propertyValue;
  203. switch (propertyName) {
  204. case "x":
  205. object.x = propertyValue
  206. break;
  207. case "y":
  208. object.y = propertyValue
  209. break;
  210. default:
  211. value = undefined;
  212. break;
  213. }
  214. }
  215. }
  216. return value;
  217. },
  218. // -- gettingProperty ----------------------------------------------------------------------
  219. gettingProperty: function( nodeID, propertyName, propertyValue ) {
  220. let self = this;
  221. let node = this.state.nodes[nodeID];
  222. let value = undefined;
  223. if (node && node.obj) {
  224. let object = node.obj;
  225. // if (self.state.isComponentNodeDefinition(node.prototypes)) {
  226. // switch (propertyName) {
  227. // }
  228. // }
  229. if (value === undefined && self.state.isPTDefinition(node.prototypes)) {
  230. switch (propertyName) {
  231. case "x":
  232. value = object.x
  233. break;
  234. case "y":
  235. value = object.y
  236. break;
  237. }
  238. }
  239. }
  240. if ( value !== undefined ) {
  241. propertyValue = value;
  242. }
  243. return value;
  244. }
  245. } );
  246. }
  247. getPrototypes(kernel, extendsID) {
  248. var prototypes = [];
  249. var id = extendsID;
  250. while (id !== undefined) {
  251. prototypes.push(id);
  252. id = kernel.prototype(id);
  253. }
  254. return prototypes;
  255. }
  256. }
  257. export {
  258. PTSModel as default
  259. }