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> /// This method is called when a child is going to be removed. /// </summary> /// <param name="blox"></param> public override void OnBeforeChildRemove(ABlox blox) { if (this.BloxParams.Contains(blox)) { booleanVariablesDropdown.interactable = true; } }
public override void OnBeforeChildRemove(ABlox blox) { if (this.BloxParams.Contains(blox)) { ValueField.gameObject.SetActive(true); } }
/// <summary> /// Adds a blox next to this one in parent children /// </summary> /// <param name="blox">Blox to add</param> protected void AddToBottom(ABlox blox) { RemoveFromParent(blox); // Gets the index of this blox in parent int thisBloxIndex = this.ParentBlox.ChildBloxes.IndexOf(this); this.ParentBlox.ChildBloxes.Insert(thisBloxIndex + 1, blox); blox.ParentBlox = this.ParentBlox; }
/// <summary> /// Verifies if in a blox scope there is already a variable with a given name. /// Ignores null or whitespace (returns false when name is null or empty) /// </summary> /// <param name="blox"></param> /// <param name="name"></param> /// <returns></returns> public bool VariableExistsInBloxScope(ABlox blox, string name) { if (string.IsNullOrWhiteSpace(name)) { return(false); } // From the list of variables available in scope, gets the ones that are not null or empty, // and counts the number of corresponding cases, and returns true if it is bigger than zero return(GetVariablesInBloxScope(blox).Where(a => !String.IsNullOrWhiteSpace(a.GetName()) && a.GetName().ToLower() == name.ToLower()).Count() > 0); }
/// <summary> /// Takes a blox and removes any reference inside it's parent /// </summary> /// <param name="blox"></param> protected void RemoveFromParent(ABlox blox) { ABlox originalParent = blox.ParentBlox; if (originalParent != null) { originalParent.OnBeforeChildRemove(blox); originalParent.ChildBloxes.Remove(blox); originalParent.BloxParams.Remove(blox); } blox.ParentBlox = null; }
public void NestObject(GameObject secondObject) { RootBlox rootBlox = GetRootBlox(); if (rootBlox != null && NestingActive) { if (secondObject.GetComponent <ABlox>() != null && secondObject != null && ValidateNesting(secondObject)) { ABlox secondObjectBlox = secondObject.GetComponent <ABlox>(); Vector2 thisObjectPosition = this.gameObject.transform.position; Vector2 secondObjectPosition = secondObject.transform.position; RectTransform gameObjectTransform = this.gameObject.GetComponent <RectTransform>(); RectTransform collidedObjectTransform = secondObject.GetComponent <RectTransform>(); BoundingBox2D thisBBox = GameObjectHelper.getBoundingBoxInWorld(this.gameObject); BoundingBox2D secondObjBBox = GameObjectHelper.getBoundingBoxInWorld(secondObject); float thisObjectWidth = GameObjectHelper.getWidthFromBBox(thisBBox); float secondObjectWidth = GameObjectHelper.getWidthFromBBox(secondObjBBox); float thisObjectHeight = GameObjectHelper.getHeightFromBBox(thisBBox); float secondObjectHeight = GameObjectHelper.getHeightFromBBox(secondObjBBox); //checks if second object top is bellow this object center if (secondObjBBox.top.y < thisObjectPosition.y) { if (!secondObjectBlox.IsParam && ValidateNestToBottom(secondObject) && MathHelper.IsNearby(secondObjBBox.left.x, thisBBox.left.x, thisObjectWidth / 4)) { AddToBottom(secondObjectBlox); OnNestToBottom(); } else if (!secondObjectBlox.IsParam && ValidateNestToBottomIdented(secondObject) && MathHelper.IsNearby(secondObjBBox.left.x, thisBBox.bottom.x, thisObjectWidth / 4)) { AddToBottomIdented(secondObjectBlox); OnNestToBottomIdented(); } } else //if it is above { // Checks if the left parth of the second object is near the right part of the first, and verifies if they are kind of aligned if (ValidateNestToTheSide(secondObject) && MathHelper.IsNearby(secondObjBBox.left.y, thisBBox.right.y, thisObjectHeight / 4) && MathHelper.IsNearby(secondObjBBox.left.x, thisBBox.right.x, thisObjectWidth / 4)) { // Nest side to side AddParam(secondObjectBlox); OnNestToSide(); } } OnNest(); SetAllBloxesPositionsOnScreen(); } } }
/// <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); }
private bool IsBloxAndIsBeingDragged(GameObject gameObject) { bool validation = false; bool isBlox = GameObjectHelper.HasComponent <ABlox>(gameObject); if (isBlox) { ABlox collidedBlox = gameObject.GetComponent <ABlox>(); bool isBeingDragged = collidedBlox.IsBeingDragged; validation = isBlox && isBeingDragged; } return(validation); }
public void OnBeginDrag(PointerEventData eventData) { bloxTransform = GetComponent <RectTransform>(); // When attempting to drag a Blox that is inside the blox bag, // instead of just dragging we create a new instance and set // the current one as not inside if (CheckIfInsideBloxBag()) { ABlox newInstance = Instantiate(this, this.transform.parent); this.transform.SetParent(this.transform.parent.parent.parent.parent); //Content, then Viewport then BloxBag then Canvas //this.InsideBloxBag = false; } }
// Update is called once per frame void Update() { if (ABlox.LastClickedBlox != LastClickedBlox) { HelpText.text = ABlox.LastClickedBlox.HelpText; if (ABlox.LastClickedBlox.HelpExampleTexture != null) { Image.texture = ABlox.LastClickedBlox.HelpExampleTexture; Image.gameObject.SetActive(true); } else { Image.gameObject.SetActive(false); } LastClickedBlox = ABlox.LastClickedBlox; } }
/// <summary> /// Positions the blox params on screen /// </summary> /// <param name="blox"></param> protected void SetBloxParamsPositionOnScreen(ABlox blox, float paramSpacing, bool nestingActive = true) { ABlox previousBloxInLine = blox; BoundingBox2D bloxBBox = GameObjectHelper.getBoundingBoxInWorld(blox.gameObject); float bloxWidth = GameObjectHelper.getWidthFromBBox(bloxBBox); foreach (ABlox param in blox.BloxParams) { param.SetNestingState(nestingActive); BoundingBox2D paramBBox = GameObjectHelper.getBoundingBoxInWorld(param.gameObject); float paramWidth = GameObjectHelper.getWidthFromBBox(paramBBox); Vector2 paramPosition = previousBloxInLine.transform.position; paramPosition.x += (bloxWidth + paramWidth) / 2 + paramSpacing; param.transform.position = paramPosition; previousBloxInLine = param; } }
/// <summary> /// Checks if object passed can be nested to this one, and returns nesting type /// </summary> /// <param name="secondObject"></param> /// <returns></returns> public NestingType DetermineNestingType(GameObject secondObject) { NestingType nestingType = NestingType.NONE; RootBlox rootBlox = GetRootBlox(); if (rootBlox != null && NestingActive) { if (secondObject.GetComponent <ABlox>() != null && secondObject != null && ValidateNesting(secondObject)) { ABlox secondObjectBlox = secondObject.GetComponent <ABlox>(); Vector2 thisObjectPosition = this.gameObject.transform.position; BoundingBox2D thisBBox = GameObjectHelper.getBoundingBoxInWorld(this.gameObject); BoundingBox2D secondObjBBox = GameObjectHelper.getBoundingBoxInWorld(secondObject); float thisObjectWidth = GameObjectHelper.getWidthFromBBox(thisBBox); float thisObjectHeight = GameObjectHelper.getHeightFromBBox(thisBBox); //checks if second object top is bellow this object center if (secondObjBBox.top.y < thisObjectPosition.y) { if (!secondObjectBlox.IsParam && ValidateNestToBottom(secondObject) && MathHelper.IsNearby(secondObjBBox.left.x, thisBBox.left.x, thisObjectWidth / 4)) { nestingType = NestingType.BOTTOM; } else if (!secondObjectBlox.IsParam && ValidateNestToBottomIdented(secondObject) && MathHelper.IsNearby(secondObjBBox.left.x, thisBBox.bottom.x, thisObjectWidth / 4)) { nestingType = NestingType.BOTTOM_IDENTED; } } else //if it is above { // Checks if the left parth of the second object is near the right part of the first, and verifies if they are kind of aligned if (ValidateNestToTheSide(secondObject) && MathHelper.IsNearby(secondObjBBox.left.y, thisBBox.right.y, thisObjectHeight / 4) && MathHelper.IsNearby(secondObjBBox.left.x, thisBBox.right.x, thisObjectWidth / 4)) { // Nest side to side nestingType = NestingType.SIDE; } } } } return(nestingType); }
// Start is called before the first frame update void Start() { Vector3 pivotPos = Pivot.position; int col = 0; int row = 0; ABlox previousBlox = null; foreach (ABlox blox in AvailableBloxes) { blox.transform.SetParent(Content); float previousBloxWidth = 0; if (previousBlox == null) { previousBlox = blox; } else { BoundingBox2D pbBBox = GameObjectHelper.getBoundingBoxInWorld(previousBlox.gameObject); previousBloxWidth = GameObjectHelper.getWidthFromBBox(pbBBox); } BoundingBox2D bloxBBox = GameObjectHelper.getBoundingBoxInWorld(blox.gameObject); float bloxWidth = GameObjectHelper.getWidthFromBBox(bloxBBox); float bloxHeight = GameObjectHelper.getHeightFromBBox(bloxBBox); Vector3 newPos = pivotPos; newPos.x += col * (previousBloxWidth + margin) + bloxWidth / 2; newPos.y -= row * (bloxHeight + margin); blox.transform.position = newPos; if (col > 0) { row++; col = 0; } else { col++; } } }
/// <summary> /// Sets the position of child bloxes and param bloxes of parent /// </summary> protected void SetChildBloxesPositionOnScreen(ABlox parentBlox, float bloxVerticalSpacing, float identSpacing, float paramSpacing, bool nestingActive = true) { BoundingBox2D parentBBox = GameObjectHelper.getBoundingBoxInWorld(parentBlox.gameObject); Vector2 parentBloxLeft = parentBBox.left; // Gets all the nodes (except the ones nested to the side and parent) List <BloxIdent> bloxIdentList = parentBlox.GetChildBloxListInVerticalOrder(); ABlox previousBlox = parentBlox; // Sets the parent blox params SetBloxParamsPositionOnScreen(parentBlox, paramSpacing, nestingActive); foreach (BloxIdent bloxIdent in bloxIdentList) { ABlox blox = bloxIdent.blox; int ident = bloxIdent.ident; Vector2 previousBloxPosition = previousBlox.transform.position; BoundingBox2D previousBloxBBox = GameObjectHelper.getBoundingBoxInWorld(previousBlox.gameObject); BoundingBox2D bloxBBox = GameObjectHelper.getBoundingBoxInWorld(blox.gameObject); float bloxWidth = GameObjectHelper.getWidthFromBBox(bloxBBox); // Determines the vertical distance of this blox to the previous one float previousBloxHeight = GameObjectHelper.getHeightFromBBox(previousBloxBBox); float verticalOffset = previousBloxHeight / 2 + bloxVerticalSpacing; Vector2 newPosition = blox.transform.position; newPosition.y = previousBloxPosition.y - verticalOffset; // Determines the horizontal offset of this blox based on its ident newPosition.x = parentBloxLeft.x + bloxWidth / 2 + ident * identSpacing; blox.transform.position = newPosition; // Sets the params bloxes positions SetBloxParamsPositionOnScreen(blox, paramSpacing, nestingActive); ABlox previousBloxInLine = blox; previousBlox = blox; } }
public virtual void OnEndDrag(PointerEventData eventData) { IsBeingDragged = false; print("End dragging"); ResetHightlight(); if (collidedObjects.Count > 0) { // Although we are saving all the simultaneous collisions // We only want this object to nest with the highest ones float maxY = collidedObjects.Max(c => c.transform.position.y); List <GameObject> highestObjects = collidedObjects.Where(c => c.transform.position.y == maxY).Select(a => a.gameObject).ToList(); foreach (GameObject collidedObject in highestObjects) { //If the collided object is a blox, attempts nesting if (GameObjectHelper.HasComponent <ABlox>(collidedObject)) { ABlox blox = collidedObject.GetComponent <ABlox>(); blox.NestObject(this.gameObject); } //If the collied object is the trashbin, destroys this blox else if (GameObjectHelper.HasComponent <Trashbin>(collidedObject)) { RemoveFromParent(this); Destroy(this.gameObject); } } } else { SetAllBloxesPositionsOnScreen(); } }
/// <summary> /// This method is called when a blox is going to be eliminated from parent /// </summary> public virtual void OnBeforeChildRemove(ABlox blox) { }
protected void AddParam(ABlox param) { RemoveFromParent(param); BloxParams.Add(param); param.ParentBlox = this; }
/// <summary> /// Adds blox as a child of this blox /// </summary> /// <param name="blox"></param> protected void AddToBottomIdented(ABlox blox) { RemoveFromParent(blox); this.ChildBloxes.Insert(0, blox); blox.ParentBlox = this; }