// Click Events // // Will be called from the children Drag3Ds public void ChildWasClicked(Drag3D box) { if (!selected) { SetSelected(); } int indx = cubes.IndexOf(box); if (Input.GetKey(KeyCode.LeftControl) || Input.GetKey(KeyCode.RightControl)) { childs_selected[indx] = !childs_selected[indx]; cubes[indx].SetSelected(childs_selected[indx]); } else { for (int i = 0; i < childs_selected.Count; i++) { childs_selected[i] = false; cubes[i].SetSelected(false); } childs_selected[indx] = true; cubes[indx].SetSelected(true); } ExecOnChildProductClickedCallbacks(transform.GetComponentInParent <StandGenerator>(), this, childs_selected.ToArray()); transform.GetComponentInParent <StandGenerator>().OnChildShelfSelected(this); }
public void OnAddButtonPressed() { BoxJSON b = new BoxJSON(myDB.contents[dbIndex]); b.x_repeats = stack_x; b.y_repeats = stack_y; b.z_repeats = stack_z; Drag3D product = _UItoSimulation.AddProduct(stand_dropdown_index, shelf_dropdown_index, b); if (product != null) { _SimualtionToUI.NotifyNewProductAdded(product); if (productIndexes != null && productIndexes.Length > 0) { bool[] new_index = new bool[productIndexes.Length + 1]; for (int i = 0; i < productIndexes.Length; i++) { new_index[i] = productIndexes[i]; } new_index[productIndexes.Length] = false; productIndexes = new_index; } } // Redraw UI //UpdateUIState(stand_dropdown_index, shelf_dropdown_index, productIndexes, false, true); }
public void UpdateCollisionMap(Drag3D cube) { int ID = cube.gameObject.GetInstanceID(); collisionNote[ID].Clear(); for (int p = 0; p < cubes.Length; p++) { if (cubes[p].gameObject.GetInstanceID() != ID) { cube.GetBottomVertices(out vertices_a); cubes[p].GetBottomVertices(out vertices_b); if (MiscFunc.BoxesColide2D(vertices_a, vertices_b)) { collisionNote[ID].Add(cubes[p].gameObject.GetInstanceID()); if (!collisionNote[cubes[p].gameObject.GetInstanceID()].Contains(ID)) { collisionNote[cubes[p].gameObject.GetInstanceID()].Add(ID); } } /* If did not colide and was previosuly colided clear the colision Note */ else if (collisionNote[cubes[p].gameObject.GetInstanceID()].Contains(ID)) { collisionNote[cubes[p].gameObject.GetInstanceID()].Remove(ID); } } } }
public void OnProductAddedToShelf(StandGenerator stand, ShelfGenerator shelf, Drag3D cube, bool[] selected) { //if (stand == _UIController.standList[_UIController.stand_dropdown_index] && //shelf == _UIController.standList[_UIController.stand_dropdown_index].shelves[_UIController.shelf_dropdown_index]) { int stand_idx = _UIController.standList.IndexOf(stand); int shelf_idx = _UIController.standList[stand_idx].shelves.IndexOf(shelf); //int len = _UIController.standList[stand_idx].shelves[shelf_idx].cubes.Count; //bool[] sel = UIController.GetSelectedArray( new int[]{len-1}, len); _UIController.UpdateUIState(stand_idx, shelf_idx, selected, true, true); } }
public void DeattachProduct(Drag3D leaving_product, bool trigger_callback = false) { int idx = cubes.IndexOf(leaving_product); cubes.Remove(leaving_product); cubesJSON.Remove(leaving_product.box); id2cube.Remove(leaving_product.gameObject.GetInstanceID()); childs_selected.RemoveAt(idx); sharedCollisionMap.UpdateProducts(cubes.ToArray()); if (trigger_callback) { ExecOnItemDeattachedCallbacks(transform.parent.GetComponent <StandGenerator>(), this, leaving_product); } }
public void Initialize(BoxJSON box, Drag3D D3D) { this.box = box; this.D3D = D3D; if (D3D != null) { D3D.RegisterOnMyCollisionEnterCallback(OnMyCollisionEnter); D3D.RegisterOnMyCollisionExitCallback(OnMyCollisionExit); D3D.RegisterOnDragStartCallback(OnDragStart); D3D.RegisterOnDragEndCallback(OnDragEnd); } else { Debug.LogError("Null D3D in Product Aesthetics"); } }
public void RemoveProducts(int stand_i, int shelf_i, bool[] to_remove) { int p = 0; for (int i = 0; i < to_remove.Length; i++) { if (to_remove[i]) { Drag3D product = standList[stand_i].shelves[shelf_i].cubes[p]; standList[stand_i].shelves[shelf_i].DeattachProduct(product); GameObject.Destroy(product.gameObject); p--; } p++; } }
public bool AddFloatingProduct(Drag3D fp, ShelfGenerator shg, StandGenerator sg) { // Only 1 product at a time if (floatingProduct != null) { return(false); } floatingProduct = fp; floatingPordOrigStand = sg; floatingProdOrigShelf = shg; // Transfer the parenthood form the shg to this // Move to the same position as the shelf generator so the position of the product is not alterd transform.localPosition = shg.transform.localPosition; fp.transform.parent = this.transform; return(true); }
// If in group controller mode all it will do is spread the "messages" from the UI to each individual cubes public void InitializeAsGroupController(BoxJSON box, Drag3D D3D) { this.box = box; this.D3D = D3D; IS_GROUP_CONTROLLER = true; paChilds = new List <ProductAesthetics>(); // Count all the childs foreach (Transform child in transform) { ProductAesthetics pa = child.GetComponent <ProductAesthetics>(); if (pa != null) { paChilds.Add(pa); } } }
public void ReAttach(ShelfGenerator sg = null) { ShelfGenerator target_shelf = sg == null ? floatingProdOrigShelf : sg; floatingProduct.deattached = false; if (target_shelf == floatingProdOrigShelf) { target_shelf.AttachProduct(floatingProduct.box, floatingProduct.gameObject); } else { target_shelf.AttachProduct2(floatingProduct.box, floatingProduct.gameObject); } if (sg != null) { floatingProduct.ReturnToLastValidPosition(); } floatingProduct = null; }
private void FindClosestAttatchmentPoint(Drag3D new_prod, out Vector3 new_pos, out int c_index, out float c_pos) { Vector3 pos = transform.InverseTransformPoint(new_prod.transform.position); float min_dist = float.PositiveInfinity; int min_dist_indx = 0; for (int c = 0; c < dragLines.points.Length - 1; c++) { float dist = (pos - dragLines.points[c]).sqrMagnitude; if (dist < min_dist) { min_dist = dist; min_dist_indx = c; } } new_pos = dragLines.points[min_dist_indx]; c_pos = 0; c_index = min_dist_indx; }
/* - - - - - STATIC METHODS - - - - - */ public static int CompareByPosition(Drag3D a, Drag3D b) { /* The closer to the initial dragline the higher result */ if (a.box.cir < b.box.cir) { return(-1); } else if (a.box.cir > b.box.cir) { return(1); } else if (a.box.cpr < b.box.cpr) { return(-1); } else if (a.box.cpr > b.box.cpr) { return(1); } else { return(0); } }
public void NotifyNewProductAdded(Drag3D new_product) { // new_product.RegisterOnClickCallback(ProductClickedInSimulation); }
public void ExecOnItemAttachedCallbacks(StandGenerator stand, ShelfGenerator shelf, Drag3D cube) { foreach (OnItemAttachedCallback f in onItemAttachedCallBacks) { f(stand, shelf, cube, childs_selected.ToArray()); } }
public void NotifyProductRemoved(Drag3D old_product) { // old_product.UnregisterOnClickCallback(ProductClickedInSimulation); }
public void OnProdcutRemovedFromShelf(StandGenerator stand, ShelfGenerator shelf, Drag3D cube, bool[] selected) { // When a product is added or removed from a shelf we should update the UI // To avoid any (out of bounds) errors and dislpaying incorrect information if (stand == _UIController.standList[_UIController.stand_dropdown_index] && shelf == _UIController.standList[_UIController.stand_dropdown_index].shelves[_UIController.shelf_dropdown_index]) { _UIController.UpdateUIState(_UIController.stand_dropdown_index, _UIController.shelf_dropdown_index, selected, true, true); } }
// Generation of products // public GameObject GenerateProduct(BoxJSON box) { GameObject gocube; if (box.x_repeats == 1 && box.y_repeats == 1 && box.z_repeats == 1) { gocube = GameObject.CreatePrimitive(PrimitiveType.Cube); gocube.transform.localScale = new Vector3(box.width, box.height, box.depth); Drag3D d3d = gocube.AddComponent(typeof(Drag3D)) as Drag3D; ProductAesthetics pa = gocube.AddComponent <ProductAesthetics>(); d3d.Initialize(box); // Make it so there is always at least a very small gap in betwwen cubes gocube.GetComponent <BoxCollider>().size *= 1.05f; gocube.GetComponent <BoxCollider>().isTrigger = true; pa.Initialize(box, d3d); gocube.name = box.name; Rigidbody rb = gocube.AddComponent <Rigidbody>(); rb.isKinematic = true; rb.useGravity = false; } else { gocube = new GameObject(); List <GameObject> gos = new List <GameObject>(); Vector3 final_size = new Vector3(box.actual_width, box.actual_height, box.actual_depth); Vector3 original_size = new Vector3(box.width, box.height, box.depth); for (int x = 0; x < box.x_repeats; x++) { for (int y = 0; y < box.y_repeats; y++) { for (int z = 0; z < box.z_repeats; z++) { GameObject go = GameObject.CreatePrimitive(PrimitiveType.Cube); go.transform.localPosition = new Vector3(box.width * x + ProductAesthetics.BOX_STACK_X_SPACING * x, box.height * y + ProductAesthetics.BOX_STACK_Y_SPACING * y, box.depth * z + ProductAesthetics.BOX_STACK_Y_SPACING * z) - final_size / 2 + original_size / 2; go.transform.localScale = original_size; go.transform.SetParent(gocube.transform); go.GetComponent <BoxCollider>().enabled = false; gos.Add(go); } } } Drag3D d3d = gocube.AddComponent(typeof(Drag3D)) as Drag3D; ProductAesthetics pae = gocube.AddComponent <ProductAesthetics>(); d3d.Initialize(box); // Make it so there is always at least a very small gap in betwwen cubes //gocube.GetComponent<BoxCollider>().size *= 1.05f; foreach (GameObject go in gos) { ProductAesthetics pa = go.AddComponent <ProductAesthetics>(); pa.Initialize(box, d3d); } pae.InitializeAsGroupController(box, d3d); gocube.AddComponent <BoxCollider>(); gocube.GetComponent <BoxCollider>().center = Vector3.zero; gocube.GetComponent <BoxCollider>().size = final_size; gocube.GetComponent <BoxCollider>().isTrigger = true; gocube.transform.localScale = Vector3.one; gocube.name = box.name; Rigidbody rb = gocube.AddComponent <Rigidbody>(); rb.isKinematic = true; rb.useGravity = false; } return(gocube); }
public void UpdateCollisionMap(Drag3D cube) { float RESOLUTION = 0.05f; int ID = cube.gameObject.GetInstanceID(); //if (collisionNote.ContainsKey(ID)) //{ // List<int> keys = new List<int>(collisionNote.Keys); // // Clear any references to collisions caused to/from ID // foreach (int key in keys) // { // if (key == ID) // { // collisionNote[key].Clear(); // } // else // { // for (int i = 0; i < collisionNote[key].Count; i++) // { // if (collisionNote[key][i] == ID) // { // collisionNote[key].RemoveAt(i); // i--; // } // } // } // } // // Clear any collision information previously calculated // for (int i = 0; i < perNodeCollision.Length; i++) // { // if (perNodeCollision[i] != null) // for (int p = 0; p < perNodeCollision[i].Count; p++) // { // if (perNodeCollision[i][p].left != null && perNodeCollision[i][p].left.ID == ID) // { // perNodeCollision[i][p].left = null; // //new CollisionInfo(ray_length, -1); // } // if (perNodeCollision[i][p].right != null && perNodeCollision[i][p].right.ID == ID) // { // perNodeCollision[i][p].right = null; // //new CollisionInfo(ray_length, -1); // } // } // } //} //else //{ // collisionNote.Add(ID, new List<int>()); //} for (int i = 0; i < mDraglines.Length - 1; i++) { BoxCollider b = cube.gameObject.GetComponent <BoxCollider>(); Vector3 segmentStart = mDraglines[i]; Vector3 segmentEnd = mDraglines[i + 1]; segmentStart = cube.gameObject.transform.parent.TransformPoint(segmentStart); segmentEnd = cube.gameObject.transform.parent.TransformPoint(segmentEnd); segmentStart.y = cube.gameObject.transform.position.y; segmentEnd.y = cube.gameObject.transform.position.y; Vector3 segmentDir = segmentEnd - segmentStart; // Pointing towards inside Vector3 perpVec = new Vector3(segmentDir.z, segmentDir.y, -segmentDir.x); // Fire an array of rays towards the cubes int num_steps = (int)(segmentDir.magnitude / RESOLUTION) + 1; Vector3 step = segmentDir / num_steps; bool contained = false; for (int z = 0; z < num_steps; z++) { Vector3 point = segmentStart + step * (z + 0.5f); Vector3 direction = perpVec.normalized; Ray r1 = new Ray(); Ray r2 = new Ray(); contained = (point == b.ClosestPointOnBounds(point)); if (!contained) { r1 = new Ray(point, direction); r2 = new Ray(point, -direction); } // If the point is inside the box, the ray won't hit the it since it is firing it from the inside // Inverse the ray to solve this else { Ray temp = new Ray(point, perpVec.normalized); // One ray in one direction r1 = new Ray(temp.GetPoint(ray_length), -direction); temp = new Ray(point, -perpVec.normalized); // One ray in the oposite one r2 = new Ray(temp.GetPoint(ray_length), direction); } RaycastHit rch1; RaycastHit rch2; bool r1hit = b.Raycast(r1, out rch1, ray_length); bool r2hit = b.Raycast(r2, out rch2, ray_length); if (r1hit || r2hit) { float max_dist = r1hit ? contained ? ray_length - rch1.distance : rch1.distance : ray_length; float min_dist = r2hit ? contained ? ray_length - rch2.distance : rch2.distance : ray_length; AddCollisionInfo(i, z, min_dist, max_dist, ID, contained); } } } }