IRectItemsLink CreateItemsLink(IRectItem item1, IRectItem item2)
    {
        var            itemsLinkGameObject = Instantiate(linkLinePrefab, _linksPanel.transform, false);
        IRectItemsLink itemsLink           = itemsLinkGameObject.GetComponent <IRectItemsLink>();

        itemsLink.CreateLink(item1, item2);

        return(itemsLink);
    }
Example #2
0
    public void CreateLink(IRectItem item1, IRectItem item2)
    {
        _item1 = item1;
        _item1.AddLink(item2, this);

        _item2 = item2;
        _item2.AddLink(item1, this);

        UpdateTransformFromItems();
    }
    void SetItemLinkMode(bool active)
    {
        _linkMode = active;
        _itemsLinkPoiter.gameObject.SetActive(active);

        if (!active)
        {
            _linkItemSelected = null;
        }
    }
Example #4
0
    public void DestroyLink()
    {
        if (_item1 != null && _item2 != null)
        {
            _item1.RemoveLink(_item2);
            _item2.RemoveLink(_item1);

            _item1 = _item2 = null;

            Destroy(gameObject);
        }
    }
    IRectItem CreateItem(Vector3 position)
    {
        GameObject newItem = Instantiate(itemPrefab, _itemsPanel.transform, false);

        IRectItem rectItem = newItem.GetComponent <IRectItem>();

        rectItem.rectTransform.position = position;

        _items.Add(rectItem);

        return(rectItem);
    }
    public bool CanDragItemToPosition(IRectItem draggingItem, Vector3 toPosition)
    {
        if (IsItemOverCameraBounds(toPosition))
        {
            return(false);
        }

        foreach (var item in _items)
        {
            if (draggingItem != item && AreRectsIntersects(item.rectTransform.position, toPosition, _itemSize))
            {
                return(false);
            }
        }

        return(true);
    }
 public void RemoveLink(IRectItem toItem)
 {
     _links.Remove(toItem);
 }
 public void AddLink(IRectItem toItem, IRectItemsLink link)
 {
     _links.Add(toItem, link);
 }
 public bool TryGetLinkToItem(IRectItem toItem, out IRectItemsLink itemsLink)
 {
     return(_links.TryGetValue(toItem, out itemsLink));
 }
    public void OnPointerClick(PointerEventData eventData)
    {
        GameObject gameObjectClicked = eventData.pointerCurrentRaycast.gameObject;
        IRectItem  rectItemClicked   = gameObjectClicked.GetComponentInParent <IRectItem>();

        switch (eventData.button)
        {
        case InputButton.Right:

            if (eventData.clickCount == 1 && rectItemClicked != null)
            {
                if (_linkItemSelected != null)
                {
                    if (rectItemClicked != _linkItemSelected)    //Можно создать связь или удалить, если она уже существует
                    {
                        if (rectItemClicked.TryGetLinkToItem(_linkItemSelected, out IRectItemsLink itemsLink))
                        {
                            itemsLink.DestroyLink();
                        }
                        else
                        {
                            CreateItemsLink(_linkItemSelected, rectItemClicked);
                        }
                    }
                }
                else
                {
                    _linkItemSelected = rectItemClicked;

                    SetItemLinkMode(true);

                    return;
                }
            }

            SetItemLinkMode(false);

            break;

        case InputButton.Left:

            if (_linkMode)
            {
                SetItemLinkMode(false);
                return;
            }

            if (eventData.clickCount == 2 && rectItemClicked != null && _itemDoubleClickIgnore != rectItemClicked)
            {
                RemoveItem(rectItemClicked);
            }
            else
            {
                _itemDoubleClickIgnore = null;

                if (eventData.clickCount == 1 && rectItemClicked == null)
                {
                    RectTransform rectTransform = eventData.pointerEnter.transform as RectTransform;

                    if (RectTransformUtility.ScreenPointToWorldPointInRectangle(rectTransform, eventData.position, eventData.pressEventCamera, out Vector3 globalMousePos))
                    {
                        //Проверяем, можно ли добавить прямоугольник, и если да, то добавляем
                        if (CanCreateItemInPosition(globalMousePos))
                        {
                            _itemDoubleClickIgnore = CreateItem(globalMousePos);    //Необходимо, чтобы не удалить сразу же только что созданный прямоугольник
                        }
                    }
                }
            }

            break;

        default:

            SetItemLinkMode(false);

            break;
        }
    }
 void RemoveItem(IRectItem item)
 {
     _items.Remove(item);
     item.Destroy();
 }