/// <summary> /// Add the `Draggable` to this slot. Will set this /// transform as the parent. /// </summary> /// <param name="item">The `Draggable` to add to this slot</param> public virtual void AddItem(Draggable item) { this.Item = item; item.transform.SetParent(transform); if (ItemAddedDelegate != null) { ItemAddedDelegate(item.gameObject); } }
/// <summary> /// Will retrieve the `Draggable` component on this object /// and subscribe to the proper callbacks /// </summary> public void Start() { draggable = GetComponent<Draggable>(); if (draggable == null) { throw new MissingComponentException("`DraggableSound` requires a `Draggable` component to play sounds for _most_ events."); } // subscribe to events draggable.OnBeginDragCallback += BeginDragCallback; draggable.OnEndDragCallback += EndDragCallback; }
/// <summary> /// Attempts to add an item to this slot. /// If the item does not have a `DraggableItemType` or /// does not match the type specified by this slot, /// will throw a `CannotAddItemException`. /// </summary> /// <param name="item">The `Draggable` item to add to this slot</param> public override void AddItem(Draggable item) { DraggableItemType draggableItemType = item.GetComponent<DraggableItemType>(); if (draggableItemType == null) { throw new CannotAddItemException("Item does not have a Type."); } if (draggableItemType.ItemType == Type) { base.AddItem(item); } else { throw new CannotAddItemException("Slot requires Type: " + Type + " and item is of Type: " + draggableItemType.ItemType); } }
/// <summary> /// Listens for presses to `KeyCode.Return` or /// `KeyCode.KeypadEnter` and will then /// attempt to retrieve the entered number of /// items from the `Stackable` and kick-off /// the `OnBeginDrag` event on it. /// </summary> public void Update() { // if they lose focus on the splitter, // destroy it if (!inputField.isFocused) { Destroy(gameObject); } // also destroy it if they hit escape if (Input.GetKeyDown(KeyCode.Escape)) { Destroy(gameObject); } // `Enter` or `Return` has been pressed, // get the value of what was entered and // retrieve that number of items from the // current stack if (Input.GetKeyDown(KeyCode.Return) || Input.GetKeyDown(KeyCode.KeypadEnter)) { int numberToRemove = int.Parse(inputField.text); if (numberToRemove == 0) { return; } Stackable newStack = Stack.Remove(numberToRemove); Slot parentSlot = Stack.GetParentSlot(); Draggable newDraggableStack = newStack.GetComponent <Draggable>(); newDraggableStack.OnBeginDrag(null); // if the item that was asked to be split is not // being dragged, put it back in it's parent // slot. Otherwise, it will also start being // dragged Draggable thisDraggable = Stack.GetComponent <Draggable>(); if (!thisDraggable.IsBeingDragged()) { parentSlot.Item = thisDraggable; } } }
/// <summary> /// Called when the item stops being dragged. /// Will get called _after_ `OnDrop` (which `Slot` /// implements). Will trigger the `ItemDidInvalidDrop` /// event if the item wasn't dropped successfully. /// </summary> /// <param name="eventData">The `PointerEventData` of the event</param> public void OnEndDrag(PointerEventData eventData) { // OnEndDrag can be triggered via mouse down // or mouse up, if there is no currently dragged // item the event has already been fired if (DraggedItem == null) { return; } DraggedItem = null; // if this item is still in the canvas transform // that means the drop was unsuccessful- trigger // the ItemDidDrop event if (transform.parent == canvas.transform) { transform.position = oldSlot.transform.position; transform.SetParent(oldSlot.transform); oldSlot.AddItem(this); if (!eventData.used) { ItemEventManager.TriggerItemDidInvalidDrop(gameObject, oldSlot, eventData); } } oldSlot = null; canvasGroup.blocksRaycasts = true; beingDragged = false; // trigger necessary events if (OnEndDragCallback != null) { OnEndDragCallback(eventData); } ItemEventManager.TriggerItemDidDrop(gameObject, eventData); }
public void Start() { Item = GetComponentInChildren<Draggable>(); }
/// <summary> /// Remove the `Draggable` from this slot. /// Will return null if no item is present. /// </summary> /// <returns>The `Draggable` item in this slot</returns> public virtual Draggable RemoveItem() { if (Item == null) { return null; } Draggable oldItem = Item; Item = null; if (ItemRemovedDelegate != null) { ItemRemovedDelegate(oldItem.gameObject); } return oldItem; }
/// <summary> /// This event is automatically fired automatically /// as this object is a MonoBehaviour. If the event /// is already used, or there is an item already /// being dragged, will do nothing. /// </summary> /// <param name="eventData">The `PointerEventData` of the event</param> public void OnBeginDrag(PointerEventData eventData) { if ((eventData != null && eventData.used) || DraggedItem != null) { return; } if (OnBeginDragCallback != null) { OnBeginDragCallback(eventData); } DraggedItem = this; canvasGroup.blocksRaycasts = false; oldSlot = GetComponentInParent<Slot>(); oldSlot.RemoveItem(); // place it in the parent canvas so // it renders above everything else transform.SetParent(canvas.transform); beingDragged = true; // trigger the item did get picked up event ItemEventManager.TriggerItemDidPickup(gameObject, eventData); }