private void Resizer_DragCompleted(object sender, System.Windows.Controls.Primitives.DragCompletedEventArgs e) { GridResizer resizer = (GridResizer)sender; int index = _data.SwappedIndexAfterResize(resizer); if (index != -1) { Size actualSize = new Size(ActualWidth, ActualHeight); ArrangeGridRects(actualSize); } }
private void Resizer_DragCompleted(object sender, System.Windows.Controls.Primitives.DragCompletedEventArgs e) { GridResizer resizer = (GridResizer)sender; int index = _data.SwappedIndexAfterResize(resizer); if (index != -1) { Rect workingArea = App.Overlay.WorkArea; Size actualSize = new Size(workingArea.Width, workingArea.Height); ArrangeGridRects(actualSize); } }
private static void DecreaseResizerValues(GridResizer resizer, Orientation orientation) { if (orientation == Orientation.Vertical) { resizer.StartCol--; resizer.EndCol--; } else { resizer.StartRow--; resizer.EndRow--; } }
private void Resizer_DragCompleted(object sender, System.Windows.Controls.Primitives.DragCompletedEventArgs e) { GridResizer resizer = (GridResizer)sender; int resizerIndex = AdornerLayer.Children.IndexOf(resizer); Size actualSize = WorkAreaSize(); double pixelDelta = resizer.Orientation == Orientation.Vertical ? _dragX / actualSize.Width * GridData.Multiplier : _dragY / actualSize.Height * GridData.Multiplier; _data.Drag(resizerIndex, Convert.ToInt32(pixelDelta)); SetupUI(); }
private void AddDragHandle(Orientation orientation, int index) { GridResizer resizer = new GridResizer(); resizer.Orientation = orientation; resizer.Index = index; resizer.Model = Model; resizer.DragDelta += Resizer_DragDelta; if (orientation == Orientation.Vertical) { index += (Model.Rows - 1); } AdornerLayer.Children.Insert(index, resizer); }
private void HandleResizerKeyDown(GridResizer resizer, KeyEventArgs e) { DragDeltaEventArgs args = null; if (resizer.Orientation == Orientation.Horizontal) { if (e.Key == Key.Up) { args = new DragDeltaEventArgs(0, -1); } else if (e.Key == Key.Down) { args = new DragDeltaEventArgs(0, 1); } } else { if (e.Key == Key.Left) { args = new DragDeltaEventArgs(-1, 0); } else if (e.Key == Key.Right) { args = new DragDeltaEventArgs(1, 0); } } if (args != null) { e.Handled = true; Resizer_DragDelta(resizer, args); } if (e.Key == Key.Delete) { int resizerIndex = AdornerLayer.Children.IndexOf(resizer); var resizerData = _data.Resizers[resizerIndex]; var indices = new List <int>(resizerData.PositiveSideIndices); indices.AddRange(resizerData.NegativeSideIndices); _data.DoMerge(indices); SetupUI(); e.Handled = true; } }
private void Resizer_DragDelta(object sender, System.Windows.Controls.Primitives.DragDeltaEventArgs e) { MergeCancelClick(null, null); GridResizer resizer = (GridResizer)sender; double delta = (resizer.Orientation == Orientation.Vertical) ? e.HorizontalChange : e.VerticalChange; if (delta == 0) { return; } GridData.ResizeInfo resizeInfo = _data.CalculateResizeInfo(resizer, delta); if (resizeInfo.IsResizeAllowed) { if (_dragHandles.HasSnappedNonAdjacentResizers(resizer)) { double spacing = 0; MainWindowSettingsModel settings = ((App)Application.Current).MainWindowSettings; if (settings.ShowSpacing) { spacing = settings.Spacing; } _data.SplitOnDrag(resizer, delta, spacing); _dragHandles.UpdateAfterDetach(resizer, delta); } else { _data.DragResizer(resizer, resizeInfo); if (_data.SwapNegativePercents(resizer.Orientation, resizer.StartRow, resizer.EndRow, resizer.StartCol, resizer.EndCol)) { _dragHandles.UpdateAfterSwap(resizer, delta); } } } Rect workingArea = App.Overlay.WorkArea; Size actualSize = new Size(workingArea.Width, workingArea.Height); ArrangeGridRects(actualSize); AdornerLayer.UpdateLayout(); }
private void PlaceResizer(GridResizer resizerThumb) { var leftZone = Preview.Children[resizerThumb.LeftReferenceZone]; var rightZone = Preview.Children[resizerThumb.RightReferenceZone]; var topZone = Preview.Children[resizerThumb.TopReferenceZone]; var bottomZone = Preview.Children[resizerThumb.BottomReferenceZone]; double left = Canvas.GetLeft(leftZone); double right = Canvas.GetLeft(rightZone) + (rightZone as GridZone).MinWidth; double top = Canvas.GetTop(topZone); double bottom = Canvas.GetTop(bottomZone) + (bottomZone as GridZone).MinHeight; double x = (left + right) / 2.0; double y = (top + bottom) / 2.0; Canvas.SetLeft(resizerThumb, x - 24); Canvas.SetTop(resizerThumb, y - 24); }
private void HandleResizerKeyUp(GridResizer resizer, KeyEventArgs e) { if (resizer.Orientation == Orientation.Horizontal) { e.Handled = e.Key == Key.Up || e.Key == Key.Down; } else { e.Handled = e.Key == Key.Left || e.Key == Key.Right; } if (e.Handled) { int resizerIndex = AdornerLayer.Children.IndexOf(resizer); Resizer_DragCompleted(resizer, null); Debug.Assert(AdornerLayer.Children.Count > resizerIndex, "Resizer index out of range"); Keyboard.Focus(AdornerLayer.Children[resizerIndex]); _dragY = _dragX = 0; } }
public void AddDragHandle(Orientation orientation, int rowStart, int rowEnd, int colStart, int colEnd, int index) { GridResizer resizer = new GridResizer { Orientation = orientation, StartRow = rowStart, EndRow = rowEnd, StartCol = colStart, EndCol = colEnd, }; resizer.DragDelta += (obj, eventArgs) => _dragDelta(obj, eventArgs); resizer.DragCompleted += (obj, eventArgs) => _dragCompleted(obj, eventArgs); if (index > _resizers.Count) { index = _resizers.Count; } _resizers.Insert(index, resizer); }
public bool HasSnappedNonAdjascentResizers(GridResizer resizer) { /** * Resizers between zones 0,1 and 4,5 are snapped to each other and not adjascent. * ------------------------------ * | 0 | 1 | * ------------------------------ * | 2 | 3 | * ------------------------------ * | 4 | 5 | * ------------------------------ * * Resizers between zones 0,1 and 2,3 are snapped to each other and adjascent. * ------------------------------ * | 0 | 1 | * ------------------------------ * | 2 | 3 | * ------------------------------ * | 4 | 5 | * ------------------------------ * * Vertical resizers should have same StartColumn and different StartRow. * Horizontal resizers should have same StartRow and different StartColumn. * Difference between rows or colums should be more than 1. */ foreach (GridResizer r in _resizers) { if (r.Orientation == resizer.Orientation) { bool isHorizontalSnapped = resizer.Orientation == Orientation.Horizontal && r.StartRow == resizer.StartRow && (Math.Abs(resizer.StartCol - r.StartCol) > 1); bool isVerticalSnapped = resizer.Orientation == Orientation.Vertical && r.StartCol == resizer.StartCol && (Math.Abs(resizer.StartRow - r.StartRow) > 1); if (isHorizontalSnapped || isVerticalSnapped) { return(true); } } } return(false); }
public int SwappedIndexAfterResize(GridResizer resizer) { if (resizer.Orientation == Orientation.Horizontal) { for (int i = 0; i < _model.Rows; i++) { if (_rowInfo[i].Percent < 0) { _rowInfo[i].Percent = -_rowInfo[i].Percent; _rowInfo[i - 1].Percent -= _rowInfo[i].Percent; _rowInfo[i + 1].Percent -= _rowInfo[i].Percent; _model.RowPercents[i - 1] = _rowInfo[i - 1].Percent; _model.RowPercents[i] = _rowInfo[i].Percent; _model.RowPercents[i + 1] = _rowInfo[i + 1].Percent; return(i); } } } else { for (int i = 1; i < _model.Columns; i++) { if (_colInfo[i].Percent < 0) { _colInfo[i - 1].Percent += _colInfo[i].Percent; _colInfo[i].Percent = -_colInfo[i].Percent; _model.ColumnPercents[i - 1] = _colInfo[i - 1].Percent; _model.ColumnPercents[i] = _colInfo[i].Percent; return(i); } } } return(-1); }
private void Resizer_DragDelta(object sender, System.Windows.Controls.Primitives.DragDeltaEventArgs e) { GridResizer resizer = (GridResizer)sender; int[] percents; RowColInfo[] info; int index = resizer.Index; double delta; if (resizer.Orientation == Orientation.Vertical) { percents = Model.ColumnPercents; info = _colInfo; delta = e.HorizontalChange; } else { percents = Model.RowPercents; info = _rowInfo; delta = e.VerticalChange; } double currentExtent = info[index].Extent; double newExtent = currentExtent + delta; int currentPercent = info[index].Percent; int totalPercent = currentPercent + info[index + 1].Percent; int newPercent = (int)(currentPercent * newExtent / currentExtent); if ((newPercent > 0) && (newPercent < totalPercent)) { percents[index] = info[index].Percent = newPercent; percents[index + 1] = info[index + 1].Percent = totalPercent - newPercent; Size actualSize = new Size(ActualWidth, ActualHeight); ArrangeGridRects(actualSize); } }
public void DragResizer(GridResizer resizer, ResizeInfo data) { List <RowColInfo> info; List <int> percents; int index; if (resizer.Orientation == Orientation.Vertical) { info = _colInfo; percents = _model.ColumnPercents; index = resizer.StartCol; } else { info = _rowInfo; percents = _model.RowPercents; index = resizer.StartRow; } int nextPercent = data.CurrentPercent + data.AdjacentPercent + info[index + 1].Percent; percents[index] = info[index].Percent = data.NewPercent - data.AdjacentPercent; percents[index + 1] = info[index + 1].Percent = nextPercent - data.NewPercent; }
public ResizeInfo CalculateResizeInfo(GridResizer resizer, double delta) { ResizeInfo res = new ResizeInfo(); int rowIndex = resizer.StartRow; int colIndex = resizer.StartCol; int[,] indices = _model.CellChildMap; List <RowColInfo> info; List <int> percents; int index; if (resizer.Orientation == Orientation.Vertical) { res.CurrentExtent = _colInfo[colIndex].Extent; res.CurrentPercent = _colInfo[colIndex].Percent; info = _colInfo; percents = _model.ColumnPercents; index = colIndex; Func <int, bool> indexCmpr = (ind) => { bool sameIndices = true; for (int i = resizer.StartRow; i < resizer.EndRow && sameIndices; i++) { sameIndices &= indices[i, ind] == indices[i, ind - 1]; } return(sameIndices); }; res.CalcAdjacentZones(colIndex, _model.Columns, _colInfo, indexCmpr); } else { res.CurrentExtent = _rowInfo[rowIndex].Extent; res.CurrentPercent = _rowInfo[rowIndex].Percent; info = _rowInfo; percents = _model.RowPercents; index = rowIndex; Func <int, bool> indexCmpr = (ind) => { bool sameIndices = true; for (int i = resizer.StartCol; i < resizer.EndCol && sameIndices; i++) { sameIndices &= indices[ind, i] == indices[ind - 1, i]; } return(sameIndices); }; res.CalcAdjacentZones(rowIndex, _model.Rows, _rowInfo, indexCmpr); } res.FixAccuracyError(info, percents, delta > 0 ? index + 2 : index + 1); res.CalcNewPercent(delta); return(res); }
public void SplitOnDrag(GridResizer resizer, double delta, double space) { if (resizer.Orientation == Orientation.Vertical) { int rows = _model.Rows; int cols = _model.Columns + 1; int[,] cellChildMap = _model.CellChildMap; int[,] newCellChildMap = new int[rows, cols]; int draggedResizerStartCol = resizer.StartCol; if (delta > 0) { int sourceCol = 0; for (int col = 0; col < cols; col++) { for (int row = 0; row < rows; row++) { if (col == draggedResizerStartCol + 1 && (row < resizer.StartRow || row >= resizer.EndRow)) { newCellChildMap[row, col] = cellChildMap[row, sourceCol + 1]; } else { newCellChildMap[row, col] = cellChildMap[row, sourceCol]; } } if (col != draggedResizerStartCol) { sourceCol++; } } RowColInfo[] split = _colInfo[draggedResizerStartCol + 1].Split(delta, space); _colInfo[draggedResizerStartCol + 1] = split[0]; _colInfo.Insert(draggedResizerStartCol + 2, split[1]); _model.ColumnPercents[draggedResizerStartCol + 1] = split[0].Percent; _model.ColumnPercents.Insert(draggedResizerStartCol + 2, split[1].Percent); } else { int sourceCol = 0; for (int col = 0; col < cols; col++) { for (int row = 0; row < rows; row++) { if (col == draggedResizerStartCol + 1 && (row >= resizer.StartRow && row < resizer.EndRow)) { newCellChildMap[row, col] = cellChildMap[row, sourceCol + 1]; } else { newCellChildMap[row, col] = cellChildMap[row, sourceCol]; } } if (col != draggedResizerStartCol) { sourceCol++; } } double offset = _colInfo[draggedResizerStartCol].End - _colInfo[draggedResizerStartCol].Start + delta; RowColInfo[] split = _colInfo[draggedResizerStartCol].Split(offset - space, space); _colInfo[draggedResizerStartCol] = split[1]; _colInfo.Insert(draggedResizerStartCol + 1, split[0]); _model.ColumnPercents[draggedResizerStartCol] = split[1].Percent; _model.ColumnPercents.Insert(draggedResizerStartCol + 1, split[0].Percent); } FixAccuracyError(_colInfo, _model.ColumnPercents); _model.CellChildMap = newCellChildMap; _model.Columns++; } else { int rows = _model.Rows + 1; int cols = _model.Columns; int[,] cellChildMap = _model.CellChildMap; int[,] newCellChildMap = new int[rows, cols]; int draggedResizerStartRow = resizer.StartRow; if (delta > 0) { int sourceRow = 0; for (int row = 0; row < rows; row++) { for (int col = 0; col < cols; col++) { if (row == draggedResizerStartRow + 1 && (col < resizer.StartCol || col >= resizer.EndCol)) { newCellChildMap[row, col] = cellChildMap[sourceRow + 1, col]; } else { newCellChildMap[row, col] = cellChildMap[sourceRow, col]; } } if (row != draggedResizerStartRow) { sourceRow++; } } RowColInfo[] split = _rowInfo[draggedResizerStartRow + 1].Split(delta, space); _rowInfo[draggedResizerStartRow + 1] = split[0]; _rowInfo.Insert(draggedResizerStartRow + 2, split[1]); _model.RowPercents[draggedResizerStartRow + 1] = split[0].Percent; _model.RowPercents.Insert(draggedResizerStartRow + 2, split[1].Percent); } else { int sourceRow = 0; for (int row = 0; row < rows; row++) { for (int col = 0; col < cols; col++) { if (row == draggedResizerStartRow + 1 && (col >= resizer.StartCol && col < resizer.EndCol)) { newCellChildMap[row, col] = cellChildMap[sourceRow + 1, col]; } else { newCellChildMap[row, col] = cellChildMap[sourceRow, col]; } } if (row != draggedResizerStartRow) { sourceRow++; } } double offset = _rowInfo[draggedResizerStartRow].End - _rowInfo[draggedResizerStartRow].Start + delta; RowColInfo[] split = _rowInfo[draggedResizerStartRow].Split(offset - space, space); _rowInfo[draggedResizerStartRow] = split[0]; _rowInfo.Insert(draggedResizerStartRow + 1, split[1]); _model.RowPercents[draggedResizerStartRow] = split[0].Percent; _model.RowPercents.Insert(draggedResizerStartRow + 1, split[1].Percent); } FixAccuracyError(_rowInfo, _model.RowPercents); _model.CellChildMap = newCellChildMap; _model.Rows++; } _model.UpdatePreview(); }
public void UpdateAfterDetach(GridResizer resizer, double delta) { bool isDeltaNegative = delta < 0; Orientation orientation = resizer.Orientation; foreach (GridResizer r in _resizers) { bool notEqual = r.StartRow != resizer.StartRow || r.EndRow != resizer.EndRow || r.StartCol != resizer.StartCol || r.EndCol != resizer.EndCol; if (r.Orientation == orientation && notEqual) { if (orientation == Orientation.Horizontal) { if (r.StartRow > resizer.StartRow || (r.StartRow == resizer.StartRow && isDeltaNegative)) { r.StartRow++; } if (r.EndRow > resizer.EndRow || (r.EndRow == resizer.EndRow && isDeltaNegative)) { r.EndRow++; } } else { if (r.StartCol > resizer.StartCol || (r.StartCol == resizer.StartCol && isDeltaNegative)) { r.StartCol++; } if (r.EndCol > resizer.EndCol || (r.EndCol == resizer.EndCol && isDeltaNegative)) { r.EndCol++; } } } } if (!isDeltaNegative) { IncreaseResizerValues(resizer, orientation); } foreach (GridResizer r in _resizers) { if (r.Orientation != orientation) { if (orientation == Orientation.Vertical) { if (isDeltaNegative) { bool isRowNonAdjacent = r.EndRow <resizer.StartRow || r.StartRow> resizer.EndRow; if (r.StartCol > resizer.StartCol + 1 || (r.StartCol == resizer.StartCol + 1 && isRowNonAdjacent)) { r.StartCol++; } if (r.EndCol > resizer.EndCol || (r.EndCol == resizer.EndCol && isRowNonAdjacent)) { r.EndCol++; } } else { if (r.StartCol > resizer.StartCol || (r.StartCol == resizer.StartCol && r.StartRow >= resizer.StartRow && r.EndRow <= resizer.EndRow)) { r.StartCol++; } if (r.EndCol > resizer.EndCol - 1 || (r.EndCol == resizer.EndCol - 1 && r.StartRow >= resizer.StartRow && r.EndRow <= resizer.EndRow)) { r.EndCol++; } } } else { if (isDeltaNegative) { bool isColNonAdjacent = r.EndCol <resizer.StartCol || r.StartCol> resizer.EndCol; if (r.StartRow > resizer.StartRow + 1 || (r.StartRow == resizer.StartRow + 1 && isColNonAdjacent)) { r.StartRow++; } if (r.EndRow > resizer.EndRow || (r.EndRow == resizer.EndRow && isColNonAdjacent)) { r.EndRow++; } } else { if (r.StartRow > resizer.StartRow || (r.StartRow == resizer.StartRow && r.StartCol >= resizer.StartCol && r.EndCol <= resizer.EndCol)) { r.StartRow++; } if (r.EndRow > resizer.EndRow - 1 || (r.EndRow == resizer.EndRow - 1 && r.StartCol >= resizer.StartCol && r.EndCol <= resizer.EndCol)) { r.EndRow++; } } } } } }
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); } }
private void Resizer_DragDelta(object sender, System.Windows.Controls.Primitives.DragDeltaEventArgs e) { MergeCancelClick(null, null); _dragX += e.HorizontalChange; _dragY += e.VerticalChange; GridResizer resizer = (GridResizer)sender; int resizerIndex = AdornerLayer.Children.IndexOf(resizer); Size actualSize = WorkAreaSize(); int delta; if (resizer.Orientation == Orientation.Vertical) { delta = Convert.ToInt32(_dragX / actualSize.Width * GridData.Multiplier); } else { delta = Convert.ToInt32(_dragY / actualSize.Height * GridData.Multiplier); } if (_data.CanDrag(resizerIndex, delta)) { // Just update the UI, don't tell _data if (resizer.Orientation == Orientation.Vertical) { _data.Resizers[resizerIndex].PositiveSideIndices.ForEach((zoneIndex) => { var zone = Preview.Children[zoneIndex]; Canvas.SetLeft(zone, Canvas.GetLeft(zone) + e.HorizontalChange); (zone as GridZone).MinWidth -= e.HorizontalChange; }); _data.Resizers[resizerIndex].NegativeSideIndices.ForEach((zoneIndex) => { var zone = Preview.Children[zoneIndex]; Canvas.SetRight(zone, Canvas.GetRight(zone) + e.HorizontalChange); (zone as GridZone).MinWidth += e.HorizontalChange; }); Canvas.SetLeft(resizer, Canvas.GetLeft(resizer) + e.HorizontalChange); } else { _data.Resizers[resizerIndex].PositiveSideIndices.ForEach((zoneIndex) => { var zone = Preview.Children[zoneIndex]; Canvas.SetTop(zone, Canvas.GetTop(zone) + e.VerticalChange); (zone as GridZone).MinHeight -= e.VerticalChange; }); _data.Resizers[resizerIndex].NegativeSideIndices.ForEach((zoneIndex) => { var zone = Preview.Children[zoneIndex]; Canvas.SetBottom(zone, Canvas.GetBottom(zone) + e.VerticalChange); (zone as GridZone).MinHeight += e.VerticalChange; }); Canvas.SetTop(resizer, Canvas.GetTop(resizer) + e.VerticalChange); } foreach (var child in AdornerLayer.Children) { GridResizer resizerThumb = child as GridResizer; if (resizerThumb != resizer) { PlaceResizer(resizerThumb); } } } else { // Undo changes _dragX -= e.HorizontalChange; _dragY -= e.VerticalChange; } }
private bool UpdateDragHandlerForExistingSplit(Orientation orientation, Func <GridResizer, bool> cmpr, Func <GridResizer, bool> endCmpr, Func <GridResizer, bool> startCmpr) { bool updCurrentResizers = false; GridResizer leftNeighbour = null; GridResizer rightNeighbour = null; for (int i = 0; i < _resizers.Count && (leftNeighbour == null || rightNeighbour == null); i++) { GridResizer resizer = (GridResizer)_resizers[i]; if (cmpr(resizer)) { if (leftNeighbour == null && endCmpr(resizer)) { leftNeighbour = resizer; updCurrentResizers = true; } if (rightNeighbour == null && startCmpr(resizer)) { rightNeighbour = resizer; updCurrentResizers = true; } } } if (updCurrentResizers) { if (leftNeighbour != null && rightNeighbour != null) { if (orientation == Orientation.Vertical) { leftNeighbour.EndRow = rightNeighbour.EndRow; } else { leftNeighbour.EndCol = rightNeighbour.EndCol; } _resizers.Remove(rightNeighbour); } else if (leftNeighbour != null) { if (orientation == Orientation.Vertical) { leftNeighbour.EndRow++; } else { leftNeighbour.EndCol++; } } else if (rightNeighbour != null) { if (orientation == Orientation.Vertical) { rightNeighbour.StartRow--; } else { rightNeighbour.StartCol--; } } } return(updCurrentResizers); }
public void UpdateAfterSwap(GridResizer resizer, double delta) { Orientation orientation = resizer.Orientation; bool isHorizontal = orientation == Orientation.Horizontal; bool isDeltaNegative = delta < 0; List <GridResizer> swappedResizers = new List <GridResizer>(); if (isDeltaNegative) { DecreaseResizerValues(resizer, orientation); } else { IncreaseResizerValues(resizer, orientation); } // same orientation resizers update foreach (GridResizer r in _resizers) { if (r.Orientation == orientation) { if ((isHorizontal && r.StartRow == resizer.StartRow && r.StartCol != resizer.StartCol) || (!isHorizontal && r.StartCol == resizer.StartCol && r.StartRow != resizer.StartRow)) { if (isDeltaNegative) { IncreaseResizerValues(r, orientation); } else { DecreaseResizerValues(r, orientation); } swappedResizers.Add(r); } } } // different orientation resizers update foreach (GridResizer r in _resizers) { if (r.Orientation != resizer.Orientation) { if (isHorizontal) { // vertical resizers corresponding to dragged resizer if (r.StartCol >= resizer.StartCol && r.EndCol < resizer.EndCol) { if (r.StartRow == resizer.StartRow + 2 && isDeltaNegative) { r.StartRow--; } if (r.EndRow == resizer.EndRow + 1 && isDeltaNegative) { r.EndRow--; } if (r.StartRow == resizer.StartRow && !isDeltaNegative) { r.StartRow++; } if (r.EndRow == resizer.EndRow - 1 && !isDeltaNegative) { r.EndRow++; } } else { // vertical resizers corresponding to swapped resizers foreach (GridResizer sr in swappedResizers) { if (r.StartCol >= sr.StartCol && r.EndCol <= sr.EndCol) { if (r.StartRow == resizer.StartRow + 1 && isDeltaNegative) { r.StartRow++; } if (r.EndRow == resizer.EndRow && isDeltaNegative) { r.EndRow++; } if (r.StartRow == resizer.StartRow + 1 && !isDeltaNegative) { r.StartRow--; } if (r.EndRow == resizer.EndRow && !isDeltaNegative) { r.EndRow--; } } } } } else { // horizontal resizers corresponding to dragged resizer if (r.StartRow >= resizer.StartRow && r.EndRow < resizer.EndRow) { if (r.StartCol == resizer.StartCol + 3 && isDeltaNegative) { r.StartCol--; } if (r.EndCol == resizer.EndCol + 1 && isDeltaNegative) { r.EndCol--; } if (r.StartCol == resizer.StartCol && !isDeltaNegative) { r.StartCol++; } if (r.EndCol == resizer.EndCol - 1 && !isDeltaNegative) { r.EndCol++; } } else { // horizontal resizers corresponding to swapped resizers foreach (GridResizer sr in swappedResizers) { if (r.StartRow >= sr.StartRow && r.EndRow <= sr.EndRow) { if (r.StartCol == resizer.StartCol + 1 && isDeltaNegative) { r.StartCol++; } if (r.EndCol == resizer.EndCol && isDeltaNegative) { r.EndCol++; } if (r.StartCol == resizer.StartCol + 1 && !isDeltaNegative) { r.StartCol--; } if (r.EndCol == resizer.EndCol && !isDeltaNegative) { r.EndCol--; } } } } } } } }
private void ArrangeGridRects(Size arrangeSize) { GridLayoutModel model = Model; if (model == null) { return; } Settings settings = ((App)Application.Current).ZoneSettings; int spacing, gutter; spacing = gutter = settings.ShowSpacing ? settings.Spacing : 0; int cols = model.Columns; int rows = model.Rows; double totalWidth = arrangeSize.Width - (gutter * 2) - (spacing * (cols - 1)); double totalHeight = arrangeSize.Height - (gutter * 2) - (spacing * (rows - 1)); double top = gutter; for (int row = 0; row < rows; row++) { double cellHeight = _rowInfo[row].Recalculate(top, totalHeight); top += cellHeight + spacing; } double left = gutter; for (int col = 0; col < cols; col++) { double cellWidth = _colInfo[col].Recalculate(left, totalWidth); left += cellWidth + spacing; } for (int row = 0; row < rows; row++) { for (int col = 0; col < cols; col++) { int i = model.CellChildMap[row, col]; if (((row == 0) || (model.CellChildMap[row - 1, col] != i)) && ((col == 0) || (model.CellChildMap[row, col - 1] != i))) { // this is not a continuation of a span GridZone zone = (GridZone)Preview.Children[i]; left = _colInfo[col].Start; top = _rowInfo[row].Start; Canvas.SetLeft(zone, left); Canvas.SetTop(zone, top); int maxRow = row; while (((maxRow + 1) < rows) && (model.CellChildMap[maxRow + 1, col] == i)) { maxRow++; } zone.HorizontalSnapPoints = null; if (maxRow > row) { zone.HorizontalSnapPoints = new double[maxRow - row]; int pointsIndex = 0; for (int walk = row; walk < maxRow; walk++) { zone.HorizontalSnapPoints[pointsIndex++] = _rowInfo[walk].End + (spacing / 2) - top; } } int maxCol = col; while (((maxCol + 1) < cols) && (model.CellChildMap[row, maxCol + 1] == i)) { maxCol++; } zone.VerticalSnapPoints = null; if (maxCol > col) { zone.VerticalSnapPoints = new double[maxCol - col]; int pointsIndex = 0; for (int walk = col; walk < maxCol; walk++) { zone.VerticalSnapPoints[pointsIndex++] = _colInfo[walk].End + (spacing / 2) - left; } } zone.MinWidth = _colInfo[maxCol].End - left; zone.MinHeight = _rowInfo[maxRow].End - top; } } } AddDragHandles(); int childIndex = 0; UIElementCollection adornerChildren = AdornerLayer.Children; for (int row = 0; row < rows - 1; row++) { GridResizer resizer = (GridResizer)adornerChildren[childIndex++]; int startCol = -1; int endCol = cols - 1; for (int col = 0; col < cols; col++) { if ((startCol == -1) && (model.CellChildMap[row, col] != model.CellChildMap[row + 1, col])) { startCol = col; } else if ((startCol != -1) && (model.CellChildMap[row, col] == model.CellChildMap[row + 1, col])) { endCol = col - 1; break; } } if (startCol != -1) { // hard coding this as (resizer.ActualHeight / 2) will still evaluate to 0 here ... a layout hasn't yet happened Canvas.SetTop(resizer, _rowInfo[row].End + (spacing / 2) - 24); Canvas.SetLeft(resizer, (_colInfo[endCol].End + _colInfo[startCol].Start) / 2); } else { resizer.Visibility = Visibility.Collapsed; } } for (int col = 0; col < cols - 1; col++) { GridResizer resizer = (GridResizer)adornerChildren[childIndex++]; int startRow = -1; int endRow = rows - 1; for (int row = 0; row < rows; row++) { if ((startRow == -1) && (model.CellChildMap[row, col] != model.CellChildMap[row, col + 1])) { startRow = row; } else if ((startRow != -1) && (model.CellChildMap[row, col] == model.CellChildMap[row, col + 1])) { endRow = row - 1; break; } } if (startRow != -1) { Canvas.SetLeft(resizer, _colInfo[col].End + (spacing / 2) - 24); // hard coding this as (resizer.ActualWidth / 2) will still evaluate to 0 here ... a layout hasn't yet happened Canvas.SetTop(resizer, (_rowInfo[endRow].End + _rowInfo[startRow].Start) / 2); } else { resizer.Visibility = Visibility.Collapsed; } } }