/// <summary> /// 用指定的序列化信息和上下文初始化 <see cref="JigsawPiece"/> 类的新实例。 /// </summary> /// <param name="info"><see cref="System.Runtime.Serialization.SerializationInfo"/> 对象, /// 包含序列化 <see cref="JigsawPiece"/> 所需的信息。</param> /// <param name="context"><see cref="System.Runtime.Serialization.StreamingContext"/> 对象, /// 该对象包含与 <see cref="JigsawPiece"/> 相关联的序列化流的源和目标。</param> /// <exception cref="System.ArgumentNullException">info 参数为 <c>null</c>。</exception> private JigsawPiece(SerializationInfo info, StreamingContext context) { if (info == null) { throw CommonExceptions.ArgumentNull("info"); } this.factory = ((JigsawSerializeContext)context.Context).Factory; this.shape = (Path)info.GetValue("Shape", typeof(Path)); this.pieceType = (JigsawPieceType)info.GetValue("Type", typeof(JigsawPieceType)); this.offset = (Vector2)info.GetValue("Offset", typeof(Vector2)); this.rotate = info.GetInt32("Rotate"); this.rotateRadian = (float)(this.rotate * Math.PI / 180); this.scale = info.GetSingle("Scale"); this.Next = (JigsawPiece)info.GetValue("Next", typeof(JigsawPiece)); this.Prev = (JigsawPiece)info.GetValue("Prev", typeof(JigsawPiece)); this.Frozen = info.GetBoolean("Frozen"); this.neighbors = (HashSet <JigsawPiece>)info.GetValue("Neighbors", typeof(HashSet <JigsawPiece>)); this.Visible = true; this.State = JigsawPieceState.None; // 重新填充路径。 this.originalPath = this.shape.GetGeometryGroup(factory); // 重新计算转换矩阵。 CalculateMatrix(); UpdatePath(); }
/// <summary> /// 将指定的拼图碎片与当前的拼图碎片合并。不检查两个拼图碎片是否可以被合并。 /// </summary> /// <param name="piece">要合并的拼图碎片。</param> public void Merge(JigsawPiece piece) { // 更新相邻拼图碎片信息。 foreach (JigsawPiece p in piece.neighbors) { p.neighbors.Remove(piece); p.neighbors.Add(this); } this.neighbors.UnionWith(piece.neighbors); this.neighbors.Remove(this); // 更新形状。 float sum = this.shape.Weight + piece.shape.Weight; this.offset = new Vector2((this.offset.X * this.shape.Weight + piece.offset.X * piece.shape.Weight) / sum, (offset.Y * this.shape.Weight + piece.offset.Y * piece.shape.Weight) / sum); this.shape.Merge(piece.shape); // 更新路径。 GeometryGroup newGroup = SharpDXUtility.Merge(this.originalPath, piece.originalPath); this.originalPath.Dispose(); this.originalPath = newGroup; this.CalculateMatrix(); this.UpdatePath(); if (piece.PieceType == JigsawPieceType.Border) { this.pieceType = JigsawPieceType.Border; } piece.Dispose(); }
/// <summary> /// 初始化拼图碎片列表。 /// </summary> /// <param name="factory">Direct2D 工厂。</param> /// <param name="shape">拼图形状。</param> private void InitializePieces(Factory factory, JigsawShape shape) { shape.GererateJigsawShape(); JigsawPiece[] pieces = new JigsawPiece[shape.HorizontalDimension]; JigsawPiece lastPiece = null; int idx = 0; for (int i = 0; i < shape.VerticalDimension; i++) { for (int j = 0; j < shape.HorizontalDimension; j++, idx++) { if (i == 0) { JigsawPiece currentPiece = new JigsawPiece(factory, shape.Paths[idx], JigsawPieceType.Border); items.Add(currentPiece); if (j == 0) { front = lastPiece = pieces[j] = currentPiece; } else { pieces[j] = currentPiece; currentPiece.Neighbors.Add(lastPiece); lastPiece.Neighbors.Add(currentPiece); currentPiece.Prev = lastPiece; lastPiece.Next = currentPiece; lastPiece = currentPiece; } } else { JigsawPieceType type = JigsawPieceType.Normal; if (j == 0 || i == shape.VerticalDimension - 1 || j == shape.HorizontalDimension - 1) { type = JigsawPieceType.Border; } JigsawPiece currentPiece = new JigsawPiece(factory, shape.Paths[idx], type); items.Add(currentPiece); if (j != 0) { currentPiece.Neighbors.Add(lastPiece); lastPiece.Neighbors.Add(currentPiece); } currentPiece.Prev = lastPiece; lastPiece.Next = currentPiece; currentPiece.Neighbors.Add(pieces[j]); pieces[j].Neighbors.Add(currentPiece); lastPiece = pieces[j] = currentPiece; } } } back = lastPiece; }
/// <summary> /// 初始化 <see cref="JigsawPiece"/> 类的新实例。 /// </summary> /// <param name="factory">Direc2D 工厂实例。</param> /// <param name="path">拼图碎片的轮廓路径。</param> /// <param name="type">拼图碎片的类型。</param> public JigsawPiece(Factory factory, Path path, JigsawPieceType type) { if (factory == null) { throw CommonExceptions.ArgumentNull("factory"); } if (path == null) { throw CommonExceptions.ArgumentNull("path"); } this.factory = factory; this.shape = path; this.originalPath = this.shape.GetGeometryGroup(this.factory); this.pieceType = type; this.Visible = true; this.Frozen = false; this.State = JigsawPieceState.None; UpdatePath(); }
/// <summary> /// 用指定的序列化信息和上下文初始化 <see cref="JigsawPiece"/> 类的新实例。 /// </summary> /// <param name="info"><see cref="System.Runtime.Serialization.SerializationInfo"/> 对象, /// 包含序列化 <see cref="JigsawPiece"/> 所需的信息。</param> /// <param name="context"><see cref="System.Runtime.Serialization.StreamingContext"/> 对象, /// 该对象包含与 <see cref="JigsawPiece"/> 相关联的序列化流的源和目标。</param> /// <exception cref="System.ArgumentNullException">info 参数为 <c>null</c>。</exception> private JigsawPiece(SerializationInfo info, StreamingContext context) { if (info == null) { throw CommonExceptions.ArgumentNull("info"); } this.factory = ((JigsawSerializeContext)context.Context).Factory; this.shape = (Path)info.GetValue("Shape", typeof(Path)); this.pieceType = (JigsawPieceType)info.GetValue("Type", typeof(JigsawPieceType)); this.offset = (Vector2)info.GetValue("Offset", typeof(Vector2)); this.rotate = info.GetInt32("Rotate"); this.rotateRadian = (float)(this.rotate * Math.PI / 180); this.scale = info.GetSingle("Scale"); this.Next = (JigsawPiece)info.GetValue("Next", typeof(JigsawPiece)); this.Prev = (JigsawPiece)info.GetValue("Prev", typeof(JigsawPiece)); this.Frozen = info.GetBoolean("Frozen"); this.neighbors = (HashSet<JigsawPiece>)info.GetValue("Neighbors", typeof(HashSet<JigsawPiece>)); this.Visible = true; this.State = JigsawPieceState.None; // 重新填充路径。 this.originalPath = this.shape.GetGeometryGroup(factory); // 重新计算转换矩阵。 CalculateMatrix(); UpdatePath(); }