예제 #1
0
    public void ApplyOffsetPositionPercent(ISlotListItem listItem)
    {
        ISlotListSlot currentSlot = listItem.GetCurrentSlot();
        ISlotListSlot targetSlot  = GetTargetSlot(listItem, offsetDirection);

        if (targetSlot != null)
        {
            Vector2 interpolatedPosition = currentSlot.GetPosition() + ((targetSlot.GetPosition() - currentSlot.GetPosition()) * offsetPositionPercent);
            listItem.SetPosition(interpolatedPosition);
        }
        else
        {
            listItem.SetPosition(currentSlot.GetPosition());
        }
    }
예제 #2
0
    public void InterpolateSize(ISlotListItem listItem)
    {
        ISlotListSlot currentSlot = listItem.GetCurrentSlot();

        if (currentSlot == null)
        {
            return;
        }

        ESlotListDirection direction  = CalculateOffsetDirection(listItem, Vector2.zero);
        ISlotListSlot      targetSlot = GetTargetSlot(listItem, direction);

        if (targetSlot == null)
        {
            listItem.SetSize(currentSlot.GetSize());
            return;
        }
        float offsetPercent = CalculateOffsetPositionPercent(
            listItem.GetPosition(),
            Vector2.zero,
            currentSlot.GetPosition(),
            targetSlot.GetPosition());

        InterpolateSize(listItem, targetSlot, offsetPercent);
    }
예제 #3
0
    private ESlotListDirection CalculateOffsetDirection(ISlotListItem listItem, Vector2 dragDelta)
    {
        if (listItem == null)
        {
            return(ESlotListDirection.None);
        }

        Vector2       listItemPosition = listItem.GetPosition() + dragDelta;
        ISlotListSlot currentSlot      = listItem.GetCurrentSlot();

        if (currentSlot == null)
        {
            return(ESlotListDirection.None);
        }

        ISlotListSlot nextSlot = currentSlot.GetNextSlot();

        if (nextSlot != null)
        {
            float distanceNextSlotToCurrentSlot  = Vector2.Distance(nextSlot.GetPosition(), currentSlot.GetPosition());
            float distanceNextSlotToItemPosition = Vector2.Distance(nextSlot.GetPosition(), listItemPosition);
            if (distanceNextSlotToItemPosition < distanceNextSlotToCurrentSlot)
            {
                return(ESlotListDirection.TowardsNextSlot);
            }
        }

        ISlotListSlot previousSlot = currentSlot.GetPreviousSlot();

        if (previousSlot != null)
        {
            float distancePreviousSlotToCurrentSlot  = Vector2.Distance(previousSlot.GetPosition(), currentSlot.GetPosition());
            float distancePreviousSlotToItemPosition = Vector2.Distance(previousSlot.GetPosition(), listItemPosition);
            if (distancePreviousSlotToItemPosition < distancePreviousSlotToCurrentSlot)
            {
                return(ESlotListDirection.TowardsPreviousSlot);
            }
        }

        return(ESlotListDirection.None);
    }
예제 #4
0
    public void OnDrag(ISlotListItem listItem, Vector2 dragDelta)
    {
        // Calculate direction
        offsetDirection = CalculateOffsetDirection(listItem, dragDelta);

        // Calculate offset from current slot
        ISlotListSlot targetSlot = GetTargetSlot(listItem, offsetDirection);

        offsetPositionPercent = targetSlot != null
            ? CalculateOffsetPositionPercent(listItem.GetPosition(), dragDelta, listItem.GetCurrentSlot().GetPosition(), targetSlot.GetPosition())
            : 0;

        // Apply this to all list items. This make them move in the same way towards their target slot.
        foreach (ISlotListItem otherListItem in ListItems)
        {
            ApplyOffsetPositionPercent(otherListItem);
        }

        // When the target has been reached, then adjust the current slot of all items
        if (offsetPositionPercent >= 0.6f)
        {
            slotChangeEventStream.OnNext(new SlotChangeEvent(offsetDirection, offsetPositionPercent));
        }
    }