Example #1
0
        public void AddChild(int x, int y, int colWidth, int colHeight, IMenuItem item)
        {
            if (x < 0 || y < 0 || x + colWidth - 1 >= width ||
                y + colHeight - 1 >= height || colWidth <= 0 || colHeight <= 0)
            {
                throw new ArgumentException("Coordinates out of range.");
            }
            if (item == null)
            {
                throw new ArgumentException("Item can not be null.");
            }
            if (!IsEmptyGrid(x, y, colWidth, colHeight))
            {
                throw new MenuItemException("Intersection with another menu item.");
            }
            MenuItemInfo info = item.GetInfo();

            if (_itemDict.ContainsKey(info.Name))
            {
                throw new MenuItemException("Name must be unique inside same layer.");
            }
            _itemDict.Add(info.Name, item);
            AddItemGrid(x, y, colWidth, colHeight, item);
            ((IGOReadable)item).GetGameObject().transform.SetParent(_gameObject.transform);
            item.GetInfo().Parent = this;
            (Vector2 anchorMin, Vector2 anchorMax) = GridToRect(x, y, colWidth, colHeight);
            item.SetRectTransform(
                anchorMin,
                anchorMax,
                new Vector2(0f, 0f));
            // everything must be set before OnEnable is called inside inserted prefab
            ((IGOReadable)item).GetGameObject().SetActive(true);
        }
Example #2
0
        public IMenuItem RemoveChild(IMenuItem child)
        {
            MenuItemInfo info = child.GetInfo();

            if (_itemDict.ContainsKey(info.Name))
            {
                RemoveObjectInGrid(child);
                _itemDict.Remove(info.Name);
                ((IGOReadable)child).GetGameObject()?.transform.SetParent(null);
                child.GetInfo().Parent = null;
            }
            else
            {
                return(null);
            }
            return(child);
        }