private void btnUndo_Click(object sender, RoutedEventArgs e)
 {
     if (polylinePoints.Any())
     {
         polylinePoints.RemoveAt(polylinePoints.Count - 1);
     }
 }
Exemple #2
0
        private void AddNewTransition(string stateName)
        {
            ModelItem stateMachineModelItem = StateContainerEditor.GetStateMachineModelItem(this.parentStateModelItem);
            ModelItem toStateModelItem      = null;

            foreach (ModelItem stateModelItem in stateMachineModelItem.Properties[StateMachineDesigner.StatesPropertyName].Collection)
            {
                if (string.Equals(stateName, stateModelItem.Properties[StateDesigner.DisplayNamePropertyName].ComputedValue as string, StringComparison.Ordinal))
                {
                    toStateModelItem = stateModelItem;
                }
            }

            if (null == toStateModelItem)
            {
                return;
            }

            Fx.Assert(toStateModelItem != null, "To state cannot be null.");

            using (EditingScope editingScope = (EditingScope)this.ModelItem.BeginEdit(SR.CreateTransition))
            {
                ModelItem triggerModelItem = this.ModelItem.Properties[TriggerPropertyName].Value;
                State     toState          = toStateModelItem.GetCurrentValue() as State;

                ModelItem newTransitionItem = this.parentStateModelItem.Properties[StateDesigner.TransitionsPropertyName].Collection.Add(new Transition()
                {
                    Trigger     = null == triggerModelItem ? null : triggerModelItem.GetCurrentValue() as Activity,
                    DisplayName = StateContainerEditor.GenerateTransitionName(stateMachineModelItem),
                    To          = toState
                });

                this.ViewStateService.StoreViewState(newTransitionItem, ExpandViewStateKey, true);

                if (null == triggerModelItem)
                {
                    PointCollection thisPointCollection = this.ViewStateService.RetrieveViewState(this.ModelItem, StateContainerEditor.ConnectorLocationViewStateKey) as PointCollection;
                    if (null != thisPointCollection && thisPointCollection.Any())
                    {
                        PointCollection newTransitionViewState = new PointCollection
                        {
                            thisPointCollection[0]     // start point
                        };

                        if (toState == this.parentStateModelItem.GetCurrentValue())
                        {
                            // add an invalid destination point for self-transition, to force a reroute of the connection point
                            newTransitionViewState.Add(new Point(0, 0));
                        }

                        this.ViewStateService.StoreViewState(newTransitionItem, StateContainerEditor.ConnectorLocationViewStateKey, newTransitionViewState);
                    }
                }

                editingScope.Complete();
            }

            this.UpdateTransitionsSharingTrigger();
        }
        private int?FindNearestPointIndex(PointCollection points, Point newPoint)
        {
            if (points == null || !points.Any())
            {
                return(null);
            }

            Func <Point, Point, double> getLength = (p1, p2) => Math.Sqrt(Math.Pow(p1.X - p2.X, 2) + Math.Pow(p1.Y - p2.Y, 2));
            var items     = points.Select((p, i) => new { Point = p, Length = getLength(p, newPoint), Index = i });
            var minLength = items.Min(item => item.Length);

            return(items.First(item => item.Length == minLength).Index);
        }
Exemple #4
0
        protected override void OnRender(DrawingContext drawingContext)
        {
            // Drawing the arrow
            StreamGeometry streamGeometry = new StreamGeometry();

            using (StreamGeometryContext geometryContext = streamGeometry.Open())
            {
                if (_points != null && _points.Any())
                {
                    geometryContext.BeginFigure(_points[0], true, true);
                    geometryContext.PolyLineTo(_points.Where(p => _points.IndexOf(p) > 0).ToList(), true, true);
                }
            }

            // Draw the polygon visual
            drawingContext.DrawGeometry(Brushes.DarkOrchid, new Pen(_btn.Background, 0.5), streamGeometry);

            base.OnRender(drawingContext);
        }
Exemple #5
0
        /// <summary>
        /// 画贝塞尔曲线
        /// 曲线需要(开始点,结束点,控制点1,控制点2)
        /// </summary>
        /// <param name="canvas"></param>
        public static Path DrawPathBezierSegment(this Canvas canvas, PointCollection points, Color color, int ellipseSize = 10, bool isAddPoint = false)
        {
            if (!points.Any())
            {
                return(new Path());
            }
            var toDrawPoint = points.OrderBy(a => a.X).ToList();
            var pf          = new PathFigure {
                StartPoint = toDrawPoint[0]
            };

            var toForCount = toDrawPoint.Count - 1;

            for (var i = 0; i < toForCount; i++)
            {
                int current = i, last = i - 1, next = i + 1, next2 = i + 2;

                if (last == -1)
                {
                    last = 0;
                }
                if (next == toDrawPoint.Count)
                {
                    next = toForCount;
                }
                if (next2 == toDrawPoint.Count)
                {
                    next2 = toForCount;
                }
                var bzs = canvas.GetBezierSegment(toDrawPoint[current], toDrawPoint[last], toDrawPoint[next], toDrawPoint[next2], 0.7);
                pf.Segments.Add(bzs);
            }
            if (isAddPoint)
            {
                //画点的圆圈
                foreach (var lipt in toDrawPoint)
                {
                    var ellipse = new Ellipse
                    {
                        Width  = ellipseSize,
                        Height = ellipseSize,
                        Margin = new Thickness(lipt.X - ellipseSize / 2, lipt.Y, 0, 0),
                        HorizontalAlignment = HorizontalAlignment.Left,
                        VerticalAlignment   = VerticalAlignment.Top,
                        Fill    = Brushes.Red,
                        ToolTip = string.Format("x:{0},y:{1} ", lipt.X, lipt.Y)
                    };
                    canvas.Children.Add(ellipse);
                }
                ;
            }

            //添加曲线到图上
            var pfc = new PathFigureCollection {
                pf
            };
            var pg   = new PathGeometry(pfc);
            var path = new Path {
                StrokeThickness = 1, Stroke = new SolidColorBrush(color), Data = pg
            };

            canvas.Children.Add(path);
            return(path);
        }
Exemple #6
0
        /// <summary>
        /// 曲线绘制,并填充色
        /// </summary>
        /// <param name="canvas"></param>
        /// <param name="colorBrush"></param>
        /// <param name="fillBrush">并填充色</param>
        /// <param name="startPoint"></param>
        /// <param name="points"></param>
        /// <returns></returns>
        public static Path DrawPolylineAndFill(this Canvas canvas,
                                               Color colorBrush,
                                               Brush fillBrush,
                                               Point startPoint,
                                               PointCollection points)
        {
            Path currPath = new Path();

            if (!points.Any())
            {
                return(currPath);
            }
            currPath.Stroke = Brushes.Black;

            var endpoint = new Point()
            {
                X = points.Last().X + 10, Y = startPoint.Y
            };
            var newStartPoint = new Point()
            {
                X = startPoint.X, Y = startPoint.Y
            };

            var maxX = points.Max(a => a.X);
            var maxY = points.Max(a => a.Y);

            GradientStopCollection gradientStops = new GradientStopCollection()
            {
                new GradientStop()
                {
                    Color = Colors.LightSeaGreen, Offset = 1
                },
                new GradientStop()
                {
                    Color = Colors.LightPink, Offset = 0.8
                },
                new GradientStop()
                {
                    Color = Colors.Black, Offset = 0.2
                }
            };
            LinearGradientBrush linearGradientBrush = new LinearGradientBrush(gradientStops, 90);

            PathFigure pathFigureArrow = new PathFigure();

            pathFigureArrow.IsClosed   = true;
            pathFigureArrow.StartPoint = newStartPoint;
            foreach (var item in points)
            {
                var toadd = new Point(item.X + 10, item.Y + 5);
                pathFigureArrow.Segments.Add(new LineSegment(toadd, false));
            }
            pathFigureArrow.Segments.Add(new LineSegment(endpoint, false));

            PathGeometry pathGeometryArrow = new PathGeometry();

            pathGeometryArrow.Figures.Add(pathFigureArrow);

            currPath.Data = pathGeometryArrow;

            currPath.Fill = fillBrush == null ? linearGradientBrush : fillBrush;

            Canvas.SetZIndex(currPath, -99);

            canvas.Children.Add(currPath);

            return(currPath);
        }