//---------------------------------------------------------// /// <summary> /// Creates puzzle pieces from a visual, and adds them into the ScatterView. /// </summary> /// <param name="visual"></param> void LoadVisualAsPuzzle(Visual visual, Direction fromDirection) { // The more columns/rows, the less each piece needs to overlap float rowOverlap = PuzzleManager.Overlap / rowCount; float colOverlap = PuzzleManager.Overlap / colCount; puzzleBrush = new VisualBrush(visual); // Tell the puzzle manager to load a puzzle with the specified dimensions puzzleManager.LoadPuzzle(colCount, rowCount); for (int row = 0; row < rowCount; row++) { for (int column = 0; column < colCount; column++) { // Calculate the size of the rectangle that will be used to create a viewbox into the puzzle image. // The size is specified as a percentage of the total image size. float boxLeft = (float)column / (float)colCount; float boxTop = (float)row / (float)rowCount; float boxWidth = 1f / colCount; float boxHeight = 1f / rowCount; // Items in column 0 don't have any male puzzle parts on their side, all others do if (column != 0) { boxLeft -= colOverlap; boxWidth += colOverlap; } // Items in row 0 don't have any male puzzle parts on their top, all others do if (row != 0) { boxTop -= rowOverlap; boxHeight += rowOverlap; } // Make a visual brush based on the rectangle that was just calculated. VisualBrush itemBrush = new VisualBrush(visual); itemBrush.Viewbox = new Rect(boxLeft, boxTop, boxWidth, boxHeight); itemBrush.ViewboxUnits = BrushMappingMode.RelativeToBoundingBox; // Get the shape of the piece Geometry shape = GetPieceGeometry(column, row); // Put the brush into a puzzle piece PuzzlePiece piece = new PuzzlePiece(column + (colCount * row), shape, itemBrush); // Add the PuzzlePiece to a ScatterViewItem SSC.ScatterViewItem item = new SSC.ScatterViewItem(); item.Content = piece; // Set the initial size of the item and prevent it from being resized item.Width = Math.Round(piece.ClipShape.Bounds.Width, 0); item.Height = Math.Round(piece.ClipShape.Bounds.Height, 0); item.CanScale = false; // Set the ability to rotate based on the difficulty selected item.CanRotate = allowRotation; // Set the item's data context so it can use the piece's shape Binding binding = new Binding(); binding.Source = piece; item.SetBinding(ScatterViewItem.DataContextProperty, binding); // Animate the item into view AddPiece(item, fromDirection); } } }
//---------------------------------------------------------// /// <summary> /// Creates puzzle pieces from a visual, and adds them into the ScatterView. /// </summary> /// <param name="visual"></param> void LoadVisualAsPuzzle(Visual visual, Direction fromDirection) { // The more columns/rows, the less each piece needs to overlap float rowOverlap = PuzzleManager.Overlap / rowCount; float colOverlap = PuzzleManager.Overlap / colCount; puzzleBrush = new VisualBrush(visual); // Tell the puzzle manager to load a puzzle with the specified dimensions puzzleManager.LoadPuzzle(colCount, rowCount); for (int row = 0; row < rowCount; row++) { for (int column = 0; column < colCount; column++) { // Calculate the size of the rectangle that will be used to create a viewbox into the puzzle image. // The size is specified as a percentage of the total image size. float boxLeft = (float) column / (float)colCount; float boxTop = (float) row / (float)rowCount; float boxWidth = 1f / colCount; float boxHeight = 1f / rowCount; // Items in column 0 don't have any male puzzle parts on their side, all others do if (column != 0) { boxLeft -= colOverlap; boxWidth += colOverlap; } // Items in row 0 don't have any male puzzle parts on their top, all others do if (row != 0) { boxTop -= rowOverlap; boxHeight += rowOverlap; } // Make a visual brush based on the rectangle that was just calculated. VisualBrush itemBrush = new VisualBrush(visual); itemBrush.Viewbox = new Rect(boxLeft, boxTop, boxWidth, boxHeight); itemBrush.ViewboxUnits = BrushMappingMode.RelativeToBoundingBox; // Get the shape of the piece Geometry shape = GetPieceGeometry(column, row); // Put the brush into a puzzle piece PuzzlePiece piece = new PuzzlePiece( column + (colCount * row), shape, itemBrush); // Add the PuzzlePiece to a ScatterViewItem SSC.ScatterViewItem item = new SSC.ScatterViewItem(); item.Content = piece; // Set the initial size of the item and prevent it from being resized item.Width = Math.Round(piece.ClipShape.Bounds.Width, 0); item.Height = Math.Round(piece.ClipShape.Bounds.Height, 0); item.CanScale = false; // Set the item's data context so it can use the piece's shape Binding binding = new Binding(); binding.Source = piece; item.SetBinding(ScatterViewItem.DataContextProperty, binding); // Animate the item into view AddPiece(item, fromDirection); } } }