예제 #1
0
        private void SetupUI()
        {
            Size actualSize = WorkAreaSize();

            if (actualSize.Width < 1 || _data == null || _data.Zones == null || Model == null)
            {
                return;
            }

            int spacing = Model.ShowSpacing ? Model.Spacing : 0;

            _data.MinZoneWidth  = Convert.ToInt32(GridData.Multiplier / actualSize.Width * (MinZoneSize + (2 * spacing)));
            _data.MinZoneHeight = Convert.ToInt32(GridData.Multiplier / actualSize.Height * (MinZoneSize + (2 * spacing)));

            Preview.Children.Clear();
            AdornerLayer.Children.Clear();

            Preview.Width  = actualSize.Width;
            Preview.Height = actualSize.Height;

            MagneticSnap snapX = new MagneticSnap(GridData.PrefixSum(Model.ColumnPercents).GetRange(1, Model.ColumnPercents.Count - 1), actualSize.Width);
            MagneticSnap snapY = new MagneticSnap(GridData.PrefixSum(Model.RowPercents).GetRange(1, Model.RowPercents.Count - 1), actualSize.Height);

            for (int zoneIndex = 0; zoneIndex < _data.Zones.Count; zoneIndex++)
            {
                // this is needed for the lambda
                int zoneIndexCopy = zoneIndex;

                var zone      = _data.Zones[zoneIndex];
                var zonePanel = new GridZone(spacing, snapX, snapY, (orientation, offset) => _data.CanSplit(zoneIndexCopy, offset, orientation), zone);
                zonePanel.UpdateShiftState(((App)Application.Current).MainWindowSettings.IsShiftKeyPressed);
                Preview.Children.Add(zonePanel);
                zonePanel.Split         += OnSplit;
                zonePanel.MergeDrag     += OnMergeDrag;
                zonePanel.MergeComplete += OnMergeComplete;
                SetZonePanelSize(zonePanel, zone);
                zonePanel.LabelID.Content = zoneIndex + 1;
            }

            foreach (var resizer in _data.Resizers)
            {
                var resizerThumb = new GridResizer();
                resizerThumb.DragStarted   += Resizer_DragStarted;
                resizerThumb.DragDelta     += Resizer_DragDelta;
                resizerThumb.DragCompleted += Resizer_DragCompleted;
                resizerThumb.Orientation    = resizer.Orientation;
                AdornerLayer.Children.Add(resizerThumb);

                if (resizer.Orientation == Orientation.Horizontal)
                {
                    resizerThumb.LeftReferenceZone   = resizer.PositiveSideIndices[0];
                    resizerThumb.RightReferenceZone  = resizer.PositiveSideIndices.Last();
                    resizerThumb.TopReferenceZone    = resizer.PositiveSideIndices[0];
                    resizerThumb.BottomReferenceZone = resizer.NegativeSideIndices[0];
                }
                else
                {
                    resizerThumb.LeftReferenceZone   = resizer.PositiveSideIndices[0];
                    resizerThumb.RightReferenceZone  = resizer.NegativeSideIndices[0];
                    resizerThumb.TopReferenceZone    = resizer.PositiveSideIndices[0];
                    resizerThumb.BottomReferenceZone = resizer.PositiveSideIndices.Last();
                }

                PlaceResizer(resizerThumb);
            }
        }
예제 #2
0
        private void RenderActualScalePreview(GridLayoutModel grid)
        {
            int    rows    = grid.Rows;
            int    cols    = grid.Columns;
            double spacing = grid.ShowSpacing ? grid.Spacing : 0;

            var rowData    = GridData.PrefixSum(grid.RowPercents);
            var columnData = GridData.PrefixSum(grid.ColumnPercents);

            var workArea = App.Overlay.WorkArea;

            Viewbox viewbox = new Viewbox
            {
                Stretch = Stretch.Uniform,
            };

            Body.Children.Add(viewbox);
            Canvas frame = new Canvas
            {
                Width  = workArea.Width,
                Height = workArea.Height,
            };

            viewbox.Child = frame;

            for (int row = 0; row < rows; row++)
            {
                for (int col = 0; col < cols; col++)
                {
                    int childIndex = grid.CellChildMap[row, col];
                    if (((row == 0) || (grid.CellChildMap[row - 1, col] != childIndex)) &&
                        ((col == 0) || (grid.CellChildMap[row, col - 1] != childIndex)))
                    {
                        // this is not a continuation of a span
                        Border rect = new Border();
                        double left = columnData[col] * workArea.Width / GridData.Multiplier;
                        double top  = rowData[row] * workArea.Height / GridData.Multiplier;

                        int maxRow = row;
                        while (((maxRow + 1) < rows) && (grid.CellChildMap[maxRow + 1, col] == childIndex))
                        {
                            maxRow++;
                        }

                        int maxCol = col;
                        while (((maxCol + 1) < cols) && (grid.CellChildMap[row, maxCol + 1] == childIndex))
                        {
                            maxCol++;
                        }

                        double right  = columnData[maxCol + 1] * workArea.Width / GridData.Multiplier;
                        double bottom = rowData[maxRow + 1] * workArea.Height / GridData.Multiplier;

                        left   += col == 0 ? spacing : spacing / 2;
                        right  -= maxCol == cols - 1 ? spacing : spacing / 2;
                        top    += row == 0 ? spacing : spacing / 2;
                        bottom -= maxRow == rows - 1 ? spacing : spacing / 2;

                        Canvas.SetTop(rect, top);
                        Canvas.SetLeft(rect, left);
                        rect.Width  = Math.Max(1, right - left);
                        rect.Height = Math.Max(1, bottom - top);

                        rect.Style = (Style)FindResource("GridLayoutActualScalePreviewStyle");
                        frame.Children.Add(rect);
                    }
                }
            }

            if (App.DebugMode)
            {
                TextBlock text = new TextBlock();
                text.Text     = "(" + workArea.X + "," + workArea.Y + ")";
                text.FontSize = 42;
                frame.Children.Add(text);
            }
        }