Ejemplo n.º 1
0
        internal MagicGridButton AddButton(MagicGridButton button, int? gridSlotIndex)
        {
            if (_slotPlaceholders.Count == 0)
            {
                RecalculateGrid();
            }

            DLog("Adding button " + (button.Text ?? "(null title)"));
            button.ParentGridControl = this;

            button.ButtonSelected += OnChildButtonSelected;
            button.ButtonUnselected += OnChildButtonUnselected;

            string labelIndex = string.Empty;

            MagicGridButtonCanvasPlaceholder targetSlot = null;

            int highestIndex = 0;
            foreach (var slot in _slotPlaceholders.OrderBy(sp => sp.MagicGridSlotIndex))
            {
                if (slot.MagicGridSlotIndex > highestIndex)
                {
                    highestIndex = slot.MagicGridSlotIndex;
                }

                if (slot.Child == null)
                {
                    if (gridSlotIndex == null)
                    {
                        gridSlotIndex = slot.MagicGridSlotIndex;
                        targetSlot = slot;
                        break;
                    }
                }

                if (gridSlotIndex != null)
                {
                    if (slot.MagicGridSlotIndex == gridSlotIndex)
                    {
                        targetSlot = slot;
                    }
                }
            }

            if (targetSlot == null)
            {
                MagicGridButtonCanvasPlaceholder newPh = new MagicGridButtonCanvasPlaceholder() { MagicGridSlotIndex = highestIndex + 1 };
                _slotPlaceholders.Add(newPh);
                targetSlot = newPh;
                _queueResize = true;
            }

            targetSlot.Child = button;
            button.Width = double.NaN;
            button.Height = double.NaN;

            labelIndex = (targetSlot.MagicGridSlotIndex + 1).ToString();

            if (!ShowButtonIndexes)
            {
                labelIndex = string.Empty;
            }

            button.IndexLabelText = labelIndex.ToString();

            button.SelectedBackground = SelectedBackground;
            button.SelectedForeground = SelectedForeground;
            button.UnselectedBackground = UnselectedBackground;
            button.UnselectedForeground = UnselectedForeground;

            if (button.IsSelected)
            {
                button.Select(false);
            } else
            {
                button.Unselect(false);
            }

            //DLog("Button added");

            _buttons.Add(button);
            //_queueReloadButtons = true;
            //ReloadButtonsCanvas();
            return button;
        }