protected override void OnCopyTo(ShapeInfo target) { base.OnCopyTo(target); PolyShapeInfo c = target as PolyShapeInfo; c.vertices = this.vertices != null ? (Vector2[])this.vertices.Clone() : null; }
public void OnInit(InitContext context) { if(context == InitContext.AddToGameObject) { this.env = (Environment)GameObj.GetComponent(typeof(Environment)); RigidBody body = (RigidBody)GameObj.GetComponent(typeof(RigidBody)); body.BodyType = BodyType.Static; Vector2[][] vectors = new Vector2[4][]; int triangleSideLength = env.TriangleSideLength; vectors[0] = new Vector2[3]; vectors[0][0] = new Vector2(-(triangleSideLength / 2), -(triangleSideLength / 2)); vectors[0][1] = new Vector2((triangleSideLength / 2), -(triangleSideLength / 2)); vectors[0][2] = new Vector2(0, 0); vectors[1] = new Vector2[3]; vectors[1][0] = new Vector2((triangleSideLength / 2), -(triangleSideLength / 2)); vectors[1][1] = new Vector2((triangleSideLength / 2), (triangleSideLength / 2)); vectors[1][2] = new Vector2(0, 0); vectors[2] = new Vector2[3]; vectors[2][0] = new Vector2((triangleSideLength / 2), (triangleSideLength / 2)); vectors[2][1] = new Vector2(-(triangleSideLength / 2), (triangleSideLength / 2)); vectors[2][2] = new Vector2(0, 0); vectors[3] = new Vector2[3]; vectors[3][0] = new Vector2(-(triangleSideLength / 2), (triangleSideLength / 2)); vectors[3][1] = new Vector2(-(triangleSideLength / 2), -(triangleSideLength / 2)); vectors[3][2] = new Vector2(0, 0); for (int x = 0; x < env.Triangles.GetLength(0); x++) { for (int y = 0; y < env.Triangles.GetLength(1); y++) { int i = 0; Vector2[] vertices = new Vector2[vectors[i].Length]; for (int v = 0; v < vectors[i].Length; v++) { vertices[v] = new Vector2(vectors[i][v].X + triangleSideLength / 2 + triangleSideLength * x, vectors[i][v].Y + triangleSideLength / 2 + triangleSideLength * y); } PolyShapeInfo shape = new PolyShapeInfo(vertices, 0); body.AddShape(shape); } } } }
protected override void OnMouseDown(MouseEventArgs e) { base.OnMouseDown(e); GameObject selGameObj = this.selectedBody != null ? this.selectedBody.GameObj : null; Transform selTransform = selGameObj != null ? selGameObj.Transform : null; if (selTransform == null) return; Vector3 spaceCoord = this.GetSpaceCoord(new Vector3(e.X, e.Y, selTransform.Pos.Z)); Vector2 localPos = selTransform.GetLocalPoint(spaceCoord).Xy; if ((this.SnapToUserGuides & UserGuideType.Position) != UserGuideType.None) { localPos = this.EditingUserGuide.SnapPosition(localPos); } if (this.mouseState == CursorState.CreateCircle) { #region CreateCircle if (e.Button == MouseButtons.Left) { CircleShapeInfo newShape = new CircleShapeInfo(1.0f, localPos, 1.0f); UndoRedoManager.BeginMacro(); UndoRedoManager.Do(new CreateRigidBodyShapeAction(this.selectedBody, newShape)); this.createAction = true; this.LeaveCursorState(); this.SelectObjects(new[] { SelShape.Create(newShape) }); this.BeginAction(ObjectAction.Scale); } else if (e.Button == MouseButtons.Right) { this.LeaveCursorState(); } #endregion } else if (this.mouseState == CursorState.CreatePolygon) { #region CreatePolygon if (e.Button == MouseButtons.Left) { bool success = false; if (!this.allObjSel.Any(sel => sel is SelPolyShape)) { PolyShapeInfo newShape = new PolyShapeInfo(new Vector2[] { localPos, localPos, localPos }, 1.0f); UndoRedoManager.Do(new CreateRigidBodyShapeAction(this.selectedBody, newShape)); this.SelectObjects(new[] { SelShape.Create(newShape) }); this.createPolyIndex++; } else { SelPolyShape selPolyShape = this.allObjSel.OfType<SelPolyShape>().First(); PolyShapeInfo polyShape = selPolyShape.ActualObject as PolyShapeInfo; if (this.createPolyIndex <= 2 || MathF.IsPolygonConvex(polyShape.Vertices)) { Vector2 lockedPos = this.createPolyIndex > 0 ? polyShape.Vertices[this.createPolyIndex - 1] : Vector2.Zero; MathF.TransformCoord(ref lockedPos.X, ref lockedPos.Y, selTransform.Angle); MathF.TransformCoord(ref localPos.X, ref localPos.Y, selTransform.Angle); localPos = this.ApplyAxisLock(localPos, lockedPos); MathF.TransformCoord(ref localPos.X, ref localPos.Y, -selTransform.Angle); if (polyShape.Vertices.Length < PolyShapeInfo.MaxVertices) { List<Vector2> vertices = polyShape.Vertices.ToList(); vertices[this.createPolyIndex] = localPos; if (this.createPolyIndex >= vertices.Count - 1) vertices.Add(localPos); polyShape.Vertices = vertices.ToArray(); selPolyShape.UpdatePolyStats(); this.createPolyIndex++; } else { Vector2[] vertices = polyShape.Vertices; vertices[this.createPolyIndex] = localPos; polyShape.Vertices = vertices; selPolyShape.UpdatePolyStats(); this.LeaveCursorState(); } } } if (success) { DualityEditorApp.NotifyObjPropChanged(this, new ObjectSelection(this.selectedBody), ReflectionInfo.Property_RigidBody_Shapes); } } else if (e.Button == MouseButtons.Right) { if (this.allObjSel.Any(sel => sel is SelPolyShape)) { SelPolyShape selPolyShape = this.allObjSel.OfType<SelPolyShape>().First(); PolyShapeInfo polyShape = selPolyShape.ActualObject as PolyShapeInfo; List<Vector2> vertices = polyShape.Vertices.ToList(); vertices.RemoveAt(this.createPolyIndex); if (vertices.Count < 3 || this.createPolyIndex < 2) { this.DeleteObjects(new SelPolyShape[] { selPolyShape }); } else { polyShape.Vertices = vertices.ToArray(); selPolyShape.UpdatePolyStats(); } DualityEditorApp.NotifyObjPropChanged(this, new ObjectSelection(this.selectedBody), ReflectionInfo.Property_RigidBody_Shapes); } this.LeaveCursorState(); } #endregion } else if (this.mouseState == CursorState.CreateLoop) { #region CreateLoop if (e.Button == MouseButtons.Left) { bool success = false; if (!this.allObjSel.Any(sel => sel is SelLoopShape)) { LoopShapeInfo newShape = new LoopShapeInfo(new Vector2[] { localPos, localPos + Vector2.UnitX, localPos + Vector2.One }); UndoRedoManager.Do(new CreateRigidBodyShapeAction(this.selectedBody, newShape)); this.SelectObjects(new[] { SelShape.Create(newShape) }); success = true; } else { SelLoopShape selPolyShape = this.allObjSel.OfType<SelLoopShape>().First(); LoopShapeInfo polyShape = selPolyShape.ActualObject as LoopShapeInfo; List<Vector2> vertices = polyShape.Vertices.ToList(); Vector2 lockedPos = this.createPolyIndex > 0 ? vertices[this.createPolyIndex - 1] : Vector2.Zero; MathF.TransformCoord(ref lockedPos.X, ref lockedPos.Y, selTransform.Angle); MathF.TransformCoord(ref localPos.X, ref localPos.Y, selTransform.Angle); localPos = this.ApplyAxisLock(localPos, lockedPos); MathF.TransformCoord(ref localPos.X, ref localPos.Y, -selTransform.Angle); vertices[this.createPolyIndex] = localPos; if (this.createPolyIndex >= vertices.Count - 1) vertices.Add(localPos); polyShape.Vertices = vertices.ToArray(); selPolyShape.UpdateLoopStats(); success = true; } if (success) { this.createPolyIndex++; DualityEditorApp.NotifyObjPropChanged(this, new ObjectSelection(this.selectedBody), ReflectionInfo.Property_RigidBody_Shapes); } } else if (e.Button == MouseButtons.Right) { if (this.allObjSel.Any(sel => sel is SelLoopShape)) { SelLoopShape selPolyShape = this.allObjSel.OfType<SelLoopShape>().First(); LoopShapeInfo polyShape = selPolyShape.ActualObject as LoopShapeInfo; List<Vector2> vertices = polyShape.Vertices.ToList(); vertices.RemoveAt(this.createPolyIndex); if (vertices.Count < 3 || this.createPolyIndex < 2) { this.DeleteObjects(new SelLoopShape[] { selPolyShape }); } else { polyShape.Vertices = vertices.ToArray(); selPolyShape.UpdateLoopStats(); } DualityEditorApp.NotifyObjPropChanged(this, new ObjectSelection(this.selectedBody), ReflectionInfo.Property_RigidBody_Shapes); } this.LeaveCursorState(); } #endregion } }
public SelVertex(PolyShapeInfo shape, int vertexIndex) { Guard.NotNull(shape, "A null shape was selected"); this.shape = shape; this.index = vertexIndex; }
public SelPolyShape(PolyShapeInfo shape) : base(shape) { this.poly = shape; this.UpdatePolyStats(); }
public RigidBodyEditorSelPolyShape(PolyShapeInfo shape) : base(shape) { }
private void DrawShapeOutline(Canvas canvas, Transform transform, PolyShapeInfo shape) { Vector3 pos = transform.Pos; float angle = transform.Angle; float scale = transform.Scale; if (this.wrapTexture) { Rect pointBoundingRect = shape.Vertices.BoundingBox(); canvas.State.TextureCoordinateRect = new Rect( pointBoundingRect.W / canvas.State.TextureBaseSize.X, pointBoundingRect.H / canvas.State.TextureBaseSize.Y); } canvas.State.TransformAngle = angle; canvas.State.TransformScale = new Vector2(scale, scale); canvas.FillPolygonOutline(shape.Vertices, this.outlineWidth, pos.X, pos.Y, pos.Z); }