public virtual Result <bool> CanDropDraggingItem(DragAndDropUtility.Model model, PointerEventData eventData)
        {
            var item      = model.dataObject as IItemInstance;
            var beginSlot = model.source.GetComponent <CollectionSlotUIBase>();
            var fromCol   = beginSlot.collection;
            var fromIndex = beginSlot.collectionIndex;
            var slotEntry = item as ICollectionSlotEntry;

            if (item == null)
            {
                return(new Result <bool>(false, Errors.UIDragFailedIncompatibleDragObject));
            }

            // Check if the item can be set to null in it's collection
            if (slotEntry?.collectionEntry?.collection == null)
            {
                // Can't drop item here, it's not in a collection (can only sell items to vendor if that item is already in a collection!)
                return(new Result <bool>(false, Errors.UIDragFailed));
            }

            var canSet = slotEntry.collectionEntry.collection.CanSetBoxed(fromIndex, item, 0);

            if (canSet.error != null)
            {
                return(canSet);
            }

            return(true);
        }
Beispiel #2
0
 public virtual void Clean(DragAndDropUtility.Model model)
 {
     if (model != null && model.draggingObject != null)
     {
         UnityEngine.Object.Destroy(model.draggingObject.gameObject);
     }
 }
Beispiel #3
0
        private void OnBeginDrag(DragAndDropUtility.Model model, PointerEventData pointerEventData)
        {
            switch (_showOnStartDrag)
            {
            case ShowOption.Always:
            {
                SetTransformsActive(_transforms, true);
                SetComponentsActive(_components, true);
                break;
            }

            case ShowOption.OnlyWhenAccepted:
            {
                if (IsAcceptedByDropArea(model, pointerEventData))
                {
                    SetTransformsActive(_transforms, true);
                    SetComponentsActive(_components, true);
                }
                break;
            }

            default:
                throw new ArgumentOutOfRangeException();
            }
        }
Beispiel #4
0
 private void OnEndDrag(DragAndDropUtility.Model arg1, PointerEventData arg2)
 {
     if (_hideOnEndDrag)
     {
         SetTransformsActive(_transforms, false);
         SetComponentsActive(_components, false);
     }
 }
Beispiel #5
0
        public virtual void Drag(DragAndDropUtility.Model model, PointerEventData eventData)
        {
            var pos = eventData.position;

            pos.y -= Screen.height * 0.5f;
            pos.x -= Screen.width * 0.5f;

            model.draggingObject.anchoredPosition = pos;
        }
        public void DropDraggingItem(DragAndDropUtility.Model model, PointerEventData eventData)
        {
            eventData.Use();

            var obj           = (IUnityItemInstance)model.dataObject;
            var worldPosition = CalcDropPosition();
            var dropped       = obj.Drop(PlayerManager.currentPlayer, worldPosition);

            _logger.Error("", dropped.error);
        }
        public Result <bool> CanDropDraggingItem(DragAndDropUtility.Model model, PointerEventData eventData)
        {
            eventData.Use();
            if (model.dataObject is IUnityItemInstance)
            {
                return(true);
            }

            return(false);
        }
        public Result <bool> CanDropDraggingItemOnTarget(DragAndDropUtility.Model model, List <IDropArea> targetDropAreas, PointerEventData eventData)
        {
            foreach (var dropArea in targetDropAreas)
            {
                if (dropArea.CanDropDraggingItem(model, eventData).result)
                {
                    return(true);
                }
            }

            return(new Result <bool>(false, Errors.UIDragFailedIncompatibleDragObject));
        }
Beispiel #9
0
        public void DropDraggingItemOnTarget(DragAndDropUtility.Model model, List <IDropArea> targetDropAreas, PointerEventData eventData)
        {
            var equipmentCol = slot.collection as IEquipmentCollection <IEquippableItemInstance>;

            if (equipmentCol == null)
            {
                logger.Warning("Equipment collection is not compatible with slot drag handler.", this);
                return;
            }

            var beginSlot = model.source.GetComponent <CollectionSlotUIBase>();
            var context   = new Collections.CollectionContext()
            {
                validationFlags = ~Collections.CollectionContext.Validations.SpecificInstance,
                originalIndex   = beginSlot.collectionIndex
            };

            foreach (var targetDropArea in targetDropAreas)
            {
                var targetSlot = (targetDropArea as UnityEngine.Component)?.GetComponent <CollectionSlotUIBase>();
                if (targetSlot != null)
                {
                    if (targetSlot.collection == equipmentCol)
                    {
                        if (targetSlot.collection.CanSetBoxed(targetSlot.collectionIndex, equipmentCol[slot.collectionIndex], equipmentCol.GetAmount(slot.collectionIndex), context).result == false)
                        {
                            // We're trying to move to another slot inside the same collection, but can't place in that slot, ignore action.
                            continue;
                        }
                    }

                    var player     = PlayerManager.currentPlayer;
                    var dragAmount = equipmentCol.GetAmount(slot.collectionIndex);

                    // If already eqipped, or the item is moving to a non-equipment collection, don't call Use again as it would unequip the item
                    if (equipmentCol[slot.collectionIndex].equippedTo == null || !(targetSlot.collection is IEquipmentCollection <IEquippableItemInstance>))
                    {
                        equipmentCol[slot.collectionIndex].Use(player, new ItemContext()
                        {
                            useAmount   = dragAmount,
                            targetIndex = targetSlot.collectionIndex
                        });
                    }
                    else
                    {
                        beginSlot.collection.SwapOrMerge(beginSlot.collectionIndex, targetSlot.collection, targetSlot.collectionIndex, dragAmount, context);
                    }

                    eventData.Use();
                    break;
                }
            }
        }
Beispiel #10
0
        private bool IsAcceptedByDropArea(DragAndDropUtility.Model model, PointerEventData pointerEventData)
        {
            foreach (var area in GetComponents <IDropArea>())
            {
                if (area.CanDropDraggingItem(model, pointerEventData).result)
                {
                    return(true);
                }
            }

            return(false);
        }
        public virtual Result <bool> CanDropDraggingItem(DragAndDropUtility.Model model, PointerEventData eventData)
        {
            var beginSlot = model.source.GetComponent <CollectionSlotUIBase>();
            var canSet    = slot.collection.CanSetBoxed(slot.collectionIndex, model.dataObject, beginSlot.collection.GetAmount(beginSlot.collectionIndex), new CollectionContext()
            {
                validationFlags = ~CollectionContext.Validations.SpecificInstance
            });

            if (canSet.error != null)
            {
                return(new Result <bool>(false, Errors.UIDragFailedIncompatibleDragObject));
            }

            return(true);
        }
        public virtual void DropDraggingItem(DragAndDropUtility.Model model, PointerEventData eventData)
        {
            var beginSlot = model.source.GetComponent <CollectionSlotUIBase>();
            var fromCol   = beginSlot.collection;
            var fromIndex = beginSlot.collectionIndex;

            if (fromCol != null)
            {
                var result = fromCol.SwapOrMerge(fromIndex, slot.collection, slot.collectionIndex, fromCol.GetAmount(fromIndex));
                logger.Error("", result.error, this);
            }
            else
            {
                logger.Warning("Couldn't drag item to new itemGuid. Source collection is not a compatible type", this);
            }
        }
        public override void DropDraggingItem(DragAndDropUtility.Model model, PointerEventData eventData)
        {
            var beginSlot = model.source.GetComponent <CollectionSlotUIBase>();

            if (beginSlot != null)
            {
                var equippable = beginSlot.collection.GetBoxed(beginSlot.collectionIndex) as IEquippableItemInstance;
                if (equippable != null)
                {
                    equippable.Use(PlayerManager.currentPlayer, new ItemContext()
                    {
                        useAmount   = beginSlot.collection.GetAmount(beginSlot.collectionIndex),
                        targetIndex = slot.collectionIndex
                    });
                }
            }
        }
Beispiel #14
0
        public virtual Result <bool> BeginDrag(DragAndDropUtility.Model model, PointerEventData eventData)
        {
            var canDrag = CanDrag(model, eventData);

            if (canDrag.result == false)
            {
                return(canDrag);
            }

            if (model.dataObject != null)
            {
                logger.LogVerbose("Start drag of item " + model.dataObject, model.source);
                return(true);
            }

            return(new Result <bool>(false, Errors.UIDragFailed));
        }
Beispiel #15
0
        public virtual Result <bool> EndDrag(DragAndDropUtility.Model model, PointerEventData eventData)
        {
            if (model.draggingObject == null)
            {
                return(new Result <bool>(false, Errors.UIDragFailed));
            }

            var dropAreas = GetHoveringDropAreas(eventData);

            if (dropAreas.Contains(model.source.GetComponent <IDropArea>()))
            {
                // Ended on self, don't do a thing...
                return(true);
            }

            var dropOverwriters = GetHoveringDropOverwriterAreas(model, eventData);
            var canEndDrag      = CanEndDrag(model, dropAreas, dropOverwriters, eventData);

            if (canEndDrag.result == false)
            {
                return(canEndDrag);
            }

            foreach (var overwriter in dropOverwriters)
            {
                var canOverwrite = overwriter.CanDropDraggingItemOnTarget(model, dropAreas, eventData);
                if (canOverwrite.result)
                {
                    overwriter.DropDraggingItemOnTarget(model, dropAreas, eventData);
                    return(true);
                }
            }

            foreach (var dropArea in dropAreas)
            {
                var canDrop = dropArea.CanDropDraggingItem(model, eventData);
                if (canDrop.result)
                {
                    dropArea.DropDraggingItem(model, eventData);
                    return(true);
                }
            }

            return(new Result <bool>(false, Errors.UIDragFailedNoReceiver));
        }
Beispiel #16
0
        public virtual Result <bool> CanEndDrag(DragAndDropUtility.Model model, List <IDropArea> dropAreas, List <IDropAreaSourceOverwriter> overwriters, PointerEventData eventData)
        {
            foreach (var overwriter in overwriters)
            {
                var canOverwrite = overwriter.CanDropDraggingItemOnTarget(model, dropAreas, eventData);
                if (canOverwrite.result)
                {
                    return(canOverwrite);
                }
            }

            foreach (var dropArea in dropAreas)
            {
                var canDrop = dropArea.CanDropDraggingItem(model, eventData);
                if (canDrop.result)
                {
                    return(canDrop);
                }
            }

            return(new Result <bool>(false, Errors.UIDragFailedNoReceiver));
        }
        public void DropDraggingItemOnTarget(DragAndDropUtility.Model model, List <IDropArea> targetDropAreas, PointerEventData eventData)
        {
            var equipmentCol = slot.collection as IEquipmentCollection <IEquippableItemInstance>;

            if (equipmentCol == null)
            {
                logger.Warning("Equipment collection is not compatible with slot drag handler.", this);
                return;
            }

            foreach (var targetDropArea in targetDropAreas)
            {
                var targetSlot = (targetDropArea as UnityEngine.Component)?.GetComponent <CollectionSlotUIBase>();
                if (targetSlot != null)
                {
                    if (targetSlot.collection == equipmentCol)
                    {
                        if (targetSlot.collection.CanSetBoxed(targetSlot.collectionIndex, equipmentCol[slot.collectionIndex], equipmentCol.GetAmount(slot.collectionIndex)).result == false)
                        {
                            // We're trying to move to another slot inside the same collection, but can't place in that slot, ignore action.
                            continue;
                        }
                    }

                    var player     = PlayerManager.currentPlayer;
                    var dragAmount = equipmentCol.GetAmount(slot.collectionIndex);
                    equipmentCol[slot.collectionIndex].Use(player, new ItemContext()
                    {
                        useAmount   = dragAmount,
                        targetIndex = targetSlot.collectionIndex
                    });

                    eventData.Use();
                    break;
                }
            }
        }
        public virtual void DropDraggingItem(DragAndDropUtility.Model model, PointerEventData eventData)
        {
            var item      = (IItemInstance)model.dataObject;
            var beginSlot = model.source.GetComponent <CollectionSlotUIBase>();
            var fromCol   = beginSlot.collection;
            var fromIndex = beginSlot.collectionIndex;

            var player          = PlayerManager.currentPlayer;
            var inventoryPlayer = player.GetComponent <InventoryPlayer>();
            var customer        = new Customer <IItemInstance>(Guid.NewGuid(), player, inventoryPlayer.itemCollectionGroup, inventoryPlayer.currencyCollectionGroup);

            // TODO: Add option for confirmation dialog with amount selector.
            // TODO: Always selling entire stack right now...
            var sold = vendorUI.vendor.SellToVendor(customer, new VendorProduct <IItemInstance>(item, item.itemDefinition.buyPrice, item.itemDefinition.sellPrice), fromCol.GetAmount(fromIndex));

            if (sold.error == null)
            {
                fromCol.SetBoxed(fromIndex, item, 0);
            }
            else
            {
                logger.Warning("Couldn't sell item to vendor. Error: " + sold.error, this);
            }
        }
Beispiel #19
0
 public virtual void Drag(DragAndDropUtility.Model model, PointerEventData eventData)
 {
     UIUtility.PositionRectTransformAtPosition(model.draggingObject, model.draggingObject, eventData.position);
 }
 public override Result <bool> CanDropDraggingItem(DragAndDropUtility.Model model, PointerEventData eventData)
 {
     return(base.CanDropDraggingItem(model, eventData));
 }
Beispiel #21
0
 public virtual Result <bool> CanDrag(DragAndDropUtility.Model model, PointerEventData eventData)
 {
     return(true);
 }
Beispiel #22
0
 protected virtual List <IDropAreaSourceOverwriter> GetHoveringDropOverwriterAreas(DragAndDropUtility.Model model, PointerEventData eventData)
 {
     model.source.GetComponents <IDropAreaSourceOverwriter>(_overwritersCache);
     return(_overwritersCache);
 }