Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
dube1503 committed Oct 29, 2017
2 parents de4e159 + a2ebe51 commit 7e67795
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 32 deletions.
65 changes: 38 additions & 27 deletions blocks/graphs.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,48 +45,59 @@ Blockly.Blocks['plot'] = {
},

dynamicOptions: function(thisBlock) {
// Options list to fill and push to dropdown
var options = []
// Variable for storing a list of all variable blocks in workspace
var allVariables = Blockly.Variables.allVariables(thisBlock.workspace);
// Variable for menu options if no Series type variables are found in workspace
var empty = ["none","none"];
var empty = ["none","none"];
// Don't push variable names if in flyout menu
if (this.isInFlyout)
options.push(empty);
// Variable for storing a list of all variable block NAMES in workspace
var allVariables = Blockly.Variables.allVariables(thisBlock.workspace);

if (!(allVariables.length==0)) {
for (var curr in allVariables) {
// allVariables is not a hash list and contains variable object functions
// Filter out the names of object funtions that get unwantingly returned
if (!(curr === "append" ||
curr === "copy" ||
curr === "extend" ||
curr === "index" ||
curr === "insert" ||
curr === "remove" ||
curr === "+")) {

var variableUses = thisBlock.workspace.getVariableUses(allVariables[curr])
if (variableUses.length > 1) {

for (var i = 0; i < allVariables.length; i++) {
// List of REFERENCES to actual blocks with given name
// Used to loop through
var variableUses = thisBlock.workspace.getVariableUses(allVariables[i]);
// List of variableUses to store only variables that are not iterators
var currentUses = thisBlock.workspace.getVariableUses(allVariables[i]);
// Check if current variable name is an iterator (or a variable_set feild on loop block)
// If current variable is iterator, remove from currentUses
for (var j = 0; j < variableUses.length; j++) {
if(variableUses[j].isIterator || variableUses[j].type === "controls_for") {
currentUses = [];
break;
}
}
// If carruentUses is now empty, then the current variable name is an iterator
// Move to the next variable name and check
// If currentUses contains more than one block reference, find the topBlock reference
// If it contains only one reference, that is topBlock decleration block
if (!(currentUses.length == 0)) {
if (currentUses.length > 1) {
var topBlock = {index: null, height: null};
for(var i = 0; i < variableUses.length; i++){
for(var k = 0; k < currentUses.length; k++){
// add the first value, then find the highest but only if it is not itself
if((topBlock.index == null || topBlock.height > variableUses[i].getRelativeToSurfaceXY().y)
&& variableUses[i].type == "variables_set" ){
topBlock["index"] = i;
topBlock["height"] = variableUses[i].getRelativeToSurfaceXY().y;
if((topBlock.index == null || topBlock.height > currentUses[k].getRelativeToSurfaceXY().y)
&& currentUses[k].type == "variables_set" ){
topBlock["index"] = k;
topBlock["height"] = currentUses[k].getRelativeToSurfaceXY().y;
}
}
var varBlock = variableUses[topBlock.index];
var varBlock = currentUses[topBlock.index];
} else {
var varBlock = variableUses[0];
var varBlock = currentUses[0];
}
varBlock = varBlock.inputList[0].connection;
// Only push variable block to menu if it has a Series type connected to it
if (!(varBlock===null)) {
if (!(varBlock.targetConnection==null) && varBlock.targetConnection.check_[0]==="Series") {
options.push([allVariables[curr],allVariables[curr].toUpperCase()]);
}
if (!(varBlock.targetConnection==null) && varBlock.targetConnection.check_[0]==="Series") {
options.push([allVariables[i],allVariables[i].toUpperCase()]);
}
}
}
}
}
if (options.length==0) {
options.push(empty);
Expand Down
14 changes: 14 additions & 0 deletions blocks/loops.js
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,20 @@ Blockly.Blocks['controls_for'] = {
option.callback = Blockly.ContextMenu.callbackFactory(this, xmlBlock);
options.push(option);
}
},
/**
* Add onchange function to find all "iterator" variables created
* by this for_loop block and assign isIterator to True.
* @this Blockly.BlockSvg
*/
onchange: function(){
var name = this.getFieldValue('VAR');
var allIterators = this.workspace.getVariableUses(name);
for (var block in allIterators) {
if (allIterators[block].type === "variables_get") {
allIterators[block].isIterator = true;
}
}
}
};

Expand Down
11 changes: 8 additions & 3 deletions blocks/variables.js
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,8 @@ Blockly.Blocks['variables_get'] = {
.appendField(new Blockly.FieldVariable(
Blockly.Msg.VARIABLES_DEFAULT_NAME , function(selection){
thisBlock.component = 'none';
thisBlock.setNewType(selection);
thisBlock.setNewType(selection)
thisBlock.isIterator = false;

})
, 'VAR');
Expand All @@ -233,6 +234,7 @@ Blockly.Blocks['variables_get'] = {
this.selectedType = null;
this.attribute = 'none';
this.component = 'none';
this.isIterator = false;
},


Expand All @@ -256,8 +258,11 @@ Blockly.Blocks['variables_get'] = {
},

onchange: function(){

this.setNewType(this.getInput("FieldVariable").fieldRow[0].value_);
if(this.isIterator){
this.modifyBlock("Number");
} else {
this.setNewType(this.getInput("FieldVariable").fieldRow[0].value_);
}
},


Expand Down
2 changes: 1 addition & 1 deletion bower.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "blocks-vpython",
"version": "0.1.4",
"version": "0.1.5",
"description": "Blockly + VPython Glowscript",
"main": [
"blockly_compressed.js",
Expand Down
5 changes: 4 additions & 1 deletion core/field_variable.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,10 @@ Blockly.FieldVariable.prototype.setValue = function(newValue) {
if (this.sourceBlock_ && Blockly.Events.isEnabled()) {
Blockly.Events.fire(new Blockly.Events.Change(
this.sourceBlock_, 'field', this.name, this.value_, newValue));
this.sourceBlock_.setNewType(newValue);
if(this.sourceBlock_.type!=='controls_for'){
this.sourceBlock_.setNewType(newValue);
}

}
this.value_ = newValue;
this.setText(newValue);
Expand Down

0 comments on commit 7e67795

Please sign in to comment.