예제 #1
0
    public void DragEnd(PointerEventData data)
    {
        if (!dragging)
        {
            return;
        }

        dragging = false;

        BuildingBarUI bar = GetComponentInParent <BuildingBarUI>();

        // Work out the old index.
        float oldX     = oldPos.x;
        int   oldIndex = Mathf.RoundToInt((oldX - 5f) / 55f);

        // Make sure that the index is in bounds.
        oldIndex = Mathf.Clamp(oldIndex, 0, bar.Items.Count);

        // Work out newly placed index...
        float x     = ((RectTransform)transform).anchoredPosition.x;
        int   index = Mathf.RoundToInt((x - 5f) / 55f);

        // Make sure that the index is in bounds.
        index = Mathf.Clamp(index, 0, bar.Items.Count);

        if (index == oldIndex)
        {
            transform.position = oldPos;
            return;
        }

        if (index > oldIndex)
        {
            // Update the bar.
            index += 1;
            index  = Mathf.Clamp(index, 0, bar.Items.Count);
            bar.Items.Insert(index, Player.Local.BuildingInventory.GetItem(Prefab));
            bar.Items.RemoveAt(oldIndex);
            bar.Refresh();
        }
        else
        {
            // Update the bar.
            index = Mathf.Clamp(index, 0, bar.Items.Count);
            bar.Items.Insert(index, Player.Local.BuildingInventory.GetItem(Prefab));
            bar.Items.RemoveAt(oldIndex + 1);
            bar.Refresh();
        }
    }
예제 #2
0
    public void Drop(PointerEventData data)
    {
        string prefab = data.pointerDrag.GetComponent <BuildingMenuItem>().Prefab;

        Vector2 localPos = data.position - (Vector2)((RectTransform)transform).position;

        // Determine index from loca position...
        // General formula for item x position is:
        // x = 5 + 55 * index
        // x - 5 = 55 * index
        // (x - 5) / 55 = index
        // index = (x - 5) / 55
        // Algebra is actually useful, aparently.

        float x     = localPos.x;
        int   index = Mathf.RoundToInt((x - 5f) / 55f);

        // Make sure that the index is in bounds.
        index = Mathf.Clamp(index, 0, Bar.Items.Count);

        Bar.Items.Insert(index, Player.Local.BuildingInventory.GetItem(prefab));
        Bar.Refresh();
    }