コード例 #1
0
ファイル: SerializedShape.cs プロジェクト: MaDDoXbr/Booster2D
 public void AddShape(VectorShape shape)
 {
     if (shape is CircleShape)
     {
         circles.Add(shape as CircleShape);
     }
     else if (shape is EllipseShape)
     {
         ellipses.Add(shape as EllipseShape);
     }
     else if (shape is PointShape)
     {
         points.Add(shape as PointShape);
     }
     else if (shape is PolyShape)
     {
         polys.Add(shape as PolyShape);
     }
     else if (shape is TextShape)
     {
         texts.Add(shape as TextShape);
     }
     else if (shape is CompoundShape)
     {
         AddShapes((shape as CompoundShape).components);
     }
     else
     {
         Debug.LogWarning("Unknown shape! " + shape);
     }
 }
コード例 #2
0
    private static VectorShape ParseContour(BezierContour contour, Matrix2D transform)
    {
        VectorShape vectorShape = PolyShape.Create(contour);

        vectorShape.TransformBy(transform);

        return(vectorShape);
    }
コード例 #3
0
        Point[] GetHull(VectorShape shape, Matrix matrix, int delta)
        {
            var vector = shape.Data;
            //vector.Transform(matrix);
            //return vector.PolygonHull(delta, true);

            var oldHull = vector.Hull(delta, true);

            matrix.Transform(oldHull);
            return(GDIConverter.Convert(oldHull));
        }
コード例 #4
0
    /// <summary>
    /// Create a Sprite icon out of a VectorShape.
    /// </summary>
    public static Sprite GetSprite(VectorShape shape)
    {
        Rect shapeBounds = shape.ShapeBounds;
        int  width       = Mathf.CeilToInt(shapeBounds.width);
        int  height      = Mathf.CeilToInt(shapeBounds.height);
        List <VectorUtils.Geometry> iconGeometry = shape.ShapeGeometry;

        Sprite sprite = VectorUtils.BuildSprite(iconGeometry, 100f, VectorUtils.Alignment.Center, Vector2.zero, 128, true);

        return(sprite);
    }
コード例 #5
0
    /// <summary>
    /// Create a Texture2D icon out of a VectorShape.
    /// </summary>
    public static Texture2D GetIcon(VectorShape shape)
    {
        Rect shapeBounds = shape.ShapeBounds;
        int  width       = Mathf.CeilToInt(shapeBounds.width);
        int  height      = Mathf.CeilToInt(shapeBounds.height);
        List <VectorUtils.Geometry> iconGeometry = shape.ShapeGeometry;

        Sprite    sprite      = VectorUtils.BuildSprite(iconGeometry, 1f, VectorUtils.Alignment.Center, Vector2.zero, 128, true);
        Texture2D iconTexture = VectorUtils.RenderSpriteToTexture2D(sprite, width, height, renderMaterial);

        return(iconTexture);
    }
コード例 #6
0
    protected void SetFocusedShape(VectorShape shape)
    {
        focusedShape = shape;
        selection.Clear();

        if (shape != null)
        {
            if (!shapes.Contains(shape))
            {
                shapes.Add(shape);
            }
            selection.Add(shape);
        }
    }
コード例 #7
0
    /// <summary>
    /// Create a Texture2D icon out of a VectorShape.
    /// </summary>
    public static Texture2D GetIcon(VectorShape shape)
    {
        VectorUtils.TessellationOptions activeOptions = VectorShape.tessellationOptions;
        VectorShape.tessellationOptions = tessellationOptions;
        shape.Dirty = true;

        Rect shapeBounds = shape.ShapeBounds;
        int  width       = Mathf.CeilToInt(shapeBounds.width);
        int  height      = Mathf.CeilToInt(shapeBounds.height);

        renderMesh = shape.ShapeMesh;

        // Save the render state and get a temporary render texture
        VerifyCamera();
        RenderTexture activeTexture = RenderTexture.active;
        RenderTexture tempTexture   = RenderTexture.GetTemporary(width, height, 8, RenderTextureFormat.ARGB32);;

        // Activate the render texture and draw the mesh into it
        renderCamera.orthographicSize   = shapeBounds.height / 2;
        renderCamera.transform.position = new Vector3(shapeBounds.center.x, shapeBounds.center.y, -1);

        renderCamera.targetTexture = tempTexture;

        Camera.onPostRender += OnPostRender;

        renderCamera.Render();

        Camera.onPostRender -= OnPostRender;

        RenderTexture.active = renderCamera.targetTexture;

        Texture2D iconTexture = new Texture2D(width, height);

        iconTexture.ReadPixels(new Rect(0, 0, width, height), 0, 0);
        iconTexture.Apply();

        // Restore the render state and release the temporary objects
        RenderTexture.active       = activeTexture;
        renderCamera.targetTexture = activeTexture;
        RenderTexture.ReleaseTemporary(tempTexture);

        VectorShape.tessellationOptions = activeOptions;
        shape.Dirty = true;

        renderMesh = null;

        return(iconTexture);
    }
コード例 #8
0
    /// <summary>
    /// Add node and children into shape list.
    /// </summary>
    public static void RecurseSVGNodes(SceneNode node, Matrix2D nodeTransform, List <VectorShape> shapes)
    {
        if (node.Shapes != null)
        {
            foreach (Shape shape in node.Shapes)
            {
                VectorShape vectorShape = TryParseShapeToCircle(shape, nodeTransform);
                if (vectorShape == null)
                {
                    vectorShape = ParseShape(shape, nodeTransform);
                }
                if (vectorShape != null)
                {
                    shapes.Add(vectorShape);
                }
                //foreach (BezierContour contour in shape.Contours)
                //{
                //	VectorShape vectorShape = ParseContour(contour, nodeTransform);
                //	if (vectorShape != null)
                //	{
                //		shapes.Add(vectorShape);
                //		if (shape.PathProps.Stroke != null)
                //		{
                //			vectorShape.colorOutline = shape.PathProps.Stroke.Color;
                //			if ((vectorShape.colorOutline.g > 0.99f) && (vectorShape.colorOutline.b < Mathf.Epsilon) && (vectorShape.colorOutline.r < Mathf.Epsilon))
                //			{
                //				foreach (BezierPathSegment segment in contour.Segments)
                //				{
                //					Debug.Log(nodeTransform.MultiplyPoint(segment.P0).x);
                //				}
                //			}
                //		}
                //	}
                //}
            }
        }

        if (node.Children != null)
        {
            foreach (SceneNode child in node.Children)
            {
                Matrix2D childTransform = nodeTransform * child.Transform;
                RecurseSVGNodes(child, childTransform, shapes);
            }
        }
    }
コード例 #9
0
    /// <summary>
    /// Create a Texture2D icon out of a VectorShape (editor only version).
    /// </summary>
    public static Texture2D GetIcon(VectorShape shape, PreviewRenderUtility renderUtil)
    {
        VectorUtils.TessellationOptions activeOptions = VectorShape.tessellationOptions;
        VectorShape.tessellationOptions = tessellationOptions;
        shape.Dirty = true;

        Rect shapeBounds = shape.ShapeBounds;
        int  width       = Mathf.CeilToInt(shapeBounds.width);
        int  height      = Mathf.CeilToInt(shapeBounds.height);

        // Save the render state and get a temporary render texture
        RenderTexture activeTexture = RenderTexture.active;

        renderUtil.camera.targetTexture   = RenderTexture.GetTemporary(width * 2, height * 2, 8, RenderTextureFormat.ARGB32);
        renderUtil.camera.backgroundColor = Color.clear;

        // Activate the render texture and draw the shape into it
        RenderTexture.active = renderUtil.camera.targetTexture;
        float   cameraSize     = renderUtil.camera.orthographicSize;
        Vector3 cameraPosition = renderUtil.camera.transform.position;

        renderUtil.camera.orthographicSize   = shapeBounds.height / 2;
        renderUtil.camera.transform.position = new Vector3(shapeBounds.center.x, shapeBounds.center.y, -1);

        Matrix4x4 drawMatrix = Matrix4x4.identity;

        renderUtil.DrawMesh(shape.ShapeMesh, drawMatrix, renderMaterial, 0);

        renderUtil.camera.Render();
        renderUtil.camera.orthographicSize   = cameraSize;
        renderUtil.camera.transform.position = cameraPosition;
        Texture2D iconTexture = new Texture2D(width * 2, height * 2);

        iconTexture.ReadPixels(new Rect(0, 0, width * 2, height * 2), 0, 0);
        iconTexture.Apply();

        // Restore the render state and release the temporary render texture
        RenderTexture.active = activeTexture;
        RenderTexture.ReleaseTemporary(renderUtil.camera.targetTexture);

        VectorShape.tessellationOptions = activeOptions;
        shape.Dirty = true;

        return(iconTexture);
    }
コード例 #10
0
    protected bool OnSelectToolMouse(Event guiEvent)
    {
        selectionRect = Rect.zero;
        if (guiEvent.type == EventType.MouseDown)
        {
            dragActive        = false;
            mouseDownPosition = guiEvent.mousePosition;
            previousSelection = selection;
            selection         = new List <VectorShape>();
            focusedShape      = null;
        }
        else if (guiEvent.type == EventType.MouseDrag)
        {
            Vector2 shapeDownPosition = MouseToShapePoint(mouseDownPosition);
            Vector2 shapePosition     = MouseToShapePoint(mousePosition);

            float minX = Mathf.Min(shapeDownPosition.x, shapePosition.x);
            float maxX = Mathf.Max(shapeDownPosition.x, shapePosition.x);
            float minY = Mathf.Min(shapeDownPosition.y, shapePosition.y);
            float maxY = Mathf.Max(shapeDownPosition.y, shapePosition.y);

            selectionRect = new Rect(minX, minY, maxX - minX, maxY - minY);

            selection.Clear();
            foreach (VectorShape shape in shapes)
            {
                if (shape.IsInside(selectionRect))
                {
                    selection.Add(shape);
                }
            }

            Repaint();
        }
        else if (guiEvent.type == EventType.MouseUp)
        {
            if (selection.Count == 1)
            {
                focusedShape = selection[0];
            }
        }

        return(true);
    }
コード例 #11
0
        void PaintWidenPolygon(Graphics g, Point start, Size size, int i)
        {
            invPath.Reset();
            var shape = new VectorShape(
                new Vector(start.ToXwt(), (start + size).ToXwt()));

            invPath.AddLine(shape.Start.ToGdi(), shape.End.ToGdi());
            pathPen.EndCap = LineCap.ArrowAnchor;

            g.DrawPath(pathPen, invPath);
            var count = shape[Anchor.MostLeft].ToGdi();

            g.DrawString(i.ToString(), SD.SystemFonts.StatusFont, SystemBrushes.Control, count);

            invPath.Reset();
            invPath.AddPolygon(GetHull(shape, Camera.Matrix, 2));
            var save = g.Transform;

            g.Transform = new SD.Drawing2D.Matrix();
            g.DrawPath(pathPen, invPath);
            g.Transform = save;
        }
コード例 #12
0
    private static VectorShape ParseShape(Shape shape, Matrix2D transform)
    {
        VectorShape vectorShape = PolyShape.Create(shape, transform);

        return(vectorShape);
    }
コード例 #13
0
    protected bool OnBrushToolMouse(Event guiEvent)
    {
        Vector2 shapePosition = MouseToShapePoint(mousePosition);

        if (guiEvent.type == EventType.MouseDown)
        {
            dragActive = false;

            PolyShape activeLine = focusedShape as PolyShape;
            if (activeLine == null)
            {
                PolyShape newLine = PolyShape.Create(new Vector2[] { shapePosition });
                newLine.penToMeshScale = mouseToShapeScale;
                newLine.colorOutline   = shapeOutlineColor;
                newLine.colorFill      = shapeFillColor;
                SetFocusedShape(newLine);
            }
            else
            {
                activeLine.LineTo(shapePosition);
                if (guiEvent.clickCount > 1)
                {
                    focusedShape = null;
                }
            }
            Repaint();
        }
        else if (guiEvent.type == EventType.MouseDrag)
        {
            PolyShape activeLine = focusedShape as PolyShape;
            if ((activeLine != null) && (activeLine.vertices.Length > 1))
            {
                int     vertexIndex = activeLine.vertices.Length - 1;
                Vector2 offset;

                if ((!dragActive) && (Vector2.Distance(mouseDownPosition, guiEvent.mousePosition) > dragMargin))
                {
                    dragActive = true;
                    int previousIndex = vertexIndex - 1;

                    if (!activeLine.vertices[previousIndex].segmentCurves)
                    {
                        activeLine.vertices[previousIndex].segmentCurves = true;
                        offset = (activeLine.vertices[vertexIndex].position - activeLine.vertices[previousIndex].position) / 2f;
                        activeLine.vertices[previousIndex].exitCP = activeLine.vertices[previousIndex].position + offset;
                        if ((previousIndex < 1) || (!activeLine.vertices[previousIndex - 1].segmentCurves))
                        {
                            activeLine.vertices[previousIndex].enterCP = activeLine.vertices[previousIndex].position - offset;
                        }
                    }
                }

                if (dragActive)
                {
                    activeLine.vertices[vertexIndex].exitCP = shapePosition;
                    offset = activeLine.vertices[vertexIndex].exitCP - activeLine.vertices[vertexIndex].position;
                    activeLine.vertices[vertexIndex].enterCP = activeLine.vertices[vertexIndex].position - offset;

                    activeLine.Dirty = true;
                    Repaint();
                }
            }
        }

        return(true);
    }
コード例 #14
0
    /// <summary>
    /// Add a component shape.
    /// </summary>
    public void AddComponent(VectorShape shape)
    {
        _components.Add(shape);

        Dirty = true;
    }
コード例 #15
0
    /// <summary>
    /// Parse DXF into VectorShape list.
    /// </summary>
    public static List <VectorShape> ReadDXF(Stream dxfStream)
    {
        List <VectorShape> shapes = new List <VectorShape>();

        DxfFile dxfFile = DxfFile.Load(dxfStream);

        Dictionary <string, Color32> layerColors = new Dictionary <string, Color32>();

        foreach (DxfLayer layer in dxfFile.Layers)
        {
            layerColors.Add(layer.Name, ConvertColor(layer.Color));
        }

        foreach (DxfEntity entity in dxfFile.Entities)
        {
            VectorShape shape = null;

            switch (entity.EntityType)
            {
            case DxfEntityType.Point:
                DxfModelPoint point = entity as DxfModelPoint;
                shape = new PointShape(ConvertPoint(point.Location));
                break;

            case DxfEntityType.Line:
                DxfLine   line      = entity as DxfLine;
                Vector2[] endpoints = new Vector2[2];
                endpoints[0] = ConvertPoint(line.P1);
                endpoints[1] = ConvertPoint(line.P2);
                shape        = new PolyShape(endpoints);
                break;

            case DxfEntityType.Spline:
                DxfSpline spline = entity as DxfSpline;
                if ((spline.NumberOfControlPoints % spline.DegreeOfCurve) != 1)
                {
                    Debug.LogError("Invalid spline data! Wrong number of points. " + spline);
                    break;
                }

                Vector2[] controlPoints = new Vector2[spline.NumberOfControlPoints];
                for (int i = 0; i < controlPoints.Length; i++)
                {
                    controlPoints[i] = ConvertPoint(spline.ControlPoints[i]);
                }
                shape = new PolyShape(controlPoints[0]);
                PolyShape shapeSpline = shape as PolyShape;

                switch (spline.DegreeOfCurve)
                {
                case 1:

                    for (int i = 1; i < controlPoints.Length; i++)
                    {
                        shapeSpline.LineTo(controlPoints[i]);
                    }
                    break;

                case 2:
                    for (int i = 1; i < controlPoints.Length; i += 2)
                    {
                        shapeSpline.CurveTo(controlPoints[i + 1], controlPoints[i]);
                    }
                    break;

                case 3:
                    for (int i = 1; i < controlPoints.Length; i += 3)
                    {
                        shapeSpline.CurveTo(controlPoints[i + 2], controlPoints[i], controlPoints[i + 1]);
                    }
                    break;

                default:
                    Debug.LogWarning("Spline with unsupported curve of degree: " + spline.DegreeOfCurve);
                    break;
                }
                break;

            case DxfEntityType.Arc:
                DxfArc arc = entity as DxfArc;
                // If the arc is a complete circle just make one of those
                float startAngle = (float)arc.StartAngle;
                while (startAngle < 0f)
                {
                    startAngle += 360f;
                }
                float endAngle = (float)arc.EndAngle;
                while (endAngle < startAngle)
                {
                    endAngle += 360f;
                }

                float sweep = endAngle - startAngle;
                shape = new CircleShape(ConvertPoint(arc.Center), (float)arc.Radius, startAngle, sweep);
                break;

            case DxfEntityType.Circle:
                DxfCircle circle = entity as DxfCircle;
                shape = new CircleShape(ConvertPoint(circle.Center), (float)circle.Radius * dxfScale);
                break;

            case DxfEntityType.Ellipse:
                DxfEllipse ellipse = entity as DxfEllipse;
                // If the ellipse is actually a circle just make one of those
                if (Mathf.Approximately((float)ellipse.MinorAxisRatio, 1f))
                {
                    shape = new CircleShape(ConvertPoint(ellipse.Center), (float)ellipse.MajorAxis.Length * dxfScale);
                }
                else
                {
                    shape = new EllipseShape(ConvertPoint(ellipse.Center), ConvertVector(ellipse.MajorAxis), (float)ellipse.MinorAxisRatio);
                }
                break;

            case DxfEntityType.Polyline:
                DxfPolyline polyline = entity as DxfPolyline;
                if (polyline.ContainsVertices)
                {
                    Vector2[] vertices = new Vector2[polyline.Vertices.Count];
                    for (int i = 0; i < vertices.Length; i++)
                    {
                        vertices[i] = ConvertPoint(polyline.Vertices[i].Location);
                    }

                    shape = new PolyShape(vertices[0]);
                    PolyShape shapePolyline = shape as PolyShape;

                    for (int i = 1; i < vertices.Length; i++)
                    {
                        float bulge = (float)polyline.Vertices[i - 1].Bulge;
                        shapePolyline.ArcToDXF(vertices[i], bulge);
                    }

                    if (polyline.IsClosed)
                    {
                        float bulge = (float)polyline.Vertices[vertices.Length - 1].Bulge;
                        shapePolyline.ArcToDXF(vertices[0], bulge);
                        shape.Closed = true;
                    }
                }
                break;

            case DxfEntityType.LwPolyline:
            {
                DxfLwPolyline lwPolyline = entity as DxfLwPolyline;
                Vector2[]     vertices   = new Vector2[lwPolyline.Vertices.Count];
                for (int i = 0; i < vertices.Length; i++)
                {
                    DxfLwPolylineVertex lwpVertex = lwPolyline.Vertices[i];
                    vertices[i] = ConvertPoint(lwpVertex.X, lwpVertex.Y);
                }

                shape = new PolyShape(vertices[0]);
                PolyShape shapePolyline = shape as PolyShape;

                for (int i = 1; i < vertices.Length; i++)
                {
                    float bulge = (float)lwPolyline.Vertices[i - 1].Bulge;
                    shapePolyline.ArcToDXF(vertices[i], bulge);
                }

                if (lwPolyline.IsClosed)
                {
                    float bulge = (float)lwPolyline.Vertices[vertices.Length - 1].Bulge;
                    shapePolyline.ArcToDXF(vertices[0], bulge);
                    shape.Closed = true;
                }
            }
            break;

            default:
                Debug.Log("Unhandled entity of type: " + entity.EntityType);
                break;
            }

            if (shape != null)
            {
                if (entity.IsVisible)
                {
                    Color32 shapeColor = ConvertColor(entity.Color);
                    //layerColors.TryGetValue(entity.Layer, out shapeColor);

                    shape.colorOutline = shapeColor;
                    shapes.Add(shape);
                }
            }
        }

        return(shapes);
    }