public List <IBloxVariable> GetVariablesInBloxScope2(ABlox blox) { List <IBloxVariable> variablesInScope = new List <IBloxVariable>(); RootBlox rootBlox = GetRootBlox(); if (rootBlox != null) { List <BloxIdent> bloxList = rootBlox.GetChildBloxListInVerticalOrder(); //Params are not loaded in GetChildBloxListInVerticalOrder() method, so we evaluate according to the parent index //on that case BloxIdent bloxIdent = bloxList.Find(b => b.blox == (blox.IsParam ? blox.ParentBlox : blox)); BloxIdent bloxIdent2 = bloxList.Where(b => b.blox == (blox.IsParam ? blox.ParentBlox : blox)).FirstOrDefault(); int indexOfBlox = -1; try { indexOfBlox = bloxList.IndexOf(bloxIdent); } catch (Exception ex) { } if (indexOfBlox >= 0) { // Gets all the bloxes above this one, that are variables // Although pure Blox objects cannot be cast as IBloxVariable, // objects of classes that inherit from Blox, and implement IBloxVariable, can variablesInScope = bloxList.GetRange(0, indexOfBlox).Where(b => GameObjectHelper.CanBeCastedAs <IBloxVariable>(b.blox)).Select(b => (IBloxVariable)b.blox).ToList(); } } return(variablesInScope); }
/// <summary> /// From the current blox, gets all the bloxes in scope, in vertical order /// The first one in list will be the closest to this one, and the last the root /// Does not consider param bloxes /// Example: /// What are the bloxes in scope of c.2.2? /// root /// -a /// --a.1 /// ---a.1.1 /// -b /// --b.1 /// --b.2 /// -c /// --c.1 /// ---c.1.1 /// --c.2 /// ---c.2.1 /// ---c.2.2 [c.2.2.p1] /// ---c.2.3 /// /// Answer: [c.2.1, c.2, c.1, c, b, a, root] /// The answer shall be the same for c.2.2.p1 (which is a param for c.2.2) /// </summary> /// <param name="blox"></param> /// <returns></returns> protected List <ABlox> GetBloxesInScope(ABlox blox) { // This is the recursion stop condition if (blox == null || GameObjectHelper.CanBeCastedAs <RootBlox>(blox)) { return new List <ABlox>() { blox } } ; ABlox auxBlox = blox.IsParam ? blox.ParentBlox : blox; //if blox is a param,considers its parent List <ABlox> scope = new List <ABlox>(); ABlox parentBlox = auxBlox.ParentBlox; int indexOfBloxInParent = parentBlox.ChildBloxes.IndexOf(auxBlox); for (int i = indexOfBloxInParent - 1; i >= 0; i--) { scope.Add(parentBlox.ChildBloxes[i]); } scope.Add(parentBlox); scope.AddRange(GetBloxesInScope(parentBlox)); return(scope); }
/// <summary> /// Converts a field choice to a node. /// If field is variable, instead of creating a new node, returns an existing /// </summary> /// <param name="field"></param> /// <param name="rootNode"></param> /// <returns></returns> protected IntegerNode FieldToNode(FieldChoice field, RootNode rootNode) { HighlightableButton highlightableButton = (GameObjectHelper.HasComponent <HighlightableButton>(this.gameObject)) ? this.GetComponent <HighlightableButton>() : null; IntegerNode fieldNode = null; // If it is a variable, the variable has already been created // so we are going to search for it if (field.isVariable) { // When it is a variable, value will have its name ICodeNode variable = rootNode.SearchChildByName(field.value); if (variable != null && GameObjectHelper.CanBeCastedAs <IntegerNode>(variable)) { fieldNode = (IntegerNode)variable; } else { throw new System.Exception("Expected " + field.value + " to be an integer node"); } } // User has input a numeric value else { int value = int.Parse(field.value); fieldNode = new IntegerNode(null, value); } return(fieldNode); }
public void ToNodes(ICodeNode parentNode) { RootNode rootNode = parentNode.GetRootNode(); List <ObjectNode> variables = BloxParams.Where(p => GameObjectHelper.CanBeCastedAs <VariableChoiceBlox>(p)).Select(p => (p as VariableChoiceBlox).ToObjectNode(rootNode)).ToList(); OutputMessageNode outputMsgNode = new OutputMessageNode(this.GetComponent <HighlightableButton>(), this.LogOutputPanel.AddMessage, variables); parentNode.AddChildNode(outputMsgNode); }
protected void CompileChildrenToNodes(ICodeNode parentNode) { foreach (ABlox child in ChildBloxes) { if (GameObjectHelper.CanBeCastedAs <ICompilableBlox>(child)) { //Creates nodes for each child. Each child will add parentNode as a parent ((ICompilableBlox)child).ToNodes(parentNode); } } }
/// <summary> /// Gets all the variable bloxes on the scope of a blox. They are presented in the following order: /// the one closest to blox, to the one closest to root. /// </summary> /// <param name="blox"></param> /// <returns></returns> public List <IBloxVariable> GetVariablesInBloxScope(ABlox blox) { List <IBloxVariable> variablesInScope = new List <IBloxVariable>(); RootBlox rootBlox = GetRootBlox(); if (rootBlox != null) { List <ABlox> bloxesInScope = blox.GetBloxesInScope(blox); variablesInScope = bloxesInScope.Where(b => GameObjectHelper.CanBeCastedAs <IBloxVariable>(b)).Select(b => (IBloxVariable)b).ToList(); } return(variablesInScope); }
protected List <BloxValidationError> ValidateBloxList(List <ABlox> bloxList) { List <BloxValidationError> errors = new List <BloxValidationError>(); foreach (ABlox child in bloxList) { if (GameObjectHelper.CanBeCastedAs <ICompilableBlox>(child)) { errors.AddRange(((ICompilableBlox)child).Validate()); } } return(errors); }
public void ToNodes(ICodeNode parentNode) { RootNode rootNode = parentNode.GetRootNode(); IfNode ifNode = null; HighlightableButton highlightableButton = (GameObjectHelper.HasComponent <HighlightableButton>(this.gameObject)) ? this.GetComponent <HighlightableButton>() : null; // IfNode can receive either a bool variable (chosen in dropdown) or a LogicalOperatorBlox as param // If it has received a param (LogicalOperatorBlox), creates a LogicalOperationNode // and instantiates ifNode with it if (BloxParams.Count > 0) { LogicalOperatorBlox blox = BloxParams[0].GetComponent <LogicalOperatorBlox>(); LogicalOperationNode logicOpNode = blox.CreateNode(rootNode); ifNode = new IfNode(highlightableButton, logicOpNode); } // If a variable was chosen, will search for an existing BooleanNode with that same name else { // Gets the selected boolean variable string selectedBoolVarName = GameObjectHelper.GetDropdownSelectedTextValue(this.booleanVariablesDropdown); ICodeNode foundNode = rootNode.SearchChildByName(selectedBoolVarName); if (foundNode != null && GameObjectHelper.CanBeCastedAs <BooleanNode>(foundNode)) { BooleanNode boolNode = (BooleanNode)foundNode; ifNode = new IfNode(highlightableButton, boolNode); } else { throw new System.Exception("Expected " + selectedBoolVarName + " to be a boolean node"); } } parentNode.AddChildNode(ifNode); CompileChildrenToNodes(ifNode); }