rubik.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461
  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 & Ohm model driver
  6. import { Fabric } from '/core/vwf/fabric.js';
  7. import {skipjs} from "/drivers/model/rubik/lib/skip.js"
  8. import {Queue} from "/drivers/model/rubik/lib/queues.js"
  9. import {Color} from "/drivers/model/rubik/lib/colors.js"
  10. import {Twist} from "/drivers/model/rubik/lib/twists.js"
  11. import {Direction} from "/drivers/model/rubik/lib/directions.js"
  12. import {Fold} from "/drivers/model/rubik/lib/folds.js"
  13. import {Cubelet} from "/drivers/model/rubik/lib/cubelets.js"
  14. import {Group} from "/drivers/model/rubik/lib/groups.js"
  15. import {Slice} from "/drivers/model/rubik/lib/slices.js"
  16. import {Cube} from "/drivers/model/rubik/lib/cubes.js"
  17. class RubikModel extends Fabric {
  18. constructor(module) {
  19. console.log("RUBIK model constructor");
  20. super(module, "Model");
  21. }
  22. factory() {
  23. let _self_ = this;
  24. return this.load( this.module,
  25. {
  26. // == Module Definition ====================================================================
  27. // -- pipeline -----------------------------------------------------------------------------
  28. // pipeline: [ log ], // vwf <=> log <=> scene
  29. // -- initialize ---------------------------------------------------------------------------
  30. initialize: function() {
  31. let self = this;
  32. skipjs();
  33. globalThis.Queue = Queue;
  34. globalThis.Color = Color;
  35. globalThis.Twist = Twist;
  36. globalThis.Direction = Direction;
  37. globalThis.Fold = Fold;
  38. globalThis.Cubelet = Cubelet;
  39. globalThis.Group = Group;
  40. globalThis.Slice = Slice;
  41. globalThis.Cube = Cube;
  42. if( globalThis.setupTasks ) {
  43. globalThis.setupTasks.forEach( function( task ){ task() })
  44. }
  45. this.state = {
  46. nodes: {},
  47. scenes: {},
  48. prototypes: {},
  49. createLocalNode: function (nodeID, childID, childExtendsID, childImplementsIDs,
  50. childSource, childType, childIndex, childName, callback) {
  51. return {
  52. "parentID": nodeID,
  53. "ID": childID,
  54. "extendsID": childExtendsID,
  55. "implementsIDs": childImplementsIDs,
  56. "source": childSource,
  57. "type": childType,
  58. "name": childName,
  59. "prototypes": undefined,
  60. };
  61. },
  62. isCubeNodeComponent: function (prototypes) {
  63. var found = false;
  64. if (prototypes) {
  65. for (var i = 0; i < prototypes.length && !found; i++) {
  66. found = (prototypes[i] === "cubeModel.vwf");
  67. }
  68. }
  69. return found;
  70. },
  71. isCubeletNodeComponent: function (prototypes) {
  72. var found = false;
  73. if (prototypes) {
  74. for (var i = 0; i < prototypes.length && !found; i++) {
  75. found = (prototypes[i] === "cubeletModel.vwf");
  76. }
  77. }
  78. return found;
  79. }
  80. };
  81. this.state.kernel = this.kernel.kernel.kernel;
  82. //this.state.kernel = this.kernel.kernel.kernel;
  83. },
  84. // == Model API ============================================================================
  85. // -- creatingNode -------------------------------------------------------------------------
  86. creatingNode: function( nodeID, childID, childExtendsID, childImplementsIDs,
  87. childSource, childType, childIndex, childName, callback /* ( ready ) */ ) {
  88. // If the parent nodeID is 0, this node is attached directly to the root and is therefore either
  89. // the scene or a prototype. In either of those cases, save the uri of the new node
  90. var childURI = (nodeID === 0 ? childIndex : undefined);
  91. var appID = this.kernel.application();
  92. // If the node being created is a prototype, construct it and add it to the array of prototypes,
  93. // and then return
  94. var prototypeID = _self_.utility.ifPrototypeGetId(appID, this.state.prototypes, nodeID, childID);
  95. if (prototypeID !== undefined) {
  96. this.state.prototypes[prototypeID] = {
  97. parentID: nodeID,
  98. ID: childID,
  99. extendsID: childExtendsID,
  100. implementsID: childImplementsIDs,
  101. source: childSource,
  102. type: childType,
  103. name: childName
  104. };
  105. return;
  106. }
  107. var protos = _self_.getPrototypes(this.kernel, childExtendsID);
  108. //var kernel = this.kernel.kernel.kernel;
  109. var node;
  110. if (this.state.isCubeNodeComponent(protos)) {
  111. // Create the local copy of the node properties
  112. if (this.state.nodes[childID] === undefined) {
  113. this.state.nodes[childID] = this.state.createLocalNode(nodeID, childID, childExtendsID, childImplementsIDs,
  114. childSource, childType, childIndex, childName, callback);
  115. }
  116. node = this.state.nodes[childID];
  117. node.prototypes = protos;
  118. node.cube = new Cube(childName);
  119. node.cube.nodeID = childID;
  120. node.cube.kernel = this.state.kernel;
  121. // let jsModel = vwf.models['/core/vwf/model/javascript'].model;
  122. // let jsNode = jsModel.nodes[childID];
  123. // jsNode.cubeModel = node.cube;
  124. //node.aframeObj = createAFrameObject(node);
  125. //addNodeToHierarchy(node);
  126. //notifyDriverOfPrototypeAndBehaviorProps();
  127. }
  128. if (this.state.isCubeletNodeComponent(protos)) {
  129. // Create the local copy of the node properties
  130. if (this.state.nodes[childID] === undefined) {
  131. this.state.nodes[childID] = this.state.createLocalNode(nodeID, childID, childExtendsID, childImplementsIDs,
  132. childSource, childType, childIndex, childName, callback);
  133. }
  134. node = this.state.nodes[childID];
  135. node.prototypes = protos;
  136. }
  137. },
  138. callingMethod: function( nodeID, methodName, methodParameters ) {
  139. //let self = this;
  140. let node = this.state.nodes[nodeID];
  141. if (node && node.cube) {
  142. // if (methodName == 'getCubeModel') {
  143. // return node.cube
  144. // }
  145. if(methodName == "inspect"){
  146. node.cube.inspect();
  147. console.log(node.cube);
  148. }
  149. if (methodName == 'isCubeTweening') {
  150. return node.cube.isTweening()
  151. }
  152. if (methodName == 'getCubeModelID') {
  153. return node.cube.id
  154. }
  155. if (methodName == 'getCubelet') {
  156. let cubelet = Object.assign({}, node.cube.cubelets[methodParameters[0]]);
  157. cubelet.cube = node.cube.id;
  158. return cubelet
  159. }
  160. if(methodName == "setCubeletID"){
  161. node.cube.cubelets[methodParameters[0]].nodeID = methodParameters[1];
  162. }
  163. if (methodName == 'progressQueue') {
  164. if (node.cube.twistQueue.future.length > 0) {
  165. node.cube.twist(node.cube.twistQueue.do());
  166. }
  167. }
  168. if (methodName == 'undo') {
  169. //TODO:
  170. }
  171. if (methodName == 'twistAction') {
  172. let key = methodParameters[0];
  173. node.cube.twistQueue.add(key);
  174. // while (node.cube.twistQueue.future.length > 0) {
  175. // node.cube.twist(node.cube.twistQueue.do());
  176. // }
  177. //node.cube.twist( node.cube.twistQueue.do() );
  178. }
  179. if (methodName == 'getCubelets') {
  180. var cubelets = [];
  181. node.cube.cubelets.forEach(el=>{
  182. let cubelet = {
  183. id: el.id.toString(),
  184. address: el.address,
  185. addressX: el.addressX,
  186. addressY: el.addressY,
  187. addressZ: el.addressZ
  188. }
  189. cubelets.push(cubelet)
  190. })
  191. return cubelets
  192. }
  193. }
  194. if(methodName == "cubeletsRemap"){
  195. let cubeletID = methodParameters[0];
  196. let cubeCallback = methodParameters[1]
  197. node.cube.remap(cubeletID, cubeCallback)
  198. }
  199. // if (this.state.isCubeletNodeComponent(node.prototypes)){
  200. // // if (methodName == 'getFaces') {
  201. // // var faces = [];
  202. // // node.cube.cubelets.forEach(el=>{
  203. // // let cubelet = {
  204. // // id: el.id.toString(),
  205. // // address: el.address,
  206. // // addressX: el.addressX,
  207. // // addressY: el.addressY,
  208. // // addressZ: el.addressZ
  209. // // }
  210. // // cubelets.push(cubelet)
  211. // // })
  212. // // return cubelets
  213. // // }
  214. // }
  215. },
  216. // -- initializingNode -------------------------------------------------------------------------
  217. initializingNode: function( nodeID, childID, childExtendsID, childImplementsIDs,
  218. childSource, childType, childIndex, childName ) {
  219. let node = this.state.nodes[childID];
  220. if ( node ) {
  221. if (this.state.isCubeletNodeComponent(node.prototypes)) {
  222. console.log("CUBElet INitializing...");
  223. let props = this.state.kernel.getProperties(childID);
  224. let nodeCube = this.state.nodes[props.cubeNodeID];
  225. nodeCube.cube.cubelets[childName].nodeID = childID;
  226. // nodeCube.cube.cubelets[childName].address = props.address;
  227. // nodeCube.cube.cubelets[childName].cubeletID = props.cubeletID;
  228. }
  229. if (this.state.isCubeNodeComponent(node.prototypes)) {
  230. console.log("CUBE INitializing...");
  231. let props = this.state.kernel.getProperties(childID);
  232. let history = props.twistQueueHistory;
  233. // let action = history.join('');
  234. // node.cube.twistQueue.add(action);
  235. // node.cube.twist(node.cube.twistQueue.do(), true );
  236. history.forEach(el=>{
  237. node.cube.twistQueue.add(el);
  238. })
  239. if(history.length > 0){
  240. while (node.cube.twistQueue.future.length > 0) {
  241. node.cube.twist(node.cube.twistQueue.do(), true );
  242. }
  243. // node.cube.twistQueue.future.map(el=>{
  244. // node.cube.twist(node.cube.twistQueue.do(), true );
  245. // })
  246. }
  247. //node.cube.map();
  248. }
  249. }
  250. },
  251. // -- deletingNode -------------------------------------------------------------------------
  252. //deletingNode: function( nodeID ) {
  253. //},
  254. // -- initializingProperty -----------------------------------------------------------------
  255. initializingProperty: function( nodeID, propertyName, propertyValue ) {
  256. var value = undefined;
  257. var node = this.state.nodes[nodeID];
  258. if (node !== undefined) {
  259. value = this.settingProperty(nodeID, propertyName, propertyValue);
  260. }
  261. return value;
  262. },
  263. // -- creatingProperty ---------------------------------------------------------------------
  264. creatingProperty: function (nodeID, propertyName, propertyValue) {
  265. return this.initializingProperty(nodeID, propertyName, propertyValue);
  266. },
  267. // -- settingProperty ----------------------------------------------------------------------
  268. settingProperty: function( nodeID, propertyName, propertyValue ) {
  269. // let node = this.state.nodes[nodeID];
  270. // var value = undefined;
  271. // if (node) {
  272. // if (this.state.isCubeletNodeComponent(node.prototypes)) {
  273. // switch ( propertyName ) {
  274. // case "cubeletID":
  275. // node.nodeID = nodeID;
  276. // value = propertyValue
  277. // break;
  278. // default:
  279. // value = undefined;
  280. // break;
  281. // }
  282. // }
  283. // }
  284. // return value;
  285. },
  286. // -- gettingProperty ----------------------------------------------------------------------
  287. gettingProperty: function( nodeID, propertyName, propertyValue ) {
  288. // let node = this.state.nodes[nodeID];
  289. // let value = undefined;
  290. // if (node) {
  291. // if (this.state.isCubeNodeComponent(node.prototypes)) {
  292. // switch ( propertyName ) {
  293. // case "cubeID":
  294. // value = node.cube.id;
  295. // break;
  296. // }
  297. // }
  298. // }
  299. // if ( value !== undefined ) {
  300. // propertyValue = value;
  301. // }
  302. // return value;
  303. }
  304. } );
  305. }
  306. getPrototypes(kernel, extendsID) {
  307. var prototypes = [];
  308. var id = extendsID;
  309. while (id !== undefined) {
  310. prototypes.push(id);
  311. id = kernel.prototype(id);
  312. }
  313. return prototypes;
  314. }
  315. }
  316. export {
  317. RubikModel as default
  318. }