Exemplo n.º 1
0
        public static DrawingImage CreateImage(PointPattern[] pointPatterns, Size size, Color color)
        {
            if (pointPatterns == null)
                throw new Exception("You must provide a gesture before trying to generate a thumbnail");

            DrawingGroup drawingGroup = new DrawingGroup();
            for (int i = 0; i < pointPatterns.Length; i++)
            {
                PathGeometry pathGeometry = new PathGeometry();

                color.A = (byte)(0xFF - i * 0x55);
                SolidColorBrush brush = new SolidColorBrush(color);
                Pen drawingPen = new Pen(brush, 3 + i * 2) { StartLineCap = PenLineCap.Round, EndLineCap = PenLineCap.Round };

                if (pointPatterns[i].Points == null) return null;
                for (int j = 0; j < pointPatterns[i].Points.Count; j++)
                {
                    if (pointPatterns[i].Points[j].Count == 1)
                    {
                        Geometry ellipse = new EllipseGeometry(new Point(size.Width * j + size.Width / 2, size.Height / 2),
                            drawingPen.Thickness / 2, drawingPen.Thickness / 2);
                        pathGeometry.AddGeometry(ellipse);
                        continue;
                    }
                    StreamGeometry sg = new StreamGeometry { FillRule = FillRule.EvenOdd };
                    using (StreamGeometryContext sgc = sg.Open())
                    {
                        // Create new size object accounting for pen width
                        Size szeAdjusted = new Size(size.Width - drawingPen.Thickness - 1,
                            (size.Height - drawingPen.Thickness - 1));

                        Size scaledSize;
                        Point[] scaledPoints = ScaleGesture(pointPatterns[i].Points[j], szeAdjusted.Width - 10, szeAdjusted.Height - 10,
                            out scaledSize);

                        // Define size that will mark the offset to center the gesture
                        double iLeftOffset = (size.Width / 2) - (scaledSize.Width / 2);
                        double iTopOffset = (size.Height / 2) - (scaledSize.Height / 2);
                        Vector sizOffset = new Vector(iLeftOffset + j * size.Width, iTopOffset);
                        sgc.BeginFigure(Point.Add(scaledPoints[0], sizOffset), false, false);
                        foreach (Point p in scaledPoints)
                        {
                            sgc.LineTo(Point.Add(p, sizOffset), true, true);
                        }
                        DrawArrow(sgc, scaledPoints, sizOffset, drawingPen.Thickness);
                    }
                    sg.Freeze();
                    pathGeometry.AddGeometry(sg);
                }
                pathGeometry.Freeze();
                GeometryDrawing drawing = new GeometryDrawing(null, drawingPen, pathGeometry);
                drawing.Freeze();
                drawingGroup.Children.Add(drawing);
            }
            //  myPath.Data = sg;
            drawingGroup.Freeze();
            DrawingImage drawingImage = new DrawingImage(drawingGroup);
            drawingImage.Freeze();

            return drawingImage;
        }
Exemplo n.º 2
0
        public static List <IGesture> LoadGesturesFromFile(string filePath, bool throwException = false)
        {
            if (!File.Exists(filePath))
            {
                return(null);
            }

            FileManager.WaitFile(filePath);

            List <IGesture> gestureList = new List <IGesture>();

            try
            {
                string         json   = File.ReadAllText(filePath);
                JsonTextReader reader = new JsonTextReader(new StringReader(json));
                while (reader.Read())
                {
                    if (reader.TokenType == JsonToken.StartObject)
                    {
                        Gesture             gesture          = new Gesture();
                        List <PointPattern> pointPatternList = new List <PointPattern>();
                        while (reader.Read())
                        {
                            if (reader.TokenType != JsonToken.PropertyName)
                            {
                                continue;
                            }
                            switch ((string)reader.Value)
                            {
                            case nameof(Gesture.Name):
                            {
                                gesture.Name = reader.ReadAsString();
                                break;
                            }

                            case nameof(Gesture.PointPatterns):
                            {
                                while (reader.Read() && reader.TokenType != JsonToken.EndArray)
                                {
                                    if (reader.TokenType == JsonToken.StartArray)
                                    {
                                        var strokeList = new List <List <Point> >();
                                        while (reader.Read() && reader.TokenType != JsonToken.EndArray)
                                        {
                                            if (reader.TokenType == JsonToken.StartArray)
                                            {
                                                var stroke = new List <Point>();
                                                while (reader.Read() && reader.TokenType != JsonToken.EndArray)
                                                {
                                                    if (reader.TokenType == JsonToken.String)
                                                    {
                                                        var num = ((string)reader.Value).Split(',');
                                                        stroke.Add(new Point(Convert.ToInt32(num[0]), Convert.ToInt32(num[1])));
                                                    }
                                                }
                                                strokeList.Add(stroke);
                                            }
                                        }
                                        PointPattern pointPattern = new PointPattern(strokeList);
                                        pointPatternList.Add(pointPattern);
                                    }
                                }
                                gesture.PointPatterns = pointPatternList.ToArray();
                                break;
                            }
                            }
                            if (gesture.Name != null && gesture.PointPatterns != null)
                            {
                                gestureList.Add(gesture);
                                break;
                            }
                        }
                    }
                }
            }
            catch (Exception e)
            {
                Log.Logging.LogException(e);
                if (throwException)
                {
                    throw;
                }
                return(null);
            }

            return(gestureList);
        }
Exemplo n.º 3
0
 public Gesture(string name, PointPattern[] pointPatterns)
 {
     this.Name = name;
     this.PointPatterns = pointPatterns;
 }