Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
98 changes: 89 additions & 9 deletions dist/plugins/iwsy.js
Original file line number Diff line number Diff line change
@@ -30,6 +30,7 @@ const EasyCoder_IWSY = {
}
break;
case `init`:
case `stop`:
compiler.next();
compiler.addCommand({
domain: `iwsy`,
@@ -38,6 +39,16 @@ const EasyCoder_IWSY = {
action
});
return true;
case `script`:
const script = compiler.getNextValue();
compiler.addCommand({
domain: `iwsy`,
keyword: `iwsy`,
lino,
action,
script
});
return true;
case `goto`:
const target = compiler.getNextValue();
compiler.addCommand({
@@ -48,16 +59,47 @@ const EasyCoder_IWSY = {
target
});
return true;
case `script`:
const script = compiler.getNextValue();
case `run`:
const pc = compiler.getPc();
compiler.next();
compiler.addCommand({
domain: `iwsy`,
keyword: `iwsy`,
lino,
action,
script
then: 0
});
// Get the 'then' code, if any
if (compiler.tokenIs(`then`)) {
const goto = compiler.getPc();
// Add a 'goto' to skip the 'then'
compiler.addCommand({
domain: `core`,
keyword: `goto`,
goto: 0
});
// Fixup the link to the 'then' branch
compiler.getCommandAt(pc).then = compiler.getPc();
// Process the 'then' branch
compiler.next();
compiler.compileOne(true);
compiler.addCommand({
domain: `core`,
keyword: `stop`
});
// Fixup the 'goto'
compiler.getCommandAt(goto).goto = compiler.getPc();
}
return true;
case `onstep`:
compiler.next();
compiler.addCommand({
domain: `iwsy`,
keyword: `iwsy`,
lino,
action
});
return compiler.completeHandler();
default:
break;
}
@@ -67,6 +109,7 @@ const EasyCoder_IWSY = {
run: (program) => {
const command = program[program.pc];
const action = command.action;
let script;
switch (action) {
case `init`:
program.require(`js`, `iwsy.js`,
@@ -80,16 +123,53 @@ const EasyCoder_IWSY = {
player.innerHTML = ``;
player.style.background = `none`;
player.style.border = `none`;
const script = program.getValue(command.script);
EasyCoder.iwsyFunctions = IWSY(player, JSON.parse(script));
script = program.getValue(command.script);
try {
script = JSON.parse(script);
EasyCoder.iwsyFunctions = IWSY(player, script);
} catch (err) {
alert(`Badly formatted script`);
}
break;
case `script`:
script = program.getValue(command.script);
try {
script = JSON.parse(script);
if (EasyCoder.iwsyFunctions) {
EasyCoder.iwsyFunctions.setScript(script);
}
} catch (err) {
alert(`Badly formatted script`);
}
break;
case `goto`:
EasyCoder.iwsyFunctions.gotoStep(program.getValue(command.target));
if (EasyCoder.iwsyFunctions) {
EasyCoder.iwsyFunctions.gotoStep(program.getValue(command.target));
}
break;
case `script`:
EasyCoder.iwsyFunctions.setScript(JSON.parse(program.getValue(command.script)));
case `run`:
if (EasyCoder.iwsyFunctions) {
EasyCoder.iwsyFunctions.run(function() {
program.run(command.then);
});
return 0;
}
break;
case `stop`:
if (EasyCoder.iwsyFunctions) {
EasyCoder.iwsyFunctions.stop();
}
break;
case `onstep`:
const cb = command.pc + 2;
if (EasyCoder.iwsyFunctions) {
EasyCoder.iwsyFunctions.onStep(function(step) {
program.iwsyStep = step;
program.run(cb);
});
}
break;
}
}
return command.pc + 1;
}
},
411 changes: 176 additions & 235 deletions iwsy/iwsy.js
Original file line number Diff line number Diff line change
@@ -3,35 +3,15 @@
const IWSY = (container, text) => {

let script = text;
let mode = `manual`;
let clicked = false;

// Initialize all the blocks
const initBlocks = () => {
const defaults = script.defaults;
const blocks = script.blocks;
for (const name in blocks) {
const block = blocks[name];
const properties = {};
// Set up the default properties
for (const name in defaults) {
properties[name] = defaults[name];
}
// Override with local values
for (const name in block) {
properties[name] = block[name];
}
block.properties = properties;
block.container = container;
}
};

// Preload all the images
const preloadImages = () => {
for (const item in script.content) {
if (item.type == `image`) {
item.img = document.createElement(`div`);
item.img.style[`background`] = `url("${item.url}")`;
const url = item.url.split(`"`).join(`"`);
item.img.style.background = `url("${url}")`;
}
}
};
@@ -64,7 +44,7 @@ const IWSY = (container, text) => {
case `Enter`:
container.style.cursor = 'none';
document.addEventListener(`click`, onClick);
mode = `auto`;
script.runMode = `auto`;
release(step);
break;
}
@@ -77,30 +57,30 @@ const IWSY = (container, text) => {
};

const hold = step => {
if (mode === `manual`) {
if (script.speed === `scan`) {
step.next();
return;
}
if (script.runMode === `manual`) {
doManual(step);
} else {
} else {
if (clicked) {
document.removeEventListener(`click`, onClick);
clicked = false;
mode = `manual`;
script.runMode = `manual`;
doManual(step);
} else {
setTimeout(() => {
step.next();
}, script.speed === `normal` ? step.duration * 1000 : 0);
}, step.duration * 1000);
}
}
};

// Create a block.
const createBlock = (block) => {
if (block.element) {
container.removeChild(block.element);
}
const w = container.getBoundingClientRect().width / 1000;
const h = container.getBoundingClientRect().height / 1000;
const properties = block.properties;
const element = document.createElement(`div`);
container.appendChild(element);
block.element = element;
@@ -109,34 +89,34 @@ const IWSY = (container, text) => {
}
element.style[`position`] = `absolute`;
element.style[`opacity`] = `0.0`;
let val = properties.left;
let val = block.left;
if (!isNaN(val)) {
val *= w;
}
element.style[`left`] = val;
val = properties.top;
val = block.top;
if (!isNaN(val)) {
val *= h;
}
element.style[`top`] = val;
val = properties.width;
val = block.width;
if (!isNaN(val)) {
val = `${val * w}px`;
}
element.style[`width`] = val;
val = properties.height;
val = block.height;
if (!isNaN(val)) {
val = `${val * h}px`;
}
element.style[`height`] = val;
element.style[`background`] = properties.background;
element.style[`border`] = properties.border;
val = properties.textMarginLeft;
element.style[`background`] = block.background;
element.style[`border`] = block.border;
val = block.textMarginLeft;
if (!isNaN(val)) {
val *= w;
}
const marginLeft = val;
val = properties.textMarginTop;
val = block.textMarginTop;
if (!isNaN(val)) {
val *= h;
}
@@ -148,17 +128,17 @@ const IWSY = (container, text) => {
text.style[`top`] = marginTop;
text.style[`width`] = `calc(100% - ${marginLeft}px - ${marginLeft}px)`;
text.style[`height`] = `calc(100% - ${marginTop}px - ${marginTop}px)`;
text.style[`font-family`] = properties.fontFamily;
val = properties.fontSize;
text.style[`font-family`] = block.fontFamily;
val = block.fontSize;
if (!isNaN(val)) {
val *= h;
}
text.style[`font-size`] = `${val}px`;
text.style[`font-weight`] = properties.fontWeight;
text.style[`font-style`] = properties.fontStyle;
text.style[`color`] = properties.fontColor;
text.style[`text-align`] = properties.textAlign;
element.textPanel = text;
text.style[`font-weight`] = block.fontWeight;
text.style[`font-style`] = block.fontStyle;
text.style[`color`] = block.fontColor;
text.style[`text-align`] = block.textAlign;
block.textPanel = text;
};

// Set the content of a block
@@ -171,7 +151,7 @@ const IWSY = (container, text) => {
if (!block.element) {
createBlock(block);
}
block.element.textPanel.innerHTML = content.split(`\n`).join(`<br>`);
block.textPanel.innerHTML = content.split(`\n`).join(`<br>`);
};

// Set the content of a block
@@ -222,15 +202,9 @@ const IWSY = (container, text) => {
}
}
if (script.speed === `scan`) {
if (Array.isArray(stepBlocks)) {
for (const block of stepBlocks)
{
script.blocks[block].opacity = upDown ? 1.0 : 0.0;
script.blocks[block].element.style.opacity = 0;
}
} else {
script.blocks[stepBlocks].opacity = upDown ? 1.0 : 0.0;
script.blocks[stepBlocks].element.style.opacity = 0;
for (const block of stepBlocks)
{
script.blocks[block].element.style.opacity = upDown ? 1.0 : 0.0;
}
step.next();
} else {
@@ -240,31 +214,18 @@ const IWSY = (container, text) => {
const interval = setInterval(() => {
if (animStep < animSteps) {
const ratio = 0.5 - Math.cos(Math.PI * animStep / animSteps) / 2;
if (Array.isArray(stepBlocks)) {
let blocks = stepBlocks.length;
for (const block of stepBlocks)
{
const element = script.blocks[block].element;
element.style[`opacity`] = upDown ? ratio : 1.0 - ratio;
}
} else {
const block = script.blocks[stepBlocks];
if (!block.element) {
clearInterval(interval);
throw Error(`I can't fade up a block with no content`);
}
block.element.style[`opacity`] = upDown ? ratio : 1.0 - ratio;
let blocks = stepBlocks.length;
for (const block of stepBlocks)
{
const element = script.blocks[block].element;
element.style[`opacity`] = upDown ? ratio : 1.0 - ratio;
}
animStep++;
} else {
clearInterval(interval);
if (Array.isArray(stepBlocks)) {
for (const block of stepBlocks)
{
script.blocks[block].opacity = upDown ? 1.0 : 0.0;
}
} else {
script.blocks[stepBlocks].opacity = upDown ? 1.0 : 0.0;
for (const block of stepBlocks)
{
script.blocks[block].opacity = upDown ? 1.0 : 0.0;
}
if (!continueFlag) {
step.next();
@@ -293,11 +254,8 @@ const IWSY = (container, text) => {
switch (content.type) {
case `text`:
newText = content.content;
if (Array.isArray(newText)) {
newText = newText.join(`<br><br>`);
}
newText = newText.split(`\n`).join(`<br>`);
block.element.inner.text.innerHTML = newText;
block.textPanel.innerHTML = newText;
break;
case `image`:
block.element.style[`background`] = `url("${content.url}")`;
@@ -308,58 +266,32 @@ const IWSY = (container, text) => {
const continueFlag = step.continue;
let element;
let newText;
switch (content.type) {
case `text`:
element = document.createElement(`div`);
element.style[`position`] = `absolute`;
element.style[`opacity`] = `0.0`;
element.style[`left`] = block.element.style[`left`];
element.style[`top`] = block.element.style[`top`];
element.style[`width`] = block.element.style[`width`];
element.style[`height`] = block.element.style[`height`];
element.style[`background`] = block.element.style[`background`]
element.style[`border`] = block.element.style[`border`]
element.style[`border-radius`] = block.element.style[`border-radius`]
container.appendChild(element);
const inner = document.createElement(`div`);
inner.style[`position`] = `absolute`;
inner.style[`left`] = block.element.inner.style[`left`];
inner.style[`top`] = block.element.inner.style[`top`];
inner.style[`width`] = block.element.inner.style[`width`];
element.appendChild(inner);
const text = document.createElement(`div`);
text.style[`font-family`] = block.element.inner.text.style[`font-family`];
text.style[`font-size`] = block.element.inner.text.style[`font-size`];
text.style[`font-weight`] = block.element.inner.text.style[`font-weight`];
text.style[`font-style`] = block.element.inner.text.style[`font-style`];
text.style[`color`] = block.element.inner.text.style[`color`];
text.style[`text-align`] = block.element.inner.text.style[`text-align`];
inner.appendChild(text);
newText = content.content;
if (Array.isArray(newText)) {
newText = newText.join(`<br><br>`);
}
newText = newText.split(`\n`).join(`<br>`);
text.innerHTML = newText;
break;
case `image`:
element = document.createElement(`div`);
element.style[`position`] = `absolute`;
element.style[`opacity`] = `0.0`;
element.style[`left`] = block.element.style[`left`];
element.style[`top`] = block.element.style[`top`];
element.style[`width`] = block.element.style[`width`];
element.style[`height`] = block.element.style[`height`];
element.style[`background`] = block.element.style[`background`];
element.style[`border`] = block.element.style[`border`];
element.style[`border-radius`] = block.element.style[`border-radius`];
container.appendChild(element);
element.style[`background`] = `url("${content.url}")`;
element.style[`background-size`] = `cover`;
break;
default:
throw Error(`Unknown content type: '${content.type}'`);
}
element = document.createElement(`div`);
element.style[`position`] = `absolute`;
element.style[`opacity`] = `0.0`;
element.style[`left`] = block.element.style[`left`];
element.style[`top`] = block.element.style[`top`];
element.style[`width`] = block.element.style[`width`];
element.style[`height`] = block.element.style[`height`];
element.style[`background`] = block.element.style[`background`]
element.style[`border`] = block.element.style[`border`]
element.style[`border-radius`] = block.element.style[`border-radius`]
container.appendChild(element);
const text = document.createElement(`div`);
text.style[`position`] = `absolute`;
text.style[`left`] = block.textPanel.style[`left`];
text.style[`top`] = block.textPanel.style[`top`];
text.style[`width`] = block.textPanel.style[`width`];
text.style[`font-family`] = block.textPanel.style[`font-family`];
text.style[`font-size`] = block.textPanel.style[`font-size`];
text.style[`font-weight`] = block.textPanel.style[`font-weight`];
text.style[`font-style`] = block.textPanel.style[`font-style`];
text.style[`color`] = block.textPanel.style[`color`];
text.style[`text-align`] = block.textPanel.style[`text-align`];
element.appendChild(text);
newText = content.content;
newText = newText.split(`\n`).join(`<br>`);
text.innerHTML = newText;

const animSteps = Math.round(step.duration * 25);
let animStep = 0;
@@ -371,15 +303,9 @@ const IWSY = (container, text) => {
animStep++;
} else {
clearInterval(interval);
switch (content.type) {
case `text`:
block.element.inner.text.innerHTML = newText;
break;
case `image`:
block.element.style[`background`] = `url("${content.url}")`;
block.element.style[`background-size`] = `cover`;
break;
}
block.textPanel.innerHTML = newText;
block.element.style[`background`] = `url("${content.url}")`;
block.element.style[`background-size`] = `cover`;
block.element.style[`opacity`] = 1.0 ;
container.removeChild(element);
if (!continueFlag) {
@@ -395,22 +321,22 @@ const IWSY = (container, text) => {

// Compute a block size
const setComputedBlockSize = (block, target, ratio) => {
const boundingRect = block.container.getBoundingClientRect();
const boundingRect = container.getBoundingClientRect();
const w = boundingRect.width / 1000;
const h = boundingRect.height / 1000;
let width = block.properties.blockWidth;
let width = block.width;
if (!isNaN(width)) {
width *= w;
}
let height = block.properties.blockWidth;
let height = block.height;
if (!isNaN(height)) {
height *= h;
}
let endWidth = target.properties.blockWidth;
let endWidth = target.width;
if (!isNaN(endWidth)) {
endWidth *= w;
}
let endHeight = target.properties.blockHeight;
let endHeight = target.height;
if (!isNaN(endHeight)) {
endHeight *= h;
}
@@ -422,22 +348,22 @@ const IWSY = (container, text) => {

// Compute a block position
const setComputedBlockPosition = (block, target, ratio) => {
const boundingRect = block.container.getBoundingClientRect();
const boundingRect = container.getBoundingClientRect();
const w = boundingRect.width / 1000;
const h = boundingRect.height / 1000;
let left = block.properties.blockLeft;
let left = block.left;
if (!isNaN(left)) {
left *= w;
}
let top = block.properties.blockTop;
let top = block.top;
if (!isNaN(top)) {
top *= h;
}
let endLeft = target.properties.blockLeft;
let endLeft = target.left;
if (!isNaN(endLeft)) {
endLeft *= w;
}
let endTop = target.properties.blockTop;
let endTop = target.top;
if (!isNaN(endTop)) {
endTop *= h;
}
@@ -449,17 +375,17 @@ const IWSY = (container, text) => {

// Compute a font size
const setComputedFontSize = (block, target, ratio) => {
const h = Math.round(block.container.getBoundingClientRect().height) / 1000;
let size = block.properties.fontSize;
const h = Math.round(container.getBoundingClientRect().height) / 1000;
let size = block.fontSize;
if (!isNaN(size)) {
size *= h;
}
let endSize = target.properties.fontSize;
let endSize = target.fontSize;
if (!isNaN(endSize)) {
endSize *= h;
}
block.element.inner.text.style[`font-size`] =
`${size + Math.round((endSize - size) * ratio)}px`;
block.textPanel.style[`font-size`] =
`${size + (endSize - size) * ratio}px`;
};

// Compute a font color
@@ -478,42 +404,23 @@ const IWSY = (container, text) => {
const r = ("0" + red.toString(16)).slice(-2);
const g = ("0" + green.toString(16)).slice(-2);
const b = ("0" + blue.toString(16)).slice(-2);
block.element.inner.text.style[`color`] = `#${r}${g}${b}`;
block.textPanel.style[`color`] = `#${r}${g}${b}`;
};

// Handle a single step of a transition
const doTransitionStep = (type, block, target, ratio) => {
switch (type) {
case `block size`:
setComputedBlockSize(block, target, ratio);
break;
case `block position`:
setComputedBlockPosition(block, target, ratio);
break;
case `font size`:
setComputedFontSize(block, target, ratio);
break;
case `font color`:
setComputedFontColor(block, target, ratio);
break;
default:
throw Error(`Unknown transition type: '${type}'`);
}
const doTransitionStep = (block, target, ratio) => {
setComputedBlockSize(block, target, ratio);
setComputedBlockPosition(block, target, ratio);
setComputedFontSize(block, target, ratio);
setComputedFontColor(block, target, ratio);
};

// Handle a transition
const transition = step => {
const block = script.blocks[step.block];
const stepType = step.type;
const target = script.blocks[step.target];
if (script.speed === `scan`) {
if (Array.isArray(stepType)) {
for (const type of stepType) {
doTransitionStep(type, block, target, 1.0);
}
} else {
doTransitionStep(type, block, target, 1.0);
}
doTransitionStep(block, target, 1.0);
step.next();
} else {
const animSteps = Math.round(step.duration * 25);
@@ -522,13 +429,7 @@ const IWSY = (container, text) => {
const interval = setInterval(() => {
if (animStep < animSteps) {
const ratio = 0.5 - Math.cos(Math.PI * animStep / animSteps) / 2;
if (Array.isArray(stepType)) {
for (const type of stepType) {
doTransitionStep(type, block, target, ratio);
}
} else {
doTransitionStep(type, block, target, ratio);
}
doTransitionStep(block, target, ratio);
animStep++;
} else {
clearInterval(interval);
@@ -554,24 +455,6 @@ const IWSY = (container, text) => {
}
};

// Scan the script
const scan = () => {
script.speed = `scan`;
removeBlocks();
doStep(script.steps[0]);
};

// Go to a specified label
const goto = step => {
const target = script.labels[step.target];
if (typeof target !== `undefined`) {
script.scanTarget = target;
scan();
} else {
throw Error(`Unknown label '${step.target}`);
}
};

// Load a plugin action
const load = step => {
if (script.speed === `scan`) {
@@ -601,20 +484,57 @@ const IWSY = (container, text) => {
if (colon > 0) {
const aspectW = aspect.substr(0, colon);
const aspectH = aspect.substr(colon + 1);
script.container = container;
const height = Math.round(parseFloat(container.offsetWidth) * aspectH / aspectW);
container.style.height = `${Math.round(height)}px`;
container.style.position = `relative`;
container.style.overflow = `hidden`;
container.style.cursor = `none`;
container.style[`background-size`] = `cover`;
}
container.style[`border`] = step[`border`];
container.style[`background`] = step[`background`];
container.style.position = `relative`;
container.style.overflow = `hidden`;
container.style.cursor = `none`;
container.style.border = step.border;
container.style.background = step.background.split(`&quot;`).join(`"`);
container.style[`background-size`] = `cover`;
}
step.next();
};

// Scan the script
const scan = () => {
script.speed = `scan`;
removeBlocks();
doStep(script.steps[0]);
};

// Go to a specified label
const goto = step => {
const target = script.labels[step.target];
if (typeof target !== `undefined`) {
script.scanTarget = target;
scan();
} else {
throw Error(`Unknown label '${step.target}`);
}
};

// Run the presentation
const run = then => {
initScript();
script.runMode = `auto`;
script.speed = `normal`;
script.singleStep = false;
script.then = then;
doStep(script.steps[0]);
};

// Stop the run
const stop = () => {
script.stop = true;
};

// Set a step callback
const onStep = onStepCB => {
script.onstepCB = onStepCB;
};

// Chain to another presentation
const chain = step => {
step.next();
@@ -629,28 +549,44 @@ const IWSY = (container, text) => {
const gotoStep = (target) => {
script.scanTarget = target;
script.singleStep = true;
script.runMode = `manual`;
scan();
};

// Restore the cursor
const restoreCursor = () => {
container.style.cursor = `pointer`;
if (script.then) {
script.then();
script.then = null;
}
};

// Replace the script
const setScript = newScript => {
removeBlocks();
script = newScript;
initScript();
initBlocks();
};

// Initialize the script
const initScript = () => {
document.onkeydown = null;
script.container = container;
container.style.position = `relative`;
container.style.overflow = `hidden`;
container.style.cursor = 'none';
container.style[`background-size`] = `cover`;
script.speed = `normal`;
script.singleStep = true;
script.labels = {};
script.stop = false;
for (const name in script.blocks) {
const block = script.blocks[name];
const element = block.element;
if (typeof element !== `undefined`) {
container.removeChild(element);
block.element = null;
}
}
for (const [index, step] of script.steps.entries()) {
step.index = index;
step.script = script;
@@ -659,21 +595,23 @@ const IWSY = (container, text) => {
}
if (index < script.steps.length - 1) {
step.next = () => {
if (script.singleStep && script.speed != `scan`) {
console.log(`Single-step`);
} else {
const next = step.index + 1;
if (script.runMode == `auto` || (script.singleStep && script.speed === `scan`)) {
setTimeout(() => {
doStep(script.steps[next]);
if (script.stop) {
script.stop = false;
restoreCursor();
} else {
doStep(script.steps[step.index + 1]);
}
}, 0);
}
}
}
else {
step.next = () => {
console.log(`Step ${index + 1}: Finished`);
container.style.cursor = 'pointer';
}
console.log(`Step ${index + 1}: Finished`);
restoreCursor();
}
};
};
}
@@ -697,6 +635,11 @@ const IWSY = (container, text) => {

// Process a single step
const doStep = step => {
if (step.title) {
console.log(`Step ${step.index}: ${step.title}`);
} else {
console.log(`Step ${step.index}: ${step.action}`);
}
if (script.speed === `scan`) {
if (step.index === script.scanTarget) {
script.speed = `normal`;
@@ -707,12 +650,6 @@ const IWSY = (container, text) => {
}
}
}
} else {
if (step.title) {
console.log(`Step ${step.index}: ${step.title}`);
} else {
console.log(`Step ${step.index}: ${step.action}`);
}
}
const actionName = step.action.split(` `).join(``);
let handler = actions[actionName];
@@ -722,21 +659,25 @@ const IWSY = (container, text) => {
throw Error(`Unknown action: '${step.action}'`);
}
}
if (script.onStepCB) {
script.onStepCB(step.index);
}
handler(step);
};

container.innerHTML = ``;
document.removeEventListener(`click`, init);
if (mode === `auto`) {
if (script.runMode === `auto`) {
document.addEventListener(`click`, onClick);
}
initScript();
IWSY.plugins = {};
initBlocks();
preloadImages();
doStep(script.steps[0]);
return {
setScript,
gotoStep
gotoStep,
run,
stop
};
};
98 changes: 89 additions & 9 deletions js/plugins/iwsy.js
Original file line number Diff line number Diff line change
@@ -30,6 +30,7 @@ const EasyCoder_IWSY = {
}
break;
case `init`:
case `stop`:
compiler.next();
compiler.addCommand({
domain: `iwsy`,
@@ -38,6 +39,16 @@ const EasyCoder_IWSY = {
action
});
return true;
case `script`:
const script = compiler.getNextValue();
compiler.addCommand({
domain: `iwsy`,
keyword: `iwsy`,
lino,
action,
script
});
return true;
case `goto`:
const target = compiler.getNextValue();
compiler.addCommand({
@@ -48,16 +59,47 @@ const EasyCoder_IWSY = {
target
});
return true;
case `script`:
const script = compiler.getNextValue();
case `run`:
const pc = compiler.getPc();
compiler.next();
compiler.addCommand({
domain: `iwsy`,
keyword: `iwsy`,
lino,
action,
script
then: 0
});
// Get the 'then' code, if any
if (compiler.tokenIs(`then`)) {
const goto = compiler.getPc();
// Add a 'goto' to skip the 'then'
compiler.addCommand({
domain: `core`,
keyword: `goto`,
goto: 0
});
// Fixup the link to the 'then' branch
compiler.getCommandAt(pc).then = compiler.getPc();
// Process the 'then' branch
compiler.next();
compiler.compileOne(true);
compiler.addCommand({
domain: `core`,
keyword: `stop`
});
// Fixup the 'goto'
compiler.getCommandAt(goto).goto = compiler.getPc();
}
return true;
case `onstep`:
compiler.next();
compiler.addCommand({
domain: `iwsy`,
keyword: `iwsy`,
lino,
action
});
return compiler.completeHandler();
default:
break;
}
@@ -67,6 +109,7 @@ const EasyCoder_IWSY = {
run: (program) => {
const command = program[program.pc];
const action = command.action;
let script;
switch (action) {
case `init`:
program.require(`js`, `iwsy.js`,
@@ -80,16 +123,53 @@ const EasyCoder_IWSY = {
player.innerHTML = ``;
player.style.background = `none`;
player.style.border = `none`;
const script = program.getValue(command.script);
EasyCoder.iwsyFunctions = IWSY(player, JSON.parse(script));
script = program.getValue(command.script);
try {
script = JSON.parse(script);
EasyCoder.iwsyFunctions = IWSY(player, script);
} catch (err) {
alert(`Badly formatted script`);
}
break;
case `script`:
script = program.getValue(command.script);
try {
script = JSON.parse(script);
if (EasyCoder.iwsyFunctions) {
EasyCoder.iwsyFunctions.setScript(script);
}
} catch (err) {
alert(`Badly formatted script`);
}
break;
case `goto`:
EasyCoder.iwsyFunctions.gotoStep(program.getValue(command.target));
if (EasyCoder.iwsyFunctions) {
EasyCoder.iwsyFunctions.gotoStep(program.getValue(command.target));
}
break;
case `script`:
EasyCoder.iwsyFunctions.setScript(JSON.parse(program.getValue(command.script)));
case `run`:
if (EasyCoder.iwsyFunctions) {
EasyCoder.iwsyFunctions.run(function() {
program.run(command.then);
});
return 0;
}
break;
case `stop`:
if (EasyCoder.iwsyFunctions) {
EasyCoder.iwsyFunctions.stop();
}
break;
case `onstep`:
const cb = command.pc + 2;
if (EasyCoder.iwsyFunctions) {
EasyCoder.iwsyFunctions.onStep(function(step) {
program.iwsyStep = step;
program.run(cb);
});
}
break;
}
}
return command.pc + 1;
}
},