Ejemplo n.º 1
0
        private void RecalculateGrid()
        {
            _queueResize = false;
            DLog("Resizing grid");

            if (EnablePaging)
            {
                gridPaging.Visibility = Visibility.Visible;
                txtPageText.Text = "Page " + (CurrentPage+1).ToString();
            } else
            {
                gridPaging.Visibility = Visibility.Collapsed;
            }

            double canvasWidth = canvasButtons.ActualWidth;
            double canvasHeight = canvasButtons.ActualHeight;
            int numExistingSlots = _slotPlaceholders.Count;

            double minButtonWidth = MinimumButtonWidth;
            if (minButtonWidth < 10) { minButtonWidth = 10; }

            int buttonsPerRow = Convert.ToInt32(System.Math.Floor(canvasWidth / minButtonWidth));
            double buttonWidth = (canvasWidth / Convert.ToDouble(buttonsPerRow));
            double buttonHeight = MinimumButtonHeight;

            if (buttonHeight < 10) { buttonHeight = 10; }

            int rowsPerPage = (Convert.ToInt32(System.Math.Floor(canvasHeight / buttonHeight)));
            GridSlotsPerPage = buttonsPerRow * rowsPerPage;

            double currentTop = 0;
            double currentLeft = 0;
            int row = 0;
            int col = 0;
            int index = CurrentPage * GridSlotsPerPage;
            bool gridFull = false;
            int startingSlotIndex = index;

            List<MagicGridButtonCanvasPlaceholder> unusedPlaceholders = new List<MagicGridButtonCanvasPlaceholder>();
            unusedPlaceholders.AddRange(_slotPlaceholders);

            while (!gridFull)
            {
                if (currentLeft + buttonWidth >= (canvasWidth + 50))
                {
                    // new line
                    currentLeft = 0;
                    col = 0;
                    currentTop += buttonHeight;
                    row += 1;
                }

                if (currentTop + buttonHeight > canvasHeight)
                {
                    gridFull = true;
                    break;
                }

                MagicGridButtonCanvasPlaceholder ph = null;
                ph = _slotPlaceholders.FirstOrDefault(sp => sp.MagicGridSlotIndex == index);

                if (ph == null)
                {
                    ph = new MagicGridButtonCanvasPlaceholder();
                    ph.MagicGridSlotIndex = index;
                    _slotPlaceholders.Add(ph);
                    _firePageChangeOnNextRecalculate = true;
                } else
                {
                    unusedPlaceholders.Remove(ph);
                }

                ph.IsActive = true;
                ph.MagicGridRow = row;
                ph.MagicGridColumn = col;
                ph.Width = buttonWidth;
                ph.Height = buttonHeight;
                Canvas.SetTop(ph, currentTop);
                Canvas.SetLeft(ph, currentLeft);

                currentLeft += buttonWidth;
                index += 1;
                col += 1;
            }

            foreach (var p in unusedPlaceholders)
            {
                p.IsActive = false;
            }

            foreach (var p in _slotPlaceholders)
            {
                if (p.IsActive)
                {
                    if (!canvasButtons.Children.Contains(p))
                    {
                        canvasButtons.Children.Add(p);
                    }
                } else
                {
                    if (canvasButtons.Children.Contains(p))
                    {
                        canvasButtons.Children.Remove(p);
                    }
                }
            }

            CurrentPageSlotIndexStart = startingSlotIndex;
            CurrentPageSlotIndexEnd = index;

            if (_firePageChangeOnNextRecalculate)
            {
                _firePageChangeOnNextRecalculate = false;
                if (PageChanged != null)
                {
                    PageChanged(this, CurrentPage);
                }
            }
        }
Ejemplo n.º 2
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;
        }