private List <ShapeInfo> PickShapes(RigidBody body, Vector2 worldCoord, Vector2 worldSize)
        {
            Rect worldRect = new Rect(worldCoord.X, worldCoord.Y, worldSize.X, worldSize.Y);

            // Do a physical picking operation
            List <ShapeInfo> result = new List <ShapeInfo>();

            body.PickShapes(worldCoord, worldSize, result);

            // Special case for Loop- and ChainShapes, because they are by definition unpickable
            foreach (ShapeInfo shape in body.Shapes)
            {
                LoopShapeInfo  loop  = shape as LoopShapeInfo;
                ChainShapeInfo chain = shape as ChainShapeInfo;

                Vector2[] vertices = null;
                if (loop != null)
                {
                    vertices = loop.Vertices;
                }
                if (chain != null)
                {
                    vertices = chain.Vertices;
                }

                if (vertices != null && IsOutlineBoxIntersection(body.GameObj.Transform, vertices, worldRect))
                {
                    result.Add(shape);
                    continue;
                }
            }

            return(result);
        }
        private ShapeInfo PickShape(RigidBody body, Vector2 worldCoord)
        {
            // Special case for Loop- and ChainShapes, because they are by definition unpickable
            Rect worldRect = Rect.Align(Alignment.Center, worldCoord.X, worldCoord.Y, 10.0f, 10.0f);

            foreach (ShapeInfo shape in body.Shapes)
            {
                LoopShapeInfo  loop  = shape as LoopShapeInfo;
                ChainShapeInfo chain = shape as ChainShapeInfo;

                Vector2[] vertices = null;
                if (loop != null)
                {
                    vertices = loop.Vertices;
                }
                if (chain != null)
                {
                    vertices = chain.Vertices;
                }

                if (vertices != null && IsOutlineBoxIntersection(body.GameObj.Transform, vertices, worldRect))
                {
                    return(shape);
                }
            }

            // Do a physical picking operation
            return(body.PickShape(worldCoord));
        }
Ejemplo n.º 3
0
        protected internal override void OnCollectWorldOverlayDrawcalls(Canvas canvas)
        {
            base.OnCollectWorldOverlayDrawcalls(canvas);
            List <RigidBody> visibleColliders = this.QueryVisibleColliders().ToList();

            RigidBody selectedBody = this.QuerySelectedCollider();

            canvas.State.TextFont           = Font.GenericMonospace10;
            canvas.State.TextInvariantScale = true;
            canvas.State.ZOffset            = -0.5f;
            Font textFont = canvas.State.TextFont.Res;

            // Draw Shape layer
            foreach (RigidBody body in visibleColliders)
            {
                if (!body.Shapes.Any())
                {
                    continue;
                }
                float   colliderAlpha = body == selectedBody ? 1.0f : (selectedBody != null ? 0.25f : 0.5f);
                float   maxDensity    = body.Shapes.Max(s => s.Density);
                float   minDensity    = body.Shapes.Min(s => s.Density);
                float   avgDensity    = (maxDensity + minDensity) * 0.5f;
                Vector3 objPos        = body.GameObj.Transform.Pos;
                float   objAngle      = body.GameObj.Transform.Angle;
                float   objScale      = body.GameObj.Transform.Scale;
                int     index         = 0;
                foreach (ShapeInfo shape in body.Shapes)
                {
                    CircleShapeInfo circle = shape as CircleShapeInfo;
                    PolyShapeInfo   poly   = shape as PolyShapeInfo;
                    ChainShapeInfo  chain  = shape as ChainShapeInfo;
                    LoopShapeInfo   loop   = shape as LoopShapeInfo;

                    ObjectEditorCamViewState editorState = this.View.ActiveState as ObjectEditorCamViewState;
                    float     shapeAlpha      = colliderAlpha * (selectedBody == null || editorState == null || editorState.SelectedObjects.Any(sel => sel.ActualObject == shape) ? 1.0f : 0.5f);
                    float     densityRelative = MathF.Abs(maxDensity - minDensity) < 0.01f ? 1.0f : shape.Density / avgDensity;
                    ColorRgba clr             = shape.IsSensor ? this.ShapeSensorColor : this.ShapeColor;
                    ColorRgba fontClr         = this.FgColor;
                    Vector2   center          = Vector2.Zero;

                    if (!body.IsAwake)
                    {
                        clr = clr.ToHsva().WithSaturation(0.0f).ToRgba();
                    }
                    if (!shape.IsValid)
                    {
                        clr = this.ShapeErrorColor;
                    }

                    bool      fillShape     = (poly != null || circle != null);
                    Vector2[] shapeVertices = null;
                    if (poly != null)
                    {
                        shapeVertices = poly.Vertices;
                    }
                    else if (loop != null)
                    {
                        shapeVertices = loop.Vertices;
                    }
                    else if (chain != null)
                    {
                        shapeVertices = chain.Vertices;
                    }

                    if (circle != null)
                    {
                        Vector2 circlePos = circle.Position * objScale;
                        MathF.TransformCoord(ref circlePos.X, ref circlePos.Y, objAngle);

                        if (fillShape)
                        {
                            canvas.State.SetMaterial(new BatchInfo(DrawTechnique.Alpha, clr.WithAlpha((0.25f + densityRelative * 0.25f) * shapeAlpha)));
                            canvas.FillCircle(
                                objPos.X + circlePos.X,
                                objPos.Y + circlePos.Y,
                                objPos.Z,
                                circle.Radius * objScale);
                        }
                        canvas.State.SetMaterial(new BatchInfo(DrawTechnique.Alpha, clr.WithAlpha(shapeAlpha)));
                        canvas.DrawCircle(
                            objPos.X + circlePos.X,
                            objPos.Y + circlePos.Y,
                            objPos.Z,
                            circle.Radius * objScale);

                        center = circlePos;
                    }
                    else if (shapeVertices != null)
                    {
                        ColorRgba vertexFillColor    = canvas.State.ColorTint * clr.WithAlpha((0.25f + densityRelative * 0.25f) * shapeAlpha);
                        ColorRgba vertexOutlineColor = canvas.State.ColorTint * clr;

                        // Prepare vertices to submit. We can't use higher-level canvas functionality
                        // here, because we want direct control over the vertex mode.
                        float   viewSpaceScale = objScale;
                        Vector3 viewSpacePos   = objPos;
                        canvas.DrawDevice.PreprocessCoords(ref viewSpacePos, ref viewSpaceScale);
                        VertexC1P3T2[] drawVertices = new VertexC1P3T2[shapeVertices.Length];
                        for (int i = 0; i < drawVertices.Length; i++)
                        {
                            drawVertices[i].Pos.X = shapeVertices[i].X;
                            drawVertices[i].Pos.Y = shapeVertices[i].Y;
                            drawVertices[i].Pos.Z = 0.0f;
                            MathF.TransformCoord(ref drawVertices[i].Pos.X, ref drawVertices[i].Pos.Y, objAngle, viewSpaceScale);

                            drawVertices[i].Pos.X += viewSpacePos.X;
                            drawVertices[i].Pos.Y += viewSpacePos.Y;
                            drawVertices[i].Pos.Z += viewSpacePos.Z;
                            drawVertices[i].Color  = vertexOutlineColor;
                        }

                        // Calculate the center coordinate
                        for (int i = 0; i < drawVertices.Length; i++)
                        {
                            center += shapeVertices[i];
                        }
                        center /= shapeVertices.Length;
                        MathF.TransformCoord(ref center.X, ref center.Y, objAngle, objScale);

                        // Make sure to render using an alpha material
                        canvas.State.SetMaterial(new BatchInfo(DrawTechnique.Alpha, ColorRgba.White));

                        // Fill the shape
                        if (fillShape)
                        {
                            VertexC1P3T2[] fillVertices = drawVertices.Clone() as VertexC1P3T2[];
                            for (int i = 0; i < fillVertices.Length; i++)
                            {
                                fillVertices[i].Color = vertexFillColor;
                            }
                            canvas.DrawVertices(fillVertices, VertexMode.TriangleFan);
                        }

                        // Draw the outline
                        canvas.DrawVertices(drawVertices, shape is ChainShapeInfo ? VertexMode.LineStrip : VertexMode.LineLoop);
                    }

                    // Draw shape index
                    if (body == selectedBody)
                    {
                        string  indexText = index.ToString();
                        Vector2 textSize  = textFont.MeasureText(indexText);
                        canvas.State.SetMaterial(new BatchInfo(DrawTechnique.Alpha, fontClr.WithAlpha((shapeAlpha + 1.0f) * 0.5f)));
                        canvas.DrawText(indexText,
                                        objPos.X + center.X,
                                        objPos.Y + center.Y,
                                        objPos.Z);
                    }

                    index++;
                }

                // Draw center of mass
                if (body.BodyType == BodyType.Dynamic)
                {
                    Vector2 localMassCenter = body.LocalMassCenter;
                    MathF.TransformCoord(ref localMassCenter.X, ref localMassCenter.Y, objAngle, objScale);
                    canvas.State.SetMaterial(new BatchInfo(DrawTechnique.Alpha, this.MassCenterColor.WithAlpha(colliderAlpha)));
                    canvas.DrawLine(
                        objPos.X + localMassCenter.X - 5.0f,
                        objPos.Y + localMassCenter.Y,
                        objPos.Z,
                        objPos.X + localMassCenter.X + 5.0f,
                        objPos.Y + localMassCenter.Y,
                        objPos.Z);
                    canvas.DrawLine(
                        objPos.X + localMassCenter.X,
                        objPos.Y + localMassCenter.Y - 5.0f,
                        objPos.Z,
                        objPos.X + localMassCenter.X,
                        objPos.Y + localMassCenter.Y + 5.0f,
                        objPos.Z);
                }
            }
        }
 private void DrawShape(Canvas canvas, Transform transform, ChainShapeInfo shape, ColorRgba fillColor, ColorRgba outlineColor)
 {
     this.DrawPolygonOutline(canvas, transform, shape.Vertices, outlineColor, false);
 }
        protected internal override void OnCollectWorldOverlayDrawcalls(Canvas canvas)
        {
            base.OnCollectWorldOverlayDrawcalls(canvas);
            List <RigidBody> visibleColliders = this.QueryVisibleColliders().ToList();

            RigidBody selectedBody = this.QuerySelectedCollider();

            canvas.State.SetMaterial(new BatchInfo(DrawTechnique.Alpha, ColorRgba.White));
            canvas.State.TextFont           = Font.GenericMonospace10;
            canvas.State.TextInvariantScale = true;
            canvas.State.ZOffset            = this.depthOffset;
            Font textFont = canvas.State.TextFont.Res;

            // Retrieve selected shapes
            ObjectEditorCamViewState editorState = this.View.ActiveState as ObjectEditorCamViewState;

            object[] editorSelectedObjects = editorState != null?editorState.SelectedObjects.Select(item => item.ActualObject).ToArray() : new object[0];

            bool isAnyBodySelected  = (selectedBody != null);
            bool isAnyShapeSelected = isAnyBodySelected && editorSelectedObjects.OfType <ShapeInfo>().Any();

            // Draw Shape layer
            foreach (RigidBody body in visibleColliders)
            {
                if (!body.Shapes.Any())
                {
                    continue;
                }

                Vector3 objPos   = body.GameObj.Transform.Pos;
                float   objAngle = body.GameObj.Transform.Angle;
                float   objScale = body.GameObj.Transform.Scale;

                bool isBodySelected = (body == selectedBody);

                float bodyAlpha  = isBodySelected ? 1.0f : (isAnyBodySelected ? 0.5f : 1.0f);
                float maxDensity = body.Shapes.Max(s => s.Density);
                float minDensity = body.Shapes.Min(s => s.Density);
                float avgDensity = (maxDensity + minDensity) * 0.5f;

                int shapeIndex = 0;
                foreach (ShapeInfo shape in body.Shapes)
                {
                    CircleShapeInfo circle = shape as CircleShapeInfo;
                    PolyShapeInfo   poly   = shape as PolyShapeInfo;
                    ChainShapeInfo  chain  = shape as ChainShapeInfo;
                    LoopShapeInfo   loop   = shape as LoopShapeInfo;

                    bool isShapeSelected = isBodySelected && editorSelectedObjects.Contains(shape);

                    float     shapeAlpha      = bodyAlpha * (isShapeSelected ? 1.0f : (isAnyShapeSelected && isBodySelected ? 0.75f : 1.0f));
                    float     densityRelative = MathF.Abs(maxDensity - minDensity) < 0.01f ? 1.0f : shape.Density / avgDensity;
                    ColorRgba shapeColor      = shape.IsSensor ? this.ShapeSensorColor : this.ShapeColor;
                    ColorRgba fontColor       = this.FgColor;

                    if (!body.IsAwake)
                    {
                        shapeColor = shapeColor.ToHsva().WithSaturation(0.0f).ToRgba();
                    }
                    if (!shape.IsValid)
                    {
                        shapeColor = this.ShapeErrorColor;
                    }

                    // Draw the shape itself
                    ColorRgba fillColor    = shapeColor.WithAlpha((0.25f + densityRelative * 0.25f) * shapeAlpha);
                    ColorRgba outlineColor = ColorRgba.Lerp(shapeColor, fontColor, isShapeSelected ? 0.75f : 0.25f).WithAlpha(shapeAlpha);
                    this.DrawShape(canvas, body.GameObj.Transform, shape, fillColor, outlineColor);

                    // Calculate the center coordinate
                    Vector2 shapeCenter = Vector2.Zero;
                    if (circle != null)
                    {
                        shapeCenter = circle.Position * objScale;
                    }
                    else
                    {
                        Vector2[] shapeVertices = null;
                        if (poly != null)
                        {
                            shapeVertices = poly.Vertices;
                        }
                        else if (loop != null)
                        {
                            shapeVertices = loop.Vertices;
                        }
                        else if (chain != null)
                        {
                            shapeVertices = chain.Vertices;
                        }

                        for (int i = 0; i < shapeVertices.Length; i++)
                        {
                            shapeCenter += shapeVertices[i];
                        }

                        shapeCenter /= shapeVertices.Length;
                        MathF.TransformCoord(ref shapeCenter.X, ref shapeCenter.Y, objAngle, objScale);
                    }

                    // Draw shape index
                    if (body == selectedBody)
                    {
                        string  indexText = shapeIndex.ToString();
                        Vector2 textSize  = textFont.MeasureText(indexText);
                        canvas.State.ColorTint = fontColor.WithAlpha((shapeAlpha + 1.0f) * 0.5f);
                        canvas.DrawText(indexText,
                                        objPos.X + shapeCenter.X,
                                        objPos.Y + shapeCenter.Y,
                                        0.0f);
                    }

                    shapeIndex++;
                }

                // Draw center of mass
                if (body.BodyType == BodyType.Dynamic)
                {
                    Vector2 localMassCenter = body.LocalMassCenter;
                    MathF.TransformCoord(ref localMassCenter.X, ref localMassCenter.Y, objAngle, objScale);

                    float size = this.GetScreenConstantScale(canvas, 6.0f);

                    canvas.State.ColorTint = this.MassCenterColor.WithAlpha(bodyAlpha);
                    canvas.DrawLine(
                        objPos.X + localMassCenter.X - size,
                        objPos.Y + localMassCenter.Y,
                        0.0f,
                        objPos.X + localMassCenter.X + size,
                        objPos.Y + localMassCenter.Y,
                        0.0f);
                    canvas.DrawLine(
                        objPos.X + localMassCenter.X,
                        objPos.Y + localMassCenter.Y - size,
                        0.0f,
                        objPos.X + localMassCenter.X,
                        objPos.Y + localMassCenter.Y + size,
                        0.0f);
                }

                // Draw transform center
                if (body.BodyType == BodyType.Dynamic)
                {
                    float size = this.GetScreenConstantScale(canvas, 3.0f);
                    canvas.State.ColorTint = this.ObjectCenterColor.WithAlpha(bodyAlpha);
                    canvas.FillCircle(objPos.X, objPos.Y, 0.0f, size);
                }
            }
        }
Ejemplo n.º 6
0
 public RigidBodyEditorSelChainShape(ChainShapeInfo shape) : base(shape)
 {
 }