예제 #1
0
        /// <summary>
        /// inserts an iterable button at position (row, column)
        /// </summary>
        /// <param name="button"></param>
        /// <param name="row"></param>
        /// <param name="column"></param>
        public void InsertAt(IterableButton button, int row, int column)
        {
            if (button == null)
            {
                string error = "InsertAt was passed a null " + typeof(IterableButton) + " in scene object " + name;
                throw new NullReferenceException(error);
            }
            if (row > Grid.Count - 1)
            {
                string error = "InsertAt was passed a row number  " + row + " that is outofbounds in scene object " + name + ". Total number of rows" +
                               "are " + Grid.Count;
                throw new IndexOutOfRangeException(error);
            }

            int maxButtonsPerRow = CalculateMaxButtonsPerRow();
            int siblingIndex     = maxButtonsPerRow * row + column; //this indexes the child in the scene

            button.SetRowAndColumnIndex(row, column);
            button.transform.SetSiblingIndex(siblingIndex);
            Grid[row].Row.Insert(row, button);


            //shift to the right, starting from the next column

            ShiftRight(button, row, column + 1);
            //UpdateNavigation();
            _count++;
        }
예제 #2
0
        /// <summary>
        /// Inserts a button into the grid, returning the Iterable button that was inserted
        /// </summary>
        /// <returns></returns>
        public IterableButton InsertNewButtonNoNavUpdate()
        {
            IterableButton button;

            if (_rootButton == null) //is there a root?
            {
                //check the inactive pool, does it have any usable items?
                if (InactiveButtonPool.Count > 0)
                {
                    _rootButton = InactiveButtonPool[0];
                    _rootButton.SetRowAndColumnIndex(0, 0);
                }
                else
                {
                    _rootButton = BuildIterableButton(0, 0);
                }

                if (Grid.Count != 0)
                {
                    Grid[0].Row.Add(_rootButton);
                    _lastRowUsed = Grid[0];
                }
                else
                {
                    ButtonRow newRow = new ButtonRow(_rootButton, 0);
                    Grid.Add(newRow);
                    _lastRowUsed = newRow;
                }
                button = _rootButton;
                EventSystem.current.SetSelectedGameObject(_rootButton.Button.gameObject);
            }
            else //yes. now check if its possible to insert this new button into the last row that was used.
            {
                IterableButton newIterableButton;
                //first, check if there are too many buttons in the last row used. if there are none, insert this button, then update navigation pointers
                if (_lastRowUsed.Count < MaxButtonsPerRow)
                {
                    int newColumnIndex = _lastRowUsed.Count;
                    int rowIndex       = _lastRowUsed.RowId;
                    newIterableButton = BuildIterableButton(rowIndex, newColumnIndex);
                    _lastRowUsed.Add(newIterableButton);
                }
                else
                {
                    const int newColumnId = 0;
                    int       rowId       = _lastRowUsed.RowId + 1;
                    newIterableButton = BuildIterableButton(rowId, newColumnId);
                    ButtonRow newButtonRow = new ButtonRow(newIterableButton, rowId);

                    Grid.Add(newButtonRow);
                    _lastRowUsed = newButtonRow;
                }
                button = newIterableButton;
            }
            _count++;
            return(button);
        }
예제 #3
0
 /// <summary>
 /// If the field Button prefab is set to null, create a generic button, returning its IterableButton component
 /// </summary>
 /// <param name="rowId"></param>
 /// <param name="columnId"></param>
 /// <returns></returns>
 private IterableButton BuildIterableButton(int rowId, int columnId)
 {
     //lets check the inactive pool first
     if (InactiveButtonPool.Count == 0)
     {
         if (ButtonPrefab == null)
         {
             var emptyGameObject = new GameObject("Button");
             emptyGameObject.transform.SetParent(ParentRectTransform);
             emptyGameObject.AddComponent <Image>();
             Button     button = emptyGameObject.AddComponent <Button>();
             ColorBlock colors = button.colors;
             colors.highlightedColor = HighlightedButtonColor;
             button.colors           = colors;
             IterableButton b = emptyGameObject.AddComponent <IterableButton>();
             b.Init(rowId, columnId, button);
             b.transform.SetAsLastSibling();
             return(b);
         }
         {
             try
             {
                 GameObject go = Instantiate(ButtonPrefab);
                 go.transform.SetParent(ParentRectTransform);
                 IterableButton b      = go.AddComponent <IterableButton>();
                 Button         button = GetComponent <Button>();
                 ColorBlock     colors = button.colors;
                 colors.highlightedColor = HighlightedButtonColor;
                 button.colors           = colors;
                 b.Init(rowId, columnId, button);
                 b.transform.SetAsLastSibling();
                 return(b);
             }
             catch (NoButtonAttachedToPrefabException ex)
             {
                 Debug.Log(ex.StackTrace);
                 throw;
             }
         }
     }
     else //return one of the iterable buttons in the pool
     {
         IterableButton b = InactiveButtonPool[0];
         b.Activate();
         b.transform.SetAsLastSibling();
         b.SetRowAndColumnIndex(rowId, columnId);
         InactiveButtonPool.RemoveAt(0);
         return(b);
     }
 }