Added inline inputs (blocks still need updating)

This commit is contained in:
Tim Stallard 2017-03-03 15:48:54 +00:00
parent 6e439be1e0
commit 537ab5f64e
8 changed files with 75 additions and 31 deletions

View File

@ -1,8 +1,14 @@
module.exports = { module.exports = {
name: "Caesar", name: "Caesar",
inputs: { inputs: {
text: "Text", text: {
shift: "Shift" name: "Text"
},
shift: {
name: "Shift",
inline: true,
type: "number"
}
}, },
output: true, output: true,
execute: function({text, shift}, elem){ execute: function({text, shift}, elem){

View File

@ -1,7 +1,9 @@
module.exports = { module.exports = {
name: "Output", name: "Output",
inputs: { inputs: {
input: "Input" input: {
name: "Input"
}
}, },
output: false, output: false,
execute: function({input}, block){ execute: function({input}, block){

View File

@ -5,12 +5,17 @@ var blocks = require("./blocks");
function resolveOutput(block, cache){ function resolveOutput(block, cache){
var inputValues = {}; var inputValues = {};
for(var input in block.inputs){ for(var input in block.inputs){
if(block.inputs[input] in cache){ if(block.inputs[input].joined){ //if it's joined to something else
inputValues[input] = cache[block.inputs[input]]; if(block.inputs[input].joined in cache){ //if output of other block is already in cache
inputValues[input] = cache[block.inputs[input].joined];
} }
else{ else{
var inputBlock = diagram.state.filter((diagramBlock)=>(diagramBlock.id == block.inputs[input]))[0]; var inputBlock = diagram.state.filter((diagramBlock)=>(diagramBlock.id == block.inputs[input].joined))[0]; //find block instance
inputValues[input] = resolveOutput(inputBlock, cache); inputValues[input] = resolveOutput(inputBlock, cache); //calculate and store output
}
}
else if(block.inputs[input].value){ //if value is already set, just save that
inputValues[input] = block.inputs[input].value;
} }
} }

View File

@ -1,4 +1,5 @@
var blocks = require("../blocks"); var blocks = require("../blocks");
var events = require("../events");
module.exports = function(newBlock){ module.exports = function(newBlock){
var newBlockElement = $( var newBlockElement = $(
@ -7,14 +8,26 @@ module.exports = function(newBlock){
instance: newBlock instance: newBlock
}) })
).appendTo("#workspace"); ).appendTo("#workspace");
newBlockElement.find(".inputs input").keyup(function(){
//when input field is updated, save the value to the block object and emit event for updates
var inputId = $(this).parent().attr("id");
if(!newBlock.inputs[inputId]){
newBlock.inputs[inputId] = {};
}
newBlock.inputs[inputId].value = $(this).val();
events.emit("inputChanged");
});
newBlock.elem = newBlockElement; newBlock.elem = newBlockElement;
if(blocks[newBlock.type].size){ if(blocks[newBlock.type].size){
//if the block declaration contains a non-standard size, resize it
newBlockElement.css({ newBlockElement.css({
height: blocks[newBlock.type].size.height, height: blocks[newBlock.type].size.height,
width: blocks[newBlock.type].size.width width: blocks[newBlock.type].size.width
}); });
} }
if(newBlock.position){ if(newBlock.position){
//if this block instance already has a position, place it there
//this will only be used for importing
newBlockElement.css({ newBlockElement.css({
top: newBlock.position.y, top: newBlock.position.y,
left: newBlock.position.x left: newBlock.position.x

View File

@ -1,7 +1,12 @@
<div class="block" data-type="{{block.type}}" id="{{instance.id}}"> <div class="block" data-type="{{block.type}}" id="{{instance.id}}">
<div class="inputs"> <div class="inputs">
{{#each block.inputs as |desc name|}} {{#each block.inputs as |input id|}}
<div id="{{name}}">{{desc}}</div> <div id="{{id}}">
{{input.name}}
{{#if input.inline}}
<input type="{{input.type}}" size="4" value="{{lookup (lookup ../instance.inputs id) "value"}}"></input>
{{/if}}
</div>
{{/each}} {{/each}}
</div> </div>
<div class="main"> <div class="main">

View File

@ -32,7 +32,15 @@ $("#blocks").on("mousedown", ".block>.main,.block>.inputs", function(event){
y: event.pageY - $(this).parent().offset().top y: event.pageY - $(this).parent().offset().top
}, },
type: $(this).parent().data("type"), type: $(this).parent().data("type"),
inputs: {}, inputs:
Object.keys(blocks[$(this).parent().data("type")].inputs) //get block ids
.reduce((inputs, input)=>{ //turn this into an object
inputs[input] = {
joined: "",
value: ""
};
return inputs;
}, {}),
properties: {} properties: {}
} }
diagram.state.push(newBlock); diagram.state.push(newBlock);

View File

@ -50,7 +50,7 @@ $("#workspace").on("mouseup", ".block>.inputs>div", function(event){
endBlock = $(this).parent().parent().attr("id"); endBlock = $(this).parent().parent().attr("id");
endInput = $(this).attr("id"); endInput = $(this).attr("id");
var endBlockInstance = diagram.state.filter((block)=>(block.id == endBlock))[0]; var endBlockInstance = diagram.state.filter((block)=>(block.id == endBlock))[0];
endBlockInstance.inputs[endInput] = startBlock; endBlockInstance.inputs[endInput].joined = startBlock;
drawJoiningLines(); drawJoiningLines();
events.emit("newJoin"); events.emit("newJoin");
} }
@ -62,7 +62,7 @@ $("#workspace").on("mousedown", ".block>.inputs>div", function(event){
var blockId = $(this).parent().parent().attr("id") var blockId = $(this).parent().parent().attr("id")
var input = $(this).attr("id"); var input = $(this).attr("id");
var block = diagram.state.filter((block)=>(block.id == blockId))[0]; var block = diagram.state.filter((block)=>(block.id == blockId))[0];
delete block.inputs[input]; block.inputs[input].joined = "";
drawJoiningLines(); drawJoiningLines();
events.emit("joinRemove"); events.emit("joinRemove");
} }
@ -76,7 +76,8 @@ function drawJoiningLines(){
$(".line").remove(); $(".line").remove();
for(var endBlock of diagram.state){ for(var endBlock of diagram.state){
for(var input in endBlock.inputs){ for(var input in endBlock.inputs){
startBlockId = endBlock.inputs[input]; if(endBlock.inputs[input].joined){
startBlockId = endBlock.inputs[input].joined;
startBlock = diagram.state.filter((block)=>(block.id == startBlockId))[0]; startBlock = diagram.state.filter((block)=>(block.id == startBlockId))[0];
lineId = startBlock.id + "-" + endBlock.id + "-" + input; lineId = startBlock.id + "-" + endBlock.id + "-" + input;
if($("#" + lineId).length){ if($("#" + lineId).length){
@ -97,4 +98,5 @@ function drawJoiningLines(){
); );
} }
} }
}
} }

View File

@ -45,6 +45,9 @@ body{
text-align: center; text-align: center;
padding-top: 5px; padding-top: 5px;
padding-bottom: 5px; padding-bottom: 5px;
>input{
width: 40px;
}
} }
} }
.main{ .main{