From dddd3214e1e20c6dcf7af0b67fe4c9a541905ff9 Mon Sep 17 00:00:00 2001 From: mmcwilliams Date: Wed, 14 Mar 2018 23:19:23 -0400 Subject: [PATCH] Mscript: * compile sequence * run sequence --- app/lib/ui/mscript.js | 98 +++++++++++++++++++++++++++++++++++++++---- 1 file changed, 91 insertions(+), 7 deletions(-) diff --git a/app/lib/ui/mscript.js b/app/lib/ui/mscript.js index 032f8d2..98611ee 100644 --- a/app/lib/ui/mscript.js +++ b/app/lib/ui/mscript.js @@ -32,17 +32,81 @@ mse.mscript.open = function () { mse.mscript.fromSequence = function () { //ehhhhh 'use strict'; - $('#mscript textarea').val(mcopy.state.sequence.arr.join('\n')); + let str; + let tmp = []; + let cont; + str = mcopy.state.sequence.arr.join('\n'); //quick hack + for (let cmd of mcopy.state.sequence.arr) { + if (tmp.length > 0 && tmp[tmp.length - 1].cmd === cmd) { + tmp[tmp.length - 1].num++; + continue; + } + tmp.push({ cmd : cmd, num : 1 }); + } + tmp = tmp.map(line => { + return `${line.cmd} ${line.num}` + }) + str = tmp.join('\n'); + nav.change('script'); + cont = confirm(`Are you sure you want to over-write the current sequence?`); + if (cont) { + mse.mscript.editor.getDoc().setValue(str); + } + + }; -mse.mscript.eval = function () { +mse.mscript.compile = function (cb) { 'use strict'; const data = mse.mscript.editor.getValue(); - const output = mscript.interpret(data, function (output) { - console.dir(output); - mse.console.print(JSON.stringify(output, null, '\t')) + let output; + mse.mscript.raw = data; + mscript.interpret(data, (output) => { + let len = output.arr.length; + mse.mscript.data = output; + //mse.console.print(JSON.stringify(output, null, '\t') + '\n') + mse.console.print(`Sequence contains ${len} step${(len === 1 ? '' : 's')}, CAM: ${output.cam}, PROJ: ${output.proj}`); + if (cb) cb(); }); }; +mse.mscript.prepare = function () { + 'use strict'; + const arr = []; + let obj; + for (let i = 0; i < mse.mscript.data.arr.length; i++) { + obj = { + cmd : mse.mscript.data.arr[i] + }; + /*if (mse.mscript.data.light[i] !== '') { + obj.light = mse.mscript.data.light[i]; + } else { + obj.light = light.color.join(','); + }*/ + arr.push(obj); + } + return arr; +}; + +mse.mscript.run = function () { + 'use strict'; + const data = mse.mscript.editor.getValue(); + let arr; + let cont; + if (data !== mse.mscript.raw) { + cont = confirm(`Current script has not been compiled. Compile first?`); + if (cont) { + return mse.mscript.compile(() => { + mse.console.print(`Started running compiled sequence...`); + arr = mse.mscript.prepare(); + return seq.exec(arr); + }) + } + } + mse.console.print(`Started running compiled sequence...`); + arr = mse.mscript.prepare(); + return seq.exec(arr); +}; + /******* * gui console *******/ @@ -67,12 +131,14 @@ mse.console.parse = function () { const line = lines[lines.length - 2].replace('>', '').trim(); mse.console.lines.push(line); }; + mse.console.exec = function () { 'use strict'; let command; mse.console.parse(); command = mse.console.lines[mse.console.lines.length - 1].replace('>', '').trim(); console.log(command); + mse.console.newLine(); if (mscript.cmd.indexOf(command) !== -1) { if (command === 'CF') { cmd.cam_forward(light.color); @@ -80,21 +146,39 @@ mse.console.exec = function () { cmd.cam_backward(light.color); } } - mse.console.newLine(); + + if (command === 'compile') { + mse.mscript.compile(); + } else if (command === 'run') { + mse.mscript.run(); + } }; + mse.console.newLine = function () { 'use strict'; let current = mse.console.elem.val(); + let height; current += '> '; mse.console.elem.val(current); + height = mse.console.elem[0].scrollHeight; + mse.console.elem.animate({ + scrollTop : height + },'normal'); }; mse.console.print = function (str) { 'use strict' let current = mse.console.elem.val(); - current += '> '; + let height; current += str; + current += '\n> '; mse.console.elem.val(current); + mse.console.elem.focus(); + + height = mse.console.elem[0].scrollHeight; + mse.console.elem.animate({ + scrollTop : height + },'normal'); }; module.exports = mse; \ No newline at end of file