/// <summary> /// This item started to drag. /// </summary> /// <param name="eventData"></param> public void OnBeginDrag(PointerEventData eventData) { if (dragDisabled == false) { sourceCell = GetCell(); // Remember source cell draggedItem = this; // Set as dragged item // Create item's icon icon = new GameObject(); icon.transform.SetParent(canvas.transform); icon.name = "Icon"; Image myImage = GetComponent <Image>(); myImage.raycastTarget = false; // Disable icon's raycast for correct drop handling Image iconImage = icon.AddComponent <Image>(); iconImage.raycastTarget = false; iconImage.sprite = myImage.sprite; RectTransform iconRect = icon.GetComponent <RectTransform>(); // Set icon's dimensions RectTransform myRect = GetComponent <RectTransform>(); iconRect.pivot = new Vector2(0.5f, 0.5f); iconRect.anchorMin = new Vector2(0.5f, 0.5f); iconRect.anchorMax = new Vector2(0.5f, 0.5f); iconRect.sizeDelta = new Vector2(myRect.rect.width, myRect.rect.height); if (OnItemDragStartEvent != null) { OnItemDragStartEvent(this); // Notify all items about drag start for raycast disabling } } }
/// <summary> /// Swap items between two cells /// </summary> /// <param name="firstCell"> Cell </param> /// <param name="secondCell"> Cell </param> public void SwapItems(DNDslot firstCell, DNDslot secondCell) { Debug.Log("Swap"); if ((firstCell != null) && (secondCell != null)) { DNDgem firstItem = firstCell.GetItem(); // Get item from first cell DNDgem secondItem = secondCell.GetItem(); // Get item from second cell // Swap items if (firstItem != null) { firstItem.transform.SetParent(secondCell.transform, false); firstItem.transform.localPosition = Vector3.zero; firstItem.MakeRaycast(true); } if (secondItem != null) { secondItem.transform.SetParent(firstCell.transform, false); secondItem.transform.localPosition = Vector3.zero; secondItem.MakeRaycast(true); } if (secondCell.tag == "Trash") { GameObject.Destroy(firstItem); } // Update state firstCell.UpdateMyItem(); secondCell.UpdateMyItem(); firstCell.UpdateBackgroundState(); secondCell.UpdateBackgroundState(); } swapSocket(); }
/// <summary> /// Put item into this cell. /// </summary> /// <param name="item">Item.</param> private void PlaceItem(DNDgem item) { Debug.Log("Place"); if (item != null) { if (item.transform.parent.name.Contains("Socket")) { removeSocket(item); } DestroyItem(); // Remove current item from this cell myDadItem = null; DNDslot cell = item.GetComponentInParent <DNDslot>(); if (cell != null) { if (cell.unlimitedSource == true) { string itemName = item.name; item = Instantiate(item); // Clone item from source cell item.name = itemName; } } item.transform.SetParent(transform, false); item.transform.localPosition = Vector3.zero; item.MakeRaycast(true); myDadItem = item; if (item.transform.parent.name.Contains("Socket")) { newSocket(item); } } UpdateBackgroundState(); }
/// <summary> /// On any item drag end enable all items raycast /// </summary> /// <param name="item"> dragged item </param> private void OnAnyItemDragEnd(DNDgem item) { UpdateMyItem(); if (myDadItem != null) { myDadItem.MakeRaycast(true); // Enable item's raycast } UpdateBackgroundState(); }
/// <summary> /// Resets all temporary conditions. /// </summary> private void ResetConditions() { if (icon != null) { Destroy(icon); // Destroy icon on item drop } if (OnItemDragEndEvent != null) { OnItemDragEndEvent(this); // Notify all cells about item drag end } draggedItem = null; icon = null; sourceCell = null; }
/// <summary> /// Manualy add item into this cell /// </summary> /// <param name="newItem"> New item </param> public void AddItem(DNDgem newItem) { if (newItem != null) { PlaceItem(newItem); DropEventDescriptor desc = new DropEventDescriptor(); // Fill event descriptor desc.triggerType = TriggerType.ItemAdded; desc.item = newItem; desc.sourceCell = this; desc.destinationCell = this; SendNotification(desc); } }
/// <summary> /// On any item drag start need to disable all items raycast for correct drop operation /// </summary> /// <param name="item"> dragged item </param> private void OnAnyItemDragStart(DNDgem item) { UpdateMyItem(); if (myDadItem != null) { myDadItem.MakeRaycast(false); // Disable item's raycast for correct drop handling if (myDadItem == item) // If item dragged from this cell { // Check cell's type switch (cellType) { case CellType.DropOnly: DNDgem.icon.SetActive(false); // Item can not be dragged. Hide icon break; } } } }
/// <summary> /// Destroy item in this cell /// </summary> private void DestroyItem() { UpdateMyItem(); if (myDadItem != null) { DropEventDescriptor desc = new DropEventDescriptor(); // Fill event descriptor desc.triggerType = TriggerType.ItemWillBeDestroyed; desc.item = myDadItem; desc.sourceCell = this; desc.destinationCell = this; SendNotification(desc); // Notify application about item destruction if (myDadItem != null) { Destroy(myDadItem.gameObject); } } myDadItem = null; UpdateBackgroundState(); }
void newSocket(DNDgem gemToSocket) { Gem newGem; switch (gemToSocket.transform.parent.gameObject.name) { case "W1Socket 1": if (gemToSocket.tag == "Gem") { newGem = new Gem(UIArmory.GemElement(gemToSocket.gameObject), System.Int32.Parse(gemToSocket.GetComponentInChildren <UnityEngine.UI.Text> ().text)); Inventory.playerWeapon1.setGem1(newGem); Inventory.inventory.RemoveAt(UIArmory.FindGem(newGem)); } else { Inventory.playerWeapon1.setGem1(new Gem()); } break; case "W1Socket 2": if (gemToSocket.tag == "Gem") { newGem = new Gem(UIArmory.GemElement(gemToSocket.gameObject), System.Int32.Parse(gemToSocket.GetComponentInChildren <UnityEngine.UI.Text> ().text)); Inventory.playerWeapon1.setGem2(newGem); Inventory.inventory.RemoveAt(UIArmory.FindGem(newGem)); } else { Inventory.playerWeapon1.setGem2(new Gem()); } break; case "W1Socket 3": if (gemToSocket.tag == "Gem") { newGem = new Gem(UIArmory.GemElement(gemToSocket.gameObject), System.Int32.Parse(gemToSocket.GetComponentInChildren <UnityEngine.UI.Text> ().text)); Inventory.playerWeapon1.setGem3(newGem); Inventory.inventory.RemoveAt(UIArmory.FindGem(newGem)); } else { Inventory.playerWeapon1.setGem3(new Gem()); } break; case "W2Socket 1": if (gemToSocket.tag == "Gem") { newGem = new Gem(UIArmory.GemElement(gemToSocket.gameObject), System.Int32.Parse(gemToSocket.GetComponentInChildren <UnityEngine.UI.Text> ().text)); Inventory.playerWeapon2.setGem1(newGem); Inventory.inventory.RemoveAt(UIArmory.FindGem(newGem)); } else { Inventory.playerWeapon2.setGem1(new Gem()); } break; case "W2Socket 2": if (gemToSocket.tag == "Gem") { newGem = new Gem(UIArmory.GemElement(gemToSocket.gameObject), System.Int32.Parse(gemToSocket.GetComponentInChildren <UnityEngine.UI.Text> ().text)); Inventory.playerWeapon2.setGem2(newGem); Inventory.inventory.RemoveAt(UIArmory.FindGem(newGem)); } else { Inventory.playerWeapon2.setGem2(new Gem()); } break; case "W2Socket 3": if (gemToSocket.tag == "Gem") { newGem = new Gem(UIArmory.GemElement(gemToSocket.gameObject), System.Int32.Parse(gemToSocket.GetComponentInChildren <UnityEngine.UI.Text> ().text)); Inventory.playerWeapon2.setGem3(newGem); Inventory.inventory.RemoveAt(UIArmory.FindGem(newGem)); } else { Inventory.playerWeapon2.setGem3(new Gem()); } break; case "ASocket 1": if (gemToSocket.tag == "Gem") { newGem = new Gem(UIArmory.GemElement(gemToSocket.gameObject), System.Int32.Parse(gemToSocket.GetComponentInChildren <UnityEngine.UI.Text> ().text)); Inventory.playerArmor.setGem1(newGem); Inventory.inventory.RemoveAt(UIArmory.FindGem(newGem)); } else { Inventory.playerArmor.setGem1(new Gem()); } break; default: break; } }
/// <summary> /// Updates my item /// </summary> public void UpdateMyItem() { myDadItem = GetComponentInChildren <DNDgem>(); }
/// <summary> /// Item is dropped in this cell /// </summary> /// <param name="data"></param> public void OnDrop(PointerEventData data) { if (DNDgem.icon != null) { DNDgem item = DNDgem.draggedItem; DNDslot sourceCell = DNDgem.sourceCell; if (DNDgem.icon.activeSelf == true) // If icon inactive do not need to drop item into cell { if ((item != null) && (sourceCell != this)) { DropEventDescriptor desc = new DropEventDescriptor(); switch (cellType) // Check this cell's type { case CellType.Swap: // Item in destination cell can be swapped UpdateMyItem(); switch (sourceCell.cellType) { case CellType.Swap: // Item in source cell can be swapped // Fill event descriptor desc.item = item; desc.sourceCell = sourceCell; desc.destinationCell = this; SendRequest(desc); // Send drop request StartCoroutine(NotifyOnDragEnd(desc)); // Send notification after drop will be finished if (desc.permission == true) // If drop permitted by application { if (myDadItem != null) // If destination cell has item // Fill event descriptor { DropEventDescriptor descAutoswap = new DropEventDescriptor(); descAutoswap.item = myDadItem; descAutoswap.sourceCell = this; descAutoswap.destinationCell = sourceCell; SendRequest(descAutoswap); // Send drop request StartCoroutine(NotifyOnDragEnd(descAutoswap)); // Send notification after drop will be finished if (descAutoswap.permission == true) // If drop permitted by application { SwapItems(sourceCell, this); // Swap items between cells } else { PlaceItem(item); // Delete old item and place dropped item into this cell } } else { PlaceItem(item); // Place dropped item into this empty cell } } break; default: // Item in source cell can not be swapped // Fill event descriptor desc.item = item; desc.sourceCell = sourceCell; desc.destinationCell = this; SendRequest(desc); // Send drop request StartCoroutine(NotifyOnDragEnd(desc)); // Send notification after drop will be finished if (desc.permission == true) // If drop permitted by application { PlaceItem(item); // Place dropped item into this cell } break; } break; case CellType.DropOnly: // Item only can be dropped into destination cell // Fill event descriptor desc.item = item; desc.sourceCell = sourceCell; desc.destinationCell = this; SendRequest(desc); // Send drop request StartCoroutine(NotifyOnDragEnd(desc)); // Send notification after drop will be finished if (desc.permission == true) // If drop permitted by application { PlaceItem(item); // Place dropped item in this cell } TrashScript temp = GetComponent <TrashScript> (); if (temp != null) { temp.trash_item(); return; } break; default: break; } } } if (item != null) { if (item.GetComponentInParent <DNDslot> () == null) // If item have no cell after drop { Destroy(item.gameObject); // Destroy it } } UpdateMyItem(); UpdateBackgroundState(); sourceCell.UpdateMyItem(); sourceCell.UpdateBackgroundState(); } }